Array Deletion at Middle Index in DSA Rust - Time & Space Complexity
We want to understand how long it takes to remove an element from the middle of an array.
Specifically, how the work grows as the array gets bigger.
Analyze the time complexity of the following code snippet.
fn delete_middle(arr: &mut Vec<i32>) {
let mid = arr.len() / 2;
for i in mid..arr.len() - 1 {
arr[i] = arr[i + 1];
}
arr.pop();
}
This code removes the middle element by shifting all elements after it one step left, then removes the last duplicate.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop shifting elements left after the middle index.
- How many times: It runs roughly n/2 times, where n is the array size.
As the array size grows, the number of shifts grows roughly half as fast.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 5 shifts |
| 100 | About 50 shifts |
| 1000 | About 500 shifts |
Pattern observation: The work grows linearly with the size of the array.
Time Complexity: O(n)
This means the time to delete grows directly with the number of elements in the array.
[X] Wrong: "Deleting an element from the middle of an array is always fast and constant time."
[OK] Correct: Because after removing the element, all later elements must move left, which takes time proportional to the number of elements after the deleted one.
Understanding this helps you explain why arrays are not always the best choice for frequent middle deletions, showing your grasp of data structure trade-offs.
"What if we changed the array to a linked list? How would the time complexity of deleting the middle element change?"
Practice
Solution
Step 1: Identify the middle index in an odd-length array
The middle index is calculated as length divided by 2 (integer division).Step 2: Remove the element at the middle index
Deleting this element reduces the array size by one and shifts elements after it left.Final Answer:
The middle element is removed and the array size decreases by one. -> Option BQuick Check:
Deleting middle element shrinks array [OK]
- Thinking array size stays the same after deletion
- Removing last element instead of middle
- Not shifting elements after deletion
v with odd length?Solution
Step 1: Identify Rust method to remove element by index
Rust's Vec type usesremove(index)to delete element at given index.Step 2: Calculate middle index and call remove
Middle index isv.len() / 2. Callingv.remove(v.len() / 2)deletes middle element.Final Answer:
v.remove(v.len() / 2); -> Option CQuick Check:
Use remove(index) to delete element [OK]
- Using non-existent methods like delete or erase
- Using pop which removes last element only
- Passing index to pop which takes no arguments
let mut v = vec![10, 20, 30, 40, 50];
v.remove(v.len() / 2);
println!("{:?}", v);Solution
Step 1: Calculate middle index of vector
Vector length is 5, so middle index = 5 / 2 = 2.Step 2: Remove element at index 2 and print vector
Element at index 2 is 30, removed. Remaining vector is [10, 20, 40, 50].Final Answer:
[10, 20, 40, 50] -> Option DQuick Check:
Removed middle element 30, vector shrinks [OK]
- Removing wrong index element
- Confusing index with value
- Expecting vector size unchanged
v:
let mut v = vec![1, 2];
v.remove(v.len() / 2 + 1);
println!("{:?}", v);Solution
Step 1: Calculate middle index for even-length vector
Vector length is 2, middle index should be 2 / 2 = 1.Step 2: Check removal index used in code
Code uses index 1 + 1 = 2, which is out of bounds.Step 3: Understand consequences
Index 2 >= length 2 causes runtime panic (out of bounds).Final Answer:
Index used for removal is off by one, causing panic. -> Option AQuick Check:
Use correct middle index = len()/2 [OK]
- Adding +1 to middle index incorrectly
- Trying to remove from immutable vector
- Misusing remove method name
v of even length, how can you delete the lower middle element (the one at index len()/2 - 1) and then print the updated vector in Rust?Solution
Step 1: Identify lower middle index for even-length vector
For even length, lower middle index islen()/2 - 1.Step 2: Remove element at lower middle index and print vector
Usev.remove(v.len() / 2 - 1)to delete lower middle element, then print withprintln!("{:?}", v);.Final Answer:
v.remove(v.len() / 2 - 1); println!("{:?}", v); -> Option AQuick Check:
Remove lower middle with len()/2 - 1 [OK]
- Removing upper middle instead of lower middle
- Using pop which removes last element
- Adding +1 to index incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
