# Range Sum Queries Without Updates using C++

C++Server Side ProgrammingProgramming

In this article, we will give an array of size n, which will be an integer. Then, we will compute the sum of elements from index L to index R and execute the queries multiple times, or we need to calculate the sum of the given range from [L, R]. For example −

Input : arr[] = {1, 2, 3, 4, 5}
L = 1, R = 3
L = 2, R = 4
Output : 9
12

Input : arr[] = {1, 2, 3, 4, 5}
L = 0, R = 4
L = 1, R = 2
Output : 15
5

## Approach to find The Solution

There are two solutions to this question. The first one is by the Brute Force approach and by the Prefix sum(Efficient) approach.

## Brute Force Approach

In this approach, we will traverse through the given range and print the sum.

## Example

#include<bits/stdc++.h>

using namespace std;

int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr)/sizeof(int); // size of given array.
int L1 = 1, R1 = 3;
int L2 = 2, R2 = 4;
int sum = 0;
for(int i = L1; i <= R1; i++) // traversing through the first range.
sum += arr[i];
cout << sum << "\n";
sum = 0;
for(int i = L2; i <= R2; i++) // traversing through the second range.
sum += arr[i];
cout << sum << "\n";
}

## Output

9
12

## Explanation of the Above Code

In this approach, we are simply traversing through the given ranges; in this case, this program is good as it has searching time complexity O(N), where N is the size of the given array. Still, this changes when we are given several queries Q then our complexity turns to O(N*Q), where Q is the number of queries and N is the size of the given array. Unfortunately, this time complexity can’t handle higher constraints, so now we will look into an efficient approach that will work for higher constraints.

## Efficient Approach

In this approach, we will make a new array named prefix which will be our prefix sum array, and then we answer the sum of ranges given.

## Example

#include<bits/stdc++.h>
using namespace std;

int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr)/sizeof(int); // size of given array.
int L1 = 1, R1 = 3;
int L2 = 2, R2 = 4;
int sum = 0;
int prefix[n];
for(int i = 0; i < n; i++){
sum += arr[i];
prefix[i] = sum;
}

if(L1) // to avoid segmentation fault
cout << prefix[R1] - prefix[L1 - 1] << "\n";
else
cout << prefix[R1] << "\n";

if(L2) // avoiding segmentation fault.
cout << prefix[R2] - prefix[L2 - 1] << "\n";
else
cout << prefix[R2] << "\n";
}

## Output

9
12

## Explanation of the above code

In this approach, we store the prefix sum values in an array called prefix. Now, this array makes our program very efficient as this gives us searching time complexity of O(1), which is the best complexity you can get, and therefore when we are given Q amount of queries, then our searching time complexity becomes O(Q) where Q is the number of queries.

## Conclusion

In this article, we solve a problem to find the Range sum queries without updates using the Prefix sum array. We also learned the C++ program for this problem and the complete approach ( Normal and efficient ) by which we solved this problem. We can write the same program in other languages such as C, java, python, and other languages. Hope you find this article helpful.

Published on 26-Nov-2021 10:07:48