Professional Documents
Culture Documents
Programs 1
Programs 1
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
The program must print the maximum number of boxes he needs to pack the balls.
Boundary Condition(s):
Input Format:
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:
Example Input/Output 2:
Input:
66
Output:
Example Input/Output 3:
Input:
10 5
Output:
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
- 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
Boundary Condition(s):
1 <= N <= 100
1 <= X, K <= N
Input Format:
The second line contains N integer values separated by a space representing the number of fruits in
the N boxes.
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:
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())
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
s = sum(lis[i:i + k])
if ma < s:
ma = s
print(ma)