Merging Two Sorted Arrays Without Extra SpaceIn this problem, we will be given two sorted arrays. Our task is to merge the given two arrays. However, the constraint is that we have to merge them without using any extra space. Thus, after sorting the arrays, the initial elements will be in the first array (according to the initial capacity of the first array), and the remaining elements in the second array. Let us see some examples to understand the input and output of the program. Examples: Input: array1 = [11], array2 = [3, 4] Output: array1 = [2], array2 = [4, 11] Input: array1 = [1, 6, 9, 11, 16, 21], array2 = [1, 4, 7, 13] Output: array1 = [1, 1, 4, 6, 7, 9], array2 = [11, 13, 16, 21] The task would have been easy if we were allowed to use extra space. Though, solving this problem without extra space makes it complicated. The brute force approach will solve this problem in O(m * n) time complexity. However, there are efficient approaches to solving it. Approach - 1We will follow this method to solve this problem: We will begin iterating from the last element of the second array array2 and search if it is available in array1. If we find an element in array1 greater than that element, we will move the last element to the first array array1 and the other element in array2. To keep the two arrays sorted, we must insert the elements in the proper position. We can use the Insertion Sort algorithm to search for this correct position. Below is the algorithmic approach to solving this problem:
Below is the Python program for the implementation of the approach mentioned above. Code Output: Initial First Array: 1 6 9 11 16 21 Initial Second array: 1 4 7 13 Merged First Array: 1 1 4 6 7 9 Merged First Array: 11 13 16 21 Time Complexity: This program will take O(M * N) to complete. The time complexity is non-linear because we are iterating over both arrays in a nested loop. Auxiliary Space: Since the problem itself states to solve it by taking only constant extra space. Hence, the space complexity is O(1). Approach - 2Now we will discuss another efficient approach to solve this problem. There is a pattern repeating itself in the above solution. When traversing both sorted arrays simultaneously, suppose we reach the jth element of the second sorted array, which is smaller than the ith element of the first sorted array. Then in the next step, we have to insert the jth element in the first array, remove some kth element from the first array and insert it into the second array. We will use this pattern to optimize the above code. Here are the steps that we will follow to solve the problem:
Below is the Python program for the above algorithm Code Output: Initial First Array: 1 6 9 11 16 21 Initial Second array: 2 4 7 13 Merged First Array: 1 2 4 6 7 9 Merged Second Array: 11 13 16 21 Time Complexity: This program will take O((N + M) * log(N + M)) time to complete. Here N and M are the sizes of the two arrays. We are iterating the list only once (N + M); however, we are sorting the arrays at the end of every iteration; therefore, the linear time is multiplied by log(N + M). Space Complexity: Since the program is not taking any extra space, O(1). Approach - 3We will solve this problem using this approach: This approach will compare the first array's last element with the second array's first element. Now, if the element of the first array is greater than that of the second array, we will swap the two elements. The next step is the sorting of the arrays. The first array is already sorted, but we have the second one. We have to repeat these steps until the first condition is true. At the end of the process, we will get two sorted arrays. We will follow the steps below for this approach.
Below is the Python program to solve the problem with the abovementioned approach. Code Output: Initial First Array: 1 6 9 11 16 21 Initial Second array: 1 4 7 13 Merged First Array: 1 1 4 6 7 9 Merged First Array: 11 13 16 21 Time Complexity: This program will take O(M * (N * logN)) time complexity. Here M and N are the sizes of the two arrays. LogN time complexity is for sorting the second array at the end of every iteration. Auxiliary Space: The space complexity is the same as O(1). Approach - 4Below is another approach to solving the problem.
Below is the Python code for the implementation of the approach mentioned above. Code Output: Initial First Array: 1 6 9 11 16 21 Initial Second array: 1 4 7 13 Merged First Array: 1 1 4 6 7 9 Merged First Array: 11 13 16 21 Time Complexity: This program will take O(max(M * logM, N * logN)) time to complete. Here log time complexity is for sorting the two arrays after each iteration. Space Complexity: This program will take up constant extra space. Hence, O(1) Approach - 5In this approach, we will merge the two sorted arrays using the insertion sort. In the above program, when we insert the elements in other arrays, we can use the insertion sort to insert the elements, as the rest of the arrays are already sorted. This will further reduce the time complexity of the program. Hence, when swapping the two elements, we have to put the swapped elements in the position so that the array is still sorted. This can be done using a single traversal. Below are the steps that we will follow to solve the problem:
Below is the Python program to solve the problem using the above approach. Code Output: Initial First Array: [1, 6, 9, 11, 16, 21] Initial Second array: [1, 4, 7, 13] Merged First Array: [1, 1, 4, 6, 7, 9] Merged First Array: [11, 13, 16, 21] Time Complexity: This program has a non-linear time complexity. The time complexity is O(M * N). Here, M and N are the lengths of the two given arrays. Space complexity: Space complexity is the required constant space complexity. Hence, it is O(1). Approach - 6We will use the Euclidean Division Lemma to merge the two arrays in this approach. This lemma is stated as (((Operation_on_array) % A) * A) Below are the steps we need to follow to solve this problem:
Below is the Python program to solve this problem using Euclidean Division Lemma. Code Output: Initial First Array: 1 6 9 11 16 21 Initial Second array: 1 4 7 13 Merged First Array: 1 1 4 6 7 9 Merged First Array: 11 13 16 21 Time Complexity: Since we are using linear loops, the time complexity of this program will be O(M + N) Space Complexity: O(1) Next TopicBinomial Distribution in Python |