Skip to content

Instantly share code, notes, and snippets.

@anjansrivathsav
Created August 5, 2023 19:47
Show Gist options
  • Save anjansrivathsav/f3e4d43c7c73273d23a90b65da9a58c5 to your computer and use it in GitHub Desktop.
Save anjansrivathsav/f3e4d43c7c73273d23a90b65da9a58c5 to your computer and use it in GitHub Desktop.
Merge Sorted Array
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int[] result = new int[m+n];
int i=0,j=0,z=0;
while(i<m && j<n){
if(nums1[i] <= nums2[j]){
result[z] = nums1[i];
i++;
z++;
}
else if(nums1[i] > nums2[j]){
result[z] = nums2[j];
j++;
z++;
}
}
if(i < m){
while(i < m){
result[z] = nums1[i];
i++;
z++;
}
}
if(j < n){
while(j < n){
result[z] = nums2[j];
j++;
z++;
}
}
for(int x=0;x<m+n;x++){
nums1[x] = result[x];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment