Array Insertion at Middle Index in DSA C++ - Time & Space Complexity
We want to understand how the time needed changes when inserting an element in the middle of an array.
How does the work grow as the array gets bigger?
Analyze the time complexity of the following code snippet.
void insertMiddle(int arr[], int &size, int capacity, int value) {
int mid = size / 2;
if (size == capacity) return; // no space
for (int i = size; i > mid; i--) {
arr[i] = arr[i - 1];
}
arr[mid] = value;
size++;
}
This code inserts a new value 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 from the end of the array down to the middle index, 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 (shifts) |
|---|---|
| 10 | 5 |
| 100 | 50 |
| 1000 | 500 |
Pattern observation: The number of operations grows linearly with the input size, about half the size.
Time Complexity: O(n)
This means the time to insert grows directly with the size of the array.
[X] Wrong: "Inserting in the middle is always fast because we just place the element there."
[OK] Correct: We must move all elements after the middle to make space, which takes time proportional to the number of elements shifted.
Understanding this helps you explain why arrays are slower for middle insertions compared to linked lists, showing your grasp of data structure trade-offs.
"What if we changed the array to a linked list? How would the time complexity of inserting in the middle change?"
Practice
Solution
Step 1: Understand middle index insertion
Inserting at the middle means placing a new element at the center position, pushing existing elements right.Step 2: Differentiate from other operations
Replacing or removing elements is not insertion; adding at the end is not middle insertion.Final Answer:
Adding a new element exactly in the center, shifting others right -> Option AQuick Check:
Middle insertion means insert and shift right [OK]
- Thinking insertion replaces the middle element
- Confusing insertion with appending at the end
- Assuming middle means removing element
Solution
Step 1: Shift elements right from middle index
Loop from end (n) down to middle (n/2 + 1) shifting elements right to make space.Step 2: Insert element at middle index
Assign new element x at index n/2 after shifting.Final Answer:
for(int i = n; i > n/2; i--) arr[i] = arr[i-1]; arr[n/2] = x; -> Option DQuick Check:
Shift right then insert at middle [OK]
- Shifting elements left instead of right
- Inserting before shifting elements
- Incorrect loop direction causing overwrite
arr = {10, 20, 30, 40, 50} using the correct insertion method?Solution
Step 1: Calculate middle index
Array size n=5, middle index = n/2 = 2 (0-based indexing).Step 2: Shift elements right and insert 99
Shift elements from index 4 to 2 right by one position, then insert 99 at index 2.Final Answer:
[10, 20, 99, 30, 40, 50] -> Option BQuick Check:
Insert at index 2 shifts right [OK]
- Inserting at wrong index (like 3 or 4)
- Not shifting elements before insertion
- Appending at the end instead of middle
for(int i = n/2; i < n; i++) {
arr[i+1] = arr[i];
}
arr[n/2] = x;Solution
Step 1: Analyze loop direction and effect
The loop moves forward from middle to end, copying arr[i] to arr[i+1], overwriting elements before they are moved.Step 2: Identify correct approach
Elements must be shifted right starting from the end backward to avoid overwriting data.Final Answer:
Loop direction causes overwriting elements -> Option CQuick Check:
Shift right must go backward to avoid overwrite [OK]
- Shifting elements forward causing data loss
- Ignoring array bounds when shifting
- Assuming forward loop works for shifting right
arr = {5, 10, 15, 20, 25, 30} of size 6, after inserting element 99 at the middle index, what will be the final array?Solution
Step 1: Calculate middle index for even-sized array
Array size n=6, middle index = n/2 = 3 (0-based indexing).Step 2: Shift elements right from index 5 to 3
Shift elements at indices 5,4,3 one position right to make space at index 3.Step 3: Insert 99 at middle index
Place 99 at index 3 after shifting.Final Answer:
[5, 10, 15, 99, 20, 25, 30] -> Option AQuick Check:
Insert at index 3 shifts right [OK]
- Inserting at index 2 instead of 3
- Not shifting elements before insertion
- Appending at the end instead of middle
Ready to prove it?
Pass the test and this pattern moves unknown
