🎉 One-stop destination for all your technical interview Preparation 🎉
// leetcode
class Solution{
public:
void merge(vector<int> &nums1, int m, vector<int> &nums2, int n)
{
int a = m - 1; // last index of nums1
int b = n - 1; // last index of nums2
int i = m + n - 1; // last index of extended nums1(with 0 wala)
while (a >= 0 && b >= 0) // while we not reach start of any array
{
if (nums1[a] > nums2[b])
{
nums1[i] = nums1[a];
i--, a--;
}
else
{
nums1[i] = nums2[b];
i--, b--;
}
}
while (b >= 0) // if there are elements remaining in b then put them in back
{
nums1[i] = nums2[b];
i--, b--;
}
}
};
class Solution{
public:
//Function to merge the arrays.
void merge(long long arr1[], long long arr2[], int n, int m){
// code here
long long i = n - 1, j = 0;
while (i >= 0 && j < m){
if (arr1[i] > arr2[j]){
swap(arr1[i], arr2[j]);
}
i--, j++;
}
sort(arr1, arr1 + n);
sort(arr2, arr2 + m);
}
};