📌  相关文章
📜  总和至少为 K 的子数组计数

📅  最后修改于: 2021-10-26 02:34:13             🧑  作者: Mango

给定一个大小为N的数组arr[]和一个整数K > 0 。任务是找到总和至少为K的子数组的数量。
例子:

方法:对于固定的左索引(比如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


Javascript


输出:
2

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程