Maximum sum subarray of size K with sum less than X (original) (raw)
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++ `
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to calculate maximum sum // among all subarrays of size K // with the sum less than X void maxSumSubarr(int A[], int N, int K, int X) {
// Initialize sum_K to 0
int sum_K = 0;
// Calculate sum of first K elements
for (int i = 0; i < K; i++) {
sum_K += A[i];
}
int Max_Sum = 0;
// If sum_K is less than X
if (sum_K < X) {
// Initialize MaxSum with sum_K
Max_Sum = sum_K;
}
// Iterate over the array from
// (K + 1)-th index
for (int i = K; i < N; i++) {
// Subtract the first element
// from the previous K elements
// and add the next element
sum_K -= (A[i - K] - A[i]);
// If sum_K is less than X
if (sum_K < X) {
// Update the Max_Sum
Max_Sum = max(Max_Sum, sum_K);
}
}
cout << Max_Sum << endl;
}
// Driver Code int main() { int arr[] = { -5, 8, 7, 2, 10, 1, 20, -4, 6, 9 }; int K = 5; int X = 30;
// Size of Array
int N = sizeof(arr)
/ sizeof(arr[0]);
// Function Call
maxSumSubarr(arr, N, K, X);
return 0;
}
Java
// Java program for the above approach import java.io.*;
class GFG{
// Function to calculate maximum sum // among all subarrays of size K // with the sum less than X private static void maxSumSubarr(int A[], int N, int K, int X) {
// Initialize sum_K to 0
int sum_K = 0;
// Calculate sum of first K elements
for(int i = 0; i < K; i++)
{
sum_K += A[i];
}
int Max_Sum = 0;
// If sum_K is less than X
if (sum_K < X)
{
// Initialize MaxSum with sum_K
Max_Sum = sum_K;
}
// Iterate over the array from
// (K + 1)-th index
for(int i = K; i < N; i++)
{
// Subtract the first element
// from the previous K elements
// and add the next element
sum_K -= (A[i - K] - A[i]);
// If sum_K is less than X
if (sum_K < X)
{
// Update the Max_Sum
Max_Sum = Math.max(Max_Sum, sum_K);
}
}
System.out.println(Max_Sum);
}
// Driver Code public static void main (String[] args) { int arr[] = { -5, 8, 7, 2, 10, 1, 20, -4, 6, 9 }; int K = 5; int X = 30;
// Size of Array
int N = arr.length;
// Function Call
maxSumSubarr(arr, N, K, X);
} }
// This code is contributed by jithin
Python3
Python3 program for the above approach
Function to calculate maximum sum
among all subarrays of size K
with the sum less than X
def maxSumSubarr(A, N, K, X):
# Initialize sum_K to 0
sum_K = 0
# Calculate sum of first K elements
for i in range(0, K):
sum_K += A[i]
Max_Sum = 0
# If sum_K is less than X
if (sum_K < X):
# Initialize MaxSum with sum_K
Max_Sum = sum_K
# Iterate over the array from
# (K + 1)-th index
for i in range(K, N):
# Subtract the first element
# from the previous K elements
# and add the next element
sum_K -= (A[i - K] - A[i])
# If sum_K is less than X
if (sum_K < X):
# Update the Max_Sum
Max_Sum = max(Max_Sum, sum_K)
print(Max_Sum)
Driver Code
arr = [ -5, 8, 7, 2, 10, 1, 20, -4, 6, 9 ] K = 5 X = 30
Size of Array
N = len(arr)
Function Call
maxSumSubarr(arr, N, K, X)
This code is contributed by sanjoy_62
C#
// C# program for the above approach using System;
class GFG{
// Function to calculate maximum sum // among all subarrays of size K // with the sum less than X private static void maxSumSubarr(int []A, int N, int K, int X) {
// Initialize sum_K to 0
int sum_K = 0;
// Calculate sum of first K elements
for(int i = 0; i < K; i++)
{
sum_K += A[i];
}
int Max_Sum = 0;
// If sum_K is less than X
if (sum_K < X)
{
// Initialize MaxSum with sum_K
Max_Sum = sum_K;
}
// Iterate over the array from
// (K + 1)-th index
for(int i = K; i < N; i++)
{
// Subtract the first element
// from the previous K elements
// and add the next element
sum_K -= (A[i - K] - A[i]);
// If sum_K is less than X
if (sum_K < X)
{
// Update the Max_Sum
Max_Sum = Math.Max(Max_Sum, sum_K);
}
}
Console.WriteLine(Max_Sum);
}
// Driver Code public static void Main(String[] args) { int []arr = { -5, 8, 7, 2, 10, 1, 20, -4, 6, 9 }; int K = 5; int X = 30;
// Size of Array
int N = arr.Length;
// Function Call
maxSumSubarr(arr, N, K, X);
} }
// This code is contributed by Amit Katiyar
JavaScript
`
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
- Maximum size square Sub-Matrix with sum less than or equals to K Given a Matrix arr[][] of size M x N having positive integers and a number K, the task is to find the size of the largest square sub-matrix whose sum of elements is less than or equals to K. Example: Input: arr[][] = { { 1, 1, 3, 2, 4, 3, 2 }, { 1, 1, 3, 2, 4, 3, 2 }, { 1, 1, 3, 2, 4, 3, 2 } }, K = 12 min read
- Maximum subarray size having all subarrays sums less than k Given an array of positive integers arr[] of size n, and an integer k. The task is to find the maximum subarray size such that all subarrays of that size have sum less than or equals to k.Examples : Input : arr[] = [1, 2, 3, 4], k = 8.Output : 2Explanation: Following are the sum of subarray of size 15+ min read
- Maximum sum subarray having sum less than given sum using Set Given an array arr[] of length N and an integer K, the task is the find the maximum sum subarray with a sum less than K.Note: If K is less than the minimum element, then return INT_MIN. Examples: Input: arr[] = {-1, 2, 2}, K = 4 Output: 3 Explanation: The subarray with maximum sum which is less than 7 min read
- Length of the smallest subarray with maximum possible sum Given an array arr[] consisting of N non-negative integers, the task is to find the minimum length of the subarray whose sum is maximum. Example: Input: arr[] = {0, 2, 0, 0, 12, 0, 0, 0}Output: 4Explanation: The sum of the subarray {2, 0, 0, 12} = 2 + 0 + 0 + 12 = 14, which is maximum sum possible a 6 min read
- Maximum count of distinct sized subarrays with given sum Given a binary array arr[] of N integers, the task is to find the maximum count of distinct sized subarrays such that the sum of each subarray is K. Example: Input: arr[] = {0, 1, 1 , 0}, K = 2Output: 3Explanation: The subset {{0, 1, 1, 0}, {0, 1, 1}, {1, 1}} is the subset of 3 subarrays such that t 7 min read
- Maximum sum subarray having sum less than or equal to given sum You are given an array of non-negative integers and a target sum. Your task is to find a contiguous subarray whose sum is the maximum possible, while ensuring that it does not exceed the given target sum.Note: The given array contains only non-negative integers.Examples: Input: arr[] = [1, 2, 3, 4, 6 min read
- Maximum length L such that the sum of all subarrays of length L is less than K Given an array of length N and an integer K. The task is to find the maximum length L such that all the subarrays of length L have sum of its elements less than K.Examples: Input: arr[] = {1, 2, 3, 4, 5}, K = 20 Output: 5 The only subarray of length 5 is the complete array and (1 + 2 + 3 + 4 + 5) = 8 min read
- Maximum sum subarray of size range [L, R] Given an integer array arr[] of size N and two integer L and R. The task is to find the maximum sum subarray of size between L and R (both inclusive). Example: Input: arr[] = {1, 2, 2, 1}, L = 1, R = 3 Output: 5 Explanation: Subarray of size 1 are {1}, {2}, {2}, {1} and maximum sum subarray = 2 for 8 min read
- Maximum circular subarray sum of size K Given an array arr of size N and an integer K, the task is to find the maximum sum subarray of size k among all contiguous sub-array (considering circular subarray also). Examples: Input: arr = {18, 4, 3, 4, 5, 6, 7, 8, 2, 10}, k = 3 Output: max circular sum = 32 start index = 9 end index = 1 Explan 6 min read
- Print subarray with maximum sum Given an array arr[], the task is to print the subarray having maximum sum.Examples:Input: arr[] = {2, 3, -8, 7, -1, 2, 3}Output: 11Explanation: The subarray {7, -1, 2, 3} has the largest sum 11.Input: arr[] = {-2, -5, 6, -2, -3, 1, 5, -6}Output: {6, -2, -3, 1, 5}Explanation: The subarray {6, -2, -3 13 min read