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:
C++
// 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:
C++
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:
C++
for (int i = 1; i < n; i++)
diff[i] += diff[i - 1];
Finally, apply these changes to the original array:
C++
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
import java.util.ArrayList;
public class GfG {
// Apply a single range update on the difference array
static void update(int[] diff, int l, int r, int x) {
diff[l] += x;
if (r + 1 < diff.length) {
diff[r + 1] -= x;
}
}
// Apply range updates using difference array technique
static ArrayList<Integer> diffArray(int[] arr, int[][] opr) {
int n = arr.length;
// Create difference array
int[] diff = new int[n];
// Apply each operation [l, r, val] on the diff array
for (int[] 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
ArrayList<Integer> res = new ArrayList<>();
diff[0] += 0;
res.add(arr[0] + diff[0]);
for (int i = 1; i < n; i++) {
diff[i] += diff[i - 1];
res.add(arr[i] + diff[i]);
}
return res;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int[][] opr = {
{1, 3, 10}, {2, 4, -5}
};
ArrayList<Integer> res = diffArray(arr, opr);
for (int num : res) {
System.out.print(num + " ");
}
System.out.println();
}
}
Python
# Apply a single range update on the difference array
def update(diff, l, r, x):
diff[l] += x
if r + 1 < len(diff):
diff[r + 1] -= x
# Apply range updates using difference array technique
def diffArray(arr, opr):
n = len(arr)
# Create difference array
diff = [0] * n
# Apply each operation [l, r, val] on the diff array
for l, r, val in opr:
update(diff, l, r, val)
# Build the result by applying prefix sum over diff
res = arr[:]
res[0] += diff[0]
for i in range(1, n):
diff[i] += diff[i - 1]
res[i] += diff[i]
return res
if __name__ == "__main__":
arr = [1, 2, 3, 4, 5]
opr = [[1, 3, 10], [2, 4, -5]]
res = diffArray(arr, opr)
for num in res:
print(num, end=" ")
print()
C#
using System;
using System.Collections.Generic;
class GfG {
// Apply a single range update on the difference array
static void update(int[] diff, int l, int r, int x) {
diff[l] += x;
if (r + 1 < diff.Length) {
diff[r + 1] -= x;
}
}
// Apply range updates using difference array technique
static List<int> diffArray(int[] arr, int[][] opr) {
int n = arr.Length;
// Create difference array
int[] diff = new int[n];
// Apply each operation [l, r, val] on the diff array
foreach (var q in 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
List<int> res = new List<int>();
res.Add(arr[0] + diff[0]);
for (int i = 1; i < n; i++) {
diff[i] += diff[i - 1];
res.Add(arr[i] + diff[i]);
}
return res;
}
static void Main() {
int[] arr = { 1, 2, 3, 4, 5 };
int[][] opr = new int[][] {
new int[] { 1, 3, 10 },
new int[] { 2, 4, -5 }
};
List<int> res = diffArray(arr, opr);
foreach (int num in res) {
Console.Write(num + " ");
}
Console.WriteLine();
}
}
JavaScript
// Apply a single range update on the difference array
function update(diff, l, r, x) {
diff[l] += x;
if (r + 1 < diff.length) {
diff[r + 1] -= x;
}
}
// Apply range updates using difference array technique
function diffArray(arr, opr) {
const n = arr.length;
// Create difference array
const diff = new Array(n).fill(0);
// Apply each operation [l, r, val] on the diff array
for (const [l, r, val] of opr) {
update(diff, l, r, val);
}
// Build the result by applying prefix sum over diff
const res = arr.slice();
res[0] += diff[0];
for (let i = 1; i < n; i++) {
diff[i] += diff[i - 1];
res[i] += diff[i];
}
return res;
}
// Driver Code
const arr = [1, 2, 3, 4, 5];
const opr = [
[1, 3, 10],
[2, 4, -5]
];
const res = diffArray(arr, opr);
console.log(res.join(" "));
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 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:
- 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. - 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. - 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
import java.util.ArrayList;
import java.util.List;
class GfG {
public static ArrayList<Integer> diffArray(int[] arr, int[][] opr) {
int n = arr.length;
// Convert arr to in-place difference array
for (int i = n - 1; i > 0; i--) {
arr[i] -= arr[i - 1];
}
// Apply each operation [l, r, v]
for (int[] q : opr) {
int l = q[0], r = q[1], v = q[2];
arr[l] += v;
if (r + 1 < n) arr[r + 1] -= v;
}
// Take prefix sum to get final array
for (int i = 1; i < n; i++) {
arr[i] += arr[i - 1];
}
// Convert array to ArrayList
ArrayList<Integer> result = new ArrayList<>();
for (int num : arr) {
result.add(num);
}
return result;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int[][] opr = {
{1, 3, 10}, {2, 4, -5}
};
ArrayList<Integer> res = diffArray(arr, opr);
for (int num : res) {
System.out.print(num + " ");
}
System.out.println();
}
}
Python
def diffArray(arr, opr):
n = len(arr)
# Convert arr to in-place difference array
for i in range(n - 1, 0, -1):
arr[i] -= arr[i - 1]
# Apply each operation directly on the original array
# Each operation is of the form [l, r, v]
for l, r, v in opr:
# 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 i in range(1, n):
arr[i] += arr[i - 1]
return arr
if __name__ == "__main__":
arr = [1, 2, 3, 4, 5]
opr = [[1, 3, 10], [2, 4, -5]]
res = diffArray(arr, opr)
print(*res)
C#
using System;
using System.Collections.Generic;
class GfG {
// Apply range updates using in-place difference array
public static List<int> diffArray(int[] arr, int[][] opr) {
int n = arr.Length;
// Convert arr to in-place difference array
for (int i = n - 1; i > 0; i--) {
arr[i] -= arr[i - 1];
}
// Apply each operation [l, r, v]
foreach (var q in opr) {
int l = q[0], r = q[1], v = q[2];
arr[l] += v;
if (r + 1 < n) arr[r + 1] -= v;
}
// Take prefix sum to restore the final array
for (int i = 1; i < n; i++) {
arr[i] += arr[i - 1];
}
// Convert array to List<int>
List<int> result = new List<int>();
foreach (int num in arr) {
result.Add(num);
}
return result;
}
public static void Main() {
int[] arr = { 1, 2, 3, 4, 5 };
int[][] opr = new int[][] {
new int[] { 1, 3, 10 },
new int[] { 2, 4, -5 }
};
List<int> res = diffArray(arr, opr);
foreach (int num in res) {
Console.Write(num + " ");
}
Console.WriteLine();
}
}
JavaScript
function diffArray(arr, opr) {
let n = arr.length;
// Convert arr to in-place difference array
for (let 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 (let [l, r, v] of opr) {
// 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 (let i = 1; i < n; i++) {
arr[i] += arr[i - 1];
}
return arr;
}
// Driver code
const arr = [1, 2, 3, 4, 5];
const opr = [
[1, 3, 10],
[2, 4, -5]
];
let res = diffArray(arr, opr);
console.log(res.join(" "));
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
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem