Maximum sum subarray of size K with sum less than X
Last Updated :
19 Mar, 2021
Given an array arr[] and two integers K and X, the task is to find the maximum sum among all subarrays of size K with the sum less than X.
Examples:
Input: arr[] = {20, 2, 3, 10, 5}, K = 3, X = 20
Output: 18
Explanation: Subarray of size 3 having maximum sum less than 20 is {3, 10, 5}. Therefore, required output is 18.
Input: arr[] = {-5, 8, 7, 2, 10, 1, 20, -4, 6, 9}, K = 5, X = 30
Output: 29
Explanation: Subarray of size 5having maximum sum less than 30 is {2, 10, 1, 20, -4}. Therefore, required output is 29.
Naive Approach: The simplest approach to solve the problem is to generate all subarrays of size K and check if its sum is less than X or not. Print the maximum sum obtained among all such subarrays.
Time Complexity: O(N * K)
Auxiliary Space: O(1)
Efficient Approach: Follow the steps below to solve the problem using Sliding Window technique:
- Initialize a variable sum_K to store the sum of first K array elements.
- If sum_K is less than X, then initialize Max_Sum with sum_K.
- Traverse the array from (K + 1)th index and perform the following:
- In each iteration, subtract the first element of the previous K length subarray and add the current element to sum_K.
- If sum_K is less than X, then compare sum_K with Max_Sum and update Max_Sum accordingly.
- Finally, print Max_Sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maxSumSubarr( int A[], int N,
int K, int X)
{
int sum_K = 0;
for ( int i = 0; i < K; i++) {
sum_K += A[i];
}
int Max_Sum = 0;
if (sum_K < X) {
Max_Sum = sum_K;
}
for ( int i = K; i < N; i++) {
sum_K -= (A[i - K] - A[i]);
if (sum_K < X) {
Max_Sum = max(Max_Sum, sum_K);
}
}
cout << Max_Sum << endl;
}
int main()
{
int arr[] = { -5, 8, 7, 2, 10,
1, 20, -4, 6, 9 };
int K = 5;
int X = 30;
int N = sizeof (arr)
/ sizeof (arr[0]);
maxSumSubarr(arr, N, K, X);
return 0;
}
|
Java
import java.io.*;
class GFG{
private static void maxSumSubarr( int A[], int N,
int K, int X)
{
int sum_K = 0 ;
for ( int i = 0 ; i < K; i++)
{
sum_K += A[i];
}
int Max_Sum = 0 ;
if (sum_K < X)
{
Max_Sum = sum_K;
}
for ( int i = K; i < N; i++)
{
sum_K -= (A[i - K] - A[i]);
if (sum_K < X)
{
Max_Sum = Math.max(Max_Sum, sum_K);
}
}
System.out.println(Max_Sum);
}
public static void main (String[] args)
{
int arr[] = { - 5 , 8 , 7 , 2 , 10 ,
1 , 20 , - 4 , 6 , 9 };
int K = 5 ;
int X = 30 ;
int N = arr.length;
maxSumSubarr(arr, N, K, X);
}
}
|
Python3
def maxSumSubarr(A, N, K, X):
sum_K = 0
for i in range ( 0 , K):
sum_K + = A[i]
Max_Sum = 0
if (sum_K < X):
Max_Sum = sum_K
for i in range (K, N):
sum_K - = (A[i - K] - A[i])
if (sum_K < X):
Max_Sum = max (Max_Sum, sum_K)
print (Max_Sum)
arr = [ - 5 , 8 , 7 , 2 , 10 ,
1 , 20 , - 4 , 6 , 9 ]
K = 5
X = 30
N = len (arr)
maxSumSubarr(arr, N, K, X)
|
C#
using System;
class GFG{
private static void maxSumSubarr( int []A, int N,
int K, int X)
{
int sum_K = 0;
for ( int i = 0; i < K; i++)
{
sum_K += A[i];
}
int Max_Sum = 0;
if (sum_K < X)
{
Max_Sum = sum_K;
}
for ( int i = K; i < N; i++)
{
sum_K -= (A[i - K] - A[i]);
if (sum_K < X)
{
Max_Sum = Math.Max(Max_Sum, sum_K);
}
}
Console.WriteLine(Max_Sum);
}
public static void Main(String[] args)
{
int []arr = { -5, 8, 7, 2, 10,
1, 20, -4, 6, 9 };
int K = 5;
int X = 30;
int N = arr.Length;
maxSumSubarr(arr, N, K, X);
}
}
|
Javascript
<script>
function maxSumSubarr(A, N, K, X)
{
let sum_K = 0;
for (let i = 0; i < K; i++) {
sum_K += A[i];
}
let Max_Sum = 0;
if (sum_K < X) {
Max_Sum = sum_K;
}
for (let i = K; i < N; i++) {
sum_K -= (A[i - K] - A[i]);
if (sum_K < X) {
Max_Sum = Math.max(Max_Sum, sum_K);
}
}
document.write(Max_Sum);
}
let arr = [ -5, 8, 7, 2, 10,
1, 20, -4, 6, 9 ];
let K = 5;
let X = 30;
let N = arr.length;
maxSumSubarr(arr, N, K, X);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...