ProgrammingHardTCS NQT · Accenture

The greedy below repeatedly increases the smallest element of the array [3, 1, 2] by 1, three times in total. What is the minimum value of the array printed after the three increments?

The greedy below repeatedly increases the smallest element of the array [3, 1, 2] by 1, three times in total. What is the minimum value of the array printed after the three increments?
for k = 1 to 3
    find the index of the minimum; increase that element by 1
end for
Print minimum of the array
  1. A. 1
  2. B. 9
  3. C. 2
  4. D. 3

Correct answer: D. 3

Increments take 1 to 2, then the new minimum 2 to 3, then the remaining 2 to 3, giving [3,3,3] with minimum 3. Incrementing the maximum leaves minimum 1, printing the sum gives 9, and stopping after two increments leaves minimum 2.

More Pseudocode and Code Tracing questions

Exams that ask this topic