
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- 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++
- Find the Number Of Subarrays Having Sum in a Given Range in C++
- Find the number of subarrays have bitwise OR >= K using C++
- Find the Number of Subarrays with Odd Sum using C++
- Maximum subarray size, such that all subarrays of that size have sum less than k in C++
- Largest number less than X having at most K set bits in C++
- Maximum subarray size, such that all subarrays of that size have sum less than k in C++ program
- Count all subsequences having product less than K in C++
- Count of alphabets having ASCII value less than and greater than k in C++
- Largest subarray having sum greater than k in C++
- Two Sum Less Than K in Python
- Find number of subarrays with even sum in C++
- Maximum sum subarray having sum less than or equal to given sums in C++
