C++ solution in O(m+n)


#1

void Solution::merge(vector &A, vector &B) {
int i=0,j=0;
int n=A.size(),m=B.size();
vector ans;
while(i<n && j<m){
if(A[i]<B[j])
{
ans.push_back(A[i]);
i++;
}
else if(A[i]>B[j]){
ans.push_back(B[j]);
j++;
}
else{
ans.push_back(A[i]);
i++;
ans.push_back(B[j]);
j++;
}
}

while(i<n){
ans.push_back(A[i]);
i++;
}
while(j<m){
ans.push_back(B[j]);
j++;
}
A.clear();
for(int i=0;i<ans.size();i++){
A.push_back(ans[i]);
}
}


#2

@mansi332009 I think so you cannot make another container!! you have resize the vector A to M+N, then you have to proceed actually!!