总和在给定范围内的最长子数组的长度 [L, R]
给定一个包含N个整数的数组arr[] ,求和在[L, R]范围内的最长子数组的长度。
例子:
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
方法:给定的问题可以使用滑动窗口技术来解决。最初,从数组的起始元素创建一个窗口,使其总和大于L 。维护两个变量i和j代表当前窗口的开始和结束索引。如果当前窗口的总和大于R ,则增加i的值,如果总和小于L ,则增加j的值。对于总和在[L, R]范围内的窗口,将它们的最大长度保持在变量len中,这是所需的答案。
下面是上述方法的实现:
C++
// C++ program of the above approach
#include
using namespace std;
// Function to find the length of
// the longest subarray having its
// sum in the given range [L, R]
int largestSubArraySum(int arr[], int N,
int L, int R)
{
// Store sum of current window
int sum = 0;
// Stores indices of current window
int i = 0, j = 0;
// Stores the maximum length
int len = 0;
// Calculating initial window
while (sum < L && j < N) {
sum += arr[j];
j++;
}
// Loop to iterate over all windows
// of having sum in range [L, R]
while (i < N && j < N) {
// If sum of window is less than L
if (sum < L) {
sum += arr[j];
j++;
}
// If sum of window is more than R
else if (sum > R) {
sum -= arr[i];
i++;
}
// If sum is in the range [L, R]
else {
// Update length
len = max(len, j - i);
sum += arr[j];
j++;
}
}
// Return Answer
return len;
}
// Driver Code
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
// Java program of the above approach
class GFG {
// Function to find the length of
// the longest subarray having its
// sum in the given range [L, R]
static int largestSubArraySum(int[] arr, int N, int L,
int R)
{
// Store sum of current window
int sum = 0;
// Stores indices of current window
int i = 0, j = 0;
// Stores the maximum length
int len = 0;
// Calculating initial window
while (sum < L && j < N) {
sum += arr[j];
j++;
}
// Loop to iterate over all windows
// of having sum in range [L, R]
while (i < N && j < N) {
// If sum of window is less than L
if (sum < L) {
sum += arr[j];
j++;
}
// If sum of window is more than R
else if (sum > R) {
sum -= arr[i];
i++;
}
// If sum is in the range [L, R]
else {
// Update length
len = Math.max(len, j - i);
sum += arr[j];
j++;
}
}
// Return Answer
return len;
}
// Driver Code
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));
}
}
// This code is contributed by Saurabh Jaiswal
Python3
# Python 3 program of the above approach
# Function to find the length of
# the longest subarray having its
# sum in the given range [L, R]
def largestSubArraySum(arr, N, L, R):
# Store sum of current window
sum = 0
# Stores indices of current window
i = 0
j = 0
# Stores the maximum length
len = 0
# Calculating initial window
while (sum < L and j < N):
sum += arr[j]
j += 1
# Loop to iterate over all windows
# of having sum in range [L, R]
while (i < N and j < N):
# If sum of window is less than L
if (sum < L):
sum += arr[j]
j += 1
# If sum of window is more than R
elif (sum > R):
sum -= arr[i]
i += 1
# If sum is in the range [L, R]
else:
# Update length
len = max(len, j - i)
sum += arr[j]
j += 1
# Return Answer
return len
# Driver Code
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))
# This code is contributed by gaurav01.
C#
// C# program of the above approach
using System;
class GFG {
// Function to find the length of
// the longest subarray having its
// sum in the given range [L, R]
static int largestSubArraySum(int[] arr, int N, int L,
int R)
{
// Store sum of current window
int sum = 0;
// Stores indices of current window
int i = 0, j = 0;
// Stores the maximum length
int len = 0;
// Calculating initial window
while (sum < L && j < N) {
sum += arr[j];
j++;
}
// Loop to iterate over all windows
// of having sum in range [L, R]
while (i < N && j < N) {
// If sum of window is less than L
if (sum < L) {
sum += arr[j];
j++;
}
// If sum of window is more than R
else if (sum > R) {
sum -= arr[i];
i++;
}
// If sum is in the range [L, R]
else {
// Update length
len = Math.Max(len, j - i);
sum += arr[j];
j++;
}
}
// Return Answer
return len;
}
// Driver Code
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));
}
}
// This code is contributed by ukasp.
Javascript
输出
4
时间复杂度: O(N)
辅助空间: O(1)