FastPrepMerge Sorted Array

Merge Sorted Array

Bloomberg LP logoBloomberg LP● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

nums1 has length m+n. Its first m values and all n values of nums2 are sorted nondecreasingly; the remaining positions of nums1 are capacity.

Merge both sorted sequences into nums1 and return it.

Function

mergeSortedArray(nums1: int[], m: int, nums2: int[], n: int) → int[]

Examples

Example 1

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

The two sorted prefixes merge in nondecreasing order.

Example 2

nums1 = [0]m = 0nums2 = [1]n = 1return = [1]

The first sequence is empty.

Constraints

  • 0 <= m, n and 1 <= m+n <= 10^5.
  • nums1.length == m+n and nums2.length == n.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int[] mergeSortedArray(int[] nums1, int m, int[] nums2, int n) {
  // Write your code here.
}
nums1[1,2,3,0,0,0]
m3
nums2[2,5,6]
n3
expected[1,2,2,3,5,6]
Checking account…