Related Articles

# Count of subarrays with sum at least K

• Difficulty Level : Easy
• Last Updated : 04 Jun, 2021

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:
{6, 1, 2, 7} and {1, 2, 7} are the only valid subarrays.
Input: arr[] = {3, 3, 3}, K = 5
Output:

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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 ``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);` `    ``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

 ``
Output:
`2`

My Personal Notes arrow_drop_up