Showing posts with label HackerEarth. Show all posts
Showing posts with label HackerEarth. Show all posts

Tuesday, July 18, 2017

HAWKEYE AND FLOODFILL - Practice Problem from HackerEarth and solution using python 2.7.6

Problem Statement:

enter image description here
Hawkeye is a MARVEL character who has a perfect aim in archery. Its almost impossible for him to miss his aim. As you are the head of technical team at MARVEL , you have been assigned a very critical task. During practice hawkeye uses a square archery practice board. For our convenience we will assume it to be a N * N matrix. He shoots an arrow at position i,j with power P. Now power reduces by 1 as we move away from position i,j . Your task is to show the graph representing the impact of the arrow on the practice board.
INPUT FORMAT
First line of input contains a single integer N denoting the size of the practice board.
Second line contains 3 integers i,j denoting position at which arrow was shot and p denoting the power used to shoot the arrow.
OUTPUT FORMAT
Output a 2D matrix representing the impact on the practice board.
CONSTRAINS
1<=N<=1000
0<=i,j 0<=p<=109
Note:- The matrix is zero indexed matrix ie: first element will be at 0,0

Programming using python 2.7.6:

Size_of_Board = int(raw_input())
Pos1,Pos2,Power = map(int,raw_input().split())
def get_max(x1,y1,x2,y2):
    x = x1 - x2
    y = y1 - y2
    return max(abs(x),abs(y))

def get_difference_value(power,maxval):
    if (power - maxval) > 0:
        return (power - maxval)
    else:
        return 0

for a in range(Size_of_Board):
    for b in range(Size_of_Board):       
        print get_difference_value(Power,get_max(Pos1,Pos2,a,b)),       
    print

Sunday, July 16, 2017

Add Alternate Elements of 2-Dimensional Array- Practice Problem from HackerEarth and solution using python 2.7.6

Problem Statement:

You are given a two-dimensional 3*3 array starting from A [0][0]. You should add the alternate elements of the array and print its sum. It should print two different numbers the first being sum of A 0 0, A 0 2, A 1 1, A 2 0, A 2 2 and A 0 1, A 1 0, A 1 2, A 2 1.
Input Format
First and only line contains the value of array separated by single space.
Output Format
Output Format
First line should print sum of A 0 0, A 0 2, A 1 1, A 2 0, A 2 2
Second line should print sum of A 0 1, A 1 0, A 1 2, A 2 1

Programming using python 2.7.6:

Add all value which are on even index and add all value which are in odd index in list. 

num = map(int,raw_input().split())
OddSum =0
EvenSum =0
for i in range(len(num)):
    if (i+1)%2 !=0
        OddSum +=num[i]
    else:
        EvenSum +=num[i]
print OddSum
print EvenSum

Roy and Symmetric Logos- Practice Problem from HackerEarth and solution using python 2.7.6

Problem Statement:

Roy likes Symmetric Logos.
How to check whether a logo is symmetric?
Align the center of logo with the origin of Cartesian plane. Now if the colored pixels of the logo are symmetric about both X-axis and Y-axis, then the logo is symmetric.
You are given a binary matrix of size N x N which represents the pixels of a logo.
1 indicates that the pixel is colored and 0 indicates no color.
For instance: Take a 5x5 matrix as follows:








Graphically it is represented as follows:




















Observe that it is symmetric about both X-axis and Y-axis.
Let's take another example of 5x5 matrix:


Graphically it is represented as follows:



















Now this logo is symmetric about Y-axis but it is not symmetric about X-axis.
Your task is to output YES if the logo is symmetric else output NO.
Input:
First line contains T - number of test cases.
T test cases follow.
First line of each test case contains the N - size of matrix.
Next N lines contains binary strings of length N.
Output:
Print YES or NO in a new line for each test case
Constraints:
1 ≤ T ≤ 10
2 ≤ N ≤ 32
Note: There will always be at least 1 colored pixel in input data.

Programming using python 2.7.6:

The logo can be symmetric if it satisfies below condition:
  • If all the row values are palindrome and column values are palindrome. 
Take an example of below matrix:
1001
0000
0000
1001
Here all row entries for the given matrix are palindrome,i.e 1001,0000,0000,1001 
at the same time if you look at the column values also they all are palindrome, i.e 1001,0000,0000,1001. Hence this is a symmetric matrix.

Let's take another example of 4x4 matrix:
0101
0110
0110
0101
Here if you look at the first row ,i.e 0101 is not palindrome, though all column values are palindrome. Hence this is not a symmetric matrix.

With above approach below is the code:

Code in Python 2.7.6:

BinaryString=[]
result =[]
flag = 0
T = input()
while T:
    N = input()
    while N:
        BinaryString.append(raw_input())
        N -=1
    for val in BinaryString: #Check each row whether it's palindrome
        if val != val[::-1]: # If not then it's not symmetric anyways.
            flag = 1
            result.append('NO') # Result is No and break
            break
       
    if flag == 0: # if all row values are palindrome then check whether all column values are palindrome
            temp = ''
            for j in zip(*BinaryString): # check column values are palindrome
                for k in range(len(j)):
                    temp +=j[k]
                if temp != temp[::-1]: # If not then it's not symmetric anyways.
                    flag = 1
                    result.append('NO') # Result is No
                    break
                temp =''
            else:
                result.append('YES')
    flag = 0
    BinaryString =[]   
    T -=1
          
for res in result: print res 

Saturday, July 15, 2017

Monk and Power of Time - Practice Problem from HackerEarth and solution using python 2.7.6

Problem Statement:

The Monk is trying to explain to its users that even a single unit of time can be extremely important and to demonstrate this particular fact he gives them a challenging task.
There are N processes to be completed by you, the chosen one, since you're Monk's favorite student. All the processes have a unique number assigned to them from 1 to N.
Now, you are given two things:
  • The calling order in which all the processes are called.
  • The ideal order in which all the processes should have been executed.
Now, let us demonstrate this by an example. Let's say that there are 3 processes, the calling order of the processes is: 3 - 2 - 1. The ideal order is: 1 - 3 - 2, i.e., process number 3 will only be executed after process number 1 has been completed; process number 2 will only be executed after process number 3 has been executed.
  • Iteration #1: Since the ideal order has process #1 to be executed firstly, the calling ordered is changed, i.e., the first element has to be pushed to the last place. Changing the position of the element takes 1 unit of time. The new calling order is: 2 - 1 - 3. Time taken in step #1: 1.
  • Iteration #2: Since the ideal order has process #1 to be executed firstly, the calling ordered has to be changed again, i.e., the first element has to be pushed to the last place. The new calling order is: 1 - 3 - 2. Time taken in step #2: 1.
  • Iteration #3: Since the first element of the calling order is same as the ideal order, that process will be executed. And it will be thus popped out. Time taken in step #3: 1.
  • Iteration #4: Since the new first element of the calling order is same as the ideal order, that process will be executed. Time taken in step #4: 1.
  • Iteration #5: Since the last element of the calling order is same as the ideal order, that process will be executed. Time taken in step #5: 1.
Total time taken: 5 units.
PS: Executing a process takes 1 unit of time. Changing the position takes 1 unit of time.
Input format:
The first line a number N, denoting the number of processes. The second line contains the calling order of the processes. The third line contains the ideal order of the processes.
Output format:
Print the total time taken for the entire queue of processes to be executed.
Constraints:
1<=N<=100

Programming using python 2.7.6:

The solution for this problem can be accomplished by splitting in to two parts:
First, always match the first value of Calling Orders with each Ideal Orders value. 
  • If not matches then we need to remove this value from Calling Orders list and append the same value at the rear list of Calling Orders. (I am storing both Calling Orders and Ideal Orders value in lists). By doing this your process counter will increase by 1.
Second, if value matches then remove the value from Calling Order list. That means your process counter also gets incremented by 1.

Now let's solve it:

Code in Python 2.7:

N = input()
CO = map(int,raw_input().split()[:N])
IO = map(int,raw_input().split()[:N])
proccesscount = 0
for i in range(N):
    while CO[0]!=IO[i]:
        CO.append(CO[0]) # Append the mismatch value at the end of the list.
        CO.remove(CO[0]) # Remove the appended value from the front
        proccesscount +=1 # Increase the process counter
    CO.remove(CO[0]) # if Calling Order value matches with Ideal Order then remove from the list
    proccesscount +=1 # Increase the process counter
print proccesscount

Friday, July 14, 2017

Mark The Answer-Practice Problem from Hackerearth and solution using python 2.7.6

Problem Statement:

Our friend Monk has an exam that has quite weird rules. Each question has a difficulty level in the form of an Integer. Now, Monk can only solve the problems that have difficulty level less than X . Now the rules are-
  • Score of the student is equal to the maximum number of answers he/she has attempted without skipping a question.
  • Student is allowed to skip just "one" question that will not be counted in the continuity of the questions.
Note- Assume the student knows the solution to the problem he/she attempts and always starts the paper from first question.
Given the number of Questions, N ,the maximum difficulty level of the problem Monk can solve , X ,and the difficulty level of each question , Ai can you help him determine his maximum score?
Input Format
First Line contains Integer N , the number of questions and the maximum difficulty X Monk can solve.
Next line contains N integers, Ai denoting the difficulty level of each question.
Output Format
Maximum score Monk can achieve in the exam.
Constraints
  • 1N105
  • 1X109

Programming using python 2.7.6:

N, X = map(int,raw_input().split())
A = map(int,raw_input().split()[:N])
score = 0
skip = 0
for i in A:
    if skip > 1:
        break
    else:
        if i > X:
            skip +=1
        else:
            score +=1

print score







Thursday, July 13, 2017

Monk and Welcome Problem-Practice Problem from Hackerearth and solution using python 2.7.6

Problem Statement:

Having a good previous year, Monk is back to teach algorithms and data structures. This year he welcomes the learners with a problem which he calls "Welcome Problem". The problem gives you two arrays A and B (each array of size N) and asks to print new array C such that:
C[i]=A[i]+B[i] ; 1iN
Now, Monk will proceed further when you solve this one. So, go on and solve it :)
Input:
First line consists of an integer N, denoting the size of A and B.
Next line consists of N space separated integers denoting the array A.
Next line consists of N space separated integers denoting the array B.
Output:
Print N space separated integers denoting the array C.
Input Constraints:
1N100000
1A[i]1000001iN
1B[i]1000001iN



Programming using python 2.7.6:

N = input()
A = map(int,raw_input().split()[:N])
B = map(int,raw_input().split()[:N])
for x in zip(A,B):print sum(x),