Find Array obtained after adding terms of AP for Q queries
Last Updated :
08 Mar, 2023
Given an array A[] consisting of N integers and Q queries, say Query[][] of the form {L, R, a, d} such that each query represents the infinite AP with the first term as a and common difference d. The task is to print the updated array after performing the given queries such that for each query {L, R, a, d} add the value of (i – L + 1)th term of the arithmetic progression to A[i] for every index i over the range [L, R].
Examples:
Input: A[]= {5, 4, 2, 8}, Q = 2, Query[][] = {{1, 2, 1, 3}, {1, 4, 4, 1}}
Output: 10 13 8 15
Explanation:
Following are the queries performed:
Query 1: The arithmetic progression is {1, 4, 7, …}. After adding the first term of the progression to index 1 and the second term of the progression to index 2, the array modifies to {6, 8, 2, 8}.
Query 2: The arithmetic progression is {4, 5, 6, 7, 8, …}. After adding 4 to A[1], 5 to A[2], 6 to A[3] and 7 to A[4] the array modifies to {10, 13, 8 15}.
Therefore, the resultant array is {10, 13, 8 15}.
Input: A[] = {1, 2, 3, 4, 5}, Q = 3, Query[][] = {{1, 2, 1, 3}, {1, 3, 4, 1}, {1, 4, 1, 2}}
Output: 7 14 14 11 5
Approach: The given problem can be solved by iterating over the range [L, R] in each operation and add the corresponding term of the given arithmetic progression at each index i. Follow the steps below to solve the problem:
- Traverse the array, Query[][] using the variable i and for each query {L, R, a, d} perform the following steps:
- Traverse the array A[], in the range [L – 1, R – 1] using the variable j
- Add the value of a to the value of A[j].
- Increment the value of a by d.
- After completing the above steps, print the array A[] as the resultant updated array.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void addAP( int A[], int Q, int operations[2][4])
{
for ( int j = 0; j < 2; ++j)
{
int L = operations[j][0], R = operations[j][1], a = operations[j][2], d = operations[j][3];
int curr = a;
for ( int i = L - 1; i < R; i++){
A[i] += curr;
curr += d;
}
}
for ( int i = 0; i < 4; ++i)
cout << A[i] << " " ;
}
int main() {
int A[] = {5, 4, 2, 8};
int Q = 2;
int Query[2][4] = {{1, 2, 1, 3}, {1, 4, 4, 1}};
addAP(A, Q, Query);
return 0;
}
|
C
#include <stdio.h>
void addAP( int A[], int Q, int operations[2][4])
{
for ( int j = 0; j < 2; ++j)
{
int L = operations[j][0], R = operations[j][1], a = operations[j][2], d = operations[j][3];
int curr = a;
for ( int i = L - 1; i < R; i++){
A[i] += curr;
curr += d;
}
}
for ( int i = 0; i < 4; ++i)
printf ( "%d " , A[i]);
}
int main() {
int A[] = {5, 4, 2, 8};
int Q = 2;
int Query[2][4] = {{1, 2, 1, 3}, {1, 4, 4, 1}};
addAP(A, Q, Query);
return 0;
}
|
Java
class GFG {
public static void addAP( int A[], int Q, int [][] operations)
{
for ( int j = 0 ; j < 2 ; ++j) {
int L = operations[j][ 0 ], R = operations[j][ 1 ],
a = operations[j][ 2 ], d = operations[j][ 3 ];
int curr = a;
for ( int i = L - 1 ; i < R; i++) {
A[i] += curr;
curr += d;
}
}
for ( int i = 0 ; i < 4 ; ++i)
System.out.print(A[i] + " " );
}
public static void main(String args[])
{
int A[] = { 5 , 4 , 2 , 8 };
int Q = 2 ;
int query[][] = { { 1 , 2 , 1 , 3 }, { 1 , 4 , 4 , 1 } };
addAP(A, Q, query);
}
}
|
Python3
def addAP(A, Q, operations):
for L, R, a, d in operations:
curr = a
for i in range (L - 1 , R):
A[i] + = curr
curr + = d
for i in A:
print (i, end = ' ' )
A = [ 5 , 4 , 2 , 8 ]
Q = 2
Query = [( 1 , 2 , 1 , 3 ), ( 1 , 4 , 4 , 1 )]
addAP(A, Q, Query)
|
C#
using System;
class GFG{
public static void addAP( int [] A, int Q,
int [,] operations)
{
for ( int j = 0; j < 2; ++j)
{
int L = operations[j, 0], R = operations[j, 1],
a = operations[j, 2], d = operations[j, 3];
int curr = a;
for ( int i = L - 1; i < R; i++)
{
A[i] += curr;
curr += d;
}
}
for ( int i = 0; i < 4; ++i)
Console.Write(A[i] + " " );
}
public static void Main( string [] args)
{
int [] A = { 5, 4, 2, 8 };
int Q = 2;
int [,] query = { { 1, 2, 1, 3 },
{ 1, 4, 4, 1 } };
addAP(A, Q, query);
}
}
|
Javascript
<script>
function addAP(A, Q, operations)
{
for ( let Q of operations)
{
let L = Q[0], R = Q[1], a = Q[2], d = Q[3]
curr = a
for (let i = L - 1; i < R; i++){
A[i] += curr
curr += d
}
}
for (let i of A){
document.write(i + " " )
}
}
let A = [5, 4, 2, 8]
let Q = 2
let Query = [[1, 2, 1, 3], [1, 4, 4, 1]]
addAP(A, Q, Query)
</script>
|
Time Complexity: O(N*Q)
Auxiliary Space: O(1)
Efficient Approach: Let us declare 2 variables X which represents all the differences running that is ( d1 + d2 + d3 … ), and S which is the cumulative Sum that represents the resultant sum of all applied AP at index {i-1}.
So the cumulative sum of the new index {i} will be:
1. X[i] =X[i-1] + X’ { correction of difference i.e. starting or ending of AP}
2. S[i] = S[i-1] + S’ { correction of Cumulative sum i.e. starting or ending of AP}
3. S[i] = S[i] + X[i]
4. Arr[i] = Arr[i] + S[i]
Below is the implementation of the above approach:
C++14
#include<bits/stdc++.h>
const char nl = '\n' ;
using namespace std;
void mysolution(vector< int > &arr, vector<vector< int >> &query){
int n = arr.size();
vector< int > collective_D(n+1, 0);
vector< int > S_collective_correction(n+1,0);
int l, r, d, a, X=0, S=0;
for ( auto q: query){
l = q[0]; r = q[1]; a = q[2]; d = q[3];
r--;
collective_D[l] += d;
collective_D[r+1] -= d;
S_collective_correction[l-1] += (a);
S_collective_correction[r+1] -= a + (r+1-l)*d;
}
for ( int i = 0; i < n; i++){
X += collective_D[i];
S += S_collective_correction[i];
S += X;
arr[i] += (S);
}
for ( int i: arr) cout<<i<< ' ' ;
cout<<nl;
}
void solve(){
vector< int > v = {5,4,2,8};
vector<vector< int >> query = {{1,2,1,3},{1,4,4,1}};
mysolution(v, query);
}
signed main() {
ios_base::sync_with_stdio( false );
cin.tie(NULL);
int T = 1;
while (T--){
solve();
}
return 0;
}
|
Java
import java.util.*;
class Main {
static void mysolution( int [] arr, int [][] query) {
int n = arr.length;
int [] collective_D = new int [n+ 1 ];
int [] S_collective_correction = new int [n+ 1 ];
int l, r, d, a, X= 0 , S= 0 ;
for ( int [] q: query){
l = q[ 0 ]; r = q[ 1 ]; a = q[ 2 ]; d = q[ 3 ];
r--;
collective_D[l] += d;
collective_D[r+ 1 ] -= d;
S_collective_correction[l- 1 ] += (a);
S_collective_correction[r+ 1 ] -= a + (r+ 1 -l)*d;
}
for ( int i = 0 ; i < n; i++){
X += collective_D[i];
S += S_collective_correction[i];
S += X;
arr[i] += (S);
}
for ( int i: arr) System.out.print(i + " " );
System.out.println();
}
public static void main(String[] args) {
int [] v = { 5 , 4 , 2 , 8 };
int [][] query = {{ 1 , 2 , 1 , 3 },{ 1 , 4 , 4 , 1 }};
mysolution(v, query);
}
}
|
Python3
def mysolution(arr, query):
n = len (arr)
collective_D = [ 0 ] * (n + 1 )
S_collective_correction = [ 0 ] * (n + 1 )
X, S = 0 , 0
for q in query:
l, r, a, d = q
r - = 1
collective_D[l] + = d
collective_D[r + 1 ] - = d
S_collective_correction[l - 1 ] + = a
S_collective_correction[r + 1 ] - = a + (r + 1 - l) * d
for i in range (n):
X + = collective_D[i]
S + = S_collective_correction[i]
S + = X
arr[i] + = S
print ( * arr)
v = [ 5 , 4 , 2 , 8 ]
query = [[ 1 , 2 , 1 , 3 ],[ 1 , 4 , 4 , 1 ]]
mysolution(v, query)
|
Javascript
function mysolution(arr, query) {
const n = arr.length;
const collective_D = new Array(n+1).fill(0);
const S_collective_correction = new Array(n+1).fill(0);
let l, r, d, a, X=0, S=0;
for (const q of query) {
[l, r, a, d] = q;
r--;
collective_D[l] += d;
collective_D[r+1] -= d;
S_collective_correction[l-1] += a;
S_collective_correction[r+1] -= a + (r+1-l)*d;
}
for (let i = 0; i < n; i++) {
X += collective_D[i];
S += S_collective_correction[i];
S += X;
arr[i] += S;
}
console.log(arr.join( ' ' ));
}
const v = [5, 4, 2, 8];
const query = [[1, 2, 1, 3], [1, 4, 4, 1]];
mysolution(v, query);
|
C#
using System;
class MainClass {
static void Mysolution( int [] arr, int [][] query)
{
int n = arr.Length;
int [] collective_D = new int [n + 1];
int [] S_collective_correction = new int [n + 1];
int l, r, d, a, X = 0, S = 0;
foreach ( int [] q in query)
{
l = q[0];
r = q[1];
a = q[2];
d = q[3];
r--;
collective_D[l] += d;
collective_D[r + 1] -= d;
S_collective_correction[l - 1] += a;
S_collective_correction[r + 1]
-= a + (r + 1 - l) * d;
}
for ( int i = 0; i < n; i++) {
X += collective_D
[i];
S += S_collective_correction
[i];
S += X;
arr[i] += S;
}
foreach ( int i in arr) Console.Write(i + " " );
Console.WriteLine();
}
public static void Main()
{
int [] v = { 5, 4, 2, 8 };
int [][] query
= new int [][] { new int [] { 1, 2, 1, 3 },
new int [] { 1, 4, 4, 1 } };
Mysolution(v, query);
}
}
|
Time Complexity : max(O(q) , O(N))
Space Complexity: O(N)
Share your thoughts in the comments
Please Login to comment...