2dbi
Home/Dell Technologies/Merge Two Sorted Arrays In Place
DDell Technologies·DSASDE-1Technical Phone Screen

Merge Two Sorted Arrays In Place

Problem

Merge nums2 into nums1 (which has trailing space) in place so the result is sorted.

Example

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

Constraints

  • 0 ≤ m,n ≤ 200

Approach

Merge from the back to avoid overwrites. O(m+n).

added 6 days ago
LeadersAccount