88. Merge Sorted Array (Leetcode)

PHOTO EMBED

Mon Oct 04 2021 10:24:48 GMT+0000 (UTC)

Saved by @Sakshamkashyap7 #c++

class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        int p = m-1, q = n-1, ind = nums1.size()-1;
        while(p>=0 && q>=0) {
            if(nums2[q] > nums1[p]) {
                nums1[ind--] = nums2[q];
                q--;
            }
            else    {
                nums1[ind--] = nums1[p];
                p--;
            }
        }
        while(q >= 0)    {
            nums1[ind--] = nums2[q--];
        }
    }
};
content_copyCOPY

Merge two sorted array and arrange only in first array Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6]

https://leetcode.com/problems/merge-sorted-array/