Skip to main content
Back to the DSA atlas
Range queries Technique Intermediate

Difference Array

Apply many range updates in O(1) each, then rebuild the final values with one prefix pass. Difference arrays are the update-side twin of prefix sums.

difference-arrayrange-updateprefix-sumarray

Interactive visualization

Use the controls to connect the idea to concrete operations before diving into the full write-up.

Pending range updates

+3 on [1, 4]

Touch only diff[1] and diff[5]

-2 on [0, 2]

Touch only diff[0] and diff[3]

+4 on [3, 5]

Touch only diff[3] and diff[6]

Difference array markers

d[0]

-2

d[1]

3

d[2]

0

d[3]

2

d[4]

0

d[5]

-3

d[6]

0

Reconstructed final array

idx 0

0

idx 1

3

idx 2

3

idx 3

5

idx 4

5

idx 5

2

Cheap range updates, one rebuild pass

A difference array marks where an update starts and where it stops. A prefix sum over those markers reconstructs the actual values, which is why difference arrays and prefix sums are really two halves of the same idea.

Family

Range queries

Static and dynamic range aggregates via indexed trees, segment trees, and precomputed tables.

Builds on

1 topic

Read these first if you want the surrounding context.

Unlocks

2 next topics

Use these follow-ups when you want to apply the current idea in a richer setting.

Learning paths

1

This topic appears in curated progressions so you can keep moving without guessing the next step.

Problem

You need to apply many updates like:

  • add x to every element in [L, R]
  • process a whole batch of range increments efficiently

Doing each update one cell at a time costs O(n)O(n) per query. That is too slow when there are many updates.

Core idea

Instead of editing the whole range, only mark its boundaries:

  • add delta at L
  • subtract delta at R + 1

Later, run a prefix sum over these markers to recover the real values.

Update rule

diff[L] <- diff[L] + delta
if R + 1 < n:
    diff[R + 1] <- diff[R + 1] - delta

That means one range update touches only two positions.

Rebuild rule

running <- 0
for i in 0..n-1:
    running <- running + diff[i]
    a[i] <- base[i] + running

The prefix sum is what turns the boundary markers back into actual values.

Why it matters

Difference arrays are the cleanest way to learn that:

  • prefix sums answer static range queries
  • difference arrays apply static range updates

They are mirror images.

From there, it is much easier to understand why Fenwick trees and segment trees are useful: they bring the same ideas into dynamic settings.

Complexity

OperationTime
One range updateO(1)O(1)
Rebuild final arrayO(n)O(n)

When to use it

Use a difference array when:

  • all updates are known ahead of time
  • you do not need to answer queries between updates
  • the operation is additive and can be accumulated with a prefix pass

If updates and queries are interleaved online, move to Fenwick trees or segment trees.

Key takeaways

  • Difference arrays make range updates constant time
  • They only work because a later prefix sum reconstructs the full values
  • Prefix sum and difference array belong together conceptually
  • This is one of the most useful “not fancy, but essential” techniques in the whole atlas

Practice problems

ProblemDifficultyKey idea
Range Addition�� MediumClassic difference-array batch update problem
Corporate Flight Bookings🟡 MediumMark route changes at interval boundaries
Car Pooling🟡 MediumDifference array over passenger count changes

Relation to other topics

  • Prefix sum rebuilds the final values from difference markers
  • Fenwick tree is the dynamic upgrade path for prefix-style updates/queries
  • Segment tree handles more general online range update/query combinations

Build on these first

These topics supply the mental model or underlying structure that this page assumes.

What this enables

Once the current idea feels natural, these are the most useful next jumps.

Related directions

These topics live nearby conceptually, even if they are not direct prerequisites.

More from Range queries

Stay in the same family when you want parallel variations of the same mental model.

Paths that include this topic

Follow one of these sequences if you want a guided next step instead of open-ended browsing.

Range-query toolkit

Start with static prefix tricks, then move into offline, persistent, dynamic, and precomputed range-query structures.

From the blog

Pair the atlas with recent writing from the rest of the site when you want a broader engineering perspective alongside the topic graph.