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++ 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 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 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# 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
<script>
// Javascript implementation of the approach
// Function to return the number of
// subarrays with sum atleast k
function k_sum(a, n, k)
{
// To store the right index
// and the current sum
let r = 0, sum = 0;
// To store the number of sub-arrays
let ans = 0;
// For all left indexes
for (let 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
let a = [6, 1, 2, 7], k = 10;
let n = a.length;
document.write(k_sum(a, n, k));
// This code is contributed by _saurabh_jaiswal.
</script>
Output:
2
Time Complexity: O(r * k)
Auxiliary Space: O(1)
Related Topic: Subarrays, Subsequences, and Subsets in Array