Array Insertion at Middle Index in DSA Python - Time & Space Complexity
When we insert an element in 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 insert_middle(arr, value):
mid = len(arr) // 2
arr.append(0) # Increase size by one
for i in range(len(arr) - 1, mid, -1):
arr[i] = arr[i - 1]
arr[mid] = value
# Example: arr = [1, 2, 3, 4]
This code inserts a new value exactly in the middle of the array by shifting elements to the right.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that shifts elements one position to the right.
- How many times: It runs roughly half the length of the array (from the end to the middle + 1).
As the array size grows, the number of elements to shift grows roughly in the middle portion.
| Input Size (n) | Approx. Operations (shifts) |
|---|---|
| 10 | 5 |
| 100 | 50 |
| 1000 | 500 |
Pattern observation: The number of shifts grows linearly with the size of the array.
Time Complexity: O(n)
This means the time to insert grows directly with the number of elements in the array.
[X] Wrong: "Inserting in the middle is always fast because we just put the value there."
[OK] Correct: We must move many elements to make space, which takes time proportional to the array size.
Understanding how array insertions work helps you explain why some data structures are better for certain tasks.
"What if we inserted at the end of the array instead of the middle? How would the time complexity change?"
Practice
Solution
Step 1: Calculate middle index for odd length array
For an array of length 7, middle index = length // 2 = 7 // 2 = 3.Step 2: Understand insertion position
Inserting at index 3 places the new element exactly in the middle.Final Answer:
3 -> Option AQuick Check:
Middle index = length // 2 = 3 [OK]
- Using length // 2 + 1 for middle index
- Using length // 2 - 1 for middle index
- Confusing middle index with last index
arr = [1, 2, 3, 4]?Solution
Step 1: Calculate middle index for arr
Length of arr is 4, middle index = 4 // 2 = 2.Step 2: Use insert method at middle index
arr.insert(2, 10) inserts 10 at index 2, which is the middle.Final Answer:
arr.insert(len(arr) // 2, 10) -> Option CQuick Check:
Use insert at middle index = len(arr)//2 [OK]
- Using append() which adds at end
- Using insert(len(arr), value) which adds at end
- Using insert(0, value) which adds at start
arr = [5, 10, 15, 20] mid = len(arr) // 2 arr.insert(mid, 12) print(arr)
Solution
Step 1: Calculate middle index
Length of arr is 4, so mid = 4 // 2 = 2.Step 2: Insert 12 at index 2
arr.insert(2, 12) inserts 12 before element at index 2 (which is 15).Final Answer:
[5, 10, 12, 15, 20] -> Option BQuick Check:
Insert at mid index shifts elements right [OK]
- Inserting at wrong index causing misplaced element
- Appending instead of inserting
- Confusing index with value
arr = [1, 2, 3, 4, 5]:arr = [1, 2, 3, 4, 5] mid = len(arr) / 2 arr.insert(mid, 99) print(arr)
Solution
Step 1: Check middle index calculation
len(arr) / 2 returns a float (2.5), but insert() requires an integer index.Step 2: Correct middle index calculation
Use integer division '//' to get integer index: mid = len(arr) // 2 = 2.Final Answer:
Using '/' instead of '//' for middle index calculation -> Option AQuick Check:
Index must be int, use '//' not '/' [OK]
- Using '/' causing TypeError
- Forgetting to calculate middle index
- Using append() instead of insert()
arr = [2, 4, 6, 8, 10, 12], write code to insert the value 7 exactly in the middle so that the array remains sorted. What will be the final array?Solution
Step 1: Calculate middle index for even length array
Length is 6, middle index = 6 // 2 = 3.Step 2: Insert 7 at index 3 to keep array sorted
Inserting 7 at index 3 places it between 6 and 8, maintaining sorted order.Final Answer:
[2, 4, 6, 7, 8, 10, 12] -> Option DQuick Check:
Insert 7 at middle index 3 to keep sorted [OK]
- Inserting 7 at wrong index breaking order
- Appending 7 at end
- Inserting 7 at start
Ready to prove it?
Pass the test and this pattern moves unknown
