VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximum-subarray-size-subarrays-size-sum-less-k/

⇱ Maximum subarray size having all subarrays sums less than k - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Maximum subarray size having all subarrays sums less than k

Last Updated : 7 Mar, 2025

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 : 2
Explanation: Following are the sum of subarray of size 1 to 4.

  • Sum of subarrays of size 1: 1, 2, 3, 4.
  • Sum of subarrays of size 2: 3, 5, 7.
  • Sum of subarrays of size 3: 6, 9.
  • Sum of subarrays of size 4: 10.

So, maximum subarray size such that all subarrays of that size have the sum of elements less than 8 is 2.

Input: arr[] = [1, 2, 10, 4], k = 8.
Output : -1
Explanation: There is an array element (10) with value greater than k, so subarray sum cannot be less than k.

Input : arr[] = [1, 2, 10, 4], k = 14
Output : 2

[Naive Approach] - Using Nested Loops - O(n ^ 3) Time and O(1) Space

The idea is to generate all possible subarrays of all sizes and find sum of their elements. To do so, use three nested loops, where the outer most loops marks the size of the subarray, the middle loop marks the starting index of the subarray, and the inner loops marks the last index of the subarray.
For any integer x in range [1, n], if all the subarrays of array arr[] of size x, has sum of there elements less than or equals to k, store the value x in answer and move to x + 1. At last print the result.


Output
2

[Better Approach] - Using Binary Search and Sliding Window - O(n * log n) Time and O(1) Space

It can be observed that if for any integer x, all subarrays of size x have the sum of there elements less than or equals to k, then x - 1 will also satisfy the condition. Similarly, if x does not satisfy the condition then x + 1 will also not work. Thus we can apply binary search in range 1 to n, to find the max integer x that satisfies the required condition.

Follow the below given steps to solve the problem:

  • Create two counters, low and high, and set their values to 1 and n respectively.
  • Now run a loop until low <= high
  • In each iteration, find the mid of low and high, i.e. (low + high) / 2.
  • Now using sliding window approach, find the maximum possible sum of a subarray of size mid.
  • If maxSum <= k, then set low = mid + 1, as we can increase the size of subarray.
  • Else, if maxSum > k, then set high = mid - 1, as we need to reduce the size of subarray.
  • The loop breaks when low > high, and high stores the final result.

Output
2

[Expected Approach] - Using Sliding Window - O(n) Time and O(1) Space

The approach is to find the minimum subarray size whose sum is greater than integer k. Out result will be this window size - 1.

  • Create two counters, start and end, to store the starting and ending point of the current subarray.
  • Also create two variables, sum and minLen, to store the sum of current subarray and the minimum length of subarray with sum of its elements greater than k respectively.
  • Initialize start and end with 0
  • Now, run a loop until end < n, and in each iteration add arr[end] to sum and increment end by 1.
  • While sum > k, store the minimum of minLen and end - start, subtract arr[start] from sum, and increment start by 1.
  • The loop breaks when end >= n, and minLen - 1 is the final answer.

Output
2
Comment