Algorithm: Question is really tricky if we don't think smartly !!!! :)
use two pointer & points l to 1st & r to 2nd array
if elementb in 1st is smaller trhen 2nd arraye elemnt at index i then increment 1st pointer
else
swap element at index i in both array &b incerment 1st pointer & sort 2nd
array its not necessary only if we need sorted output in each array
Solution:
size of a=m size of b =n
a[]={1,3,77,78,90} and b[]={2,5,79,81}
l r
while(l<=m && r<=n)
{
if(b[r] {
swap(a[l],b[r]);
l++;
sort(b[]);
}
elseif(a[l] l++;
}
Time Complexity O(mlogm) sorting to 2nd array
Space Complexity O(1)
No comments :
Post a Comment