You are given two integer arrays nums1 and nums2 of lengths m and n respectively. nums1 and nums2 represent the digits of two numbers. You are also given an integer k.
Create the maximum number of length k <= m + n from digits of the two numbers. The relative order of the digits from the same array must be preserved.
Return an array of the k digits representing the answer.
Example 1:
Input: nums1 = [3,4,6,5], nums2 = [9,1,2,5,8,3], k = 5 Output: [9,8,6,5,3]
Example 2:
Input: nums1 = [6,7], nums2 = [6,0,4], k = 5 Output: [6,7,6,0,4]
Example 3:
Input: nums1 = [3,9], nums2 = [8,9], k = 3 Output: [9,8,9]
Constraints:
m == nums1.lengthn == nums2.length1 <= m, n <= 5000 <= nums1[i], nums2[i] <= 91 <= k <= m + nnums1 and nums2 do not have leading zeros.Problem Overview: You are given two arrays of digits and an integer k. Pick digits from both arrays to build the largest possible number of length k, while preserving the relative order of digits within each array. The challenge is deciding how many digits to take from each array and merging them so the final sequence is lexicographically maximum.
Approach 1: Greedy Combine Method (Greedy + Monotonic Stack) (Time: O((n+m)k), Space: O(k))
This approach breaks the problem into two subproblems: selecting the best subsequence from each array and merging them optimally. For every possible split where you take i digits from nums1 and k-i from nums2, compute the maximum subsequence of that length using a monotonic decreasing stack. While iterating through the array, pop smaller digits from the stack if you still have removals available so larger digits can appear earlier.
Once both subsequences are built, merge them greedily. At each step compare the remaining suffixes of both sequences and append the lexicographically larger one. This comparison ensures the final number stays globally optimal instead of locally greedy. The algorithm iterates through all valid splits, generates candidate numbers, and keeps the maximum result. This technique heavily uses ideas from greedy algorithms, stack, and monotonic stack patterns.
Approach 2: Dynamic Programming Approach (Time: O(n*m*k), Space: O(n*m*k))
The dynamic programming formulation tracks the best possible sequence formed using prefixes of both arrays. Define a DP state representing the best number you can form using the first i elements of nums1, the first j elements of nums2, and building a sequence of length t. From each state, decide whether to take the next digit from nums1 or nums2, updating the lexicographically maximum result.
This approach guarantees correctness but becomes expensive because each state stores or compares sequences. The number of states grows with n × m × k, and merging candidate sequences is costly. While useful for understanding the decision structure, it is rarely used in production solutions due to memory and runtime overhead.
Recommended for interviews: The greedy monotonic stack + merge method is the expected solution. Interviewers want to see that you can decompose the problem: generate maximum subsequences, then merge them with lexicographic comparison. Mentioning the brute-force split and explaining why the stack-based subsequence extraction works shows strong reasoning with array and greedy patterns.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Greedy Combine with Monotonic Stack | O((n+m)k) | O(k) | Best practical solution. Efficient for large arrays and commonly expected in interviews. |
| Dynamic Programming | O(n*m*k) | O(n*m*k) | Useful for understanding state transitions or academic discussion, but too slow for large inputs. |
LeetCode was HARD until I Learned these 15 Patterns • Ashish Pratap Singh • 1,002,125 views views
Watch 9 more video solutions →Practice Create Maximum Number with our built-in code editor and test cases.
Practice on FleetCode