Ctrl K

Largest Number From Two Rows

Hard
You get two rows of digits, the arrays left and right, and a number size. Pick exactly size digits from the two rows (any number from each row, possibly none) and lay them out in a single line. Digits taken from the same row must keep their order from that row; digits from different rows may be interleaved freely. Return the line that is the largest as a size-digit number, as an array of digits.
Example 1
left=[2, 7, 1], right=[5, 8, 3, 6], size=4 → [8, 7, 6, 1]

Take 7 and 1 from left and 8 and 6 from right, then interleave them keeping each row's order: [8, 7, 6, 1]. No larger four-digit number can be built.

Example 2
left=[4, 0], right=[4, 9], size=4 → [4, 9, 4, 0]

All digits are needed. Take the 4 from right first to reach its 9, then 9, 4, 0: the answer is [4, 9, 4, 0].

Example 3
left=[1, 5], right=[3], size=1 → [5]
Constraints
  • 1 ≤ len(left), len(right) ≤ 500
  • 0 ≤ left[i], right[i] ≤ 9
  • 1 ≤ size ≤ len(left) + len(right)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFix the Middle of the Line MediumWater Between Walls HardSorting Batches With Repeats HardRaising by Ranges HardWidest Fence Span Around the Marked Board Hard
loading Python…
Input
left=[2, 7, 1], right=[5, 8, 3, 6], size=4
Expected
[8, 7, 6, 1]

Run checks the first 3 tests in your browser. Submit checks all 119 tests on the server, hidden ones included.