Watch 10 video solutions for Constructing Two Increasing Arrays, a hard level problem involving Array, Dynamic Programming. This walkthrough by Reducible has 1,234,244 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given 2 integer arrays nums1 and nums2 consisting only of 0 and 1, your task is to calculate the minimum possible largest number in arrays nums1 and nums2, after doing the following.
Replace every 0 with an even positive integer and every 1 with an odd positive integer. After replacement, both arrays should be increasing and each integer should be used at most once.
Return the minimum possible largest number after applying the changes.
Example 1:
Input: nums1 = [], nums2 = [1,0,1,1]
Output: 5
Explanation:
After replacing, nums1 = [], and nums2 = [1, 2, 3, 5].
Example 2:
Input: nums1 = [0,1,0,1], nums2 = [1,0,0,1]
Output: 9
Explanation:
One way to replace, having 9 as the largest element is nums1 = [2, 3, 8, 9], and nums2 = [1, 4, 6, 7].
Example 3:
Input: nums1 = [0,1,0,0,1], nums2 = [0,0,0,1]
Output: 13
Explanation:
One way to replace, having 13 as the largest element is nums1 = [2, 3, 4, 6, 7], and nums2 = [8, 10, 12, 13].
Constraints:
0 <= nums1.length <= 10001 <= nums2.length <= 1000nums1 and nums2 consist only of 0 and 1.Problem Overview: You are given an array nums. The task is to construct two arrays a and b such that for every index i, a[i] + b[i] = nums[i], while both arrays remain strictly increasing. The goal is to count how many valid constructions exist.
Approach 1: Brute Force Enumeration (Exponential Time)
At each index i, iterate over all possible values for a[i] from 0 to nums[i], then set b[i] = nums[i] - a[i]. Track the previous values to ensure a and b stay strictly increasing. This produces a branching search tree where each index explores multiple splits. The time complexity grows exponentially because every position can have many candidate pairs, and the space complexity is O(n) due to recursion depth. This approach only works for very small inputs and mainly helps clarify the constraints.
Approach 2: Dynamic Programming on Value Transitions (O(n · m^2) time, O(m) space)
Use Dynamic Programming to track valid choices for a[i]. Let dp[i][x] represent the number of ways to build arrays up to index i with a[i] = x. Since b[i] = nums[i] - x, both arrays must remain strictly increasing: x > a[i-1] and nums[i] - x > b[i-1]. For each candidate x, iterate over all previous a[i-1] values that satisfy both constraints. The maximum candidate value is bounded by nums[i], so if m is the maximum value in nums, the transition costs O(m^2) per index.
Approach 3: Optimized DP with Prefix Sums (O(n · m) time, O(m) space)
The DP transition can be optimized by noticing that valid previous states form a continuous range. Instead of iterating over all previous values, maintain prefix sums of the DP row. For a chosen a[i] = x, compute the maximum allowed previous value derived from both increasing constraints. Then query the prefix sum to count all valid states in constant time. This removes the inner loop and reduces the transition cost to O(1). The overall complexity becomes O(n · m) time with O(m) space. The technique combines array traversal with dynamic programming state transitions and prefix accumulation.
Recommended for interviews: The prefix-sum optimized dynamic programming approach is the expected solution. Interviewers want to see the DP formulation first (state definition and transition constraints), then the optimization that reduces the transition from quadratic to linear per index. Showing the brute force reasoning demonstrates understanding, but implementing the optimized DP shows strong algorithmic skill.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Enumeration | O(k^n) | O(n) | Conceptual understanding or very small inputs |
| Basic Dynamic Programming | O(n · m^2) | O(m) | When deriving DP transitions before optimization |
| DP with Prefix Sum Optimization | O(n · m) | O(m) | Optimal solution for large constraints and interview settings |