Number of subarrays having sum less than K
Last Updated :
19 Oct, 2022
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}
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++
#include <bits/stdc++.h>
using namespace std;
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 + arr[j] < k) {
sum = arr[j] + sum;
count++;
}
else {
break ;
}
}
}
return count;
}
int main()
{
int array[] = { 1, 11, 2, 3, 15 };
int k = 10;
int size = sizeof (array) / sizeof (array[0]);
int count = countSubarray(array, size, k);
cout << count << "\n" ;
}
|
Java
import java.io.*;
class GFG {
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 + arr[j] < k) {
sum = arr[j] + sum;
count++;
}
else {
break ;
}
}
}
return count;
}
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);
}
}
|
Python3
def countSubarray(arr, n, k):
count = 0
for i in range ( 0 , n):
sum = 0 ;
for j in range (i, n):
if ( sum + arr[j] < k):
sum = arr[j] + sum
count + = 1
else :
break
return count;
array = [ 1 , 11 , 2 , 3 , 15 ]
k = 10
size = len (array)
count = countSubarray(array, size, k);
print (count)
|
C#
using System;
class GFG {
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 + arr[j] < k) {
sum = arr[j] + sum;
count++;
}
else {
break ;
}
}
}
return count;
}
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);
}
}
|
PHP
<?php
function countSubarray( $arr , $n , $k )
{
$count = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$sum = 0;
for ( $j = $i ; $j < $n ; $j ++)
{
if ( $sum + $arr [ $j ] < $k )
{
$sum = $arr [ $j ] + $sum ;
$count ++;
}
else
{
break ;
}
}
}
return $count ;
}
$array = array (1, 11, 2, 3, 15);
$k = 10;
$size = sizeof( $array );
$count = countSubarray( $array , $size , $k );
echo $count , "\n" ;
?>
|
Javascript
<script>
function countSubarray(arr , n , k)
{
var count = 0;
for (i = 0; i < n; i++)
{
var sum = 0;
for (j = i; j < n; j++)
{
if (sum + arr[j] < k)
{
sum = arr[j] + sum;
count++;
}
else
{
break ;
}
}
}
return count;
}
var array = [ 1, 11, 2, 3, 15 ];
var k = 10;
var size = array.length;
var count = countSubarray(array, size, k);
document.write(count);
</script>
|
Time complexity: O(n^2)
Auxiliary Space: O(1)
An efficient solution is based on a sliding window technique that can be used to solve the problem. We use two pointers start and end to represent starting and ending points of the sliding window. (Not that we need to find contiguous parts).
Initially both start and endpoint 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 end 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++
#include <bits/stdc++.h>
using namespace std;
int countSubarrays( int arr[],
int n, int k)
{
int start = 0, end = 0,
count = 0, sum = arr[0];
while (start < n && end < n) {
if (sum < k) {
end++;
if (end >= start)
count += end - start;
if (end < n)
sum += arr[end];
}
else {
sum -= arr[start];
start++;
}
}
return count;
}
int main()
{
int array[] = { 1, 11, 2, 3, 15 };
int k = 10;
int size = sizeof (array) / sizeof (array[0]);
cout << countSubarrays(array, size, k);
}
|
Java
import java.io.*;
class GFG {
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 < k) {
end++;
if (end >= start)
count += end - start;
if (end < n)
sum += arr[end];
}
else {
sum -= arr[start];
start++;
}
}
return count;
}
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);
}
}
|
Python 3
def countSubarrays(arr, n, k):
start = 0
end = 0
count = 0
sum = arr[ 0 ]
while (start < n and end < n) :
if ( sum < k) :
end + = 1
if (end > = start):
count + = end - start
if (end < n):
sum + = arr[end]
else :
sum - = arr[start]
start + = 1
return count
if __name__ = = "__main__" :
array = [ 1 , 11 , 2 , 3 , 15 ]
k = 10
size = len (array)
print (countSubarrays(array, size, k))
|
C#
using System;
class GFG {
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 < k) {
end++;
if (end >= start)
count += end - start;
if (end < n)
sum += arr[end];
}
else {
sum -= arr[start];
start++;
}
}
return count;
}
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);
}
}
|
PHP
<?php
function countSubarrays( $arr ,
$n , $k )
{
$start = 0;
$end = 0;
$count = 0;
$sum = $arr [0];
while ( $start < $n && $end < $n )
{
if ( $sum < $k )
{
$end ++;
if ( $end >= $start )
$count += $end - $start ;
if ( $end < $n )
$sum += $arr [ $end ];
}
else
{
$sum -= $arr [ $start ];
$start ++;
}
}
return $count ;
}
$array = array (1, 11, 2, 3, 15);
$k = 10;
$size = sizeof( $array ) ;
echo countSubarrays( $array , $size , $k );
?>
|
Javascript
<script>
function countSubarray(arr, n, k)
{
let start = 0, end = 0;
let count = 0, sum = arr[0];
while (start < n && end < n) {
if (sum < k) {
end++;
if (end >= start)
count += end - start;
if (end < n)
sum += arr[end];
}
else {
sum -= arr[start];
start++;
}
}
return count;
}
let array = [ 1, 11, 2, 3, 15 ];
let k = 10;
let size = array.length;
let count = countSubarray(array, size, k);
document.write(count);
</script>
|
Time complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...