0% found this document useful (0 votes)
46 views6 pages

Programs 1

The program accepts the number of boxes, fruits in each box, and a boy's criteria of selecting K consecutive boxes including the Xth box. It prints the maximum fruits the boy can collect by trying all possible box selections.

Uploaded by

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

Programs 1

The program accepts the number of boxes, fruits in each box, and a boy's criteria of selecting K consecutive boxes including the Xth box. It prints the maximum fruits the boy can collect by trying all possible box selections.

Uploaded by

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

TCS Digital programs:

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:

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