828. Constructing Two Increasing Arrays

Given two integer arrays nums1 and nums2, each containing only 0s and 1s, your goal is to determine the minimum possible value of the largest number across both arrays after performing the following transformation.

Replace every 0 with a positive even integer and every 1 with a positive odd integer. After these replacements, both arrays must be strictly increasing, and each integer can be used at most once across both arrays.

Return the minimum possible largest number that can appear in either array after making the changes.

Example 1:

Input : nums1 = [], nums2 = [1,0,1]

Output : 3

Explanation :

Since nums1 is empty, focus on nums2. One valid transformation is to replace the first 1 with 1, the 0 with 2, and the last

Example 2:

Input : nums1 = [0,0,1], nums2 = [1,0]

Output : 6

Explanation :

A possible assignment is:

  • For nums1 (which is [0,0,1]): assign even numbers 2 and 4 for the two 0s, and assign 5 (an odd number) for the 1, yielding [2, 4, 5].
  • For nums2 (which is [1,0]): assign 1 (the smallest available odd) for the 1, and assign 6 (the smallest even greater than 1 and not used in nums1) for the 0, yielding [1, 6].

Both arrays are strictly increasing with distinct numbers, and the largest number used is 6.

Now Your Turn!

Pick the correct output for the given input

Input : nums1 = [1,1,0,1], nums2 = [0,1,0]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 0 <= nums1.length <= 1000
  • 1 <= nums2.length <= 1000
  • nums1 and nums2 consist only of 0 and 1.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int minLargest(vector<int>& nums1, vector<int>& nums2) {
// Your code goes here
}
};
Test Case

Input:

Nums1
Nums2