Array Deletion at Middle Index in DSA C++ - Time & Space Complexity
We want to understand how long it takes to remove an element from the middle of an array.
How does the time needed change as the array gets bigger?
Analyze the time complexity of the following code snippet.
void deleteMiddle(int arr[], int &n) {
int mid = n / 2;
for (int i = mid; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
n--;
}
This code removes the middle element by shifting all elements after it one step left.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop shifting elements left after the middle index.
- How many times: Approximately 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 |
|---|---|
| 10 | 5 shifts |
| 100 | 50 shifts |
| 1000 | 500 shifts |
Pattern observation: The operations grow linearly with input size, about half the size.
Time Complexity: O(n)
This means the time to delete the middle element grows in direct proportion to the array size.
[X] Wrong: "Deleting an element from the middle of an array is always fast and constant time."
[OK] Correct: Because elements after the deleted one must be shifted left, which takes time proportional to how many elements follow.
Knowing this helps you explain why arrays are not always the best choice for frequent middle deletions, showing your understanding of data structure trade-offs.
"What if we used a linked list instead of an array? How would the time complexity of deleting the middle element change?"
Practice
int arr[5] = {10, 20, 30, 40, 50};Delete the element at index 2 (middle). What is the new array?
Solution
Step 1: Identify the middle index and element
The array size is 5, so middle index is 2. The element at index 2 is 30.Step 2: Delete the middle element and shift remaining elements
Remove 30, then shift elements after index 2 left by one: 40 moves to index 2, 50 moves to index 3.Final Answer:
[10, 20, 40, 50] -> Option DQuick Check:
Deleted middle element 30, shifted left [OK]
- Not shifting elements after deletion
- Deleting wrong index
- Leaving array size unchanged
arr of size n?Solution
Step 1: Understand deletion logic
To delete middle element at index n/2, shift all elements after it one position left.Step 2: Check each code snippet
for(int i = n/2; i < n-1; i++) arr[i] = arr[i+1]; n--; correctly shifts elements from middle to end left by one and reduces size. Others either shift wrong elements or just set zero without shifting.Final Answer:
for(int i = n/2; i < n-1; i++) arr[i] = arr[i+1]; n--; -> Option AQuick Check:
Shift elements after middle left [OK]
- Shifting elements before middle index
- Not reducing array size
- Assigning zero instead of shifting
int arr[6] = {5, 10, 15, 20, 25, 30};
int n = 6;
int mid = n / 2;
for(int i = mid; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
n--;
for(int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}Solution
Step 1: Calculate middle index and delete element
n=6, mid=3. Element at index 3 is 20. We delete 20 by shifting elements after index 3 left.Step 2: Shift elements and print array
Shift arr[3]=arr[4]=25, arr[4]=arr[5]=30. New array: 5,10,15,25,30. Size reduced to 5.Final Answer:
5 10 15 25 30 -> Option AQuick Check:
Deleted 20, shifted left [OK]
- Not reducing array size
- Printing old array size
- Not shifting elements correctly
arr of size n:int mid = n / 2;
for(int i = mid; i <= n - 1; i++) {
arr[i] = arr[i + 1];
}
n--;Solution
Step 1: Analyze loop range for shifting elements
The loop runs from mid to n-1 inclusive, so arr[i+1] accesses arr[n], which is out of bounds.Step 2: Correct loop condition
Loop should run while i < n-1 to avoid accessing beyond array size.Final Answer:
Loop condition should be i < n - 1, not i <= n - 1 -> Option CQuick Check:
Prevent out-of-bounds by correct loop limit [OK]
- Using <= instead of < in loop condition
- Not decrementing array size
- Wrong middle index calculation
arr of size n, write the logic to delete the middle element and return the new array size. What will be the size after deletion if n is even?Choose the correct approach.
Solution
Step 1: Understand middle index for even-sized array
In standard array operations, middle index is n/2 (integer division), consistent with odd sizes.Step 2: Delete element at n/2 and shift elements left
Remove element at index n/2, shift all elements after it left by one, reduce size by 1.Final Answer:
Delete element at index n/2, shift elements left, new size = n - 1 -> Option BQuick Check:
Middle index = n/2 for even n [OK]
- Deleting lower middle index (n/2 - 1) for even n
- Not shifting elements after deletion
- Not reducing array size
Ready to prove it?
Pass the test and this pattern moves unknown
