Open In App

1D Difference Array

Last Updated : 30 Jul, 2025
Comments
Improve
Suggest changes
133 Likes
Like
Report

In many problems, we’re asked to perform multiple range updates like adding a value to all elements from index l to r. A direct approach updates each element in the range, leading to a time complexity of O(k × n) for k updates, which becomes inefficient for large inputs.

The 1D Difference Array optimizes this by updating only the boundaries of each range in a helper array. After all updates, a prefix sum reconstructs the final array in O(n) time.

This allows each range update to be applied in O(1) time, making it highly efficient for problems involving multiple subarray modifications.

How 1D Difference Array Works

To efficiently handle multiple range updates, we use a helper array called a difference array diff[], initialized with all zeros:

// n is the size of original array
vector<int> diff(n, 0);  

Instead of directly updating every index from l to r, we update only the boundaries:
To add a value v to a range [l, r], we do:

diff[l] += v;

if (r + 1 < n)
    diff[r + 1] -= v;

This way, the effect of +v starts at index l and is canceled after index r.
After all operations, we compute the prefix sum of diff[] to propagate the updates across the array:

for (int i = 1; i < n; i++)
    diff[i] += diff[i - 1];

Finally, apply these changes to the original array:

for (int i = 0; i < n; i++)
    arr[i] += diff[i];

This approach ensures that each update is applied in O(1), and the final array is constructed in a single pass making it highly efficient for problems with multiple range updates.

Example:

Given an array arr[] and a 2D array opr[][], where each row represents an operation in the form [l, r, v]. For each operation, add v to all elements from index l to r in arr. Return the updated array after applying all operations.

Step By Step Implementations:

  • Create a diff[] array of size n + 1 initialized with zeros.
    This array will store increment/decrement markers to indicate where the effect of a value should start and end.
  • For each operation [l, r, v], perform the following two updates:
    -> Add v to diff[l] to start applying the increment from index l.
    -> Subtract v from diff[r + 1] to stop the effect after index r (only if r + 1 < n).
  • Compute the prefix sum of the diff[] array so that each position reflects the total value to be added:
    For each index i from 1 to n - 1, do:
    diff[i] += diff[i - 1]
  • Apply the net updates from diff[] to the original array:
    For each index i from 0 to n - 1, do:
    arr[i] += diff[i]

Illustrations:

C++
#include <iostream>
#include <vector>
using namespace std;

// Apply a single range update on the difference array
void update(vector<int>& diff, int l, int r, int x) {
    diff[l] += x;
    if (r + 1 < diff.size()) {
        diff[r + 1] -= x;
    }
}

// Apply range updates using difference array technique
vector<int> diffArray(vector<int>& arr, vector<vector<int>>& opr) {
    int n = arr.size();

    // Create difference array 
    vector<int> diff(n, 0);

    // Apply each operation [l, r, val] on the diff array
    for (auto& q : opr) {
        int l = q[0], r = q[1], val = q[2];
        update(diff, l, r, val);
    }

    // Build the result by applying prefix sum over diff
    vector<int> res = arr;
    res[0] += diff[0];
    for (int i = 1; i < n; i++) {
        diff[i] += diff[i - 1]; 
        res[i] += diff[i];      
    }

    return res;
}

int main() {
    vector<int> arr = {1, 2, 3, 4, 5};
    vector<vector<int>> opr = {
        {1, 3, 10}, {2, 4, -5}
    };

    vector<int> res = diffArray(arr, opr);

    for (int num : res) {
        cout << num << " ";
    }
    cout << endl;

    return 0;
}
Java Python C# JavaScript

Output
1 12 8 9 0 

Time Complexity:

  • Each update operation: O(1)
  • Total operations (m updates): O(m)
  • Final array construction using prefix sum: O(n)

Auxiliary Space:

  • Difference array diff[]: O(n)
  • Result array res[]: O(n)

Why 1D Difference Array Technique Works

Instead of directly updating every element between l and r for each operation (which takes O(r − l + 1) time), we use a smarter trick with a difference array diff[].

Think of it like placing flags:

  • Place a +val at index l → this means “start adding val from here”
  • Place a -val at index r + 1 → this means “stop adding val after index r”

You don’t update every element between l and r right away.
You just mark where the effect starts and ends in the diff[] array.

After applying all such operations, you build the final result using a prefix sum:

  • Traverse diff[] from left to right.
  • Maintain a running total and add it to each index of the original array arr.

This way, each element of the array gets all the updates it should, without updating each one manually per operation.

So instead of O(m × n) for m operations, the total time becomes just O(m + n).

In-Place Implementation (Without Extra Space)

In the previous implementation, we used an extra array (diff[]) to track range updates and another result array to store the final output. But if memory is a concern, and you're allowed to modify the original array, you can implement the same logic in-place without using any extra space.

How it Works

To save extra space, we modify the input array arr itself to behave like a difference array. Here's the step-by-step idea:

  1. Convert the array to a difference array in-place
    We go from right to left, starting from index n - 1 down to 1, and subtract the previous element:
    arr[i] = arr[i] - arr[i - 1];
    This effectively turns arr into a difference array without using extra space.
  2. Apply each range update [l, r, v]
    -> Add v at index l → arr[l] += v
    -> Subtract v at index r + 1 (if it's within bounds) → arr[r + 1] -= v
    This is the same flag mechanism as before: start adding at l, stop at r + 1.
  3. Reconstruct the final array with prefix sum
    Now that all updates are marked in arr, we traverse from left to right, converting it back to the final array:
    arr[i] = arr[i - 1] + arr[i];
    Every element ends up with the correct net value from all updates.
C++
#include <iostream>
#include <vector>
using namespace std;

vector<int> diffArray(vector<int>& arr, vector<vector<int>>& opr) {
    int n = arr.size();

    // Convert arr to in-place difference array
    for (int i = n - 1; i > 0; i--) {
        arr[i] -= arr[i - 1];
    }

    // Apply each operation directly on the original array
    // Each operation is of the form [l, r, v]
    for (auto& q : opr) {
        int l = q[0], r = q[1], v = q[2];

        // Adding v at index l
        arr[l] += v;

        // Subtracting v at index r + 1 ensures the addition 
        // stops at index r when prefix sums are applied
        if (r + 1 < n) arr[r + 1] -= v;
    }

    // Take prefix sum to get the final updated array
    for (int i = 1; i < n; i++) {
        arr[i] += arr[i - 1];
    }

    return arr;
}


int main() {
    vector<int> arr = {1, 2, 3, 4, 5};
    vector<vector<int>> opr = {
        {1, 3, 10}, {2, 4, -5}
    };

    vector<int> res = diffArray(arr, opr);
    
    for (int num : res) {
        cout << num << " ";
    }
    cout << endl;

    return 0;
}
Java Python C# JavaScript

Output
1 12 8 9 0 

Why Right-to-Left?
-> We subtract arr[i - 1] from arr[i] to build the difference array. Doing this from right to left ensures we don’t overwrite values (arr[i - 1]) before they're used preventing incorrect results.


Similar Reads