Bird
Raised Fist0
DSA Cprogramming

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 position to the 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 the books after it to the left to close the empty space.
Index: 0   1   2   3   4
Array: [1]->[2]->[3]->[4]->[5]
               ↑
           delete here
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: Start deletion at index 2 (value 3)
1 -> 2 -> [3↑] -> 4 -> 5
Why: We identify the element to remove
Step 2: Shift element at index 3 (4) to index 2
1 -> 2 -> [4] -> 4 -> 5
Why: Fill the gap by moving next element left
Step 3: Shift element at index 4 (5) to index 3
1 -> 2 -> 4 -> [5] -> 5
Why: Continue shifting elements left
Step 4: Reduce array size by 1, last element ignored
1 -> 2 -> 4 -> 5 -> null
Why: Array size decreases after deletion
Result:
1 -> 2 -> 4 -> 5 -> null
Annotated Code
DSA C
#include <stdio.h>

void deleteAtIndex(int arr[], int *size, int index) {
    if (index < 0 || index >= *size) {
        printf("Index out of bounds\n");
        return;
    }
    for (int i = index; i < *size - 1; i++) {
        arr[i] = arr[i + 1]; // shift element left to fill gap
    }
    (*size)--; // reduce size after deletion
}

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d", arr[i]);
        if (i < size - 1) printf(" -> ");
    }
    printf(" -> null\n");
}

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

    printf("Original array:\n");
    printArray(arr, size);

    deleteAtIndex(arr, &size, deleteIndex);

    printf("Array after deleting element at index %d:\n", deleteIndex);
    printArray(arr, size);

    return 0;
}
if (index < 0 || index >= *size) {
check if index is valid to avoid errors
for (int i = index; i < *size - 1; i++) {
loop to shift elements left starting from deletion index
arr[i] = arr[i + 1]; // shift element left to fill gap
move next element to current position to fill gap
(*size)--; // reduce size after deletion
decrease array size after removing element
OutputSuccess
Original array: 1 -> 2 -> 3 -> 4 -> 5 -> null Array after deleting element at index 2: 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 element (which uses O(n) space), this method is more space efficient
Edge Cases
delete at index 0 (first element)
All elements shift left, first element removed
DSA C
if (index < 0 || index >= *size) {
delete at last index
No shifting needed, just reduce size
DSA C
for (int i = index; i < *size - 1; i++) {
delete with invalid index (negative or >= size)
Print error message, no change to array
DSA C
if (index < 0 || index >= *size) {
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 array size after shifting elements
Fix: Decrease the size variable by one after shifting to reflect the new array length
Mistake: Shifting elements in the wrong direction (right instead of left)
Fix: Shift elements from the deletion index towards the left to fill the gap
Summary
Deletes an element at a given index by shifting subsequent elements left.
Use when you want to remove an element from the middle of an array and keep the order.
The key is to slide all elements after the deleted one left by one position to close the gap.

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. [10, 20, 40, 50]
B. [10, 20, 30, 40, 50]
C. [10, 20, 30, 50]
D. [20, 30, 40, 50]

Solution

  1. Step 1: Identify the middle index and element

    The array has 5 elements, so the middle index is 2 (0-based). 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 A
  4. Quick Check:

    Middle element removed, array size reduced by 1 [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 > 0; i--) arr[i] = arr[i - 1]; n--;
B. for (int i = 0; i < n/2; i++) arr[i] = arr[i + 1]; n--;
C. for (int i = n - 1; i > n/2; i--) arr[i] = arr[i - 1]; n--;
D. for (int i = n/2; i < n - 1; i++) arr[i] = arr[i + 1]; n--;

Solution

  1. Step 1: Identify the middle index and loop direction

    The middle index is n/2 (integer division). To delete, shift elements after middle left.
  2. Step 2: Check loop correctness

    for (int i = n/2; i < n - 1; i++) arr[i] = arr[i + 1]; n--; shifts elements from middle to end left by one, then reduces size. This is correct.
  3. Final Answer:

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

    Shift elements after middle left [OK]
Hint: Shift elements from middle to end left by one [OK]
Common Mistakes:
  • Shifting elements before middle
  • Looping in wrong direction
  • Not reducing array size
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++) {
    printf("%d ", arr[i]);
}
medium
A. 5 10 15 20 25 30
B. 5 10 15 20 30
C. 5 10 15 25 30
D. 5 10 20 25 30

Solution

  1. Step 1: Calculate middle index and deletion

    n=6, mid=3. Element at index 3 is 20. It will be deleted.
  2. Step 2: Shift elements after index 3 left by one

    arr[3] = arr[4] (25), arr[4] = arr[5] (30), n becomes 5.
  3. Step 3: Print updated array

    Elements: 5, 10, 15, 25, 30
  4. Final Answer:

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

    Deleted middle element, shifted right elements left [OK]
Hint: Delete middle, shift right elements left, print first n-1 elements [OK]
Common Mistakes:
  • Printing original array size
  • Not shifting elements correctly
  • Deleting wrong index
4. Find the error in this code snippet that deletes the middle element from an array arr of size n:
int mid = n / 2;
for (int i = 0; i < mid; i++) {
    arr[i] = arr[i + 1];
}
n--;
medium
A. Middle index calculation is wrong, should be (n-1)/2
B. Loop shifts elements before middle, should shift after middle
C. Array size decrement should be before loop
D. No error, code is correct

Solution

  1. Step 1: Analyze loop range and shifting direction

    The loop runs from 0 to mid-1, shifting elements before middle left, which is incorrect.
  2. Step 2: Correct shifting after middle index

    To delete middle element, shift elements from middle index to end left by one, not before middle.
  3. Final Answer:

    Loop shifts elements before middle, should shift after middle -> Option B
  4. Quick Check:

    Shift elements after middle, not before [OK]
Hint: Shift elements after middle, not before [OK]
Common Mistakes:
  • Shifting elements before middle
  • Wrong loop boundaries
  • Not reducing array size
5. Given an array arr of size n where n is even, write the steps to delete the middle two elements and shift the rest. What will be the new array after deleting middle elements from {1, 2, 3, 4, 5, 6}?
hard
A. [1, 2, 5, 6]
B. [1, 2, 3, 6]
C. [1, 2, 4, 5, 6]
D. [1, 3, 4, 5, 6]

Solution

  1. Step 1: Identify middle two indices for even-sized array

    For n=6, middle two indices are 2 and 3 (0-based), elements 3 and 4.
  2. Step 2: Delete these two elements and shift remaining elements left

    Shift elements from index 4 and 5 left by two positions: 5 moves to index 2, 6 moves to index 3. Reduce size by 2.
  3. Step 3: New array after deletion

    Array becomes [1, 2, 5, 6]
  4. Final Answer:

    [1, 2, 5, 6] -> Option A
  5. Quick Check:

    Delete two middle elements, shift right elements left by two [OK]
Hint: Delete two middles, shift right elements left by two [OK]
Common Mistakes:
  • Deleting only one middle element
  • Shifting elements incorrectly
  • Not reducing array size by two
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