TypeScriptMediumTwo Pointers25 min

Merge two sorted arrays

Merge two nondecreasing arrays into one nondecreasing array, preserving duplicates. Inputs are valid JSON and must not be modified unless the algorithm uses a local copy. This exercise probes typed data modeling, boundary handling, and algorithm selection.

Input

One JSON value: { left: number[]; right: number[] }. See the sample for its exact shape.

Output

One compact JSON value of type number[]. Strings include JSON quotes; arrays preserve the specified order.

Constraints

Array/string lengths are 0 to 10000 unless stated otherwise. Numbers are integers in [-1000000,1000000]; all computed results fit the safe integer range.

Example

Input

{"left":[1,3,5],"right":[2,4]}

Output

[1,2,3,4,5]

Example 2 input

{"left":[-5,0],"right":[-4,9]}

Output

[-5,-4,0,9]

Checks a boundary or contrasting case beyond the public examples.

Public test cases (2)

  • Input

    {"left":[1,3,5],"right":[2,4]}

    Expected output

    [1,2,3,4,5]
  • Input

    {"left":[],"right":[]}

    Expected output

    []
Hints (2)
  1. Start with the required output order and the empty-input behavior.
  2. Advance whichever input currently has the smaller value

Target complexity: time O(n+m) · space O(n+m)

Your solution

Checking runner availability…

Reference solution

Sign in for solution
All TypeScript programs