
Problem
Solution
Submissions
Create Maximum Number
Certification: Advanced Level
Accuracy: 0%
Submissions: 0
Points: 15
Write a C program to create the maximum number by picking k digits from two given arrays nums1 and nums2. The relative order of the digits from the same array must be preserved. Return an array of length k representing the largest number you can create.
Example 1
- Input: nums1 = [3,4,6,5], nums2 = [9,1,2,5,8,3], k = 5
- Output: [9,8,6,5,3]
- Explanation:
- Need to pick 5 digits total from both arrays.
- Try different combinations: 0 from nums1 + 5 from nums2, 1 from nums1 + 4 from nums2, etc.
- Best combination is 2 from nums1 ([6,5]) and 3 from nums2 ([9,8,3]).
- Merge to get [9,8,6,5,3] which is the maximum possible.
- Need to pick 5 digits total from both arrays.
Example 2
- Input: nums1 = [6,7], nums2 = [6,0,4], k = 5
- Output: [6,7,6,0,4]
- Explanation:
- Need all 5 digits from both arrays (2 + 3 = 5).
- Take all from nums1 ([6,7]) and all from nums2 ([6,0,4]).
- Merge arrays to maximize result: [6,7,6,0,4].
- This gives the maximum number possible with k=5 digits.
- Need all 5 digits from both arrays (2 + 3 = 5).
Constraints
- 1 ≤ nums1.length, nums2.length ≤ 500
- 1 ≤ nums1.length + nums2.length ≤ 1000
- 0 ≤ nums1[i], nums2[i] ≤ 9
- 1 ≤ k ≤ nums1.length + nums2.length
- Time Complexity: O(k*(m+n)^2) where m, n are array lengths
- Space Complexity: O(k) for result array
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- You hve to try all possible ways to distribute k digits between the two arrays
- For each distribution, find maximum subsequence of required length from each array
- Use a stack-based approach to find maximum subsequence maintaining relative order
- Merge the two maximum subsequences to create the final maximum number
- Compare different combinations and return the lexicographically largest result
- When merging, always pick the digit that makes the result larger
- Handle edge cases where one array contributes 0 digits