Programming QN With Sol

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

A milkman has a tin of milk, he wants to deliver it to a hotel.

He adopts a procedure to maximize the


quantity of milk. A milk tin contains M liters of milk. T liters of milk is drawn out from the milk tin and
the same T liters of water is mixed into the milk. Then T liters of milk and water mixture is drawn out
from the milk tin and the same T liters of water are mixed into the milk. The program must accept two
integer values M and T as the input. The program must print the quantity of milk left in the tin with the
precision up to 2 decimal places as the output.

Cricket Tournament - Maximum Matches

N teams are participating in a knockout cricket tournament. In each game, there

are 2 teams playing against each other. Only one team can be the winning team of the

tournament. The value of N is passed as the input to the program. The program must print the

maximum number of matches a winning team can play as the output. If the number of teams

participating in the tournament is less than 2, the program must print the string value MATCH NOT

POSSIBLE as the output.

The function/method countFascinatingNumbers accepts two arguments start and end representing two
numbers, where start <= end. The function/method countFascinatingNumbers must return the count of
fascinating numbers in the range from start to end. A fascinating number is defined as a number that
has odd number of factors. Your task is to implement the function countFascinatingNumbers so that the
program runs successfully.

IMPORTANT: Do not write the main() function as it is already defined.

Boundary Condition:

1 <= start <= end <= 10^7

Example Input/Output 1:

Input:
22 26

Output:

Explanation:

Here start = 22 and end = 26.

22 -> 1, 2, 11, 12 (4 factors)

23 -> 1, 23 (2 factors)

24 -> 1, 2, 3, 4, 6, 8, 12, 24 (8 factors)

25 -> 1, 5, 25 (3 factors)

26 -> 1, 2, 13, 26 (4 factors)

In the given range, only 25 has odd number of factors.

Hence 1 is printed as the output.

Example Input/Output 2:

Input:

10 150

Output:

Clothes - Maximum Count


A clothing company is manufacturing a new collection of clothes. The clothes are of two colors red(r)
and blue(b). The colors of the clothes are represented as a string S consisting of r's and b's of length L.
Then they are packed into boxes, where each box contains N number of clothes. If L is not a multiple N
then the remaining clothes are packed into the last box. The box with the maximum number of blue
color clothes is labeled. The string value S and the integer value of N are passed as the input. The
program must print the number of blue color clothes in the labeled box as the output.

Packing Balls - Maximum Boxes

The program must accept two integers R and W as the input. R indicates the number of red

balls and W indicates the number of white balls in a shop. The shopkeeper wants to pack the balls

based on the following conditions.

- He can pack 2 red balls and 1 white ball in a box.

- He can pack 1 red ball and 2 white balls in a box.

The program must print the maximum number of boxes he needs to pack the balls.

Boundary Condition(s):

1 <= R, W <= 10^5

Input Format:

The first line contains R and W separated by a space.

Output Format:

The first line contains an integer representing the maximum number of boxes he needs to pack the

balls.

Example Input/Output 1:

Input:

35

Output:

2
Explanation:

R = 3 => R1, R2, R3 (three red balls)

W = 5 => W1, W2, W3, W4, W5 (five white balls)

One of the possible ways to pack the balls is given below.

Box 1: R1, W1, W2

Box 2: R2, W3, W4

Example Input/Output 2:

Input:

66

Output:

Example Input/Output 3:

Input:

10 5

Output:

Max Execution Time Limit: 50 millisecs

MNC COMPANIES - SET 020

There are N boxes arranged in a row. Each box contains a certain number of fruits. The number of

fruits in each box is passed as the input to the program. A boy wants to collect a maximum number

of fruits based on the following condition.


- He must select the K consecutive boxes but the Xth box must be in his list.

The values of K and X are passed as the input. The program must print the maximum number of

fruits that the boy can collect as the output.

Boundary Condition(s):

1 <= N <= 100

1 <= Each integer value <= 10^5

1 <= X, K <= N

Input Format:

The first line contains N.

The second line contains N integer values separated by a space representing the number of fruits in

the N boxes.

The third line contains X and K separated by a space.

Output Format:

The first line contains the maximum number of fruits that the boy can collect.

Test case:

Input:

22 1 25 20 6 5 4

54

Output:
56

Explanation:

Here N=7, X=5 and K=4.

The given 7 integers are 22, 1, 25, 20, 6, 5 and 4.

The possible 4 ways to choose 4 consecutive boxes are given below.

1 25 20 6 -> 52

25 20 6 5 -> 56

20 6 5 4 -> 35

The maximum number of fruits that the boy can collect is 56. So 56 is printed as the output.

Test case:

Input:

14 25 32 5 15 11 13 23 16

55

Output:

91

Test case:

Input:

25

487 779 1 158 255 406 454 667 643 896 150 435 259 473 641 908 77 407 694 374 278 598 33 348 157
19

Expected Output:

3850

method1:

n=int(input())

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

x,k=map(int,input().split())

st=x-k

if st<0:

st=0

en=x+k-1

if en>n:

en=n

m=-1

for i in range(st,en-k+1):

s=sum(lis[i:i+k])

if m<s:

m=s

print(m)
method2:

n = int(input())

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

x, k = map(int, input().split())

# Subtract 1 from x because Python uses 0-based indexing

x -= 1

start = x - k + 1

if start < 0:

start = 0

end = x + k

if end > n:

end = n

ma = -1

for i in range(start, end - k + 1):

s = sum(lis[i:i + k])

if ma < s:

ma = s

print(ma)

You might also like