/88. Merge Sorted Array

88. Merge Sorted Array

Easy
Arrays54.5% acceptance

Given two sorted integer arrays arr_a and arr_b, and two integers len_a and len_b representing the number of valid elements in arr_a and arr_b respectively, merge arr_b into arr_a so that arr_a contains all elements from both arrays in non-decreasing order. arr_a has a length of len_a + len_b, with the first len_a elements valid and the rest reserved for merging. The merged result must be stored in arr_a in-place.

Example 1

Input: arr_a = [3, 4, 7, 0, 0, 0], len_a = 3, arr_b = [1, 5, 8], len_b = 3

Output: [1, 3, 4, 5, 7, 8]

Explanation: Merge [3,4,7] and [1,5,8] into arr_a.

Example 2

Input: arr_a = [2, 0], len_a = 1, arr_b = [1], len_b = 1

Output: [1, 2]

Explanation: Merge [2] and [1] into arr_a.

Example 3

Input: arr_a = [0], len_a = 0, arr_b = [4], len_b = 1

Output: [4]

Explanation: Merge [] and [4] into arr_a.

Constraints

  • arr_a has length len_a + len_b
  • arr_b has length len_b
  • 0 <= len_a, len_b <= 200
  • 1 <= len_a + len_b <= 200
  • -109 <= arr_a[i], arr_b[j] <= 109
Python (current runtime)

Case 1

Input: arr_a = [6, 9, 10, 0, 0, 0], len_a = 3, arr_b = [2, 7, 11], len_b = 3

Expected: [2, 6, 7, 9, 10, 11]

Case 2

Input: arr_a = [5, 0, 0], len_a = 1, arr_b = [1, 3], len_b = 2

Expected: [1, 3, 5]

Case 3

Input: arr_a = [0, 0, 0], len_a = 0, arr_b = [-1, 0, 2], len_b = 3

Expected: [-1, 0, 2]

Case 4

Input: arr_a = [7, 8, 9, 0, 0], len_a = 3, arr_b = [1, 2], len_b = 2

Expected: [1, 2, 7, 8, 9]

Case 5

Input: arr_a = [1, 2, 3, 0], len_a = 3, arr_b = [4], len_b = 1

Expected: [1, 2, 3, 4]