0% found this document useful (0 votes)
27 views8 pages

Hacker Rank Problems

The document outlines various coding problems and their solutions, including 'Staircase', 'Find the Digits', 'Migratory Birds', 'Left Rotation', 'Sherlock and Array', 'Ice Cream Parlour', 'Apple & Orange', and 'Cats and Mouse'. Each problem is accompanied by example code and expected output. The solutions utilize basic programming constructs such as loops, conditionals, and data structures.

Uploaded by

dates69672
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views8 pages

Hacker Rank Problems

The document outlines various coding problems and their solutions, including 'Staircase', 'Find the Digits', 'Migratory Birds', 'Left Rotation', 'Sherlock and Array', 'Ice Cream Parlour', 'Apple & Orange', and 'Cats and Mouse'. Each problem is accompanied by example code and expected output. The solutions utilize basic programming constructs such as loops, conditionals, and data structures.

Uploaded by

dates69672
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

HACKER RANK PROBLEMS

1. STAIRCASE

n = int(input())
for i in range(n):
for j in range(n-i-1):
print(" ",end="")
for k in range(i+1):
print("#",end="")
print()

OR

#staircase
n=int(input())
for i in range(n):
print(" "*(n-i-1)+"#"*(i+1))

output:
5
#
##
###
####
#####
2. FIND THE DIGITS

# find digits
n=int(input()) # 1
for i in range(n):
num=int(input()) # 124
count=0
for i in str(num):
if (int(i) !=0 ) and (num % int(i) == 0):
count+=1
print(count)

or

n=int(input()) # 1
for i in range(n):
num=int(input()) # 124
count=0
for i in str(num):
try:
if num % int(i) == 0:
count+=1
except ZeroDivisionError:
pass
print(count)

Output:

1
124

3
3. MIGRATORY BIRDS

#migratory birds( with explanation )


n=int(input()) # 5
l=list(map(int,input().split()))# 1 1 2 2 3
birds=[0]*n # [0,0,0,0,0]

for i in l:
birds[i]+=1
# birds[1]=1 [0,1,0,0,0]
# birds[1]=1+1 [0,2,0,0,0]
# birds[2]=1 [0,2,1,0,0]
# birds[2]=1+1 [0,2,2,0,0]
# birds[3]=1 [0,2,2,1,0]

print(birds.index(max(birds)))
# max will give value as 2
# index will give us the position as 1
# 1

OR
#migratory birds(without explaination)
n=int(input())
l=list(map(int,input().split()))
birds=[0]*n
for i in l:
birds[i]+=1 X
print(birds.index(max(birds)))

Output:
5
1 1 2 2 2
2
4. LEFT ROTATION

_, times = map(int, input().split())


items = list(map(int, input().split()))

for item in items[times:] + items[:times]:


print(item, end=" ")

# _ and times → no of inputs, no of times of left rotation

# items → list of values

#Output:

5 4 # _ & times
1 2 3 4 5 # items

51234
5. SHERLOCK AND ARRAY

t = int(input())

for i in range(t):
n = int(input())
arr = list(map(int, input().split()))
left_sum = 0
right_sum = sum(arr)
for num in arr:
right_sum -= num
if left_sum == right_sum:
print("YES")
break
left_sum += num
else:
print("NO")

#output:
2 # total number of inputs
3 # number of values for first input
1 2 3 # values of first input
4 # number of values for second input
1 2 3 4 # values for second input

NO # output for first input


YES # output for second input
6. Ice cream Parlour
# Number of test cases
t = int(input())

# Loop over each test case


for _ in range(t):
# Read the budget k and the number of ice cream flavors n
k = int(input()) # Budget
n = int(input()) # Number of flavors (not actually needed directly)

# Read the list of ice cream prices


cost = list(map(int, input().split()))

# Dictionary to store the price and its index


price_map = {}

# Iterate through the prices


for i in range(n):
complement = k - cost[i] # The complementary price we're looking for

# If the complement is already in the dictionary, we found the answer


if complement in price_map:
print(price_map[complement] + 1, i + 1) # Output 1-based indices
break

price_map[cost[i]] = i
7. Apple & orange

# Read the first line for Sam's house location


s, t = map(int, input().split())

# Read the second line for the positions of the apple and orange trees
a, b = map(int, input().split())

# Read the number of apples and oranges


m, n = map(int, input().split())

# Read the distances that apples fall


apple_distances = list(map(int, input().split()))

# Read the distances that oranges fall


orange_distances = list(map(int, input().split()))

# Count how many apples fall on the house


apple_count = sum(1 for x in apple_distances if s <= a + x <= t)

# Count how many oranges fall on the house


orange_count = sum(1 for x in orange_distances if s <= b + x <= t)

# Print the result


print(apple_count)
print(orange_count)
8. Cats and mouse

def catAndMouse(x, y, z):


a = abs(x-z)
b = abs(y-z)
if(a == b):
return 'Mouse C'
elif(a<b):
return 'Cat A'
elif(a>b):
return 'Cat B'

t=int(input())
for i in range(t):
n=list(map(int,input().split()))
print(catAndMouse(n[0],n[1],n[2]))

You might also like