Bird
Raised Fist0
DSA C++programming

Array Deletion at Middle Index in DSA C++

Choose your learning style10 modes available
Mental Model
To delete an element from the middle of an array, we shift all elements after it one step left to fill the gap.
Analogy: Imagine a row of books on a shelf. If you remove one book from the middle, you slide all books after it one spot left to close the empty space.
Index:  0   1   2   3   4
Array: [1]->[2]->[3]->[4]->[5]
Dry Run Walkthrough
Input: array: [1, 2, 3, 4, 5], delete element at index 2
Goal: Remove the element at index 2 and shift remaining elements left
Step 1: Remove element at index 2 (value 3)
[1]->[2]->[ ]->[4]->[5]
Why: We want to delete the element at index 2, so it becomes empty
Step 2: Shift element at index 3 (value 4) to index 2
[1]->[2]->[4]->[4]->[5]
Why: Shift left to fill the empty spot at index 2
Step 3: Shift element at index 4 (value 5) to index 3
[1]->[2]->[4]->[5]->[5]
Why: Continue shifting elements left to close the gap
Step 4: Reduce array size by 1, ignore last duplicate
[1]->[2]->[4]->[5]
Why: Final array size is one less after deletion
Result:
Index:  0   1   2   3
Array: [1]->[2]->[4]->[5]
Annotated Code
DSA C++
#include <iostream>
using namespace std;

void deleteAtMiddle(int arr[], int &size, int index) {
    if (index < 0 || index >= size) return; // invalid index guard
    for (int i = index; i < size - 1; i++) {
        arr[i] = arr[i + 1]; // shift elements left to fill gap
    }
    size--; // reduce size after deletion
}

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int size = 5;
    int deleteIndex = 2;

    deleteAtMiddle(arr, size, deleteIndex);

    for (int i = 0; i < size; i++) {
        cout << arr[i];
        if (i != size - 1) cout << " -> ";
    }
    cout << " -> null" << endl;
    return 0;
}
if (index < 0 || index >= size) return; // invalid index guard
Check if index is valid to avoid errors
for (int i = index; i < size - 1; i++) { arr[i] = arr[i + 1]; // shift elements left to fill gap }
Shift all elements after the deleted index one step left
size--; // reduce size after deletion
Decrease array size to reflect deletion
OutputSuccess
1 -> 2 -> 4 -> 5 -> null
Complexity Analysis
Time: O(n) because in the worst case we shift all elements after the deleted index once
Space: O(1) because deletion is done in place without extra storage
vs Alternative: Compared to creating a new array without the deleted element (O(n) time and O(n) space), this method is more space efficient but still O(n) time
Edge Cases
delete at index 0 (first element)
All elements shift left starting from index 0, size reduces by 1
DSA C++
if (index < 0 || index >= size) return; // invalid index guard
delete at last index (size-1)
No shifting needed, just reduce size by 1
DSA C++
for (int i = index; i < size - 1; i++) { arr[i] = arr[i + 1]; }
delete with invalid index (negative or >= size)
Function returns immediately without changes
DSA C++
if (index < 0 || index >= size) return; // invalid index guard
When to Use This Pattern
When you need to remove an element from the middle of a fixed-size list and maintain order, use array shifting to fill the gap.
Common Mistakes
Mistake: Not reducing the size after shifting, leaving a duplicate at the end
Fix: Always decrease the size variable by 1 after shifting elements
Mistake: Shifting elements incorrectly starting from the wrong index
Fix: Start shifting from the deleted index to size-2 to correctly fill the gap
Summary
Deletes an element at a given middle index by shifting subsequent elements left.
Use when you have a fixed-size array and want to remove an element while keeping order.
The key is to shift all elements after the deleted index one step left and reduce the size.

Practice

(1/5)
1. What happens when you delete the middle element from an array of size 5?
int arr[5] = {10, 20, 30, 40, 50};
Delete the element at index 2 (middle). What is the new array?
easy
A. [20, 30, 40, 50]
B. [10, 20, 30, 40, 50]
C. [10, 20, 30, 50]
D. [10, 20, 40, 50]

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    [10, 20, 40, 50] -> Option D
  4. Quick Check:

    Deleted middle element 30, shifted left [OK]
Hint: Remove middle, shift right elements left by one [OK]
Common Mistakes:
  • Not shifting elements after deletion
  • Deleting wrong index
  • Leaving array size unchanged
2. Which of the following C++ code snippets correctly deletes the middle element from an array arr of size n?
easy
A. for(int i = n/2; i < n-1; i++) arr[i] = arr[i+1]; n--;
B. for(int i = 0; i < n/2; i++) arr[i] = arr[i+1]; n--;
C. arr[n/2] = 0; n--;
D. for(int i = n-1; i > n/2; i--) arr[i] = arr[i-1]; n--;

Solution

  1. Step 1: Understand deletion logic

    To delete middle element at index n/2, shift all elements after it one position left.
  2. 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.
  3. Final Answer:

    for(int i = n/2; i < n-1; i++) arr[i] = arr[i+1]; n--; -> Option A
  4. Quick Check:

    Shift elements after middle left [OK]
Hint: Shift elements from middle index to end left [OK]
Common Mistakes:
  • Shifting elements before middle index
  • Not reducing array size
  • Assigning zero instead of shifting
3. What is the output of the following C++ code?
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] << " ";
}
medium
A. 5 10 15 25 30
B. 5 10 15 20 25 30
C. 5 10 15 20 30
D. 5 10 20 25 30

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    5 10 15 25 30 -> Option A
  4. Quick Check:

    Deleted 20, shifted left [OK]
Hint: Shift elements after middle left, reduce size by one [OK]
Common Mistakes:
  • Not reducing array size
  • Printing old array size
  • Not shifting elements correctly
4. Find the error in this code that tries to delete the middle element from array arr of size n:
int mid = n / 2;
for(int i = mid; i <= n - 1; i++) {
  arr[i] = arr[i + 1];
}
n--;
medium
A. Array size n should not be decremented
B. Middle index calculation is wrong
C. Loop condition should be i < n - 1, not i <= n - 1
D. Loop should start from 0, not mid

Solution

  1. 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.
  2. Step 2: Correct loop condition

    Loop should run while i < n-1 to avoid accessing beyond array size.
  3. Final Answer:

    Loop condition should be i < n - 1, not i <= n - 1 -> Option C
  4. Quick Check:

    Prevent out-of-bounds by correct loop limit [OK]
Hint: Loop till one before last index to avoid overflow [OK]
Common Mistakes:
  • Using <= instead of < in loop condition
  • Not decrementing array size
  • Wrong middle index calculation
5. Given an array 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.
hard
A. Delete element at index (n-1)/2, shift elements left, new size = n - 1
B. Delete element at index n/2, shift elements left, new size = n - 1
C. Delete element at index n/2, no shifting needed, new size = n - 1
D. Delete element at index n/2 - 1, shift elements left, new size = n - 1

Solution

  1. Step 1: Understand middle index for even-sized array

    In standard array operations, middle index is n/2 (integer division), consistent with odd sizes.
  2. 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.
  3. Final Answer:

    Delete element at index n/2, shift elements left, new size = n - 1 -> Option B
  4. Quick Check:

    Middle index = n/2 for even n [OK]
Hint: Use n/2 as middle index regardless of parity [OK]
Common Mistakes:
  • Deleting lower middle index (n/2 - 1) for even n
  • Not shifting elements after deletion
  • Not reducing array size
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard