Array Deletion at Middle Index in DSA Python - Time & Space Complexity
When we delete an element from the middle of an array, the time it takes depends on how many elements need to move.
We want to know how the work grows as the array gets bigger.
Analyze the time complexity of the following code snippet.
def delete_middle(arr):
mid = len(arr) // 2
for i in range(mid, len(arr) - 1):
arr[i] = arr[i + 1]
arr.pop()
# Deletes the middle element by shifting elements left
This code removes the middle element by shifting all elements after it one step to the left.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that shifts elements left after the middle index.
- How many times: It runs approximately n/2 times, where n is the array size.
As the array grows, the number of shifts grows roughly in half.
| 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 in a straight line as the array gets bigger.
[X] Wrong: "Deleting an element from the middle is always fast and takes constant time."
[OK] Correct: Because after deletion, elements must move to fill the gap, which takes time proportional to how many elements are shifted.
Understanding how array operations scale helps you explain trade-offs in data structures clearly and confidently.
"What if we deleted the last element instead of the middle? How would the time complexity change?"
Practice
Solution
Step 1: Identify the middle index in an odd-length array
For an array with odd length n, the middle index is n//2 (integer division).Step 2: Remove the element at the middle index
Deleting the element at this index reduces the array size by one and shifts elements after it left.Final Answer:
The element at the exact middle index is removed, and the array size decreases by one. -> Option AQuick Check:
Middle element removed = The element at the exact middle index is removed, and the array size decreases by one. [OK]
- Removing first or last element instead of middle
- Not reducing array size after deletion
- Confusing middle index calculation
arr with odd length?Solution
Step 1: Understand Python deletion syntax
Thedelstatement removes an element at a specific index from a list.Step 2: Calculate middle index and delete
Usinglen(arr)//2gives the middle index for odd length. Sodel arr[len(arr)//2]deletes the middle element correctly.Final Answer:
del arr[len(arr)//2] -> Option BQuick Check:
Correct syntax for deletion = del arr[len(arr)//2] [OK]
- Using remove() which deletes by value, not index
- Using pop() with wrong index
- Using non-existent delete() method
arr = [10, 20, 30, 40, 50] del arr[len(arr)//2] print(arr)
Solution
Step 1: Calculate middle index
Length of arr is 5, so middle index = 5 // 2 = 2.Step 2: Delete element at index 2 and print
Element at index 2 is 30. After deletion, arr becomes [10, 20, 40, 50].Final Answer:
[10, 20, 40, 50] -> Option DQuick Check:
Deleted arr[2] = 30, remaining = [10, 20, 40, 50] [OK]
- Deleting wrong index (like 3 or 1)
- Confusing element value with index
- Printing before deletion
arr. What is the error?arr = [1, 2, 3, 4, 5] arr.remove(len(arr)//2) print(arr)
Solution
Step 1: Understand remove() method behavior
remove(x)deletes the first element with value x, not at index x.Step 2: Analyze the argument passed to remove()
len(arr)//2 = 5//2 = 2, soarr.remove(2)deletes element with value 2, not element at index 2.Final Answer:
It deletes the element with value 2 instead of the middle index element. -> Option CQuick Check:
remove deletes by value, not index = It deletes the element with value 2 instead of the middle index element. [OK]
- Confusing remove() with del or pop()
- Expecting remove() to delete by index
- Not calculating middle index correctly
arr = [5, 10, 15, 20, 25, 30] with even length, which code correctly deletes the middle element(s) so that after deletion the array is [5, 10, 25, 30]?Solution
Step 1: Identify middle indices for even length array
Length is 6, middle indices are 2 and 3 (0-based), values 15 and 20.Step 2: Delete elements at indices 2 and 3 accounting for length change
del arr[len(arr)//2 - 1]deletes index 2 (15), array becomes [5, 10, 20, 25, 30]. Thendel arr[len(arr)//2]deletes new index 2 (20), resulting in [5, 10, 25, 30].Final Answer:
del arr[len(arr)//2 - 1]; del arr[len(arr)//2] -> Option AQuick Check:
del arr[2] then del arr[2] (after shift) = [5, 10, 25, 30] [OK]
- Deleting higher index first without adjusting for length change
- Using pop() in wrong order causing incorrect shifts
- Using remove() which deletes by value
Ready to prove it?
Pass the test and this pattern moves unknown
