Consider an array A[] of integers and following two types of queries.
- update(l, r, x) : Adds x to all values from A[l] to A[r] (both inclusive).
- printArray() : Prints the current modified array.
Examples :
Input : A [] { 10, 5, 20, 40 }
update(0, 1, 10)
printArray()
update(1, 3, 20)
update(2, 2, 30)
printArray()
Output : 20 15 20 40
20 35 70 60
Explanation : The query update(0, 1, 10)
adds 10 to A[0] and A[1]. After update,
A[] becomes {20, 15, 20, 40}
Query update(1, 3, 20) adds 20 to A[1],
A[2] and A[3]. After update, A[] becomes
{20, 35, 40, 60}.
Query update(2, 2, 30) adds 30 to A[2].
After update, A[] becomes {20, 35, 70, 60}.
A simple solution is to do following :
- update(l, r, x) : Run a loop from l to r and add x to all elements from A[l] to A[r]
- printArray() : Simply print A[].
Time complexities of both of the above operations is O(n)
An efficient solution is to use difference array.
Difference array D[i] of a given array A[i] is defined as D[i] = A[i]-A[i-1] (for 0 < i < N ) and D[0] = A[0] considering 0 based indexing. Difference array can be used to perform range update queries “l r x” where l is left index, r is right index and x is value to be added and after all queries you can return original array from it. Where update range operations can be performed in O(1) complexity.
- update(l, r, x) : Add x to D[l] and subtract it from D[r+1], i.e., we do D[l] += x, D[r+1] -= x
- printArray() : Do A[0] = D[0] and print it. For rest of the elements, do A[i] = A[i-1] + D[i] and print them.
Time complexity of update here is improved to O(1). Note that printArray() still takes O(n) time.
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > initializeDiffArray(vector< int >& A)
{
int n = A.size();
vector< int > D(n + 1);
D[0] = A[0], D[n] = 0;
for ( int i = 1; i < n; i++)
D[i] = A[i] - A[i - 1];
return D;
}
void update(vector< int >& D, int l, int r, int x)
{
D[l] += x;
D[r + 1] -= x;
}
int printArray(vector< int >& A, vector< int >& D)
{
for ( int i = 0; i < A.size(); i++) {
if (i == 0)
A[i] = D[i];
else
A[i] = D[i] + A[i - 1];
cout << A[i] << " " ;
}
cout << endl;
}
int main()
{
vector< int > A{ 10, 5, 20, 40 };
vector< int > D = initializeDiffArray(A);
update(D, 0, 1, 10);
printArray(A, D);
update(D, 1, 3, 20);
update(D, 2, 2, 30);
printArray(A, D);
return 0;
}
|
Java
class GFG {
static void initializeDiffArray( int A[], int D[])
{
int n = A.length;
D[ 0 ] = A[ 0 ];
D[n] = 0 ;
for ( int i = 1 ; i < n; i++)
D[i] = A[i] - A[i - 1 ];
}
static void update( int D[], int l, int r, int x)
{
D[l] += x;
D[r + 1 ] -= x;
}
static int printArray( int A[], int D[])
{
for ( int i = 0 ; i < A.length; i++) {
if (i == 0 )
A[i] = D[i];
else
A[i] = D[i] + A[i - 1 ];
System.out.print(A[i] + " " );
}
System.out.println();
return 0 ;
}
public static void main(String[] args)
{
int A[] = { 10 , 5 , 20 , 40 };
int n = A.length;
int D[] = new int [n + 1 ];
initializeDiffArray(A, D);
update(D, 0 , 1 , 10 );
printArray(A, D);
update(D, 1 , 3 , 20 );
update(D, 2 , 2 , 30 );
printArray(A, D);
}
}
|
Python3
def initializeDiffArray( A):
n = len (A)
D = [ 0 for i in range ( 0 , n + 1 )]
D[ 0 ] = A[ 0 ]; D[n] = 0
for i in range ( 1 , n ):
D[i] = A[i] - A[i - 1 ]
return D
def update(D, l, r, x):
D[l] + = x
D[r + 1 ] - = x
def printArray(A, D):
for i in range ( 0 , len (A)):
if (i = = 0 ):
A[i] = D[i]
else :
A[i] = D[i] + A[i - 1 ]
print (A[i], end = " " )
print ("")
A = [ 10 , 5 , 20 , 40 ]
D = initializeDiffArray(A)
update(D, 0 , 1 , 10 )
printArray(A, D)
update(D, 1 , 3 , 20 )
update(D, 2 , 2 , 30 )
printArray(A, D)
|
C#
using System;
class GFG {
static void initializeDiffArray( int []A, int []D)
{
int n = A.Length;
D[0] = A[0];
D[n] = 0;
for ( int i = 1; i < n; i++)
D[i] = A[i] - A[i - 1];
}
static void update( int []D, int l, int r, int x)
{
D[l] += x;
D[r + 1] -= x;
}
static int printArray( int []A, int []D)
{
for ( int i = 0; i < A.Length; i++) {
if (i == 0)
A[i] = D[i];
else
A[i] = D[i] + A[i - 1];
Console.Write(A[i] + " " );
}
Console.WriteLine();
return 0;
}
public static void Main()
{
int []A = { 10, 5, 20, 40 };
int n = A.Length;
int []D = new int [n + 1];
initializeDiffArray(A, D);
update(D, 0, 1, 10);
printArray(A, D);
update(D, 1, 3, 20);
update(D, 2, 2, 30);
printArray(A, D);
}
}
|
Javascript
<script>
function initializeDiffArray( A){
let n = A.length;
let D= [];
D[0] = A[0], D[n] = 0;
for (let i = 1; i < n; i++)
D[i] = A[i] - A[i - 1];
return D;
}
function update(D, l, r, x){
D[l] += x;
D[r + 1] -= x;
return D;
}
function printArray( A, D){
for (let i = 0; i < A.length; i++) {
if (i == 0)
A[i] = D[i];
else
A[i] = D[i] + A[i - 1];
document.write( A[i]+ " " );
}
document.write( "<br>" );
}
let A = [ 10, 5, 20, 40 ];
let D = initializeDiffArray(A);
D = update(D, 0, 1, 10);
printArray(A, D);
D = update(D, 1, 3, 20);
D = update(D, 2, 2, 30);
printArray(A, D);
</script>
|
Output:
20 15 20 40
20 35 70 60
Time complexity: O(n)
Auxiliary Space: O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!