![]() |
VOOZH | about |
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] = 6Input: 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
Table of Content
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:
2. Calculate the prefix sum for each index.
3. Dynamic Programming Approach:
4. Result: The value at dp[n - 1] will gives the length of the longest non-decreasing subarray that can be formed.
Code Implementation:
4 6
Time Complexity: O(n2)
Auxiliary Space: O(n)
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].
Code Implementation:
4 6
Time Complexity: O(n*log(n))
Auxiliary Space: O(n)