10. Merge Two Sorted Arrays

EasyArrayArray

Given two integer arrays nums1 and nums2, both sorted in non-decreasing order, merge them into a single sorted array and return it.

The merged array should also be in non-decreasing order.

Input: Two sorted integer arrays nums1 and nums2.

Output: A single sorted merged array containing all elements from both arrays.

Examples

Example 1
Input: [1,2,3],[2,5,6]
Output: [1,2,2,3,5,6]
Explanation: Two pointers: pick 1(i), 2(j), 2(i), 3(i), 5(j), 6(j) → [1,2,2,3,5,6].
Example 2
Input: [1,3,5],[2,4,6]
Output: [1,2,3,4,5,6]
Explanation: Alternately pick from both arrays: 1,2,3,4,5,6.
Example 3
Input: [],[]
Output: []
Explanation: Both empty. Return [].

Constraints

Asked by

MetaHCLTechBloombergMicrosoftGoogleAmazon
Solve this problem in the editor →