Merge without extra space

Merge without extra space

POTD : GEEKSFORGEEKS, DIFFICULTY: HARD, 07/07/2023

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:

  1. Initialize two pointers, first and second. first starts from the last index of arr1 (n-1), and second starts from the first index of arr2 (0).

  2. Use a while loop to iterate as long as first is greater than or equal to 0 and second is less than m. This ensures that we compare elements from both arrays until we reach the beginning of arr1 or the end of arr2.

  3. Compare the elements arr1[first] and arr2[second]: a. If arr1[first] is greater than arr2[second], swap the elements. This is done to ensure that the largest elements from both arrays are in arr1 at the end. Increment second by 1 to move to the next element in arr2. b. If arr1[first] is less than or equal to arr2[second], break out of the loop since the remaining elements in arr1 are already smaller than the elements in arr2.

  4. Sort arr1 and arr2 using the sort() method. This step is necessary to ensure that the modified arr1 and arr2 maintain their sorted order after the swap and potential breaking of the loop.

  5. After executing the code, arr1 will contain the first n elements in sorted order, and arr2 will contain the last m 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

  1. Initialize the pointers first = 3 (n-1) and second = 0.

  2. Start the while loop. Since first = 3 is greater than or equal to 0 and second = 0 is less than m = 5, we enter the loop.

  3. Compare the elements arr1[first] = 7 and arr2[second] = 0. Since 7 > 0, we swap the elements:

    • arr1 = [1, 3, 5, 0]

    • arr2 = [7, 2, 6, 8, 9] Increment second by 1.

  4. Compare the elements arr1[first] = 5 and arr2[second] = 2. Since 5 > 2, we swap the elements:

    • arr1 = [1, 3, 2, 0]

    • arr2 = [7, 5, 6, 8, 9] Increment second by 1.

  5. Compare the elements arr1[first] = 3 and arr2[second] = 5. Since 3 <= 5, we break out of the loop.

  6. Sort arr1 and arr2:

    • 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 ✨