- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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.

- Related Questions & Answers
- Find the Number Of Subarrays Having Sum in a Given Range in C++
- Find the Number of subarrays having sum less than K using C++
- Find the Number of subarrays having sum of the form k^m, m >= 0 using C++
- Find the Number of Subarrays with Odd Sum using C++
- Find the Number of Prefix Sum Prime in Given Range Query using C++
- Find the Number Whose Sum of XOR with Given Array Range is Maximum using C++
- Find number of subarrays with even sum in C++
- Program to find count of numbers having odd number of divisors in given range in C++
- Count number of triplets in an array having sum in the range [a,b] in C++
- PHP program to find the sum of odd numbers within a given range
- Python - Find the number of prime numbers within a given range of numbers
- Find the Number of Subarrays with m Odd Numbers using C++
- Program to find maximum number of non-overlapping subarrays with sum equals target using Python
- Find the number of subarrays have bitwise OR >= K using C++
- Program to find expected sum of subarrays of a given array by performing some operations in Python