Merge sort issue

can we perform merge in O(1) space complexity.if yes,how?

@Rj.25 hey ,you can’t perform merge on two sorted arrays in O(1) space complexity as where will you store the result of merge operation as the size of merged array will be size of 1st array + 2nd array but yes you could do like that to store merge result in 1st array and than remaining in second array .The idea is to begin from last element of ar2[] and search it in ar1[]. If there is a greater element in ar1[], then we move last element of ar1[] to ar2[]. To keep ar1[] and ar2[] sorted, we need to place last element of ar2[] at correct place in ar1[]. We can use Insertion Sort type of insertion for this.Hope you get it :slight_smile:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.