# 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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ` `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); ` ` `  `    ``// Given Queries ` `    ``Query q[] = { { 2, 5 }, { 6, 8 },  ` `                  ``{ 1, 7 }, { 4, 8 },  ` `                  ``{ 0, 5 } }; ` ` `  `    ``int` `m = ``sizeof``(q) / ``sizeof``(q); ` ` `  `    ``// Function Call ` `    ``findResult(arr, n, q, m); ` `    ``return` `0; ` `} `

Output:

```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:

1. Initialize the first element of prefix array(say pre[]) to first element of the arr[].
2. 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.
3. 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.
4. Below is the implementation of the above approach:

 `// C++ program for the above approach ` `#include ` `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 = arr; ` ` `  `    ``// 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); ` ` `  `    ``// Given Queries ` `    ``Query q[] = { { 2, 5 }, { 6, 8 }, ` `                  ``{ 1, 7 }, { 4, 8 },  ` `                  ``{ 0, 5 } }; ` ` `  `    ``int` `m = ``sizeof``(q) / ``sizeof``(q); ` ` `  `    ``// Function Call ` `    ``findResult(arr, n, q, m); ` `    ``return` `0; ` `} `

Output:

```27 46 -33 60 24
```

Time Complexity: O(N + Q)
Auxiliary Space: O(N)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.