Find the Number Of Subarrays Having Sum in a Given Range using C++

C++Server Side ProgrammingProgramming

In this article, we will solve the number of subarrays having sum in a given range using the C++ program. We have an array arr[] of positive integers, and a range {L, R} and we have to calculate the total number of subarrays having sum in the given range form L to R. So here is the simple example for the problem −

Input : arr[] = {1, 4, 6}, L = 3, R = 8

Output : 3

The subarrays are {1, 4}, {4}, {6}.


Input : arr[] = {2, 3, 5, 8}, L = 4, R = 13

Output : 6

The subarrays are {2, 3}, {2, 3, 5}, {3, 5},
{5}, {5, 8}, {8}.

Approaches to Find the Solution

We will explain two methods to solve this problem using C++ problems −

Brute Force Approach

The most basic brute force approach is used to calculate the sum of every subarray and then find if that sum exists in the given range or not. (But this approach will cost us a lot of time as its time complexity is O(n*n) where n is the size of the array).

Efficient approach

To save time, we use another method which is known as an efficient approach.Now the efficient approach is using the sliding window technique, using this technique we will calculate our result much faster or efficiently in O(n).

Example

#include <bits/stdc++.h>
using namespace std;
int subCount(int *arr, int n, int x){
    int start = 0, end = 0, sum = 0, count = 0;
    while (end < n){ // we will be moving right border in this loop
        sum = sum + arr[end];
        while(start <= end && sum >= x){ // this loop will move our left border
            sum = sum - arr[start]; // we will decrement sum while moving left border.
                                   // For excluding the previous elements.
            start++;                // and move the left border.
        }
        count = count + ((end - start) + 1); // counting the subarrays.
        end++;
    }
    return count;
}
int main(){
    int arr[] = { 1, 4, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int L = 3;
    int R = 8;
    int answer;
    answer = subCount(arr, n, R)  - subCount(arr, n, (L - 1)); // Final Answer.
    cout << answer << "
";     return 0; }

Output

3

Explanation of the Above Code

In this approach, we are counting the number of subarrays with a sum less than the upper limit of the given range, and then we are subtracting the number of subarrays whose sum is less than the lower limit of our given range using our subcount function.

Subcount Function

This function uses the sliding window technique for finding the count of subarrays whose count is less than x.

At first, we start with both 'end and start' with 0 as their value. As we traverse the array, we maintain the sum of the elements from the start till the end. After that, if our start is equal to our end and the sum is greater or equal to x, we start moving our start and keep decreasing our sum as we are taking off the elements from the sum.

Till our sum becomes less than x or our start becomes greater than the end. Now we increment the count by the subarray count and then increment the right border with 1. Now, after our outer loop ends, we return the total count of our subarrays.

Conclusion

In this article, we solve a problem to find the number of subarrays having sum in a given range in O(n) time complexity using the sliding window technique. We also learned from the C++ program for this problem and the complete approach (Normal and efficient) by which we can solve this problem easily. We can write the same program in other languages such as C, java, python, etc.

raja
Updated on 25-Nov-2021 05:48:46

Advertisements