ProgrammingHardInfosys · Cognizant

For the array a = [7, 2, 4, 9, 1, 5] the procedure below computes the sum of the minimum of every contiguous window of length 2 (windows start at index 0 to n-2). What is printed?

For the array a = [7, 2, 4, 9, 1, 5] the procedure below computes the sum of the minimum of every contiguous window of length 2 (windows start at index 0 to n-2). What is printed?
total = 0
for i = 0 to n - 2
    total = total + min(a[i], a[i+1])
end for
Print total
  1. A. 34
  2. B. 9
  3. C. 10
  4. D. 23

Correct answer: C. 10

The pairwise minima are 2, 2, 4, 1, 1, so the total is 10. Using maxima gives 34, stopping one window early gives 9, and adding the first elements gives 23.

More Pseudocode and Code Tracing questions

Exams that ask this topic