Pattern #8

Difference Array

Important interview questions, thinking patterns, range update logic, and rules for solving difference array problems in Go.

Must Solve

10 core questions — solve these first.

  1. 1.Range Addition
    medium
  2. 2.Corporate Flight Bookings
    medium
  3. 3.Car Pooling
    medium
  4. 4.Maximum Population Year
    easy
  5. 5.Shifting Letters II
    medium
  6. 6.Minimum Number of K Consecutive Bit Flips
    hard
  7. 7.Zero Array Transformation I
    medium
  8. 8.Zero Array Transformation II
    medium
  9. 9.Check if Grid can be Cut into Sections
    medium
  10. 10.Describe the Painting
    medium

Also Important

7 more questions worth practicing.

  1. 11.Range Addition II
    easy
  2. 12.My Calendar problems
    medium
  3. 13.Meeting Rooms / Overlapping Intervals
    easy
  4. 14.Amount of New Area Painted Each Day
    hard
  5. 15.Number of Flowers in Full Bloom
    hard
  6. 16.Range Coverage Problems
    medium
  7. 17.Sweep Line + Difference Array problems
    medium

How to Think

  1. Many updates on ranges?Difference Array
  2. Add +V from index L to R?diff[L] += V, diff[R+1] -= V
  3. Need many interval additions?start += val, end + 1 -= val
  4. Need final values after all updates?Difference Array + Prefix Sum

Go Thinking & Reference

Basic Range Update & Rebuild

// 1. Mark range updates in O(1)
diff := make([]int, n+1)

diff[l] += value
if r+1 < n {
    diff[r+1] -= value
}

// 2. Rebuild final values in O(n)
current := 0
for i := 0; i < n; i++ {
    current += diff[i]
    arr[i] += current
}

👉 M updates + 1 rebuild: O(M + N) time | O(N) space

Main Idea & Example

Original:   [0,  0, 0, 0,  0]
Update:     Add +5 from index 1 to 3
Difference: [0, +5, 0, 0, -5]
Prefix sum: [0,  5, 5, 5,  0]
Result:     [0,  5, 5, 5,  0] ✅
Visual Memory Rule
L ---------------- R
↑                  ↑
+X              stop after R (R+1)

diff[L] += X
diff[R+1] -= X

Prefix Sum  →  Final Array

💡 Golden Rule: "Mark where the change starts and where it stops — don't update every element."

Difference Array vs Prefix Sum

TechniquePrimary PurposeUpdate CostQuery Cost
Prefix SumMany range QUERIESO(n) per updateO(1) per query
Difference ArrayMany range UPDATESO(1) per updateO(n) rebuild at end

Interview Rules

  1. 1. Many range updates? → Difference Array
  2. 2. Add same value to L..R? → Difference Array
  3. 3. Start/end events? → Difference Array
  4. 4. Overlapping intervals? → Think Difference / Sweep Line
  5. 5. Need final result after all updates? → Prefix once at the end
  6. 6. Start effectdiff[L] += x
  7. 7. Stop effectdiff[R+1] -= x
  8. 8. Many range queries instead? → Prefix Sum

Small Rules

  1. Rule 1: Difference array makes single range update O(1) instead of O(len).
  2. Rule 2: M updates + 1 rebuild is O(M + N) instead of O(M × N).
  3. Rule 3: L starts the effect, R + 1 stops the effect.
  4. Rule 4: Check bounds! If R + 1 == n, do not access outside array.
  5. Rule 5: Best when many updates happen first, and final values are read later.

Production Thinking

Pricing / PromotionsDiscount on product range 1000..5000 in O(1)

Traffic / Capacity+10 passengers at stop 2, -10 after stop 6

Booking SystemsRoom occupied 10:00..12:00 → +1 at start, -1 at end

Server Load+3 workers min 10..20, +5 workers min 15..25

Remember This

Range update        → Difference Array
Start at L          → +X
Stop after R (R+1)  → -X
Final values        → Prefix Sum
Many updates        → O(1) each
Many range queries  → Prefix Sum instead

💡 Golden Rule: "Mark where the change starts and where it stops — don't update every element."