public class Solution {
public void Merge(int[] nums1, int m, int[] nums2, int n) {
int i=m-1;
int j=n-1;
for(int x=m+n-1;x>=0;x--){
if(i >= 0 && j >= 0){
if(nums1[i]>nums2[j]){
nums1[x]=nums1[i];
i--;
}
else{
nums1[x]=nums2[j];
j--;
}
}
else{
if(i>=0){
nums1[x]=nums1[i];
i--;
}
if(j>=0){
nums1[x]=nums2[j];
j--;
}
}
}
}
}- 1The Merge function combines two sorted arrays into the first array in non-decreasing order.
- 2It uses a reverse iteration approach to efficiently merge elements from both arrays.
- 3The algorithm operates in O(m+n) time complexity, where m and n are the lengths of the input arrays.
AI-generated summary · May not capture all nuances
"The Merge function combines two sorted arrays into the first array in non-decreasing order."
— Merge Sorted Array - Array - Easy - LeetCode
🛒 You'll Need This
Products related to this article

Marshall
Marshall Emberton II Compact Portable Bluetooth Speaker with 30+ Hours of Playtime, (360° Sound), Dust & Waterproof (IP67) – Cream.

Yamaha
Yamaha DBR12 (12") Powered Speakers 1000 watts Auxiliary (Black)

Canon
Canon Eos Ef 75-300Mm F/4-5.6 Ii Telephoto Zoom Lens For Dslr Camera - Black
As an Amazon Associate, AskGif earns from qualifying purchases