Problem · Array

Merge Sorted Array

Learn this problem
EasyAtlassian logoAtlassianFULLTIMEOA

Problem statement

nums1 has length m + n. Its first m values and the first n values of nums2 are each sorted in non-decreasing order; the remaining slots of nums1 are placeholders.

Merge the two valid portions into non-decreasing order and return the resulting nums1.

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 valid portions [1,2,3] and [2,5,6] merge into the shown order.

Constraints

  • 0 <= m, n <= 100000
  • 1 <= m + n
  • nums1.length = m + n
  • -10^9 <= value <= 10^9

More Atlassian problems

drafts saved locally
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