LeetCode Problem

How to Solve Create Maximum Number

To solve the Create Maximum Number problem, efficiently merge digits from two arrays while maintaining their relative order. Use techniques like two-pointer scanning, monotonic stacks, and greedy strategies to form the maximum number of length k. This problem tests your ability to merge arrays while considering optimality in choosing digits.

GhostInterview Help

Need help with Create Maximum Number without spending extra time grinding it?

GhostInterview can read Create Maximum Number from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #321Two-pointer scanning with invariant trackingReviewed 2026-03-07
Difficulty
Hard
Primary pattern
Two-pointer scanning with invariant tracking
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve the Create Maximum Number problem, efficiently merge digits from two arrays while maintaining their relative order. Use techniques like two-pointer scanning, monotonic stacks, and greedy strategies to form the maximum number of length k. This problem tests your ability to merge arrays while considering optimality in choosing digits.

Problem Statement

You are given two integer arrays nums1 and nums2 of lengths m and n, respectively, and an integer k. Your task is to form the maximum number of length k from the digits of nums1 and nums2 while preserving the relative order of digits in each array.

Return an array of the k digits that represents the maximum number possible, where the digits are chosen from both arrays. The relative order of digits from the same array must remain unchanged.

Examples

Example 1

Input: nums1 = [3,4,6,5], nums2 = [9,1,2,5,8,3], k = 5

Output: [9,8,6,5,3]

Example details omitted.

Example 2

Input: nums1 = [6,7], nums2 = [6,0,4], k = 5

Output: [6,7,6,0,4]

Example details omitted.

Example 3

Input: nums1 = [3,9], nums2 = [8,9], k = 3

Output: [9,8,9]

Example details omitted.

Constraints

  • m == nums1.length
  • n == nums2.length
  • 1 <= m, n <= 500
  • 0 <= nums1[i], nums2[i] <= 9
  • 1 <= k <= m + n
  • nums1 and nums2 do not have leading zeros.

Solution Approach

Two-Pointer Merging with Invariant Tracking

The key approach is to merge digits from both arrays using two pointers, ensuring that the relative order is preserved. At each step, we choose the larger possible digit while maintaining the order from each array. By tracking the remaining digits efficiently, this method ensures the largest possible number.

Greedy Selection with Monotonic Stack

Using a monotonic stack helps to efficiently choose the largest digits. The stack maintains the order and allows us to push and pop digits greedily, ensuring that we always pick the next largest digit that contributes to maximizing the final number.

Combining Results from Two Arrays

After selecting the maximum digits from each array, merge the results. This involves picking the optimal digits from nums1 and nums2 based on the two-pointer approach and ensuring that the order is maximized. The result is a merged number of length k.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity depends on the final approach chosen, but a typical solution with two-pointer scanning will have a time complexity of O(m + n), where m and n are the lengths of nums1 and nums2, respectively. The space complexity is also O(m + n), due to the space required to store the resulting array and intermediate calculations.

What Interviewers Usually Probe

  • The candidate demonstrates understanding of two-pointer scanning and invariant tracking.
  • The candidate can articulate why a greedy approach works in this problem and how monotonic stacks are used.
  • The candidate shows ability to merge results from multiple arrays while maintaining order and maximizing the result.

Common Pitfalls or Variants

Common pitfalls

  • Failing to maintain the relative order of digits from each array.
  • Choosing digits greedily without considering the impact on future selections.
  • Incorrectly merging the two arrays and not adjusting the pointer positions properly.

Follow-up variants

  • Problem with larger arrays where the number k is near the total length of nums1 and nums2.
  • A version where only one array is non-empty.
  • A variation where the target number length k is smaller than the combined length of nums1 and nums2.

How GhostInterview Helps

  • GhostInterview provides step-by-step guidance on how to implement two-pointer merging with efficient tracking.
  • GhostInterview helps in optimizing the selection process by providing insights into greedy algorithms and monotonic stacks.
  • GhostInterview simulates interviews by giving feedback on optimal approaches for merging arrays to form the maximum number.

Topic Pages

FAQ

How can I efficiently merge two arrays to create the maximum number?

Use a two-pointer approach combined with a monotonic stack to select the largest digits while maintaining the relative order of the arrays.

What are common mistakes when solving Create Maximum Number?

Common mistakes include failing to maintain the relative order of digits and greedy selection without considering future choices.

What is the time complexity of the Create Maximum Number problem?

The time complexity is typically O(m + n), where m and n are the lengths of the input arrays nums1 and nums2.

What is the pattern for solving Create Maximum Number?

The pattern involves using two-pointer scanning with invariant tracking, along with a greedy approach and monotonic stack to maximize the result.

How does GhostInterview help with the Create Maximum Number problem?

GhostInterview provides solutions using two-pointer techniques, explaining the greedy approach and offering feedback for optimal merging strategies.

GhostInterview Solver

Need direct help with Create Maximum Number instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Create Maximum Number from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.