DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
easy
Array

Merge Sorted Array

**Problem Statement:** You are given two integer arrays `nums1` and `nums2`, sorted in non-decreasing order, and two integers `m` and `n`, representing the number of elements in `nums1` and `nums2` respectively. Merge `nums1` and `nums2` into a single array sorted in non-decreasing order. **Hint / Expected Approach:** Three-pointer merge from the end **Edge Cases to Consider:** - (1) m = 0 (first array empty) - (2) n = 0 - (3) All elements of one array are greater

Examples

Example 1
Input: [1,2,3,0,0,0], 3, [2,5,6], 3
Output: [1,2,2,3,5,6]
Example 2
Input: [1], 1, [], 0
Output: [1]
Example 3
Input: [0], 0, [1], 1
Output: [1]

Constraints

  • ▪1 <= nums.length <= 10^4
  • ▪-10^9 <= nums[i] <= 10^9
  • ▪Time complexity should be O(n) or O(n log n)

Watch Out For Edge Cases

  • ▪m = 0 (first array empty)
  • ▪n = 0
  • ▪All elements of one array are greater
Frequently Asked At
GoogleAmazonMicrosoftMetaAdobeFlipkart