ProgrammingHardAmazon OA · TCS Digital

What will be the output of the following pseudocode?

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

Correct answer: D. 3 10

c counts inversions: (2, 1), (4, 1), (4, 3) → 3. A fully reversed array of 5 has every pair inverted: C(5, 2) = 10. Output 3 10.

More Pseudocode and Code Tracing questions

Exams that ask this topic