#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++; } } ..

#### 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++; } } ..

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() ..

Our instructor asked us to create our own Merge Sort Algorithm before showing us how to make one. He explained how it works and this is what I came up. It works but there is a bug, my code catches garbage number. First garbage number occurs at vector/array size is 7 then next is 11 ..

I have a collection of vectors and want to merge them into one big vector. I’ve written a function that takes in a list of integers, which all represent a different vector(e.g. v1 and v2), and returns a vector containing all the elements of the desired vectors. std::vector<int> mergeVectors(std::vector<unsigned int> indices) { std::vector<int> v1 = ..

This is my code,I’ve run an iterator i in the first array and swapped each element with the min_element in array 2 and then called sort on the second array.But the output isn’t sorted at all.I dont know whats wrong with this approach. #include<bits/stdc++.h> class Solution{ public: void swap(int *a,int *b){ int temp=*a; *a=*b; *b=temp; ..

## Recent Comments