/88. Merge Sorted Array

88. Merge Sorted Array

Easy
Arrays54.5% acceptance

Given two integer arrays arr_a and arr_b, both sorted in non-decreasing order, 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 the merged sorted array. arr_a has a length of len_a + len_b, with the first len_a elements valid and the remaining len_b elements set to 0 and ignored. arr_b has a length of len_b. The function should modify arr_a in-place to contain the merged sorted array.

Example 1

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

Output: [2,4,5,7,8,9]

Explanation: Merge [4,7,9] and [2,5,8] into [2,4,5,7,8,9].

Example 2

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

Output: [1,2,3]

Explanation: Merge [3] and [1,2] into [1,2,3].

Example 3

Input: arr_a = [0,0,0], len_a = 0, arr_b = [5,6,7], len_b = 3

Output: [5,6,7]

Explanation: Merge [] and [5,6,7] into [5,6,7].

Constraints

  • arr_a.length == len_a + len_b
  • arr_b.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 = [10,20,30,0,0,0], len_a = 3, arr_b = [15,25,35], len_b = 3

Expected: [10,15,20,25,30,35]

Case 2

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

Expected: [1,2,3]

Case 3

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

Expected: [-5]

Case 4

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

Expected: [-2,-1,0]

Case 5

Input: arr_a = [100,200,300,0,0], len_a = 3, arr_b = [150,250], len_b = 2

Expected: [100,150,200,250,300]