Length of longest subarray having sum in given range [L, R]
Last Updated :
17 Dec, 2021
Given an array arr[] of N integers, find the length of the longest subarray having sum in the range [L, R].
Examples:
Input: arr[] = {1, 4, 6}, L = 3, R = 8
Output: 2
Explanation: The valid subarrays with there sum in range [3, 8] are {1, 4}, {4}, {6}. Longest subarray among them is {1, 4} having its length as 2.
Input: arr[] = {15, 2, 4, 8, 9, 5, 10, 23}, L = 10, R = 23
Output: 4
Approach: The given problem can be solved using the sliding window technique. Initially, create a window from the starting elements of the array such that its sum is greater than L. Maintain two variables i and j representing the starting and the ending index of the current window. If the sum of the current window is more than R, increment the value of i and if the sum is less than L, increment the value of j. For the windows with their sum in the range [L, R], maintain their maximum length in a variable len, which is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int largestSubArraySum( int arr[], int N,
int L, int R)
{
int sum = 0;
int i = 0, j = 0;
int len = 0;
while (sum < L && j < N) {
sum += arr[j];
j++;
}
while (i < N && j < N) {
if (sum < L) {
sum += arr[j];
j++;
}
else if (sum > R) {
sum -= arr[i];
i++;
}
else {
len = max(len, j - i);
sum += arr[j];
j++;
}
}
return len;
}
int main()
{
int arr[] = { 15, 2, 4, 8, 9, 5, 10, 23 };
int N = sizeof (arr) / sizeof (arr[0]);
int L = 10, R = 23;
cout << largestSubArraySum(arr, N, L, R);
return 0;
}
|
Java
class GFG {
static int largestSubArraySum( int [] arr, int N, int L,
int R)
{
int sum = 0 ;
int i = 0 , j = 0 ;
int len = 0 ;
while (sum < L && j < N) {
sum += arr[j];
j++;
}
while (i < N && j < N) {
if (sum < L) {
sum += arr[j];
j++;
}
else if (sum > R) {
sum -= arr[i];
i++;
}
else {
len = Math.max(len, j - i);
sum += arr[j];
j++;
}
}
return len;
}
public static void main(String args[])
{
int [] arr = { 15 , 2 , 4 , 8 , 9 , 5 , 10 , 23 };
int N = arr.length;
int L = 10 , R = 23 ;
System.out.println(largestSubArraySum(arr, N, L, R));
}
}
|
Python3
def largestSubArraySum(arr, N, L, R):
sum = 0
i = 0
j = 0
len = 0
while ( sum < L and j < N):
sum + = arr[j]
j + = 1
while (i < N and j < N):
if ( sum < L):
sum + = arr[j]
j + = 1
elif ( sum > R):
sum - = arr[i]
i + = 1
else :
len = max ( len , j - i)
sum + = arr[j]
j + = 1
return len
if __name__ = = "__main__" :
arr = [ 15 , 2 , 4 , 8 , 9 , 5 , 10 , 23 ]
N = len (arr)
L = 10
R = 23
print (largestSubArraySum(arr, N, L, R))
|
C#
using System;
class GFG {
static int largestSubArraySum( int [] arr, int N, int L,
int R)
{
int sum = 0;
int i = 0, j = 0;
int len = 0;
while (sum < L && j < N) {
sum += arr[j];
j++;
}
while (i < N && j < N) {
if (sum < L) {
sum += arr[j];
j++;
}
else if (sum > R) {
sum -= arr[i];
i++;
}
else {
len = Math.Max(len, j - i);
sum += arr[j];
j++;
}
}
return len;
}
public static void Main()
{
int [] arr = { 15, 2, 4, 8, 9, 5, 10, 23 };
int N = arr.Length;
int L = 10, R = 23;
Console.WriteLine(largestSubArraySum(arr, N, L, R));
}
}
|
Javascript
<script>
const largestSubArraySum = (arr, N, L, R) => {
let sum = 0;
let i = 0, j = 0;
let len = 0;
while (sum < L && j < N) {
sum += arr[j];
j++;
}
while (i < N && j < N) {
if (sum < L) {
sum += arr[j];
j++;
}
else if (sum > R) {
sum -= arr[i];
i++;
}
else {
len = Math.max(len, j - i);
sum += arr[j];
j++;
}
}
return len;
}
let arr = [15, 2, 4, 8, 9, 5, 10, 23];
let N = arr.length;
let L = 10, R = 23;
document.write(largestSubArraySum(arr, N, L, R));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...