Problem Statement 1
Ravi and her friends are playing a game of verbal Kho-Kho. Ravi is acting as a
mediator, and the rest of the N friends are seated on N chairs, one each. Ravi
starts by providing a paper with a single-digit number to the friend present at
number 1. Let’s denote friends by F, where F will be of size N. F[1]…F[N]
represents friends seated respectively. After receiving the paper with a digit,
F[1] will enact and try to tell F[2] without speaking. Similarly, F[2] will
communicate to the next person i.e., F[3]. This continues until the last person
F[N] understands the digit. Finally, the last person will write the digit on a
separate paper and give it to Ravi to compare both papers. If the digits are
similar then, Ravi will give a T-shirt to each friend. However, if the digits do
not match, Ravi will ask each friend’s digits, and she will offer the T-shirts to
only those who understood the digits correctly.
Given N number of friends and digit array D, denoting the digit understood by
each friend F. finds out how many of Ravi’s friends have not enacted well OR
Example 1:
3 -> N, number of friends
4 4 4 – array D. denoting digit understanding by N friends
Output:
0
Explanation:
All of them understood the digits correctly.
Example 2:
5
12322
Output:
4
Explanation:
1st, 2nd, 3rd, and 4th friends could not enact OR understand the enactment.
n = int(input())
arr = list(map(int, input().split()))
print(n-arr.count(arr[0]))
Problem Statement 2
Krishna is going to bet today on horse riding. There are N horses listed in a
sequence of 1 to N.
The probability of winning each horse is different so the prices for making a bet
on the horses are not the same. There is no limit on the number of horses on
which he can bet, but he thinks that if he bets on a continuous sequence of
horses then he has a better chance to win. Krishna will get K units of money if
any horse on which he bets will win. But as the award is only K units so he
wants to put money less than K. Krishna wants to bet on as many horses as he
can. As you are his best friend, he reached out to you for help, can you please
find the length of the maximum continuous sequence of horses on which
Krishna can make a bet, and remember he will invest money less than K units. If
there is more than one possible combination, Krishna will bet randomly on any
one of them.
Given the number of horses(N), reward money(K), and price of betting on N
horses in order.
Hint: For each starting index of a horse, its end index in sequences will be equal
to or greater than the end index of the previous starting index.
Example 1:
Input:
90 100 -> N = 10, K=100
30 40 50 20 20 10 90 10 10 10 -> Price to make bet on each horse in order
Output:
3
Explanation:
There are 10 horses, and the reward money is 100. So, Krishna will put money in
less than 100. There are two possible o sequences of length three whose total
money for betting is less than 100, i.e. [50 20 20] (sum is 90) and [10 10 10] (sum is
30). Krishna will choose randomly one sequence from these two. As none of the
other sequences with a length greater than 3 will have a price less than 100 so the
answer will be 3.
Example 2:
Input:
10 100 -> N = 10, K=100
10 90 80 20 90 60 40 60 70 75 -> Price to make bet on each horse in order
Output:
1
Explanation:
There are no two consecutive horses for which the sum of price is less than 100. So,
Krishna will choose randomly any one horse. And the max length of the sequence
will be 1.
k=40
su=0
n=10
ans=0
arr = list(map(int, input().split()))
for i in range(n - 1):
for j in range(i + 1, n):
su=sum(arr[i:j])
print(su)
if su<k:
ans = max(ans, j - i)
print(ans)
else:
break
Problem Statement 3
This vacation you went to visit the golden house. There are N rooms in this
golden house and its owner needs someone to take care of the management of
this house. As you have been unemployed for a long time, you are interested in
this job. The owner of this house wanted an intelligent manager for this role, so
he created one puzzle within that golden house. The person who will be able to
solve that puzzle will be the manager of the golden house.
The owner of the house kept some number of golden coins in each room. You
have to choose two rooms, one from where you will enter and the other one
from where you will exit. From any room either you can exit, or you can move
to the next room. While visiting any room you will collect all the gold coins, and
if you enter any room then you can’t skip collecting gold coins from that room,
you have to take those coins. The owner wants to have exactly K coins, when
you exit the room, he guarantees that there will be at least one possible
solution for this puzzle.
Given several rooms (N), and several gold coins in N rooms. You
have to find room numbers from where you will start and from
where you will exit. If there is more than one solution possible, then
you have to provide a solution with a smaller starting room number.
Hint:
Find a You can assume that room numbers will start from 1 and end
at N.
continuous subsequence whose sum will be exactly equal to K.
Example 1:
Input:
10 15 -> N =10, K = 15
5 3 7 14 18 1 18 4 8 3 -> Number of gold coins in each room.
Output:
13
Explanation: There are ten rooms in the house. You want to have the Total sum of
gold coins in a continuous sequence of rooms to be 15 There are two solutions to
this i.e. [1, 3] and [8, 10] then the solution with a smaller starting room number will
be printed hence [1, 3] is output.
def fun(n, k, arr):
for i in range(n - 1):
for j in range(i, n):
if sum(arr[i:j]) == k:
return [i + 1, j]
n, k = map(int, input().split())
arr = list(map(int, input().split()))
s = fun(n, k, arr)
print(s[0], s[1])
Problem Statement 4
Jack is a sports teacher at St. Patrick’s School. He makes games not only to
make the student fit but So smart. So, he lined up all the N number classes. of
students in his class. At each position, he has fixed a board with the Integer
number printed on it. Each number is unique and in exactly the range of N. Let
us say there are 10 students, then the boards will be printed with numbers from
1 to 10 in a random order given by the sequence A[ ]. As a rule, all students
wear a jersey with their numbers printed on it. So if there are students, each
will have a unique jersey number just like a football team. Now, in the
beginning, all the students will stand as per the increasing order of their jersey
numbers, from left to right. The only difference will be their respective board
number which is placed at their respective location. The board location is fixed
and cannot be changed. We can consider the arrangement as below. Suppose
there are students, and the board is placed in the order of [2 3 1 5 4].
Board — 2, 3, 1, 5, 4
Student’s Jersey — 1, 2, 3, 4, 5
Now the game begins
After every beat of the drum, each student will have to move to that location
(index), where his board is pointing to. In the above case student with jersey #1
is standing with board #2, so now he will have to move to location #2. Similarly,
all the other students will do.
So after the first beat of the drum, the alignment will be:
Board — 2, 3, 1, 5, 4.
This keeps going on and on until all the students are back to the way they were
at the beginning. So, after 6 beats of the drum, all the students will be aligned
the same way as before.
Given N and the order of the board of the respective positions, find the number
of beats required to bring back the students to their original position.
So, for the above case, the answer is 6
Example 1:
Input:
3 -> Input integer, N
{1, 2, 3} ->Input integer. B[], board alignment.
Output:
1 -> Output
Explanation:
All the students will be standing in board positions;
Board — 1, 2, 3
Student’s Jersey –1, 2, 3After the first beat of the drum:
Jersey #1 will move to index 1.
Jersey #2 will move to index 2.
Jersey #3 will move to index 3.
Hence, they will be back in their position in just 1 beat.
So, the answer is 1.
Example 2:
Input:
5 -> Input integer, N
{2, 3, 1, 5, 4} -> Input integer, B[ ], board alignment. After Beat-3 of the drum:
Output: Board — 2, 3, 1, 5, 4
6 -> Output
Explanation: After Beat-4 of the drum:
All the students will be standing as below, with the Board — 2, 3, 1, 5, 4
board positions: Student’s Jersey — 3, 1, 2, 4, 5
Board — 2, 3, 1, 5, 4 After Beat-5 of the drum:
Student’s Jersey — 1, 2, 3, 4, 5After Beat-1of the Board — 2, 3, 1, 5, 4
drum:
Student’s Jersey — 2, 3, 1, 5, 4
Jersey #1 has moved to index 2.
Jersey #2 has moved to index 3. After Beat-6 of the drum:
Jersey #3 has moved to index 1. Board — 2, 3, 1, 5, 4
Jersey #4 has moved to index 5. Student’s Jersey — 1, 2, 3, 4, 5
Jersey #5 has moved to index 4. Hence, they will be back in their positions after 6 beats.
Board – 2, 3, 1, 5, 4 So, the answer is 6.
Student’s Jersey — 3, 1, 2, 5, 4
After Beat-2 of the drum:
Jersey #3 has moved to index 2.
Jersey #1 has moved to index 3.
Jersey #2 has moved to index 1.
Jersey #5 has moved to index 5.
Jersey #4 has moved to index 4.
Board — 2, 3, 1, 5, 4
Student’s Jersey — 2, 3, 1, 4, 5
Student’s Jersey — 1, 2, 3, 5, 4
n = int(input())
B = []
arr = []
for i in range(n):
B.append(int(input()))
arr.append(i + 1)
ans = 0
while 1:
ans += 1
ar = [None] * n
for i in range(n):
ar[i] = arr[B[i] - 1]
if ar == sorted(ar):
break
arr = ar
print(ans)
Problem Statements 5
At the security checkpoint, airport security personnel have seized a
number of travellers’ belongings. Everything has been thrown into a big
box (array). Each product carries a specific level of risk[0,1,2]. The risk
severity of the items in this case is represented by an array[] of N integer
values. Sorting the elements in the array according to the degrees of
danger is the task at hand. Between 0 and 2 are the risk values.
Example:
Input :
7 -> Value of N
[1,0,2,0,1,0,2]-> Element of arr[0] to arr[N-1], while input each element is
separated by new line.
Output :
0 0 0 1 1 2 2 -> Element after sorting based on risk severity
Example 2:
input : 10 -> Value of N
[2,1,0,2,1,0,0,1,2,0] -> Element of arr[0] to arr[N-1], while input each
element is separated by a new line.
Output :
0 0 0 0 1 1 1 2 2 2 ->Elements after sorting based on risk severity.
Explanation:
In the above example, the input is an array of size N consisting of only 0’s, 1’s
and 2s. The output is a sorted array from 0 to 2 based on risk severity.
n = int(input())
a = list(map(int, input().split()))
l=0
m=0
h=n-1
while m <= h:
if a[m] == 0:
a[l], a[m] = a[m], a[l]
l += 1
m += 1
elif a[m] == 1:
m += 1
else:
a[m], a[h] = a[h], a[m]
h -= 1
print(*a)
Problem Statement 6
• For all of its products, a supermarket maintains a pricing structure. Each
product has a value N printed on it. The price of the item is determined
by multiplying the value N, which is read by the scanner, by the sum of all
its digits. The goal here is to create software that, given the code of any
item N, will compute the product (multiplication) of all the value digits
(price).
Example 1:
Input :
5244 -> Value of N
Output :
160 -> Price
Explanation:
From the input above
Product of the digits 5,2,4,4
5*2*4*4= 160
Hence, output is 160.
n=input()
p=1
for i in n:
p*=int(i)
print(p)
Problem Statement 7
There are two banks – Bank A and Bank B. Their interest rates vary. You have
received offers from both banks in terms of the annual rate of interest, tenure,
and variations of the rate of interest over the entire tenure. You have to choose
the offer which costs you the least interest and reject the other. Do the
computation and make a wise choice.
The loan repayment happens at a monthly frequency and Equated Monthly
Installment (EMI) is calculated using the formula given below :
EMI = loan amount * monthly interest rate / ( 1 – 1 / (1 + monthly interest
rate)^(number of years * 12))
Constraints:
1 <= P <= 1000000
1 <=T <= 50
1<= N1 <= 30
1<= N2 <= 30
Input Format:
First line: P principal (Loan Amount)
Second line: T Total Tenure (in years).
Third Line: N1 is the number of slabs of interest rates for a given period by Bank A.
The first slab starts from the first year and the second slab starts from the end of
the first slab and so on.
Next N1 line will contain the interest rate and their period.
After N1 lines we will receive N2 viz. the number of slabs offered by the second
bank.
Next N2 lines are the number of slabs of interest rates for a given period by Bank B.
The first slab starts from the first year and the second slab starts from the end of
the first slab and so on.
The period and rate will be delimited by a single white space.
Output Format: Your decision is either Bank A or Bank B.
Example 1
Input
10000
20
3
5 9.5
10 9.6
5 8.5
3
10 6.9
5 8.5
5 7.9
Output: Bank B
Example 2
Input
500000
26
3
13 9.5
3 6.9
10 5.6
3
14 8.5
6 7.4
6 9.6
Output: Bank A
bank = []
principal = int(input())
year = int(input())
for i in range(2):
installments = int(input())
sum = 0
for i in range(0, installments):
year, roi = map(float,input().split())
year = int(year)
square = pow((1+roi),year*12)
emi = (principal*(roi)/(1-1/square))
sum = sum + emi
bank.append(sum)
if bank[0] < bank[1]:
print("Bank A")
else:
print("Bank B")
Problem Statement 8
Some prime numbers can be expressed as a sum of other consecutive
prime numbers. For example 5 = 2 + 3, 17 = 2 + 3 + 5 + 7, 41 = 2 + 3 + 5 + 7
+ 11 + 13. Your task is to find out how many prime numbers which satisfy
this property are present in the range 3 to N subject to a constraint that
summation should always start with number 2.
Write code to find out the number of prime numbers that satisfy the
above-mentioned property in a given range.
Input Format: First line contains a number N
Output Format: Print the total number of all such prime numbers which
are less than or equal to N.
Example:
Input :
20
Output :
2
Explanation :
Below 20, there are 2 such numbers,
5=2+3
17=2+3+5+7
num = int(input()), arr = [],sum = 0,count = 0
if num > 1:
for i in range(2, num + 2):
for j in range(2, i):
if i % j == 0:
break
else:
arr.append(i)
def is_prime(sum):
for i in range(2, (sum // 2) +2):
if sum % i == 0:
return False
else:
return True
for i in range(0, len(arr)):
sum = sum + arr[i]
if sum <= num:
if is_prime(sum):
count = count + 1
print(count)
Problem Statement 9: Collecting Candies
Krishna loves candies a lot, so whenever he gets them, he
stores them so that he can eat them later whenever he
wants to.
He has recently received N boxes of candies each containing
Ci candies where Ci represents the total number of candies
in the ith box. Krishna wants to store them in a single box.
The only constraint is that he can choose any two boxes and
store their joint contents in an empty box only. Assume that
there are an infinite number of empty boxes available.
At a time he can pick up any two boxes for transferring and
if both the boxes contain X and Y number of candies
respectively, then it takes him exactly X+Y seconds of time.
As he is too eager to collect all of them he has approached
you to tell him the minimum time in which all the candies
can be collected.
Input Format:
The first line of input is the number of test case T
Each test case is comprised of two inputs
The first input of a test case is the number of boxes N
The second input is N integers delimited by whitespace denoting the number of
candies in each box
Output Format: Print minimum time required, in seconds, for each of the test
cases. Print each output on a new line.
Input :
1
4
1234
Output :
19
Explanation :
4 boxes, each containing 1, 2, 3 and 4 candies respectively. Adding 1 + 2 in a new
box takes 3 seconds. Adding 3 + 3 in a new box takes 6 seconds. Adding 4 + 6 in a
new box takes 10 seconds. Hence total time taken is 19 seconds. There could be
other combinations also, but overall time does not go below 19 seconds.
T = int(input())
arr1 = []
for i in range(0, T):
N = int(input())
arr = list(map(int,input().split()))
arr.sort()
count = arr[0]
for i in range(1, len(arr)):
count = count + arr[i]
arr1.append(count)
print(sum(arr1))
Problem Statement 10:Houses Problem
There are n houses built in a line, each of which contains some value in it.
A thief is going to steal the maximal value of these houses, but he can’t steal in
two adjacent houses because the owner of the stolen houses will tell his two
neighbours left and right side.
What is the maximum stolen value?
Input Format
First an integer n, denoting how many houses are there.
Then n space separated integers denoting the values for the n houses.
Output Format
An integer denoting the maximum value possible to steal.
Input
7
6713825
Output
20
Explanation
6+1+8+5 = 20.
It is the max possible value.
n=int(input())
arr=list(map(int,input().split()))
incl=0
excl=0
mx=0
for i in range(n):
mx=max(incl,excl)
incl=excl+arr[i]
excl=mx
print(max(incl,excl))