Bird
0
0
DSA Cprogramming

Array Insertion at Middle Index in DSA C

Choose your learning style9 modes available
Mental Model
To insert a new value in the middle of an array, we shift all elements from that position to the right by one to make space.
Analogy: Imagine a row of chairs where you want to add a new person in the middle. Everyone from that spot onward must stand up and move one chair to the right to make room.
Index: 0   1   2   3   4
Array: [1]->[2]->[3]->[4]->[5]
Insert at index 2 (middle)
Dry Run Walkthrough
Input: array: [1, 2, 3, 4, 5], insert value 99 at index 2
Goal: Insert 99 at index 2 and shift elements right to keep order
Step 1: Shift element at index 4 (5) to index 5
[1]->[2]->[3]->[4]->[5]->[5]
Indexes: 0 1 2 3 4 5
Why: Make space at index 5 for shifting next element
Step 2: Shift element at index 3 (4) to index 4
[1]->[2]->[3]->[4]->[4]->[5]
Why: Move element right to free index 3
Step 3: Shift element at index 2 (3) to index 3
[1]->[2]->[3]->[3]->[4]->[5]
Why: Move element right to free index 2
Step 4: Insert 99 at index 2
[1]->[2]->[99]->[3]->[4]->[5]
Why: Place new value in the freed middle position
Result:
[1]->[2]->[99]->[3]->[4]->[5]
Annotated Code
DSA C
#include <stdio.h>

void insertAtMiddle(int arr[], int *size, int capacity, int value, int index) {
    if (*size >= capacity) {
        printf("Array is full, cannot insert.\n");
        return;
    }
    // Shift elements right from the end to index
    for (int i = *size - 1; i >= index; i--) {
        arr[i + 1] = arr[i]; // shift element right
    }
    arr[index] = value; // insert new value
    (*size)++; // increase size
}

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

int main() {
    int capacity = 10;
    int arr[10] = {1, 2, 3, 4, 5};
    int size = 5;
    int value = 99;
    int index = 2; // middle index

    insertAtMiddle(arr, &size, capacity, value, index);
    printArray(arr, size);

    return 0;
}
for (int i = *size - 1; i >= index; i--) { arr[i + 1] = arr[i]; }
Shift elements right from the end to index to make space
arr[index] = value;
Insert new value at the middle index
(*size)++;
Increase array size after insertion
OutputSuccess
1 -> 2 -> 99 -> 3 -> 4 -> 5
Complexity Analysis
Time: O(n) because in the worst case we shift half of the elements to the right
Space: O(1) because insertion is done in place without extra arrays
vs Alternative: Compared to creating a new array and copying elements, this in-place shift saves space but still requires shifting elements which costs O(n)
Edge Cases
Inserting at index 0 (start of array)
All elements shift right, new value placed at start
DSA C
for (int i = *size - 1; i >= index; i--) { arr[i + 1] = arr[i]; }
Inserting at index equal to current size (end of array)
No shifting needed, value appended at end
DSA C
for (int i = *size - 1; i >= index; i--) { arr[i + 1] = arr[i]; }
Array is full (size == capacity)
Insertion is rejected with message
DSA C
if (*size >= capacity) { printf("Array is full, cannot insert.\n"); return; }
When to Use This Pattern
When you need to add an element inside an array and keep order, look for the shifting pattern to make space before insertion.
Common Mistakes
Mistake: Not shifting elements from the end to the index, causing overwriting data
Fix: Always shift elements starting from the last element down to the insertion index to avoid overwriting
Mistake: Forgetting to increase the size after insertion
Fix: Increment the size variable after inserting the new element
Summary
Inserts a new value at a given middle index by shifting elements right to make space.
Use when you want to add an element inside an array without losing existing data order.
The key is to shift elements from the end towards the insertion point before placing the new value.