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?
- A. O(n^3) time, because each cell sums its whole rectangle
- B. O(n^2) time, with each cell computed from three neighbours in the table
- C. O(n log n) time, because the table is sorted
- 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
- int grid[4][4] is filled by grid[i][j] = i j. What is grid[3][2]?
- For the grid [[1, 2, 3], [4, 5, 6]], what is the sum of the column 2 values (the third column, 0-based index 2)?
- In row-major order, the cell a[i][j] of a matrix with C columns sits at which flat index?
- A matrix multiplication of a 3 x 4 matrix by a 4 x 2 matrix produces a result of what size?
- How many scalar multiplications does the naive method use for a 3 x 4 times 4 x 2 product?
- A tic-tac-toe board stored as char b[3][3] is full. What is the minimum number of moves that could have been made on it?