We get O(m+n)
on merging 2 sorted arrays if we are allowed to use O(n)
extra space of temporary array. So all elements are merged into one temporary array.
But, how to perform merging if I am only allowed to use O(1)
extra space?
We get O(m+n)
on merging 2 sorted arrays if we are allowed to use O(n)
extra space of temporary array. So all elements are merged into one temporary array.
But, how to perform merging if I am only allowed to use O(1)
extra space?
Yes it is possible to merge two sorted arrays with O(1) extra space.
We can achieve this by using Insertion sort.
But the time complexity to merge two sorted arrays using O(1) extra space would be O(n^2).
If We allowed to use O(n) extra space, then we can use Merge sort with time complexity of just O(nlogn).