ProgrammingHardAmazon OA · Infosys SP

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer a[6] = {3, 8, 1, 9, 4, 7}
Integer best = -1, i = 0, j = 5
while (i < j)
    best = max(best, a[i] + a[j])
    if (a[i] < a[j])
        i = i + 1
    else
        j = j - 1
    end if
end while
Print best
  1. A. 16
  2. B. 10
  3. C. 15
  4. D. 17

Correct answer: D. 17

The pointers move inward from both ends, always dropping the smaller value: pairs (3, 7), (8, 7), (8, 4), (8, 9), (1, 9) give sums 10, 15, 12, 17, 10. best = 17, which here is also the largest pair sum (8 + 9).

More Pseudocode and Code Tracing questions

Exams that ask this topic