0
0
DSA Pythonprogramming~5 mins

Array Deletion at Middle Index in DSA Python - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Array Deletion at Middle Index
O(n)
Understanding Time 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.

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

As the array grows, the number of shifts grows roughly in half.

Input Size (n)Approx. Operations
10About 5 shifts
100About 50 shifts
1000About 500 shifts

Pattern observation: The work grows linearly with the size of the array.

Final Time Complexity

Time Complexity: O(n)

This means the time to delete grows in a straight line as the array gets bigger.

Common Mistake

[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.

Interview Connect

Understanding how array operations scale helps you explain trade-offs in data structures clearly and confidently.

Self-Check

"What if we deleted the last element instead of the middle? How would the time complexity change?"