- 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 less than K using C++

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.

- Related Questions & Answers
- Find the Number of subarrays having sum of the form k^m, m >= 0 using C++
- Count the number of words having sum of ASCII values less than and greater than k in C++
- Find the Number Of Subarrays Having Sum in a Given Range using C++
- Two Sum Less Than K in Python
- Find the Number Of Subarrays Having Sum in a Given Range in C++
- Largest number less than X having at most K set bits in C++
- Find the Number of Subarrays with Odd Sum using C++
- Count of alphabets having ASCII value less than and greater than k in C++
- Count all subsequences having product less than K in C++
- Maximum subarray size, such that all subarrays of that size have sum less than k in C++
- Maximum subarray size, such that all subarrays of that size have sum less than k in C++ program
- Find the number of subarrays have bitwise OR >= K using C++
- Largest subarray having sum greater than k in C++
- Largest number less than N with digit sum greater than the digit sum of N in C++
- Maximum sum subarray having sum less than or equal to given sums in C++