Help me find out the error

//https://leetcode.com/problems/merge-sorted-array/
class Solution {
public:
void merge(vector& nums1, int m, vector& nums2, int n) {
int i=0;
for(int i=nums1.size()-1;i>=0;i–){
if(nums1[i]==0)
nums1.pop_back();
else
break;
}
for(int i=0;i<nums2.size();i++){
nums1.push_back(nums2[i]);
}
sort(nums1.begin(),nums1.end());
}
};

Your solution has 2 major loopholes.

  1. What will happen if the first vector has all zeros? Your code will clear the first vector which is not requires here.
  2. You are simply appending the second vector after the first vector which is wrong because you can have elements in the first vector which are greater than elements in the second vector. Example:
    vector1 : {1, 2, 3, 4, 5, 0, 0, 0, 0, 0}
    vector2 : {2, 4, 5, 6, 7}
    merged vector1: {1, 2, 2, 3, 4, 4, 5, 5, 6, 7}
    which is a combination of both vector1 and vector2.