Count of subarrays with sum at least K

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

2


My Personal Notes arrow_drop_up

pawanasipugmailcom

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.