07. Merge Without Extra Space
The problem can be found at the following link: Question Link
My Approach
This problem is a variation of the simple merge sort problem, with the added challenge of achieving O(1) space complexity.
Storing two values at the same index concept
To accomplish this, we can employ a technique that allows us to store two values at the same index of an array.
Given that the maximum value in the array is
1e7
, we can utilize a method where we store the first number in the last 8 digits of the array value and the second number in the remaining digits.
For instance, if we need to store 13 and 1045 at the same index of the array, we would represent it as 130001045. By dividing the number by 1e7, we retrieve 13, and the remainder gives us 1045. This approach simplifies the task while maintaining O(1) space complexity.
Merge concept:
To accomplish the merging process,
We can apply the merge algorithm which typically used in merge sort.
Given that the arrays are sorted, we can select the smaller value between the ith index of
arr1
and the jth index ofarr2
, and place it in the kth index of the combined array (arr1+arr2).
The process for merging in this problem follows the standard procedure used in the merge algorithm of merge sort.
For a more comprehensive understanding of the merge and merge sort concepts, you can refer to this link.
Storing values in sorted array and retrieval
To address the issue of storing values at the kth index of the combined sorted array, we can utilize the approach described earlier, where we multiply the new value by an offset (
1e7
) and add it to the index.To store a value at the kth index of the combined array, we perform a check. If the value of k is less than n, we store the newly sorted value in
arr1[k]
. However, ifk
is greater than or equal ton
, we store the value atarr2[k-n]
.Once all the merge operations are completed, we can retrieve the values from
arr1
andarr2
by simply dividing them by the offset value (1e7
).
For further clarity and understanding, please refer to the provided code.
Time and Auxiliary Space Complexity
Time Complexity:
O(n + m)
because we iterate through alln
elements of arr1 andm
elements of arr2 in the merge function.Auxiliary Space Complexity:
constant
auxiliary space complexity as it does not require any extra space.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the getlost01/gfg-potd repository.
Last updated