Tutorialspoint
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.
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.
Constraints
  • 1 ≤ nums1.length, nums2.length500
  • 1 ≤ nums1.length + nums2.length1000
  • 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
NumberHCL TechnologiesD. E. Shaw
Editorial

Login to view the detailed solution and explanation for this problem.

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.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

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

Steps to solve by this approach:

 Step 1: Iterate through all possible ways to distribute k digits between nums1 and nums2 arrays

 Step 2: For each distribution (i digits from nums1, k-i digits from nums2), find maximum subsequence from each array
 Step 3: Use stack-based greedy approach to extract maximum subsequence while maintaining relative order
 Step 4: Merge the two maximum subsequences by always choosing the digit that creates larger result
 Step 5: When merging equal digits, look ahead in both arrays to determine which contributes to larger number
 Step 6: Compare all possible merged results and keep track of the lexicographically largest one
 Step 7: Return the maximum number found across all possible distributions of k digits

Submitted Code :