# Find the Number of subarrays having sum less than K using C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

In this article, we will find out the number of subarrays having a sum less than K using C++. In this problem, we have an array arr[] and an integer K. So now we have to find subarrays that have a sum less than K. Here is the example −

Input : arr[] = {1, 11, 2, 3, 15}
K = 10
Output : 4
{1}, {2}, {3} and {2, 3}

## Approach to Find Solution

Now we will use two different methods to solve the given problem −

## Brute Force

In this approach, we will iterate through all the subarrays and calculate their sum and compare with k if the sum is less than k to increment our answer.

## Example

#include <bits/stdc++.h>
using namespace std;
int main(){
int arr[] = {1, 11, 2, 3, 15}; // given array
int k = 10; // given k
int size = sizeof(arr) / sizeof(int); // size of our array.
int ans = 0; // counter variable.
for(int i = 0; i < size; i++){ // outer loop.
int sum = 0;
for(int j = i; j < size; j++){ // inner loop.
sum = sum + arr[j];
if(sum < k) // comparing with k.
ans++; // incrementing our ans if sum is less than k.
}
}
cout << ans << "\n";
return 0;
}

## Output

4

However, this approach is not very good as it's the time complexity is very high O(N*N), where n is the size of our array.

We'll look at an alternative solution using the Sliding Window method(That will help us decrease the time complexity of the program).

## Efficient Approach

Unlike Brute Force, we will not be going through all the subarrays. Instead, we will traverse only when the sum of a subarray exceeds k and moves our left border till our right border and keeps repeating until the whole array is traversed.

## Example

#include <bits/stdc++.h>
using namespace std;
int main(){
int arr[] = {1, 11, 2, 3, 15}; // given array
int k = 10; // given k
int size = sizeof(arr) / sizeof(int); // size of our array.
int ans = 0; // counter variable.
int start = 0; // left border.
int end = 0; // right border.
int sum = 0;
while(end < size && start < size){ // till the whole array is traversed.
while(sum >= k && start < end){
sum = sum - arr[start];
start++;
}
if(end >= start)
ans = ans + end - start;
sum += arr[end];
end++;
}
cout << ans << "\n";
return 0;
}

## Output

4

We use the Sliding Window Technique to make our program faster or time-efficient to run for bigger constraints in this approach.

## Explanation of the Above Code

In this approach, we are normally traversing till our sum is less than k and incrementing our answer according to it is now the crucial change in the code that occurs when the sum becomes greater or equal to k. In this situation, we start to increment our left border smaller to our right border or till the sum is greater or equal to k. As we process further, it traverses through other subarrays that can be formed. These new subarrays whose sum is less than k are added to our answer, so our answer is incremented.

This approach is very efficient compared to the earlier Brute Force we applied as its time complexity is O(N), where N is the size of our array.

## Conclusion

In this article, we solve a problem to find the Number of subarrays with a sum less than k using the Sliding Window Technique. 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.