Loading practice problems…
Loading your practice workspace…
Loading code editor…
17 / 29
Sign in
Algorithms/Sorting

Merge Sorted Array

easy25 min

nums1 contains m sorted values followed by enough zero placeholders to hold nums2. nums2 contains n sorted values.

Merge nums2 into nums1 in non-decreasing order and return nums1.

Example 1

Inputnums1 = [1, 2, 3, 0, 0, 0], m = 3, nums2 = [2, 5, 6], n = 3

Output[1, 2, 2, 3, 5, 6]

Example 2

Inputnums1 = [1], m = 1, nums2 = [], n = 0

Output[1]

Constraints

  • nums1.length = m + n
  • Both input ranges are sorted.
SortingTwo Pointers