Number of subarrays having sum in a given range
Given an array arr[] of positive integers and a range (L, R). Find the number of subarrays having sum in the range L to R.
Examples:
Input : arr[] = {1, 4, 6}, L = 3, R = 8
Output : 3
The subarrays are {1, 4}, {4}, {6}.
Input : arr[] = {2, 3, 5, 8}, L = 4, R = 13
Output : 6
The subarrays are {2, 3}, {2, 3, 5}, {3, 5},
{5}, {5, 8}, {8}.
A simple solution is to one by one consider each subarray and find its sum. If the sum lies in the range [L, R], then increment the count. The time complexity of this solution is O(n^2).
An efficient solution is to first find the number of subarrays having sum less than or equal to R. From this count, subtract the number of subarrays having sum less than or equal to L-1. To find the number of subarrays having sum less than or equal to the given value, the linear time method using the sliding window discussed in the following post is used:
Number of subarrays having sum less than or equal to k.
Steps to solve this problem:
1. Declare a variable Rcnt and store the value of function countSub(arr,n,R)
2. Declare a variable lcnt and store the variable of function countSub(arr,n,L-1).
3. Return Rcnt-Lcnt.
*In countSub function:
1. Declare variables st=0,end=0,sum=0,and cnt=0.
2. While end is smaller than n:
*Update sum+=arr[end].
*While St is smaller than equal to n and sum is greater than x:
*Update sum-=arr[St] and increment St.
*Update cnt+=(end-st+1) and increment end.
3. Return cnt.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countSub( int arr[], int n, int x)
{
int st = 0;
int end = 0;
int sum = 0;
int cnt = 0;
while (end < n) {
sum += arr[end];
while (st <= end && sum > x) {
sum -= arr[st];
st++;
}
cnt += (end - st + 1);
end++;
}
return cnt;
}
int findSubSumLtoR( int arr[], int n, int L, int R)
{
int Rcnt = countSub(arr, n, R);
int Lcnt = countSub(arr, n, L - 1);
return Rcnt - Lcnt;
}
int main()
{
int arr[] = { 1, 4, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
int L = 3;
int R = 8;
cout << findSubSumLtoR(arr, n, L, R);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int countSub( int arr[],
int n, int x)
{
int st = 0 ;
int end = 0 ;
int sum = 0 ;
int cnt = 0 ;
while (end < n)
{
sum += arr[end];
while (st <= end && sum > x)
{
sum -= arr[st];
st++;
}
cnt += (end - st + 1 );
end++;
}
return cnt;
}
static int findSubSumLtoR( int arr[], int n,
int L, int R)
{
int Rcnt = countSub(arr, n, R);
int Lcnt = countSub(arr, n, L - 1 );
return Rcnt - Lcnt;
}
public static void main (String[] args)
{
int arr[] = { 1 , 4 , 6 };
int n = arr.length;
int L = 3 ;
int R = 8 ;
System.out.println(findSubSumLtoR(arr, n, L, R));
}
}
|
C#
using System;
class GFG
{
static int countSub( int [] arr,
int n, int x)
{
int st = 0;
int end = 0;
int sum = 0;
int cnt = 0;
while (end < n)
{
sum += arr[end];
while (st <= end && sum > x)
{
sum -= arr[st];
st++;
}
cnt += (end - st + 1);
end++;
}
return cnt;
}
static int findSubSumLtoR( int [] arr, int n,
int L, int R)
{
int Rcnt = countSub(arr, n, R);
int Lcnt = countSub(arr, n, L - 1);
return Rcnt - Lcnt;
}
public static void Main ()
{
int [] arr = { 1, 4, 6 };
int n = arr.Length;
int L = 3;
int R = 8;
Console.Write(findSubSumLtoR(arr, n, L, R));
}
}
|
Python 3
def countSub(arr, n, x):
st = 0
end = 0
sum = 0
cnt = 0
while end < n :
sum + = arr[end]
while (st < = end and sum > x) :
sum - = arr[st]
st + = 1
cnt + = (end - st + 1 )
end + = 1
return cnt
def findSubSumLtoR(arr, n, L, R):
Rcnt = countSub(arr, n, R)
Lcnt = countSub(arr, n, L - 1 )
return Rcnt - Lcnt
arr = [ 1 , 4 , 6 ]
n = len (arr)
L = 3
R = 8
print (findSubSumLtoR(arr, n, L, R))
|
PHP
<?php
function countSub(& $arr , $n , $x )
{
$st = 0;
$end = 0;
$sum = 0;
$cnt = 0;
while ( $end < $n )
{
$sum += $arr [ $end ];
while ( $st <= $end && $sum > $x )
{
$sum -= $arr [ $st ];
$st ++;
}
$cnt += ( $end - $st + 1);
$end ++;
}
return $cnt ;
}
function findSubSumLtoR(& $arr , $n , $L , $R )
{
$Rcnt = countSub( $arr , $n , $R );
$Lcnt = countSub( $arr , $n , $L - 1);
return $Rcnt - $Lcnt ;
}
$arr = array ( 1, 4, 6 );
$n = sizeof( $arr );
$L = 3;
$R = 8;
echo findSubSumLtoR( $arr , $n , $L , $R );
?>
|
Javascript
<script>
function countSub(arr, n, x)
{
var st = 0;
var end = 0;
var sum = 0;
var cnt = 0;
while (end < n) {
sum += arr[end];
while (st <= end && sum > x) {
sum -= arr[st];
st++;
}
cnt += (end - st + 1);
end++;
}
return cnt;
}
function findSubSumLtoR(arr, n, L, R)
{
var Rcnt = countSub(arr, n, R);
var Lcnt = countSub(arr, n, L - 1);
return Rcnt - Lcnt;
}
var arr = [ 1, 4, 6 ];
var n = arr.length;
var L = 3;
var R = 8;
document.write( findSubSumLtoR(arr, n, L, R));
</script>
|
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Last Updated :
14 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...