ProgrammingVery hard

int g[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; With a 2D prefix-sum table P, the sum of the rectangle with rows 1 to 2 and columns 1 to 2 (0-based, inclusive) is computed by which expression?

  1. A. P[2][2] - P[1][2] - P[2][1] + P[1][1]
  2. B. P[3][3] - P[1][3] - P[3][1]
  3. C. P[3][3] - P[1][3] - P[3][1] + P[1][1]
  4. D. P[3][3] - P[0][3] - P[3][0] + P[0][0]

Correct answer: C. P[3][3] - P[1][3] - P[3][1] + P[1][1]

A 2D prefix table is 1-based with an extra zero row and column. Inclusion-exclusion subtracts the two strips and adds back the overlap. Here it gives 5 + 6 + 8 + 9 = 28.

More C Programming questions

Exams that ask this topic