0
0
CProgramBeginner · 2 min read

C Program to Insert Element in Array with Output

To insert an element in a C array, shift elements right from the insertion point using a loop, then assign the new element at the desired index; for example, use for (int i = n; i >= pos; i--) arr[i] = arr[i-1]; arr[pos-1] = element;.
📋

Examples

InputArray: [1, 2, 4, 5], Element: 3, Position: 3
OutputArray after insertion: 1 2 3 4 5
InputArray: [10, 20, 30], Element: 15, Position: 2
OutputArray after insertion: 10 15 20 30
InputArray: [5, 6, 7], Element: 4, Position: 1
OutputArray after insertion: 4 5 6 7
🧠

How to Think About It

To insert an element in an array, first decide where you want to put it. Then, move all elements from that position to the right by one place to make space. Finally, put the new element in the empty spot. This keeps the order and size consistent.
📐

Algorithm

1
Get the array and its current size.
2
Get the element to insert and the position where it should go.
3
Shift all elements from the position to the right by one place, starting from the end.
4
Insert the new element at the given position.
5
Increase the size of the array by one.
6
Print the updated array.
💻

Code

c
#include <stdio.h>

int main() {
    int arr[100] = {1, 2, 4, 5};
    int n = 4, element = 3, pos = 3;

    for (int i = n; i >= pos; i--) {
        arr[i] = arr[i - 1];
    }
    arr[pos - 1] = element;
    n++;

    printf("Array after insertion: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
Output
Array after insertion: 1 2 3 4 5
🔍

Dry Run

Let's trace inserting element 3 at position 3 in array [1, 2, 4, 5].

1

Initial array and variables

arr = [1, 2, 4, 5], n = 4, element = 3, pos = 3

2

Shift elements right

Move arr[3] to arr[4]: arr[4] = arr[3] (5) Move arr[2] to arr[3]: arr[3] = arr[2] (4)

3

Insert new element

arr[2] = 3

4

Updated array

arr = [1, 2, 3, 4, 5], n = 5

iarr[i] beforearr[i] after
4undefined5
344
💡

Why This Works

Step 1: Shifting elements

We move elements from the end to the insertion position one step right to avoid overwriting data.

Step 2: Inserting element

After shifting, the target position is free, so we place the new element there.

Step 3: Updating size

We increase the array size by one to reflect the new element added.

🔄

Alternative Approaches

Using a new array
c
#include <stdio.h>

int main() {
    int arr[] = {1, 2, 4, 5};
    int n = 4, element = 3, pos = 3;
    int newArr[100];

    for (int i = 0; i < pos - 1; i++) {
        newArr[i] = arr[i];
    }
    newArr[pos - 1] = element;
    for (int i = pos - 1; i < n; i++) {
        newArr[i + 1] = arr[i];
    }
    n++;

    printf("Array after insertion: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", newArr[i]);
    }
    return 0;
}
This method uses extra space but avoids shifting in the original array.
Insert at end and swap
c
#include <stdio.h>

int main() {
    int arr[100] = {1, 2, 4, 5};
    int n = 4, element = 3, pos = 3;

    arr[n] = element;
    n++;

    for (int i = n - 1; i > pos - 1; i--) {
        int temp = arr[i];
        arr[i] = arr[i - 1];
        arr[i - 1] = temp;
    }

    printf("Array after insertion: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
This swaps elements from the end to the position, which can be less efficient but is a different approach.

Complexity: O(n) time, O(1) space

Time Complexity

The loop shifts elements from the insertion position to the end, which can be up to n operations.

Space Complexity

Insertion is done in-place, so no extra space is needed beyond the array.

Which Approach is Fastest?

In-place shifting is fastest for memory, while using a new array trades space for simpler logic.

ApproachTimeSpaceBest For
In-place shiftingO(n)O(1)Memory efficiency
Using new arrayO(n)O(n)Simplicity and safety
Insert and swapO(n)O(1)Alternative logic
💡
Always shift elements from the end to avoid overwriting data when inserting in an array.
⚠️
Beginners often overwrite elements by shifting from the start instead of from the end.