Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

9. Merge Sorted Array

EasyDay 3 of 60Arrays & Hashing

You get two arrays sorted in non-decreasing order: nums1 with m real elements followed by n zeros (empty slots), and nums2 with n elements. Merge nums2 into nums1 so that nums1 holds all m + n values in sorted order. Modify nums1 in place and return nothing.

Example 1
Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
The two sorted lists [1,2,3] and [2,5,6] are combined inside nums1.
Example 2
Input: nums1 = [0], m = 0, nums2 = [1], n = 1
Output: [1]
nums1 has no real elements, so the result is just nums2.
Constraints
  • nums1.length == m + n
  • nums2.length == n
  • 0 <= m, n <= 200
  • 1 <= m + n <= 200
  • -10^9 <= nums1[i], nums2[j] <= 10^9
Hints
Frequently asked at: Microsoft, Meta, Amazon, Bloomberg, Accenture
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min