ProgrammingHardAccenture · Cognizant pattern

What will be the output of the following pseudocode (counting pairs summing to a target, brute force)? (Arrays are indexed from 0.)

What will be the output of the following pseudocode (counting pairs summing to a target, brute force)? (Arrays are indexed from 0.)
Integer arr[5] = {1, 5, 7, -1, 5}
Integer target = 6, cnt = 0
for (each i from 0 to 4)
    for (each j from i + 1 to 4)
        if (arr[i] + arr[j] == target)
            cnt = cnt + 1
        end if
    end for
end for
Print cnt
  1. A. 2
  2. B. 4
  3. C. 3
  4. D. 1

Correct answer: C. 3

The pairs summing to 6 are (1,5) at indices (0,1), (1,5) at indices (0,4), and (7,-1) at indices (2,3) -- three matching pairs in total.

More Pseudocode and Code Tracing questions

Exams that ask this topic