# Maximum subarray size, such that all subarrays of that size have sum less than k in C++ program

C++Server Side ProgrammingProgramming

In this problem, we are given an array arr[] consisting of n positive integers and an integer k. Our task is to create a program to find the Maximum subarray size, such that all subarrays of that size have sum less than k.

Problem Description − we need to find the largest size of a subarray, such that all subarray created of the size from the elements of the array will have the sum of elements less than or equal to k.

Let’s take an example to understand the problem,

## Input

arr[n] = {4, 1, 3, 2}, k = 9

## Output

3

## Explanation

All subarrays of size 3 and their sum −

{4, 1, 3} = 8
{1, 3, 2} = 6
The sum of all subarrays of size 3 is less than or equal to k.

## Solution Approach

A simple solution to the problem is by finding the subarray which can have a size greater than k. For this, we will create a prefix sum which denotes the sum of elements till the given index. For this prefix sum, we will find the maximum result which is less than k and the index of this will be our result. Here, we have used the fact that if the prefix sum is greater than k for any size, and all the rest have sum less, then all subarray of size −1 length will have sum less than k.

## Example

Program to illustrate the working of our solution,

Live Demo

#include<iostream>
using namespace std;
int calcSubArraySize(int arr[], int n, int k){
int prefixSum[n + 1];
prefixSum[0] = 0;
for (int i = 0; i < n; i++)
prefixSum[i + 1] = prefixSum[i] + arr[i];
// Searching size
int maxLen = −1;
int start = 1, end = n;
int mid, i;
while (start <= end){
int mid = (start + end) / 2;
for (i = mid; i <= n; i++){
if (prefixSum[i] − prefixSum[i − mid] > k)
break;
}
if (i == n + 1){
start = mid + 1;
maxLen = mid;
}
else
end = mid − 1;
}
return maxLen;
}
int main(){
int arr[] = {4, 1, 2, 3};
int n = sizeof(arr) / sizeof(arr[0]);
int k = 9;
cout<<"The maximum subarray size, such that all subarrays of that
size have sum less than k is "<<calcSubArraySize(arr, n, k);
return 0;
}

## Output

This method is efficient but a better approach can be made to solve the problem,

In this approach, we will use the sliding Window method for finding the sum of the subarray. Starting by taking all elements we will find the length till which the sum remains above k. And then return the length − 1 which is the maximum size of the subarray for which the sum of all subarrays is less than or equal to 0.

## Example

Program to illustrate the working of our solution,

Live Demo

#include <iostream>
using namespace std;
int calcSubArraySizeSW(int arr[], int n, int k){
int maxLen = n;
int subArraySum = 0;
int start = 0;
for (int end = 0; end < n; end++){
subArraySum += arr[end];
while (subArraySum > k) {
subArraySum −= arr[start];
start++;
maxLen = min(maxLen, end − start + 1);
if (subArraySum == 0)
break;
}
if (subArraySum == 0) {
maxLen = −1;
break;
}
}
return maxLen;
}
int main(){
int arr[] = { 4, 1, 3, 2, 6 };
int k = 12;
int n = sizeof(arr)/ sizeof(arr[0]);
cout<<"The maximum subarray size, such that all subarrays of that
size have sum less than k is "<<calcSubArraySizeSW(arr, n, k);
return 0;
}

## Output

The maximum subarray size, such that all subarrays of that size have sum
less than k is 4
Published on 09-Dec-2020 12:48:11