Skip to content
Related Articles

Related Articles

Improve Article

Find Array obtained after adding terms of AP for Q queries

  • Last Updated : 22 Jul, 2021

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++




// C++ program for the above approach
#include <iostream>
using namespace std;
 
// Function to find array after performing
// the given query to the array elements
void addAP(int A[], int Q, int operations[2][4])
{
 
    // Traverse the given query
    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;
 
        // Traverse the given array
        for(int i = L - 1; i < R; i++){
             
            // Update the value of A[i]
            A[i] += curr;
 
            // Update the value of curr
            curr += d;
        }
    }
     
    // Print the array elements
    for (int i = 0; i < 4; ++i)
        cout << A[i] << " ";
}
 
// Driver Code
int main() {
    int A[] = {5, 4, 2, 8};
    int Q = 2;
    int Query[2][4] = {{1, 2, 1, 3}, {1, 4, 4, 1}};
     
    // Function Call
    addAP(A, Q, Query);
    return 0;
}
 
// This code is contributed by shubhamsingh10.

C




// C program for the above approach
#include <stdio.h>
 
// Function to find array after performing
// the given query to the array elements
void addAP(int A[], int Q, int operations[2][4])
{
 
    // Traverse the given query
    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;
 
        // Traverse the given array
        for(int i = L - 1; i < R; i++){
             
            // Update the value of A[i]
            A[i] += curr;
 
            // Update the value of curr
            curr += d;
        }
    }
     
    // Print the array elements
    for (int i = 0; i < 4; ++i)
        printf("%d ", A[i]);
}
 
// Driver Code
int main() {
    int A[] = {5, 4, 2, 8};
    int Q = 2;
    int Query[2][4] = {{1, 2, 1, 3}, {1, 4, 4, 1}};
     
    // Function Call
    addAP(A, Q, Query);
    return 0;
}
 
// This code is contributed by shubhamsingh10.

Java




// Java program for the above approach
class GFG {
 
    // Function to find array after performing
    // the given query to the array elements
    public static void addAP(int A[], int Q, int[][] operations)
    {
 
        // Traverse the given query
        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;
 
            // Traverse the given array
            for (int i = L - 1; i < R; i++) {
 
                // Update the value of A[i]
                A[i] += curr;
 
                // Update the value of curr
                curr += d;
            }
        }
 
        // Print the array elements
        for (int i = 0; i < 4; ++i)
            System.out.print(A[i] + " ");
    }
 
    // Driver Code
    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 } };
 
        // Function Call
        addAP(A, Q, query);
    }
}
 
// This code is contributed by _saurabh_jaiswal

Python3




# Python program for the above approach
 
# Function to find array after performing
# the given query to the array elements
def addAP(A, Q, operations):
 
    # Traverse the given query
    for L, R, a, d in operations:
 
        curr = a
 
        # Traverse the given array
        for i in range(L-1, R):
           
            # Update the value of A[i]
            A[i] += curr
 
            # Update the value of curr
            curr += d
     
    # Print the array elements
    for i in A:
        print(i, end =' ')
 
 
# Driver Code
 
A = [5, 4, 2, 8]
Q = 2
Query = [(1, 2, 1, 3), (1, 4, 4, 1)]
 
# Function Call
addAP(A, Q, Query)

C#




// C# program for the above approach
using System;
 
class GFG{
 
// Function to find array after performing
// the given query to the array elements
public static void addAP(int[] A, int Q,
                         int[,] operations)
{
     
    // Traverse the given query
    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;
 
        // Traverse the given array
        for(int i = L - 1; i < R; i++)
        {
             
            // Update the value of A[i]
            A[i] += curr;
 
            // Update the value of curr
            curr += d;
        }
    }
 
    // Print the array elements
    for(int i = 0; i < 4; ++i)
        Console.Write(A[i] + " ");
}
 
// Driver code
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 } };
 
    // Function Call
    addAP(A, Q, query);
}
}
 
// This code is contributed by avijitmondal1998

Javascript




<script>
// Javascript program for the above approach
 
// Function to find array after performing
// the given query to the array elements
function addAP(A, Q, operations)
{
 
    // Traverse the given query
    for( let Q of operations)
    {
        let L = Q[0], R = Q[1], a = Q[2], d = Q[3]
        curr = a
 
        // Traverse the given array
        for(let i = L - 1; i < R; i++){
             
            // Update the value of A[i]
            A[i] += curr
 
            // Update the value of curr
            curr += d
        }
    }
     
    // Print the array elements
    for (let i of A){
        document.write(i + " ")
    }
}
 
// Driver Code
let A = [5, 4, 2, 8]
let Q = 2
let Query = [[1, 2, 1, 3], [1, 4, 4, 1]]
 
// Function Call
addAP(A, Q, Query)
 
// Thi code is contributed by gfgking.
</script>
Output
10 13 8 15 

Time Complexity: O(N*Q)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :