给定大小为N的数组arr []和整数K> 0 。任务是找到总和至少为K的子数组的数量。
例子:
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
方法:对于固定的左索引(例如l ),尝试找到l右边的第一个索引(例如r ),使得(arr [l] + arr [l + 1] +…+ arr [r])≥ ķ。然后将N – r + 1添加到所需的答案。对所有左侧索引重复此过程。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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