VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximize-non-decreasing-array-size-by-replacing-subarray-with-sum/

⇱ Maximize non decreasing Array size by replacing Subarray with sum - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Maximize non decreasing Array size by replacing Subarray with sum

Last Updated : 23 Jul, 2025

Given an array arr[] of size N. In one operation only one subarray can be selected and replaced with the sum of the subarray. The task is to find the maximum size of the array after making it non-decreasing.

Examples:

Input: N = 5, arr[] = {5, 1, 6, 6, 6}
Output: 4
Explanation: maximum size non-decreasing array, in this case, is {6, 6, 6, 6} which is obtained by replacing subarray(0, 1) = arr[0] + arr[1] = 6

Input: N = 9, arr[] = {5, 1, 6, 7, 7, 1, 6, 4, 5 }
Output: 6
Explanation: maximum size non-decreasing array, in this case, is {5, 7, 7, 7, 7, 9} which is obtained by replacing subarray(1, 2) = arr[1] + arr[2] = 7. Subarray(5, 6) = arr[5] + arr[6] = 7. Subarray(7, 8) = arr[7] + arr[8] = 9

[Naive Approach] Using Dynamic Programming (DP) - O(n^2) time and O(n) space

The idea behind this solution is to use dynamic programming to solve the problem, the main observation is that the maximum length of a non-decreasing array that can be created up to the current index i depends on the maximum length of a non-decreasing array that can be created up to a previous index j, where j < i. This is because the operation of replacing a subarray with its sum can only be performed on a contiguous subarray, and the resulting array must be non-decreasing.

By using dynamic programming, the solution can efficiently keep track of the maximum length of a non-decreasing array that can be created up to each index, and use this information to determine the maximum length that can be created up to the current index.

1. Initialize Arrays:

  • prefix: Stores the cumulative sum of elements up to each index.
  • dp: Stores the maximum length of the non-decreasing subarray that can be formed ending at each index.
  • last: Stores the maximum subarray sum that ends at each index.

2. Calculate the prefix sum for each index.
3. Dynamic Programming Approach:

  • Iterate through each index i starting from the second element.
    • For each i, check all previous indices j to find the longest subarray that can be extended to include i.
    • Update dp[i] and last[i] based on the conditions:
      • If the current prefix sum prefix[i] is greater than or equal to the required sum last[j] + prefix[j], update dp[i] to dp[j] + 1 and set last[i] to prefix[i] - prefix[j]

4. Result: The value at dp[n - 1] will gives the length of the longest non-decreasing subarray that can be formed.

Code Implementation:


Output
4
6

Time Complexity: O(n2)
Auxiliary Space: O(n)

[Expected Approach] Using DP + Binary Search + Monotonic Stack - O(n log(n)) time and O(n) space

The problem is all about, we need to divide the array into subarrays such that replacing each subarray with its sum results in a non-decreasing array. At any index i, we need to determine the maximum number of subarrays that can be formed from the start up to i while maintaining the non-decreasing property.

We always have a minimum answer of 1 at any index (from 0 to i). We need to look how can we maximize our answer for ith index. Maximizing our answer for each index gives us a hint that our answer for ith index will always be >= (i-1)th index, as if answer for ith index comes to be smaller than (i-1)th index, we can always add ith element to subarray which has (i-1)th element and get our answer for ith index = answer for (i-1)th index.

Create a dp array, where dp[i] as the best answer for the subarray ending at index i.

To find dp[i], we need to look for an index j (where 0 <= j < i) such that the sum of the subarray from j+1 to i is greater than or equal to the sum of the subarray that includes arr[j] (let's call this last[j]). This can be represented as

  • prefixsum[i] - prefixsum[j] ≥ sumlast[j]
  • prefixsum[i] ≥ sumlast[j] + prefixsum[j]

Here, last[i] is defined as prefixsum[i] - prefixsum[j]. For each index i, any index j satisfying the above condition can be considered for calculating dp[i]. Thus: dp[i] = max(dp[j]+1) for all 0 ≤ j < i. We will look for bigger value to j to maximise the result at dp[j].

  • Calculate the prefix sums of the array.
  • Initialize a vector stk where each element is a pair consisting of (prefix[index] + sumlast[index], index).
  • For every index i, perform a binary search to find the index j such that (sumlast[j] + prefixsum[j] <= prefixsum[i]) and is closest to i.
  • Update dp[i] = dp[j] + 1.
  • Maintain the monotonic stack property by removing elements that do not satisfy the condition because this will help use to do binary search on monotonic stack.

Code Implementation:


Output
4
6

Time Complexity: O(n*log(n))
Auxiliary Space: O(n)

Comment