Monday 27 April 2020

Note : some modification required !! Hackerearth Byteland Problem : Solution in python

 PROBLEM STATEMENT
Points: 50
In Byteland they have a very strange monetary system. Each Bytelandian gold coin has an integer number written on it. A coin n can be exchanged in a bank into three coins: n/2, n/3 and n/4. But these numbers are all rounded down (the banks have to make a profit).
You can also sell Bytelandian coins for American dollars. The exchange rate is 1:1. But you can not buy Bytelandian coins. You have one gold coin. What is the maximum amount of American dollars you can get for it?
Input The input will contain several test cases (not more than 10). Each testcase is a single line with a number n, 0 <= n <= 1 000 000 000. It is the number written on your coin.
Output For each test case output a single line, containing the maximum amount of American dollars you can make.
Explanation You can change 12 into 6, 4 and 3, and then change these into $6+$4+$3 = $13. If you try changing the coin 2 into 3 smaller coins, you will get 1, 0 and 0, and later you can get no more than $1 out of them. It is better just to change the 2 coin directly into $2.
SAMPLE INPUT
 
12
2

SAMPLE OUTPUT
 
13
2

Time Limit: 9.0 sec(s) for all input files combined.


Solution in python :





list1 = []
while True :
    try :
        n = input()
        if n :
            count = 0
            n = int(n)
            count+=int(n/2)+int(n/3)+int(n/4)
            if n>count :
               count = n
            list1.append(count)
    except EOFError :
           break
for i in list1 :
    print(i) 

Hackerearth : checking horizontal and vertical symmetric matrix : Solution in python

PROBLEM STATEMENT
Points: 30
You are given a square matrix of size n. Rows are indexed 1 to n from top to bottom and columns are indexed 1 to n form left to right. Matrix consists of only '*' and '.'. You need to check whether matrix is symmetric or not. if it is, check it is symmetric about vertical axis or horizontal axis or both.
A matrix is said to be symmetric about horizontal axis if 1st row is identical to nth row, 2nd is identical to (n1)th row and so on...
A matrix is said to be symmetric about vertical axis if 1st column is identical to nth column, 2nd identical to (n1)th and so on for all columns.
INPUT :
First line contains t,the number of test cases. First line of each test case contains n the size of matrix. Each of next n lines contain n characters.
OUTPUT:
Output t lines, answer for each test case. Print "HORIZONTAL" if symmetric about horizontal axis. Print "VERTICAL" if symmetric about vertical axis. Print "BOTH" if symmetric about both axes. print "NO" if it is not symmetric.
Constraints :
1<t500
1<n<50
SAMPLE INPUT
 
3
4
*.*.
.*.*
*.*.
.*.*
3
.*.
*.*
.*.
3
..*
**.
..*


SAMPLE OUTPUT
 
NO
BOTH
HORIZONTAL

Solution in python:

list2 = []
t = int(input())
for i in range(t) :
    list1 = []
    count = 0 
    count2 = 0
    n  = int(input())
    for j in range(0,n) :
        list1.append(input().split([0])
    for k in range(0,n) :
        if list1[0+k] != list1[n-1-k] :
            count = 0
            break 
        else : 
            count=1

    for j in range(n) :
        for k in range(n) :
                if list1[0+k][0+j] != list1[0+k][n-1-j] :
                    count2 = 0
                    break 
                else : 
                    count2 = 1
    if count==0 and count2==0 :
        list2.append('NO')
    elif count==0 and count2 :
        list2.append('VERTICAL')
    elif count and count2 == 0 :
         list2.append('HORIZONTAL')
    else :
         list2.append('BOTH')
for i in list2 :
    print(i)


Hackerearth Cats , Dogs and fencing problem : Solution in python

PROBLEM STATEMENT
Points: 50
  • Russian version of the problem can be read here.
As you probably know, cats usually fight with dogs.
There are N doghouses in a backyard. For simplicity, we consider the backyard as a plane, and the doghouses as points on the plane. All doghouses are numbered from 1 to N. The i-th doghouse is located at point (i, Y[i]). (Note that this means that there are no two doghouses with the same x-coordinate.)
Cats are about to build some rectangular fence, parallel to the axis. If some doghouse is located at the perimeter of the fence, it should be removed. Your task it to find the maximum possible number of doghouses that may be removed after choosing arbitrary fence sizes and location. Please note that the area covered by the fence may be equal to 0.
Input format
The first line of the input contains the single integer N - the number of doghouses. The next line contains N space-separated integers that are Y-coordinates of the corresponding doghouse.
Output format
In the only line of the output print the maximum number of doghouses that may be removed.
Constraints
1 ≤ N ≤ 1,000, 0 ≤ Y[i] ≤ 1,000.
SAMPLE INPUT
 
5
2 3 4 1 1
SAMPLE OUTPUT
 
4

Solution in python: 


import math
lset = set([])
count = 0
t = int(input())
list1 = input().split()
for j in list1 :
        inj = int(j)
        length = int(math.sqrt(count*count+inj*inj))
        count+=1
        lset.add(length)
l = len(lset)
c = count+1-l+1
print(c)

Saturday 25 April 2020

Hackerearth Problem min-max of N positive Integers : Solution in Python

PROBLEM STATEMENT
Points: 30
Given A Series Of N Positive Integers a1,a2,a3........an. , Find The Minimum And Maximum Values That Can Be Calculated By Summing Exactly N-1 Of The N Integers. Then Print The Respective Minimum And Maximum Values As A Single Line Of Two Space-Separated Long Integers.
Input Format
First Line Take Input Value Of N
Second Line Take Input N Space Separated Integer Value
Output Format
Two Space Separated Value ( One Maximum Sum And One Minimum Sum )
Constraints
  • 0 < N < 100001
  • 0 <= ai < 1013
SAMPLE INPUT
5
1 2 3 4 5
SAMPLE OUTPUT
10 14
Explanation
Our initial numbers are 1,2,3,4 and 5. We can calculate the following sums using four of the five integers:
  1. If we sum everything except 1, our sum is 2+3+4+5=14 .
  2. If we sum everything except 2, our sum is 1+3+4+5=13 .
  3. If we sum everything except 3, our sum is 1+2+4+5=12 .
  4. If we sum everything except 4, our sum is 1+3+4+5=11 .
  5. If we sum everything except 5, our sum is 1+2+3+4=10 .
As you can see, the minimal sum is 1+2+3+4=10 and the maximal sum is 2+3+4+5=14. Thus, we print these minimal and maximal sums as two space-separated integers on a new line.





Solution in Python :


import array
n = int(input())
nums = input()
list1 = nums.split()
list1 = [int(i) for i in list1]
list1.sort()
total = sum(list1)
min = total - list1[len(list1)-1]
max = total - list1[0]
print(min,max)




Saturday 18 April 2020

Hackerearth : Roy's birthday and balloons problem : Solution in Python






PROBLEM STATEMENT
Points: 30
Its Roy's birthday and he is decorating his house with balloons.
He has 26 different types of balloons. They are represented by [a-z].
Now Roy does not want to have few particular balloons together. Actually he doesn't want to keep any of the two vowel (i.e [a,e,i,o,u] ) balloons together.
So, in one arrangement no two vowels should be together. Given the string of balloons there can be many such arrangements possible.
Help Roy in finding the number of possible arrangements.
Number of such arrangements can be quite large, so print it modulo 1000000007 (109 + 7)
Also, there's a chance that no such arrangement is possible. Print "-1" in such cases (without quotes)
Input:
First line contains T, number of test cases.
Each of next T lines contains a string composed of lower case letters representing all the balloons.
Output:
For each test case print the required answer.
Constraints:
1 <= T <= 100
1 <= Length of string <= 1000000 (106)
Note: Sum of string lengths over all the test cases in one file does not exceed 1000000 (106)
SAMPLE INPUT
 
2
abe
ae
SAMPLE OUTPUT
 
2
-1
Explanation
Test #1: Possible arrangements are [1] abe [2] eba
Test #2: There are no any arrangements possible such that any two vowels are not together. So print -1.
Time Limit: 1.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded if any testcase passes.


Solution in Python :


import math
import array
total = array.array('i',[])
i = 0
t = int(input())
while i<t :
    count = 0;
    s = input()
    for j in s :
        if j == 'a' or j == 'e' or j == 'i' or j == 'o' or j == 'u' :
           count+=1
    cons = len(s) - count
    factcons = math.factorial(cons)
    factvowl = math.factorial(count)
    factdiff = math.factorial(abs(cons-count))
    total.append(int(factcons*factvowl*(cons+1)*factcons/(factvowl*factdiff)))
    i+=1
for ele in total :
   print(ele)

Hackerearth string problem : Solution in Python




PROBLEM STATEMENT
Points: 30
Two letter strings are the strings consisting of only two letters "X" and "Y". A string is "super two letter string" if
a) It does not have leading "X" letters.
b) It does not contain P consecutive "X" letters.
Your task is to find total number of Super two letter strings of length N.
Input :
The first line contains the number of test cases T . Each test case consists of two space separated integers - N and P .
Output :
For each test case output total number of Super two letter strings of length N modulo 1000000007(10^9+7).
Constraints :
1 <= T <= 100
1 <= N <= 10^4
1 <= P <= 10
SAMPLE INPUT
2
2 1
4 2
SAMPLE OUTPUT
1
5
Explanation
For first sample : Only possible string is : YY For second sample : Possible strings are : YXYX , YXYY, YYYX, YYXY, YYYY





Solution  in   Python



import math
import array
ar = array.array('i',[]);
i = 1
c=0
T = input()
while i <=int(T):
    N1 = input()
    n2 = N1
    n = int(n2.split()[0])-1
    P = int(n2.split()[1])
    a=[0 for i in range(n)]
    b=[0 for i in range(n)]
    a[0] = b[0] = 1
    for i in range(1,n):
        a[i] = a[i-1] + b[i-1]
        b[i] = a[i-1]
        c+=a[n-1] + b[n-1]
    ar.append(c)
    i+=1
for j in ar :
    print(j)