ProgrammingModerateAccenture · Capgemini

What will be the output of the following pseudocode?

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

Correct answer: B. 6

This is a sliding window of size 3. Window sums: 5, 6, 5, 6, 4, so the best is 6. 12 is the best sum without the length limit.

More Pseudocode and Code Tracing questions

Exams that ask this topic