ProgrammingHardTCS NQT · Infosys

A 3 x 3 grid holds 1 to 9 row by row, so row 0 is 1 2 3, row 1 is 4 5 6 and row 2 is 7 8 9. What is the sum of the 2 x 2 block with rows 1 to 2 and columns 1 to 2?

  1. A. 28
  2. B. 24
  3. C. 30
  4. D. 26

Correct answer: A. 28

The block holds 5, 6, 8 and 9, which sum to 28. A 2D prefix sum answers any such block in O(1) after O(n*m) preprocessing.

More C Programming questions

Exams that ask this topic