ProgrammingModerateTCS NQT · Accenture

What will be the output of the following pseudocode?

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

Correct answer: A. 3

Each unordered pair is checked once: (1, 3), (5, 3) and (4, 2) differ by 2. c = 3.

More Pseudocode and Code Tracing questions

Exams that ask this topic