VOOZH about

URL: https://www.geeksforgeeks.org/dsa/two-dimensional-difference-array/

⇱ Two Dimensional Difference Array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Two Dimensional Difference Array

Last Updated : 28 Jul, 2025

In many grid-based problems, we are asked to perform multiple updates on rectangular submatrices of a 2D array. A direct approach would be to iterate over each cell within the rectangle for every update, which leads to a time complexity of O(k × n × m) for k updates too slow for large inputs.

To handle such cases efficiently, we use a technique called the 2D Difference Array. It is an extension of the 1D difference array, designed for grids. Instead of updating every cell in a submatrix individually, we update just four corners in a helper matrix. Later, we reconstruct the final values using prefix sums.

This allows each range update to be applied in constant time, and the final matrix to be computed in O(n × m) time.

How 2D Difference Array Works

To update a submatrix from (r1, c1) to (r2, c2) with a value v, we use a helper matrix diff[][] instead of updating every cell.
We perform these 4 operations in constant time:

Then, we compute prefix sums over diff[][] to get the final matrix. Only the target submatrix reflects the added value fast and efficient.

Example:

Given a 2D integer matrix mat[][] and a list of operations opr[][]. Each operation is represented as a list of the form [v, r1, c1, r2, c2], where:

  • v is the value to be added,
  • (r1, c1) is the top-left coordinate of a submatrix, and
  • (r2, c2) is the bottom-right coordinate of the submatrix (both inclusive).

Return the final matrix after all operations.

Step By Step Implementations:

  • Create a diff[][] matrix of size n × m initialized with zeros. This matrix will store increment/decrement markers to indicate where the effect of v should begin or end.
  • For each query [v, r1, c1, r2, c2], perform the following 4 operations:
    => diff[r1][c1] += v;
    => diff[r1][c2 + 1] -= v; (if c1 + 1 is less than n)
    => diff[r2 + 1][c1] -= v; ( if r2 + 1 is less than m)
    => diff[r2 + 1][c2 + 1] += v; ( r2 + 1 is less than n and c1 + 1 is less than m )
  • Take row wise prefix sum i.e, for each i => diff[i][j] += diff[i][j-1]
  • Take column wise prefix sum i.e, for each j => diff[i][j] += diff[i-1][j]
  • Now apply the net updates from diff back to the original matrix (i.e., mat[i][j] += diff[i][j]).

Illustrations:


Output
3 4 3 
2 2 -1 
3 -3 1 

Time Complexity

  • Each update: O(1)
  • Final matrix construction: O(n × m)

Auxiliary Space

  • Difference matrix diff[][]: O(n × m) (extra space used for marking updates)
  • Final matrix mat[][]: uses existing space or same input

Applications of 2D Difference Arrays

The 2D difference array is a powerful technique for efficiently handling repeated updates to rectangular regions within a matrix. It’s particularly useful in scenarios like:

  • Updating submatrices in grid-based simulations
  • Adjusting brightness in image regions
  • Modifying terrain in game maps
  • Batch editing in spreadsheet-like data

Instead of looping through every cell in each rectangle which is inefficient on large grids the 2D difference array lets you apply each update in O(1) time by marking only the rectangle’s boundaries. Once all updates are recorded, the final matrix is computed in O(n × m) time using prefix sums, making it ideal for high-performance, large-scale matrix operations.

Why the 2D Difference Array Technique Works

Let’s analyze the effect:

  • We add v at the top-left corner of the rectangle.
  • We subtract v right after the right column, and one row below the bottom.
  • We add v back at the bottom-right overflow point (to counteract the two subtractions).

This ensures that:

  • When we compute prefix sums row-wise and column-wise, each cell in the range [r1..r2][c1..c2] gets +v added exactly once.
  • All other cells remain unaffected.
Comment
Article Tags: