Problem Statement
Given two sorted arrays arr1[] and arr2[] of sizes n and m in non-decreasing order. Merge them in sorted order without using any extra space. Modify arr1 so that it contains the first N elements and modify arr2 so that it contains the last M elements.
Example 1:
Input:
n = 4, arr1[] = [1 3 5 7]
m = 5, arr2[] = [0 2 6 8 9]
Output:
arr1[] = [0 1 2 3]
arr2[] = [5 6 7 8 9]
Explanation:
After merging the two
non-decreasing arrays, we get,
0 1 2 3 5 6 7 8 9.
Example 2:
Input:
n = 2, arr1[] = [10 12]
m = 3, arr2[] = [5 18 20]
Output:
arr1[] = [5 10]
arr2[] = [12 18 20]
Explanation:
After merging two sorted arrays
we get 5 10 12 18 20.
Your Task:
You don't need to read input or print anything. You only need to complete the function merge() that takes arr1, arr2, n and m as input parameters and modifies them in-place so that they look like the sorted merged array when concatenated.
Expected Time Complexity: O((n+m) log(n+m))
Expected Auxilliary Space: O(1)
Constraints:
1 <= n, m <= 105
0 <= arr1i, arr2i <= 107
Ideology of Solving:
The ideology of the code is to start from the end of arr1
and the beginning of arr2
, comparing elements and swapping them if necessary. By sorting both arrays again, the final result is achieved with arr1
and arr2
modified to hold the desired elements in a sorted manner.
Optimized Approach:
Initialize two pointers,
first
andsecond
.first
starts from the last index ofarr1
(n-1
), andsecond
starts from the first index ofarr2
(0).Use a while loop to iterate as long as
first
is greater than or equal to 0 andsecond
is less thanm
. This ensures that we compare elements from both arrays until we reach the beginning ofarr1
or the end ofarr2
.Compare the elements
arr1[first]
andarr2[second]
: a. Ifarr1[first]
is greater thanarr2[second]
, swap the elements. This is done to ensure that the largest elements from both arrays are inarr1
at the end. Incrementsecond
by 1 to move to the next element inarr2
. b. Ifarr1[first]
is less than or equal toarr2[second]
, break out of the loop since the remaining elements inarr1
are already smaller than the elements inarr2
.Sort
arr1
andarr2
using thesort()
method. This step is necessary to ensure that the modifiedarr1
andarr2
maintain their sorted order after the swap and potential breaking of the loop.After executing the code,
arr1
will contain the firstn
elements in sorted order, andarr2
will contain the lastm
elements in sorted order.
def merge(self,arr1,arr2,n,m):
#code here
first = n-1
second = 0
while(first >= 0 and second < m):
if(arr1[first] > arr2[second]):
arr1[first], arr2[second] = arr2[second], arr1[first]
first -= 1
second += 1
else:
break
arr1.sort()
arr2.sort()
Example Illustration:
arr1 = [1, 3, 5, 7], n = 4 arr2 = [0, 2, 6, 8, 9], m = 5
Initialize the pointers
first = 3
(n-1) andsecond = 0
.Start the while loop. Since
first = 3
is greater than or equal to 0 andsecond = 0
is less than m = 5, we enter the loop.Compare the elements
arr1[first] = 7
andarr2[second] = 0
. Since7 > 0
, we swap the elements:arr1 = [1, 3, 5, 0]
arr2 = [7, 2, 6, 8, 9] Increment
second
by 1.
Compare the elements
arr1[first] = 5
andarr2[second] = 2
. Since5 > 2
, we swap the elements:arr1 = [1, 3, 2, 0]
arr2 = [7, 5, 6, 8, 9] Increment
second
by 1.
Compare the elements
arr1[first] = 3
andarr2[second] = 5
. Since3 <= 5
, we break out of the loop.Sort
arr1
andarr2
:arr1 = [0, 1, 2, 3]
arr2 = [5, 6, 7, 8, 9]
After executing the code,
arr1 contains the first 4 elements in sorted order: [0, 1, 2, 3],
and arr2 contains the last 5 elements in sorted order: [5, 6, 7, 8, 9].
The code effectively merges the two sorted arrays while modifying arr1 and arr2 in place.
Happy Coding ✨