How to increment all values in an array interval by a given amount
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
If you need to add a value to every element in an array range, the direct loop is obvious and often perfectly fine for one update. The more interesting case is when there are many interval updates, because then a difference-array technique can reduce each range update from linear time to constant time.
Core Sections
The naive update is simple and correct
For a single update on a normal array, just loop over the interval.
This is easy to read and runs in O(r - l + 1) time for one operation. If you only perform a few updates, this may be the best solution because it has minimal complexity in both code and reasoning.
Why repeated range updates get expensive
Suppose you have an array of length n and q updates. If each update touches a long interval, the total runtime can approach O(nq). That becomes wasteful when the update pattern is heavy.
The core insight is that for repeated interval additions, you do not need to change every element immediately. You can record only where the increment starts and where it stops.
Use a difference array for many updates
A difference array stores boundary changes instead of fully updated values. To add delta to every position from left to right:
- add
deltaatdiff[left] - subtract
deltaatdiff[right + 1]if that index exists
After processing all updates, reconstruct the final values with a prefix sum.
This makes each update O(1) and the final reconstruction O(n). For many updates, that is a major improvement.
Start from an existing array, not just zeros
If the array already contains values, build the updates on top of it by reconstructing the accumulated increments and then adding them back to the original array.
This is the more general version you would use in real applications.
When to choose which approach
Use the naive loop when:
- you only have one or a few updates
- the intervals are short
- code simplicity matters more than asymptotic improvement
Use the difference array when:
- you have many range updates
- the array is large
- you only need the final array after all updates
The last point is important. If you need to answer queries between updates, the problem becomes more like a segment tree or Fenwick tree family of problems rather than a pure difference-array pass.
Common Pitfalls
- Using the naive loop for thousands of large interval updates can create unnecessary
O(nq)work. - Forgetting the
right + 1boundary check in the difference-array approach causes out-of-bounds errors. - Reconstructing the final array incorrectly without a running prefix sum defeats the whole method.
- Applying the technique when intermediate query results are needed mixes up offline updates with online-query problems.
- Confusing inclusive intervals with half-open intervals leads to off-by-one mistakes in both the naive and optimized versions.
Summary
- A direct loop is the simplest solution for a single or small number of interval increments.
- A difference array turns each range increment into two point updates.
- After all updates, a prefix sum reconstructs the final values.
- The optimized approach is
O(1)per update andO(n)for the final pass. - Choose the technique based on whether you need simplicity for a few updates or efficiency for many updates.
Related reading
- How to intersect two sorted integer arrays without duplicates?
- How to iterate over n dimensions?
- How to iterate through SparseArray?
- How to keep track of depth in breadth first search?
- How to inference Tensorflow model with input queue pipeline?
- How to initialize a dict with keys from a list and empty value in Python?
- How to know if a binary number divides by 3?
- How to know when a recursive, asynchronous task finishes

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.