Loading the journal
Loading the journal
Range Query / 2D Prefix Sum (Optimized)Solved Oct 23, 2025
Problem
Given a matrix of size N × N and four integers (i, j, k, l), calculate the sum of the sub-matrix from the top-left coordinate (i, j) to the bottom-right coordinate (k, l).
Input: 3 1 1 2 2 1 2 3 4 5 6 7 8 9
Output: 28
Explanation: Sum of elements (5, 6, 8, 9) = 28.
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code