# Number of subarrays having sum less than K

Given an array of non-negative numbers and a non-negative number k, find the number of subarrays having sum less than k. We may assume that there is no overflow.

Examples :

```Input : arr[] = {2, 5, 6}
K = 10
Output : 4
The subarrays are {2}, {5}, {6} and
{2, 5},

Input : arr[] = {1, 11, 2, 3, 15}
K = 10
Output : 4
{1}, {2}, {3} and {2, 3}
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A simple solution is to generate all subarrays of the array and then count the number of arrays having sum less than K.
Below is the implementation of above approach :

## C++

 `// CPP program to count ` `// subarrays having sum ` `// less than k. ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find number ` `// of subarrays having sum ` `// less than k. ` `int` `countSubarray(``int` `arr[], ` `                  ``int` `n, ``int` `k) ` `{ ` `    ``int` `count = 0; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``int` `sum = 0; ` `        ``for` `(``int` `j = i; j < n; j++) { ` ` `  `            ``// If sum is less than k ` `            ``// then update sum and ` `            ``// increment count ` `            ``if` `(sum + arr[j] < k) { ` `                ``sum = arr[j] + sum; ` `                ``count++; ` `            ``} ` `            ``else` `{ ` `                ``break``; ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``return` `count; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `array[] = { 1, 11, 2, 3, 15 }; ` `    ``int` `k = 10; ` `    ``int` `size = ``sizeof``(array) / ``sizeof``(array); ` `    ``int` `count = countSubarray(array, size, k); ` `    ``cout << count << ``"\n"``; ` `} `

## Java

 `// Java program to count subarrays ` `// having sum less than k. ` `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  `    ``// Function to find number of ` `    ``// subarrays having sum less than k. ` `    ``static` `int` `countSubarray(``int` `arr[], ` `                             ``int` `n, ``int` `k) ` `    ``{ ` `        ``int` `count = ``0``; ` ` `  `        ``for` `(``int` `i = ``0``; i < n; i++) { ` `            ``int` `sum = ``0``; ` `            ``for` `(``int` `j = i; j < n; j++) { ` ` `  `                ``// If sum is less than ` `                ``// k then update sum and ` `                ``// increment count ` `                ``if` `(sum + arr[j] < k) { ` `                    ``sum = arr[j] + sum; ` `                    ``count++; ` `                ``} ` `                ``else` `{ ` `                    ``break``; ` `                ``} ` `            ``} ` `        ``} ` `        ``return` `count; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `array[] = { ``1``, ``11``, ``2``, ``3``, ``15` `}; ` `        ``int` `k = ``10``; ` `        ``int` `size = array.length; ` `        ``int` `count = countSubarray(array, size, k); ` `        ``System.out.println(count); ` `    ``} ` `} ` ` `  `// This code is contributed by Sam007 `

## Python3

 `# python program to count subarrays ` `# having sum less than k. ` ` `  `# Function to find number of subarrays  ` `# having sum less than k. ` `def` `countSubarray(arr, n, k): ` `    ``count ``=` `0` ` `  `    ``for` `i ``in` `range``(``0``, n): ` `        ``sum` `=` `0``; ` `        ``for` `j ``in` `range``(i, n): ` `             `  `            ``# If sum is less than k ` `            ``# then update sum and  ` `            ``# increment count ` `            ``if` `(``sum` `+` `arr[j] < k): ` `                ``sum` `=` `arr[j] ``+` `sum` `                ``count``+``=` `1` `            ``else``: ` `                ``break` `    ``return` `count; ` ` `  ` `  `# Driver Code ` `array ``=` `[``1``, ``11``, ``2``, ``3``, ``15``] ` `k ``=` `10` `size ``=` `len``(array) ` `count ``=` `countSubarray(array, size, k); ` `print``(count) ` ` `  `# This code is contributed by Sam007 `

## C#

 `// C# program to count subarrays ` `// having sum less than k. ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// Function to find number ` `    ``// of subarrays having sum ` `    ``// less than k. ` `    ``static` `int` `countSubarray(``int``[] arr, ` `                             ``int` `n, ``int` `k) ` `    ``{ ` `        ``int` `count = 0; ` ` `  `        ``for` `(``int` `i = 0; i < n; i++) { ` `            ``int` `sum = 0; ` `            ``for` `(``int` `j = i; j < n; j++) { ` ` `  `                ``// If sum is less than k ` `                ``// then update sum and ` `                ``// increment count ` `                ``if` `(sum + arr[j] < k) { ` `                    ``sum = arr[j] + sum; ` `                    ``count++; ` `                ``} ` `                ``else` `{ ` `                    ``break``; ` `                ``} ` `            ``} ` `        ``} ` `        ``return` `count; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `Main(String[] args) ` `    ``{ ` `        ``int``[] array = { 1, 11, 2, 3, 15 }; ` `        ``int` `k = 10; ` `        ``int` `size = array.Length; ` `        ``int` `count = countSubarray(array, size, k); ` `        ``Console.WriteLine(count); ` `    ``} ` `} ` ` `  `// This code is contributed by Sam007 `

## PHP

 ` `

Output :

```4
```

Time complexity :
O(n^2).

An efficient solution is based on sliding window technique that can be used to solve the problem. We use two pointers start and end to represent starting and ending point of sliding window. (Not that we need to find contiguous parts).

Initially both start and end point to the beginning of the array, i.e. index 0. Now, let’s try to add a new element el. There are two possible conditions.

1st case :
If sum is less than k, increment end by one position. So contiguous arrays this step produce are (end – start). We also add el to previous sum. There are as many such arrays as the length of the window.

2nd case :
If sum becomes greater than or equal to k, this means we need to subtract starting element from sum so that the sum again becomes less than k. So we adjust the window’s left border by incrementing start.

We follow the same procedure until end < array size.

Implementation:

## C++

 `// CPP program to count ` `// subarrays having sum ` `// less than k. ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find number ` `// of subarrays having sum ` `// less than k. ` `int` `countSubarrays(``int` `arr[], ` `                   ``int` `n, ``int` `k) ` `{ ` `    ``int` `start = 0, end = 0, ` `        ``count = 0, sum = arr; ` ` `  `    ``while` `(start < n && end < n) { ` ` `  `        ``// If sum is less than k, ` `        ``// move end by one position. ` `        ``// Update count and sum ` `        ``// accordingly. ` `        ``if` `(sum < k) { ` `            ``end++; ` ` `  `            ``if` `(end >= start) ` `                ``count += end - start; ` ` `  `            ``// For last element, ` `            ``// end may become n ` `            ``if` `(end < n) ` `                ``sum += arr[end]; ` `        ``} ` ` `  `        ``// If sum is greater than or ` `        ``// equal to k, subtract ` `        ``// arr[start] from sum and ` `        ``// decrease sliding window by ` `        ``// moving start by one position ` `        ``else` `{ ` `            ``sum -= arr[start]; ` `            ``start++; ` `        ``} ` `    ``} ` ` `  `    ``return` `count; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `array[] = { 1, 11, 2, 3, 15 }; ` `    ``int` `k = 10; ` `    ``int` `size = ``sizeof``(array) / ``sizeof``(array); ` `    ``cout << countSubarrays(array, size, k); ` `} `

## Java

 `// Java program to count ` `// subarrays having sum ` `// less than k. ` `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  `    ``// Function to find number ` `    ``// of subarrays having sum ` `    ``// less than k. ` `    ``static` `int` `countSubarray(``int` `arr[], ` `                             ``int` `n, ``int` `k) ` `    ``{ ` `        ``int` `start = ``0``, end = ``0``; ` `        ``int` `count = ``0``, sum = arr[``0``]; ` ` `  `        ``while` `(start < n && end < n) { ` ` `  `            ``// If sum is less than k, ` `            ``// move end by one position. ` `            ``// Update count and sum ` `            ``// accordingly. ` `            ``if` `(sum < k) { ` `                ``end++; ` ` `  `                ``if` `(end >= start) ` `                    ``count += end - start; ` ` `  `                ``// For last element, ` `                ``// end may become n. ` `                ``if` `(end < n) ` `                    ``sum += arr[end]; ` `            ``} ` ` `  `            ``// If sum is greater than or ` `            ``// equal to k, subtract ` `            ``// arr[start] from sum and ` `            ``// decrease sliding window by ` `            ``// moving start by one position ` `            ``else` `{ ` `                ``sum -= arr[start]; ` `                ``start++; ` `            ``} ` `        ``} ` ` `  `        ``return` `count; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `array[] = { ``1``, ``11``, ``2``, ``3``, ``15` `}; ` `        ``int` `k = ``10``; ` `        ``int` `size = array.length; ` `        ``int` `count = countSubarray(array, size, k); ` `        ``System.out.println(count); ` `    ``} ` `} ` ` `  `// This code is contributed by Sam007 `

## Python 3

 `# Python 3 program to count subarrays  ` `# having sum less than k. ` ` `  `# Function to find number of subarrays  ` `# having sum less than k. ` `def` `countSubarrays(arr, n, k): ` ` `  `    ``start ``=` `0` `    ``end ``=` `0` `    ``count ``=` `0` `    ``sum` `=` `arr[``0``] ` ` `  `    ``while` `(start < n ``and` `end < n) : ` ` `  `        ``# If sum is less than k, move end  ` `        ``# by one position. Update count and  ` `        ``# sum accordingly. ` `        ``if` `(``sum` `< k) : ` `            ``end ``+``=` `1` ` `  `            ``if` `(end >``=` `start): ` `                ``count ``+``=` `end ``-` `start ` ` `  `            ``# For last element, end may become n ` `            ``if` `(end < n): ` `                ``sum` `+``=` `arr[end] ` ` `  `        ``# If sum is greater than or equal to k,  ` `        ``# subtract arr[start] from sum and ` `        ``# decrease sliding window by moving  ` `        ``# start by one position ` `        ``else` `: ` `            ``sum` `-``=` `arr[start] ` `            ``start ``+``=` `1` ` `  `    ``return` `count ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: ` `     `  `    ``array ``=` `[ ``1``, ``11``, ``2``, ``3``, ``15` `] ` `    ``k ``=` `10` `    ``size ``=` `len``(array) ` `    ``print``(countSubarrays(array, size, k)) ` ` `  `# This code is contributed by ita_c `

## C#

 `// C# program to count ` `// subarrays having sum ` `// less than k. ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// Function to find number ` `    ``// of subarrays having sum ` `    ``// less than k. ` `    ``static` `int` `countSubarray(``int``[] arr, ` `                             ``int` `n, ``int` `k) ` `    ``{ ` `        ``int` `start = 0, end = 0; ` `        ``int` `count = 0, sum = arr; ` ` `  `        ``while` `(start < n && end < n) { ` ` `  `            ``// If sum is less than k, ` `            ``// move end by one position. ` `            ``// Update count and sum ` `            ``// accordingly. ` `            ``if` `(sum < k) { ` `                ``end++; ` ` `  `                ``if` `(end >= start) ` `                    ``count += end - start; ` ` `  `                ``// For last element, ` `                ``// end may become n. ` `                ``if` `(end < n) ` `                    ``sum += arr[end]; ` `            ``} ` ` `  `            ``// If sum is greater than or ` `            ``// equal to k, subtract ` `            ``// arr[start] from sum and ` `            ``// decrease sliding window by ` `            ``// moving start by one position ` `            ``else` `{ ` `                ``sum -= arr[start]; ` `                ``start++; ` `            ``} ` `        ``} ` ` `  `        ``return` `count; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `Main(String[] args) ` `    ``{ ` `        ``int``[] array = { 1, 11, 2, 3, 15 }; ` `        ``int` `k = 10; ` `        ``int` `size = array.Length; ` `        ``int` `count = countSubarray(array, size, k); ` `        ``Console.WriteLine(count); ` `    ``} ` `} ` ` `  `// This code is contributed by Sam007 `

## PHP

 `= ``\$start``) ` `            ``\$count` `+= ``\$end` `- ``\$start``; ` ` `  `            ``// For last element,  ` `            ``// end may become n ` `            ``if` `(``\$end` `< ``\$n``) ` `            ``\$sum` `+= ``\$arr``[``\$end``]; ` `        ``} ` ` `  `        ``// If sum is greater than or  ` `        ``// equal to k, subtract  ` `        ``// arr[start] from sum and  ` `        ``// decrease sliding window by  ` `        ``// moving start by one position ` `        ``else` `        ``{ ` `            ``\$sum` `-= ``\$arr``[``\$start``]; ` `            ``\$start``++; ` `        ``} ` `    ``} ` ` `  `    ``return` `\$count``; ` `} ` ` `  `    ``// Driver Code ` `    ``\$array` `=``array` `(1, 11, 2, 3, 15); ` `    ``\$k` `= 10; ` `    ``\$size` `= sizeof(``\$array``) ; ` `    ``echo` `countSubarrays(``\$array``, ``\$size``, ``\$k``); ` ` `  `// This code is contributed by ajit ` `?> `

Output:

```4
```

Time complexity : O(n).

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

8

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.