← Quiz: Prefix Sums and Difference Arrays: all 13 questions
Quiz: Prefix Sums and Difference Arrays · Question 7 of 13
In a 2D prefix sum, why is P[i][j] subtracted when building P[i + 1][j + 1]?
In a 2D prefix sum, why is P[i][j] subtracted when building P[i + 1][j + 1]?
- A.It is the cell being added
- B.The top-left rectangle is counted twice — once in the cell above and once in the cell to the left
- C.To keep the values small
- D.It isn't needed; it is a mistake
Answer & explanation
Correct answer: The top-left rectangle is counted twice — once in the cell above and once in the cell to the left
This is inclusion–exclusion: the overlap of the two rectangles must be removed once.
Can you get the rest right without peeking?
Take the timed Quiz: Prefix Sums and Difference Arrays quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →