Array Insertion at Middle Index in DSA C - Time & Space Complexity
We want to understand how the time needed to insert an element in the middle of an array changes as the array grows.
Specifically, how does the work increase when the array size gets bigger?
Analyze the time complexity of the following code snippet.
void insertMiddle(int arr[], int n, int x) {
int mid = n / 2;
for (int i = n; i > mid; i--) {
arr[i] = arr[i - 1];
}
arr[mid] = x;
}
This code inserts a new element x into the middle of an 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: Approximately n/2 times, where n is the current size of the array.
As the array size grows, the number of elements to shift grows roughly half as fast.
| Input Size (n) | Approx. Operations (shifts) |
|---|---|
| 10 | 5 |
| 100 | 50 |
| 1000 | 500 |
Pattern observation: The number of operations grows linearly with the size of the array.
Time Complexity: O(n)
This means the time to insert in the middle grows in direct proportion to the array size.
[X] Wrong: "Inserting in the middle is always fast because we just put the element there."
[OK] Correct: We must move many elements to make space, which takes time proportional to the number of elements shifted.
Understanding this helps you explain why arrays are not always the best choice for frequent middle insertions, showing your grasp of data structure trade-offs.
"What if we inserted at the end of the array instead? How would the time complexity change?"
Practice
Solution
Step 1: Calculate middle index for even-sized array
For an array of size 6, middle index is size/2 = 6/2 = 3.Step 2: Confirm insertion position
Inserting at index 3 places the element exactly in the middle.Final Answer:
3 -> Option DQuick Check:
Middle index = size/2 = 3 [OK]
- Using size-1 as middle index
- Using size/2 - 1 for even size
- Confusing zero-based indexing
arr of size n?Solution
Step 1: Identify correct shifting direction
To insert at middle, elements from middle to end must shift right to make space.Step 2: Check code correctness
int mid = n / 2; for(int i = n; i > mid; i--) arr[i] = arr[i-1]; arr[mid] = x; shifts elements right from end to middle, then inserts x at mid. Others shift incorrectly or overwrite.Final Answer:
int mid = n / 2; for(int i = n; i > mid; i--) arr[i] = arr[i-1]; arr[mid] = x; -> Option BQuick Check:
Shift right from end to mid, then insert [OK]
- Shifting elements left instead of right
- Not shifting elements before insertion
- Overwriting elements without shifting
arr = {10, 20, 30, 40, 50} using the correct insertion method?Solution
Step 1: Calculate middle index
Array size is 5, middle index = 5/2 = 2 (integer division).Step 2: Shift elements right and insert 99
Shift elements from index 4 to 2 right by one position. Insert 99 at index 2.Final Answer:
[10, 20, 99, 30, 40, 50] -> Option CQuick Check:
Insert at index 2 shifts elements right [OK]
- Inserting at wrong index
- Not shifting elements before insertion
- Overwriting elements
x at the middle of array arr of size n:
int mid = n / 2;
for(int i = mid; i < n; i++) {
arr[i+1] = arr[i];
}
arr[mid] = x;Solution
Step 1: Analyze loop direction
The loop shifts elements from mid to n forward, but does so from low to high index, causing overwrite.Step 2: Correct loop direction
Loop must run backwards from n to mid to shift elements right safely without overwriting.Final Answer:
Loop should run backwards from n to mid -> Option AQuick Check:
Shift right must be done backwards [OK]
- Looping forward causing data overwrite
- Not updating array size after insertion
- Incorrect middle index calculation
arr = {5, 10, 15, 20, 25, 30} of size 6, write the final array after inserting 99 at the middle index and then inserting 77 at the new middle index of the updated array.Solution
Step 1: Insert 99 at middle of original array
Original size = 6, middle index = 3. Insert 99 at index 3 shifting elements right: [5, 10, 15, 99, 20, 25, 30]Step 2: Insert 77 at middle of updated array
New size = 7, middle index = 7/2 = 3. Insert 77 at index 3 shifting elements right: [5, 10, 15, 77, 99, 20, 25, 30]Final Answer:
[5, 10, 15, 77, 99, 20, 25, 30] -> Option AQuick Check:
Insert twice at middle recalculating index each time [OK]
- Not recalculating middle index after first insertion
- Overwriting elements without shifting
- Inserting at wrong index second time
Ready to prove it?
Pass the test and this pattern moves unknown
