Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
25 views

Programming Questions

The document describes a problem of counting fascinating numbers in a given range, where a fascinating number is defined as a number having an odd number of factors. It provides examples and the boundary condition that the start and end numbers are between 1 and 10^7.

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)
25 views

Programming Questions

The document describes a problem of counting fascinating numbers in a given range, where a fascinating number is defined as a number having an odd number of factors. It provides examples and the boundary condition that the start and end numbers are between 1 and 10^7.

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/ 8

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:

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:
5

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