Queries to update array by adding or multiplying array elements and print the element present at specified index
Last Updated :
20 Jul, 2022
Given an array arr[] consisting of N integers and an array Q[] of M pairs representing a query of type {X, Y}, the task is to perform queries of the following type:
- Query(0, X): Add integer X to every array elements.
- Query(1, Y): Multiply each array element by Y.
- Query(2, i): Print the value at index i.
Examples:
Input: arr[] = {5, 1, 2, 3, 9}, Q[][] = {{0, 5}, {2, 4}, {1, 4}, {0, 2}, {2, 3}}
Output: 14 34
Explanation:
Below are the results after performing each query:
- Query(0, 5): Adding 5 to every array elements, modifies the array as arr[] = {10, 6, 7, 8, 14}.
- Query(2, 4): Print the array element at index 4, i.e., arr[4] = 14.
- Query(1, 4): Multiplying every array elements with 4, modifies the array as arr[] = {40, 24, 28, 32, 56}
- Query(0, 2): Adding 2 to every array elements, modifies the array as arr[] = {42, 26, 30, 34, 58}
- Query(2, 3): Print the element at index 4, i.e., arr[3] = 34.
Input: arr[] = {3, 1, 23, 45, 100}, Q[][] = {{1, 2}, {0, 10}, {2, 3}, {1, 5}, {2, 4}}
Output: 100 1050
Naive Approach: The simplest approach is to solve the given problem is to perform each query by traversing the given array and print the result accordingly for each query.
Time Complexity: O(N * M)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the following observations:
- Suppose A is an element and the following operation is performed:
- Adding a1 to A, then the value of A becomes A = A + a1.
- Multiply A by b1, then the value of A becomes A = b1 * A + b1 * a1.
- Add a2 to A, then the value of A becomes A = b1 * A + b1 * a1 + a2.
- Multiply A by b2, then the value of A becomes, A = b1 * b2 * A + b1 * b2 * a1 + a2 * b2
- Suppose Mul is the multiplication of all the integers of the query of type 1 and Add stores the value of all the query of type 0, and type 1 performed in the given order starting as 0. Then it can be observed from above that any array element arr[i] modifies to (arr[i] * Mul + Add).
Follow the steps below to solve the problem:
- Initialize two variables, say Mul as 1 and Add as 0, stores the multiplication of all the integers in the query of type 2 and stores the value got after performing the query of type 1 and 2 in the given order on a number 0.
- Traverse the given array of queries Q[][2] and perform the following steps:
- If Q[i][0] is 0, then increment the value of Add by Q[i][1].
- Otherwise, if the value of Q[i][0] is 1 then multiply Mul and Add by Q[i][1].
- Otherwise, print the value (arr[Q[i][1]] * Mul + Add) as the result for the query of type 2.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to modify the array
// by performing given queries
void Query(int arr[], int N,
vector<vector<int> > Q)
{
// Stores the multiplication
// of all integers of type 1
int mul = 1;
// Stores the value obtained after
// performing queries of type 1 & 2
int add = 0;
// Iterate over the queries
for (int i = 0; i < Q.size(); i++) {
// Query of type 0
if (Q[i][0] == 0) {
// Update the value of add
add = add + Q[i][1];
}
// Query of type 1
else if (Q[i][0] == 1) {
// Update the value of mul
mul = mul * Q[i][1];
// Update the value of add
add = add * Q[i][1];
}
// Otherwise
else {
// Store the element at index Q[i][1]
int ans = arr[Q[i][1]] * mul + add;
// Print the result for
// the current query
cout << ans << " ";
}
}
}
// Driver Code
int main()
{
int arr[] = { 3, 1, 23, 45, 100 };
int N = sizeof(arr) / sizeof(arr[0]);
vector<vector<int> > Q = {
{ 1, 2 }, { 0, 10 },
{ 2, 3 }, { 1, 5 }, { 2, 4 }
};
Query(arr, N, Q);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG
{
// Function to modify the array
// by performing given queries
static void Query(int arr[], int N, int Q[][])
{
// Stores the multiplication
// of all integers of type 1
int mul = 1;
// Stores the value obtained after
// performing queries of type 1 & 2
int add = 0;
// Iterate over the queries
for (int i = 0; i < Q.length; i++) {
// Query of type 0
if (Q[i][0] == 0) {
// Update the value of add
add = add + Q[i][1];
}
// Query of type 1
else if (Q[i][0] == 1) {
// Update the value of mul
mul = mul * Q[i][1];
// Update the value of add
add = add * Q[i][1];
}
// Otherwise
else {
// Store the element at index Q[i][1]
int ans = arr[Q[i][1]] * mul + add;
// Print the result for
// the current query
System.out.print(ans + " ");
}
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 3, 1, 23, 45, 100 };
int N = arr.length;
int Q[][] = { { 1, 2 },
{ 0, 10 },
{ 2, 3 },
{ 1, 5 },
{ 2, 4 } };
Query(arr, N, Q);
}
}
// This code is contributed by Kingash.
Python3
# Python3 program for the above approach
# Function to modify the array
# by performing given queries
def Query(arr, N, Q):
# Stores the multiplication
# of all integers of type 1
mul = 1
# Stores the value obtained after
# performing queries of type 1 & 2
add = 0
# Iterate over the queries
for i in range(len(Q)):
# Query of type 0
if (Q[i][0] == 0):
# Update the value of add
add = add + Q[i][1]
# Query of type 1
elif (Q[i][0] == 1):
# Update the value of mul
mul = mul * Q[i][1]
# Update the value of add
add = add * Q[i][1]
# Otherwise
else:
# Store the element at index Q[i][1]
ans = arr[Q[i][1]] * mul + add
# Print result for
# the current query
print(ans,end=" ")
# Driver Code
if __name__ == '__main__':
arr = [3, 1, 23, 45, 100]
N = len(arr)
Q = [
[ 1, 2 ], [ 0, 10 ],
[ 2, 3 ], [ 1, 5 ], [ 2, 4 ]
]
Query(arr, N, Q)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG {
// Function to modify the array
// by performing given queries
static void Query(int[] arr, int N, int[, ] Q)
{
// Stores the multiplication
// of all integers of type 1
int mul = 1;
// Stores the value obtained after
// performing queries of type 1 & 2
int add = 0;
// Iterate over the queries
for (int i = 0; i < Q.GetLength(0); i++) {
// Query of type 0
if (Q[i, 0] == 0) {
// Update the value of add
add = add + Q[i, 1];
}
// Query of type 1
else if (Q[i, 0] == 1) {
// Update the value of mul
mul = mul * Q[i, 1];
// Update the value of add
add = add * Q[i, 1];
}
// Otherwise
else {
// Store the element at index Q[i][1]
int ans = arr[Q[i, 1]] * mul + add;
// Print the result for
// the current query
Console.Write(ans + " ");
}
}
}
// Driver Code
public static void Main()
{
int[] arr = { 3, 1, 23, 45, 100 };
int N = arr.Length;
int[, ] Q = { { 1, 2 },
{ 0, 10 },
{ 2, 3 },
{ 1, 5 },
{ 2, 4 } };
Query(arr, N, Q);
}
}
// This code is contributed by ukasp.
JavaScript
<script>
// Javascript implementation of the above approach
// Function to modify the array
// by performing given queries
function Query(arr, N, Q)
{
// Stores the multiplication
// of all integers of type 1
let mul = 1;
// Stores the value obtained after
// performing queries of type 1 & 2
let add = 0;
// Iterate over the queries
for (let i = 0; i < Q.length; i++) {
// Query of type 0
if (Q[i][0] == 0) {
// Update the value of add
add = add + Q[i][1];
}
// Query of type 1
else if (Q[i][0] == 1) {
// Update the value of mul
mul = mul * Q[i][1];
// Update the value of add
add = add * Q[i][1];
}
// Otherwise
else {
// Store the element at index Q[i][1]
let ans = arr[Q[i][1]] * mul + add;
// Print the result for
// the current query
document.write(ans + " ");
}
}
}
// Driver Code
let arr = [ 3, 1, 23, 45, 100 ];
let N = arr.length;
let Q = [[ 1, 2 ],
[ 0, 10 ],
[ 2, 3 ],
[ 1, 5 ],
[ 2, 4 ]];
Query(arr, N, Q);
</script>
Time Complexity: O(M)
Auxiliary Space: O(1), since no extra space has been taken.
Similar Reads
Print modified array after performing queries to add (i - L + 1) to each element present in the range [L, R] Given an array arr[] consisting of N 0s (1-based indexing) and another array query[], with each row of the form {L, R}, the task for each query (L, R) is to add a value of (i - L + 1) over the range [L, R] and print the array arr[] obtained after performing all the queries. Examples: Input: arr[] =
13 min read
Rearrange and update array elements as specified by the given queries Given an array arr[] of size N and queries Q[][], the task is to perform the following types of queries on the given array. 0: Left shift the array by one position. 1: Right shift the array by one position.2 X Y: Update the value of arr[X] = Y.3 X: Print arr[X]. Example: Input: arr[]={1, 2, 3, 4, 5}
9 min read
Queries to print count of distinct array elements after replacing element at index P by a given element Given an array arr[] consisting of N integers and 2D array queries[][] consisting of Q queries of the form {p, x}, the task for each query is to replace the element at position p with x and print the count of distinct elements present in the array. Examples: Input: Q = 3, arr[] = {2, 2, 5, 5, 4, 6,
14 min read
Queries to calculate sum of array elements present at every Yth index starting from the index X Given an array arr[] of size N, and an array Q[][] with each row representing a query of the form { X, Y }, the task for each query is to find the sum of array elements present at indices X, X + Y, X + 2 * Y + ... Examples: Input: arr[] = { 1, 2, 7, 5, 4 }, Q[][] = { { 2, 1 }, { 3, 2 } } Output: 16
15+ min read
Queries to calculate average of an array after removing K smallest and largest elements with updates Given two positive integers N and K, initialize an empty array arr[] and Q number of queries of the following two types: addInteger(x): Insert element X in the array arr[]. If the size of the array becomes greater than N, then remove the element from the beginning of the array.calculateSpecialAverag
15+ min read
Queries to calculate GCD of an array after multiplying first or last K elements by X Given an array arr[] consisting of N positive integers and a 2D array queries[][] of the type {a, K, X} such that if the value of a is 1, then multiply first K array elements by X. Otherwise, multiply last K array elements by X. The task is to calculate GCD of the array after performing each query o
10 min read