Merge Two Sorted Arrays Without Extra Space in DSA Rust - Time & Space Complexity
We want to know how long it takes to merge two sorted arrays without using extra space.
How does the time grow as the arrays get bigger?
Analyze the time complexity of the following code snippet.
fn merge_without_extra_space(arr1: &mut [i32], arr2: &mut [i32]) {
let n = arr1.len();
let m = arr2.len();
let mut i = 0;
let mut j = 0;
while i < n && j < m {
if arr1[i] > arr2[j] {
arr1.swap(i, n - 1);
arr2.swap(j, 0);
i += 1;
j += 1;
} else {
i += 1;
}
}
arr1.sort();
arr2.sort();
}
This code merges two sorted arrays by swapping elements and then sorting each array again.
Look for loops and repeated steps.
- Primary operation: The while loop runs comparing and swapping elements.
- How many times: Up to n times, where n is the size of the first array.
- Additional operations: Sorting both arrays at the end, which involves multiple passes over the data.
As the arrays get bigger, the number of comparisons and swaps grows, plus sorting takes more time.
| Input Size (n + m) | Approx. Operations |
|---|---|
| 10 | About 10 comparisons + sorting small arrays |
| 100 | About 100 comparisons + sorting arrays of size 100 |
| 1000 | About 1000 comparisons + sorting arrays of size 1000 |
Pattern observation: The comparisons grow linearly, but sorting grows faster, roughly proportional to n log n.
Time Complexity: O((n + m) log (n + m))
This means the time grows a bit faster than the size of the arrays because sorting takes more work as arrays get bigger.
[X] Wrong: "Swapping elements alone makes the merge fast and linear."
[OK] Correct: Because sorting is still needed after swapping, which takes more time than just swapping.
Understanding this helps you explain trade-offs between using extra space and time, a common topic in interviews.
"What if we used a gap method to merge without sorting at the end? How would the time complexity change?"
Practice
Solution
Step 1: Understand the problem requirement
The problem asks to merge two sorted arrays into a sorted sequence without using extra space, meaning no new arrays can be created.Step 2: Identify the correct approach
We must rearrange elements within the given arrays to maintain sorted order combined, without additional memory.Final Answer:
Combine both arrays into one sorted sequence without using extra memory -> Option BQuick Check:
Merge without extra space = Combine in place [OK]
- Trying to create a new array for merged result
- Sorting arrays separately without merging
- Ignoring the no extra space constraint
Solution
Step 1: Identify correct Rust mutable array declaration
In Rust, arrays are declared with let mut for mutability and square brackets for arrays.Step 2: Check each option
let mut arr1 = [1, 3, 5]; let mut arr2 = [2, 4, 6]; uses let mut with arrays correctly. let arr1 = [1, 3, 5]; let arr2 = [2, 4, 6]; misses mut keyword. mut arr1 = [1, 3, 5]; mut arr2 = [2, 4, 6]; misses let keyword. let mut arr1 = (1, 3, 5); let mut arr2 = (2, 4, 6); uses tuples, not arrays.Final Answer:
let mut arr1 = [1, 3, 5]; let mut arr2 = [2, 4, 6]; -> Option AQuick Check:
Mutable arrays use let mut and [] [OK]
- Forgetting mut keyword for mutable arrays
- Using tuples instead of arrays
- Missing let keyword
arr1 = [1, 4, 7] and arr2 = [2, 5, 6] without extra space using the gap method?Solution
Step 1: Apply gap method to merge arrays
Start with gap = ceil((3+3)/2) = 3. Compare and swap elements at gap distance across arrays until gap reduces to 0.Step 2: Final arrays after merging
After applying gap method, arr1 becomes [1, 2, 4] and arr2 becomes [5, 6, 7], maintaining sorted order without extra space.Final Answer:
arr1 = [1, 2, 4], arr2 = [5, 6, 7] -> Option DQuick Check:
Gap method merges arrays in place [OK]
- Not reducing gap correctly
- Swapping elements incorrectly across arrays
- Confusing array indices during merge
let mut arr1 = [1, 5, 9];
let mut arr2 = [2, 6, 8];
for i in 0..arr1.len() {
if arr1[i] > arr2[0] {
let temp = arr1[i];
arr1[i] = arr2[0];
arr2[0] = temp;
}
arr2.sort();
}Solution
Step 1: Analyze the loop and sorting inside it
The code sorts arr2 inside the loop for every iteration, which is inefficient and unnecessary.Step 2: Correct approach for sorting arr2
Sorting arr2 should be done once after all swaps, not inside the loop, to improve performance and correctness.Final Answer:
Sorting arr2 inside the loop is inefficient and incorrect -> Option CQuick Check:
Sort arr2 once after loop [OK]
- Sorting arr2 repeatedly inside loop
- Confusing loop ranges
- Misunderstanding Rust ownership with temp variable
arr1 = [1, 3, 5, 7] and arr2 = [0, 2, 6, 8, 9], how can you merge them without extra space so that after merging, arr1 contains the first 4 smallest elements and arr2 contains the remaining elements sorted? Choose the correct final arrays.Solution
Step 1: Understand merging without extra space for unequal sizes
We must rearrange elements so arr1 holds smallest 4 elements and arr2 holds the rest, both sorted.Step 2: Apply gap method or swapping approach
By comparing and swapping elements between arrays and sorting arr2, final arrays become arr1 = [0, 1, 2, 3] and arr2 = [5, 6, 7, 8, 9].Final Answer:
arr1 = [0, 1, 2, 3], arr2 = [5, 6, 7, 8, 9] -> Option AQuick Check:
First 4 smallest in arr1, rest in arr2 [OK]
- Not handling different array sizes correctly
- Failing to keep both arrays sorted after merge
- Assuming extra space is allowed
Ready to prove it?
Pass the test and this pattern moves unknown
