'Merge and Sort in Array
In this question I am trying to solve the question of merge and sort. I think my approach is correct but in the question its written that
You have to return the size of the array formed (4 in this case) and update the answer array in the function mergeNsort().
so I am unable to do its step.
class Solution{
public:
int mergeNsort(int a[], int b[], int n, int m, int answer[])
{
vector<int> z;
int i=0;
int j=0;
while(i<n && j<m)
{
if(a[i]<b[j])
{
z.push_back(a[i]);
i++;
}
else
{
z.push_back(a[j]);
j++;
}
}
while(i>n)
{
z.push_back(a[i]);
i++;
}
while(j>m)
{
z.push_back(b[j]);
j++;
}
for(int i=0;i<n;i++)
{
answer[i]=z[i];
}
}
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
