Given an array arr[] of size N and an integer K > 0. The task is to find the number of subarrays with sum at least K.
Examples:
Input: arr[] = {6, 1, 2, 7}, K = 10
Output: 2
{6, 1, 2, 7} and {1, 2, 7} are the only valid subarrays.Input: arr[] = {3, 3, 3}, K = 5
Output: 3
Approach: For a fixed left index (say l), try to find the first index on the right of l (say r) such that (arr[l] + arr[l + 1] + … + arr[r]) ≥ K. Then add N – r + 1 to the required answer. Repeat this process for all the left indices.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the number of // subarrays with sum atleast k int k_sum(int a[], int n, int k) { // To store the right index // and the current sum int r = 0, sum = 0; // To store the number of sub-arrays int ans = 0; // For all left indexes for (int l = 0; l < n; l++) { // Get elements till current sum // is less than k while (sum < k) { if (r == n) break; else { sum += a[r]; r++; } } // No such subarray is possible if (sum < k) break; // Add all possible subarrays ans += n - r + 1; // Remove the left most element sum -= a[l]; } // Return the required answer return ans; } // Driver code int main() { int a[] = { 6, 1, 2, 7 }, k = 10; int n = sizeof(a) / sizeof(a[0]); cout << k_sum(a, n, k); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to return the number of // subarrays with sum atleast k static int k_sum(int a[], int n, int k) { // To store the right index // and the current sum int r = 0, sum = 0; // To store the number of sub-arrays int ans = 0; // For all left indexes for (int l = 0; l < n; l++) { // Get elements till current sum // is less than k while (sum < k) { if (r == n) break; else { sum += a[r]; r++; } } // No such subarray is possible if (sum < k) break; // Add all possible subarrays ans += n - r + 1; // Remove the left most element sum -= a[l]; } // Return the required answer return ans; } // Driver code public static void main (String[] args) { int a[] = { 6, 1, 2, 7 }, k = 10; int n = a.length; System.out.println(k_sum(a, n, k)); } } // This code is contributed by kanugargng |
Python3
# Python3 implementation of the approach # Function to return the number of # subarrays with sum atleast k def k_sum(a, n, k): # To store the right index # and the current sum r, sum = 0, 0; # To store the number of sub-arrays ans = 0; # For all left indexes for l in range(n): # Get elements till current sum # is less than k while (sum < k): if (r == n): break; else: sum += a[r]; r += 1; # No such subarray is possible if (sum < k): break; # Add all possible subarrays ans += n - r + 1; # Remove the left most element sum -= a[l]; # Return the required answer return ans; # Driver code a = [ 6, 1, 2, 7 ]; k = 10; n = len(a); print(k_sum(a, n, k)); # This code contributed by PrinciRaj1992 |
C#
// C# implementation of the approach using System; class GFG { // Function to return the number of // subarrays with sum atleast k static int k_sum(int []a, int n, int k) { // To store the right index // and the current sum int r = 0, sum = 0; // To store the number of sub-arrays int ans = 0; // For all left indexes for (int l = 0; l < n; l++) { // Get elements till current sum // is less than k while (sum < k) { if (r == n) break; else { sum += a[r]; r++; } } // No such subarray is possible if (sum < k) break; // Add all possible subarrays ans += n - r + 1; // Remove the left most element sum -= a[l]; } // Return the required answer return ans; } // Driver code public static void Main() { int []a = { 6, 1, 2, 7 }; int k = 10; int n = a.Length; Console.WriteLine(k_sum(a, n, k)); } } // This code is contributed by AnkitRai01 |
2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count the number of subarrays having a given XOR
- Count of all possible bitonic subarrays
- Count subarrays with non-zero sum in the given Array
- Count subarrays with same even and odd elements
- Count subarrays with Prime sum
- Count subarrays with sum equal to its XOR value
- Count of subarrays having exactly K distinct elements
- Count of all possible reverse bitonic subarrays
- Count of subarrays whose sum is a perfect square
- Count of subarrays having exactly K prime numbers
- Count of longest possible subarrays with sum not divisible by K
- Count of subarrays having sum as a perfect cube
- Count of subarrays which contains a given number exactly K times
- Count Strictly Increasing Subarrays
- Count subarrays with equal number of 1's and 0's
- Count the number of non-increasing subarrays
- Count subarrays with all elements greater than K
- Count of common subarrays in two different permutations of 1 to N
- Count of subarrays which start and end with the same element
- Count distinct Bitwise OR of all Subarrays
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

