Range queries for alternatively addition and subtraction on given Array
Given an array arr[] of N integers and Q queries where every row consists of two numbers L and R which denotes the range [L, R], the task is to find value of alternate addition and subtraction of the array element between range [L, R].
Examples:
Input: arr[] = {10, 13, 15, 2, 45, 31, 22, 3, 27}, Q[][] = {{2, 5}, {6, 8}, {1, 7}, {4, 8}, {0, 5}}
Output: 27 46 -33 60 24
Explanation:
Result of query {2, 5} is 27 ( 15 – 2 + 45 – 31)
Result of query {6, 8} is 46 ( 22 – 3 + 27)
Result of query {1, 7} is -33 ( 13 – 15 + 2 – 45 + 31 – 22 + 3)
Result of query {4, 8} is 60 ( 45 – 31 + 22 – 3 + 27)
Result of query {0, 5} is 24 ( 10 – 13 + 15 – 2 + 45 – 31)Input: arr[] = {1, 1, 1, 1, 1, 1, 1, 1, 1}, Q[] = {{2, 5}, {6, 8}, {1, 7}, {4, 8}, {0, 5}}
Output: 0 1 1 1 0
Naive Approach: The naive idea is to iterate from index L to R for each query and find the value of alternatively adding and subtracting the elements of the array and print the value after the operations performed.
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Structure to represent a range query struct Query { int L, R; }; // Function to find the result of // alternatively adding and subtracting // elements in the range [L, R] int findResultUtil(int arr[], int L, int R) { int result = 0; // A boolean variable flag to // alternatively add and subtract bool flag = false; // Iterate from [L, R] for (int i = L; i <= R; i++) { // if flag is false, then // add & toggle the flag if (flag == false) { result = result + arr[i]; flag = true; } // if flag is true subtract // and toggle the flag else { result = result - arr[i]; flag = false; } } // Return the final result return result; } // Function to find the value // for each query void findResult(int arr[], int n, Query q[], int m) { // Iterate for each query for (int i = 0; i < m; i++) { cout << findResultUtil(arr, q[i].L, q[i].R) << " "; } } // Driver Code int main() { // Given array int arr[] = { 10, 13, 15, 2, 45, 31, 22, 3, 27 }; int n = sizeof(arr) / sizeof(arr[0]); // Given Queries Query q[] = { { 2, 5 }, { 6, 8 }, { 1, 7 }, { 4, 8 }, { 0, 5 } }; int m = sizeof(q) / sizeof(q[0]); // Function Call findResult(arr, n, q, m); return 0; } |
27 46 -33 60 24
Time Complexity: O(N*Q)
Auxiliary Space: O(1)
Efficient Approach: The efficient approach is to use Prefix Sum Array to solve the above problem. Below are the steps:
- Initialize the first element of prefix array(say pre[]) to first element of the arr[].
- Traverse through index from 1 to N-1 and alternative add and subtract the elements of arr[i] from pre[i-1] and store it in pre[i] to make a prefix array.
- Now, Iterate through each query from 1 to Q, and find the result on the basis of below cases:
- Case 1: If L is zero then result is pre[R].
- Case 2: If L is non-zero, find the result using the equation:
result = Query(L, R) = pre[R] – pre[L - 1]
- If L is odd multiply the above result by -1.
- Print modified array after executing the commands of addition and subtraction
- Minimum steps in which N can be obtained using addition or subtraction at every step
- Array range queries over range queries
- Min-Max Range Queries in Array
- Find the Initial Array from given array after range sum queries
- Range product queries in an array
- Queries for number of array elements in a range with Kth Bit Set
- Range Queries for Frequencies of array elements
- Queries for bitwise OR in the index range [L, R] of the given array
- Queries for bitwise AND in the index range [L, R] of the given array
- Array range queries for elements with frequency same as value
- Array range queries for searching an element
- Range Update Queries to XOR with 1 in a Binary Array.
- Queries for GCD of all numbers of an array except elements in a given range
- Count of divisors of product of an Array in range L to R for Q queries
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Structure to represent a query struct Query { int L, R; }; // This function fills the Prefix Array void fillPrefixArray(int arr[], int n, int prefixArray[]) { // Initialise the prefix array prefixArray[0] = arr[0]; // Iterate all the element of arr[] // and update the prefix array for (int i = 1; i < n; i++) { // If n is even then, add the // previous value of prefix array // with the current value of arr if (i % 2 == 0) { prefixArray[i] = prefixArray[i - 1] + arr[i]; } // if n is odd, then subtract // the previous value of prefix // Array from current value else { prefixArray[i] = prefixArray[i - 1] - arr[i]; } } } // Function to find the result of // alternatively adding and subtracting // elements in the range [L< R] int findResultUtil(int prefixArray[], int L, int R) { int result; // Case 1 : when L is zero if (L == 0) { result = prefixArray[R]; } // Case 2 : When L is non zero else { result = prefixArray[R] - prefixArray[L - 1]; } // If L is odd means range starts from // odd position multiply result by -1 if (L & 1) { result = result * (-1); } // Return the final result return result; } // Function to find the sum of all // alternative add and subtract // between ranges [L, R] void findResult(int arr[], int n, Query q[], int m) { // Declare prefix array int prefixArray[n]; // Function Call to fill prefix arr[] fillPrefixArray(arr, n, prefixArray); // Iterate for each query for (int i = 0; i < m; i++) { cout << findResultUtil(prefixArray, q[i].L, q[i].R) << " "; } } // Driver Code int main() { // Given array int arr[] = { 10, 13, 15, 2, 45, 31, 22, 3, 27 }; int n = sizeof(arr) / sizeof(arr[0]); // Given Queries Query q[] = { { 2, 5 }, { 6, 8 }, { 1, 7 }, { 4, 8 }, { 0, 5 } }; int m = sizeof(q) / sizeof(q[0]); // Function Call findResult(arr, n, q, m); return 0; } |
27 46 -33 60 24
Time Complexity: O(N + Q)
Auxiliary Space: O(N)
Got what you were looking for? Learn more and become self sufficient. Start learning Data Sructures & Algorithms with the help of the most trusted DSA Self Paced course, and that too at the most student-friendly price.
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

