Constructing Two Increasing Arrays - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: nums1 = [0,1], nums2 = [1]
Output: 3
💡 Note: Replace nums1=[0,1] with [2,3] and nums2=[1] with [1]. Both arrays are increasing and max value is 3.
Example 2 — All Zeros
$ Input: nums1 = [0,0], nums2 = [0]
Output: 6
💡 Note: Replace nums1=[0,0] with [2,4] and nums2=[0] with [6]. All even numbers, max is 6.
Example 3 — All Ones
$ Input: nums1 = [1], nums2 = [1,1]
Output: 5
💡 Note: Replace nums1=[1] with [1] and nums2=[1,1] with [3,5]. All odd numbers, max is 5.

Constraints

  • 1 ≤ nums1.length, nums2.length ≤ 1000
  • nums1[i], nums2[i] ∈ {0, 1}

Visualization

Tap to expand
Constructing Two Increasing Arrays01nums1 = [0,1]1nums2 = [1]Transform: 0→even, 1→odd23[2,3] increasing ✓1[1] increasing ✓Minimum Maximum: 3
Understanding the Visualization
1
Input
Two binary arrays: nums1=[0,1], nums2=[1]
2
Transform
Replace 0→even, 1→odd, maintain increasing order
3
Output
Minimum possible maximum value: 3
Key Takeaway
🎯 Key Insight: Count zeros and ones, then use formula: max(2×zeros, 2×ones-1) for optimal assignment
Asked in
Google 15 Amazon 12 Microsoft 8
8.5K Views
Medium Frequency
~15 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen