1,013 views
3 votes
3 votes

Consider the following function.

Assume that the array contains list of all the numbers from 1 to k –1. What is the return value of function BSR?

  • Sum of all numbers
  • Repeated number in the list
  • Average of all the numbers
  • None of these

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

Here I think answer should Be D, None of these, as as per me this program will add up all no from 1 to k-1. i.e. (k-1 * k ) /2 in value & substracting last from it which is also (k-1*k) /2 . So it will always return 0.

Q 37

Made Easy FLT 6-Practice Test 14

2 Answers

2 votes
2 votes

Assume that the array contains list of all the numbers from 1 to k –1. //main line 

i.e. we have two take all no. form 1- k-1 when no. of values are k.

take random input

k= 7

1,2,3,4,5,6,6

now For  loop find sum of all array element= 27

now last = 7(6)/2= 21

return = 27-21= 6

so 6 is repeated.

0 votes
0 votes
Here answer is D) None of the above.

Size of array is k & Array should contain all no from 1 to k-1. So there are k-1 no that must be there. Once we fill up k-1 places with 1 to k-1 no, we get one extra space.

 We can put any No X  there ! (Not necessary to be in 1 to k-1, it is not said that array contains ONLY no from 1 to k-1)

Last = (k * k - 1 ) /2;

value = sum of 1 to k-1 + X = (K * k-1 ) / 2 + X

Value - Last = X

So answer is D.

It seem like B is correct, but it is not so. As we do not need to repeat any no, we can take no outside of given range 1 to k-1  !

Value - Last = Extra no

Related questions