#### Category : array-merge

#include<bits/stdc++.h> using namespace std; class Solution{ public: void merge(int arr1[], int arr2[], int n, int m) { // code here int gap,cnt=0; gap=(m+n)%2+(m+n)/2; while(gap>=1 && cnt<=1) { for(int i=0,j=gap-1;j<m+n;j++,i++ ) { if(i>=n ) { if(arr2[j-n]<arr2[i-n]) swap(arr2[j-n],arr2[i-n]); } else{ if(j<n) { if(arr1[j]<arr1[i]) swap(arr1[j],arr1[i]); } else { if(arr2[j-n]<arr1[i]) swap(arr2[j-n],arr1[i]); } } } gap=(gap/2)+(gap%2); if(gap==1) cnt++; } } ..

#include<bits/stdc++.h> using namespace std; class Solution{ public: void merge(int arr1[], int arr2[], int n, int m) { // code here int gap,cnt=0; gap=(m+n)%2+(m+n)/2; while(gap>=1 && cnt<=1) { for(int i=0,j=gap-1;j<m+n;j++,i++ ) { if(i>=n ) { if(arr2[j-n]<arr2[i-n]) swap(arr2[j-n],arr2[i-n]); } else{ if(j<n) { if(arr1[j]<arr1[i]) swap(arr1[j],arr1[i]); } else { if(arr2[j-n]<arr1[i]) swap(arr2[j-n],arr1[i]); } } } gap=(gap/2)+(gap%2); if(gap==1) cnt++; } } ..

I was solving the leetcode question Merge Sorted Array and I am getting runtime error while running the following code on leetcode Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. The number of elements initialized in nums1 and nums2 are m and n respectively. You may assume ..

I am struggling to solve this problem, merging two sorted arrays (or vectors in this specific case). I am getting very weird output when logging the vector elements to the console. My ideal output would be all of the numbers in order. Here is the code: #include <iostream> #include <vector> using namespace std; int main() ..