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

Read more

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

Read more

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 = ..

Read more