ProgrammingHardAmazon OA · Cognizant GenC Next

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer a[7] = {16, 17, 4, 3, 5, 2, 1}
Integer mx = a[6], c = 1
for (each i from 5 down to 0)
    if (a[i] > mx)
        mx = a[i]
        c = c + 1
    end if
end for
Print c, mx
  1. A. 3 17
  2. B. 5 16
  3. C. 4 16
  4. D. 4 17

Correct answer: D. 4 17

Scanning from the right counts 'leaders' (elements greater than everything to their right): 1, 2, 5, 17. So c = 4 and mx = 17. 16 is not a leader because 17 lies to its right.

More Pseudocode and Code Tracing questions

Exams that ask this topic