674 views

1 Answer

Best answer
1 votes
1 votes

ANSWER: B


EXPLANATION

The for loop (line 4-5) calculates the sum of numbers in the array and store it in a variable called value.

Sum of k natural numbers is given by

$x = \frac{k(k-1)}{2}$

Validity of this formula can be checked with the help of Principle of Mathematical Induction. On line 7 in the code, the same is being calculated and stored in variable called last.

So if we have an array with no repetition of numbers, then calculating the sum of numbers in array iteratively will be same as that of result produced from the above formula. However, the results will not be same if there is a repetition of numbers in the array. In fact, the difference between the two is the number which was repeated in the array.

Let us assume an array $(k = 5)$

$arr = [1,2,3,2,4]$

As we can see that $2$ is repeatd in the assumed array above. 

Let's calculate the sum of numbers in the array iteratively; we get 

$value = 12$. 

Now calculate the sum using the formula; we get

$last = \frac{5*4}{2} = 10$

If we subtract the two values calculated above we get the repeated number in the array.

$value-last = 2$


P.S You might find this blogpost helpful.

selected by

Related questions

1 votes
1 votes
1 answer
1
iarnav asked May 21, 2017
594 views
Hello may someone tell me what trees need to be studied in Data Structures subject.
0 votes
0 votes
0 answers
3
Rahul_Rathod_ asked Dec 28, 2018
335 views
let R be the class of recursive program and l be the class of iterative program now consider below statement S S : every program in R uses strictly more space compare to ...