Ctrl K

Merge Into the Spare Tail

Easy
You get two arrays sorted in non-decreasing order. In target only the first filled elements matter; after them there is a spare tail with room for exactly extra_len items (it holds zeros, which you should ignore). The array extra holds extra_len elements. Move every element of extra into target so that the whole of target ends up sorted in non-decreasing order. Return the resulting target. Try to do it in O(filled + extra_len) time with no extra array.
Example 1
target=[2, 6, 9, 0, 0], filled=3, extra=[4, 10], extra_len=2 → [2, 4, 6, 9, 10]

After the merge target becomes [2, 4, 6, 9, 10], and that is what you return.

Example 2
target=[0, 0], filled=0, extra=[-3, 5], extra_len=2 → [-3, 5]

target has no elements of its own, so it simply receives extra: [-3, 5].

Example 3
target=[1, 1, 7], filled=3, extra=[], extra_len=0 → [1, 1, 7]
Constraints
  • len(target) = filled + extra_len
  • len(extra) = extra_len
  • 0 ≤ filled, extra_len ≤ 200
  • 1 ≤ filled + extra_len ≤ 200
  • -10⁹ ≤ target[i], extra[j] ≤ 10⁹
  • The first filled elements of target and all of extra are sorted in non-decreasing order
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsValues in Both Lists EasyShared Values With Repeats EasyCake Slices for Guests EasyEvens to the Left EasyParity in Its Place Easy
loading Python…
Input
target=[2, 6, 9, 0, 0], filled=3, extra=[4, 10], extra_len=2
Expected
[2, 4, 6, 9, 10]

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