ProgrammingModerate

For a 2D prefix sum with P[i][j] = sum of the first i rows and j columns, what is the cost of building it for an n x n grid?

  1. A. O(n^3) time, because each cell sums its whole rectangle
  2. B. O(n^2) time, with each cell computed from three neighbours in the table
  3. C. O(n log n) time, because the table is sorted
  4. D. O(n) time, because only the last row is needed

Correct answer: B. O(n^2) time, with each cell computed from three neighbours in the table

Each cell of the prefix table is computed in constant time from its left, top and top-left neighbours, so the total is O(n^2).

More C Programming questions

Exams that ask this topic