Inserting Elements in an Array at any Position
Insert operation in an array at any position can be performed by shifting elements to the right, which are on the right side of the required position
Coding implementation of inserting an element at any position:
// C++ Program to Insert an element
// at a specific position in an Array
#include <bits/stdc++.h>
using namespace std;
// Function to insert element
// at a specific position
void insertElement(int arr[], int n, int x, int pos)
{
// shift elements to the right
// which are on the right side of pos
for (int i = n - 1; i >= pos; i--)
arr[i + 1] = arr[i];
arr[pos] = x;
}
// Driver's code
int main()
{
int arr[15] = { 2, 4, 1, 8, 5 };
int n = 5;
cout << "Before insertion : ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
int x = 10, pos = 2;
// Function call
insertElement(arr, n, x, pos);
n++;
cout << "After insertion : ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
// C Program to Insert an element
// at a specific position in an Array
#include <stdio.h>
// Function to insert element
// at a specific position
void insertElement(int arr[], int n, int x, int pos)
{
// shift elements to the right
// which are on the right side of pos
for (int i = n - 1; i >= pos; i--)
arr[i + 1] = arr[i];
arr[pos] = x;
}
// Driver's code
int main()
{
int arr[15] = { 2, 4, 1, 8, 5 };
int n = 5;
printf("Before insertion : ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
int x = 10, pos = 2;
// Function call
insertElement(arr, n, x, pos);
n++;
printf("After insertion : ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
/*package whatever //do not write package name here */
import java.io.*;
// Java Program to Insert an element
// at a specific position in an Array
class GFG {
static void insertElement(int arr[], int n, int x,
int pos)
{
// shift elements to the right
// which are on the right side of pos
for (int i = n - 1; i >= pos; i--)
arr[i + 1] = arr[i];
arr[pos] = x;
}
public static void main(String[] args)
{
int arr[] = new int[15];
arr[0] = 2;
arr[1] = 4;
arr[2] = 1;
arr[3] = 8;
arr[4] = 5;
int n = 5;
int x = 10, pos = 2;
System.out.print("Before Insertion: ");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
// Inserting key at specific position
insertElement(arr, n, x, pos);
n += 1;
System.out.print("\n\nAfter Insertion: ");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
}
// This code is contributed by syedsarfarazahammed
# python Program to Insert an element
# at a specific position in an Array
def insertElement(arr, n, x, pos):
# shift elements to the right
# which are on the right side of pos
for i in range(n-1, pos-1, -1):
arr[i + 1] = arr[i]
arr[pos] = x
# Driver's code
if __name__ == '__main__':
# Declaring array and key to delete
# here -1 is for empty space
arr = [2, 4, 1, 8, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
n = 5
print("Before insertion : ")
for i in range(0, n):
print(arr[i], end=' ')
print("\n")
x = 10
pos = 2
# Function call
insertElement(arr, n, x, pos)
n += 1
print("After insertion : ")
for i in range(0, n):
print(arr[i], end=' ')
# This Code is contributed by aditya942003patil
// C# program to implement insert
// operation in an unsorted array.
using System;
class main {
static void insertElement(int[] arr, int n, int x,
int pos)
{
// shift elements to the right
// which are on the right side of pos
for (int i = n - 1; i >= pos; i--)
arr[i + 1] = arr[i];
arr[pos] = x;
}
public static void Main()
{
int[] arr = new int[20];
arr[0] = 2;
arr[1] = 4;
arr[2] = 1;
arr[3] = 8;
arr[4] = 5;
int x = 10;
int n = 5;
int pos = 2;
Console.Write("Before Insertion: ");
for (int i = 0; i < n; i++)
Console.Write(arr[i] + " ");
Console.WriteLine();
// Inserting key at specific position
insertElement(arr, n, x, pos);
n += 1;
Console.Write("\n\nAfter Insertion: ");
for (int i = 0; i < n; i++)
Console.Write(arr[i] + " ");
}
}
// This code is contributed by sourabhdalal0001
// javascript Program to Insert an element
// at a specific position in an Array
function insertElement(arr, n, x, pos)
{
// shift elements to the right
// which are on the right side of pos
var i = n - 1;
for (i; i >= pos; i--)
{
arr[i + 1] = arr[i];
}
arr[pos] = x;
}
var arr = Array(15).fill(0);
arr[0] = 2;
arr[1] = 4;
arr[2] = 1;
arr[3] = 8;
arr[4] = 5;
var n = 5;
var x = 10;
var pos = 2;
console.log("Before Insertion: ");
var i = 0;
for (i; i < n; i++)
{
console.log(arr[i] + " ");
}
// Inserting key at specific position
insertElement(arr, n, x, pos);
n += 1;
console.log("\n\nAfter Insertion: ");
i = 0;
for (i; i < n; i++)
{
console.log(arr[i] + " ");
}
// This code is contributed by sourabhdalal0001.
<?php
// Function to insert element at a specific position
function insertElement($arr, $n, $x, $pos) {
// shift elements to the right which are on the right side of pos
for ($i = $n - 1; $i >= $pos; $i--) {
$arr[$i + 1] = $arr[$i];
}
// insert the new element at the specified position
$arr[$pos] = $x;
return $arr; // return the modified array
}
// Driver's code
$arr = array(2, 4, 1, 8, 5);
$n = 5;
echo "Before insertion : ";
for ($i = 0; $i < $n; $i++) {
echo $arr[$i] . " ";
}
echo "\n";
$x = 10;
$pos = 2;
// Function call
$arr = insertElement($arr, $n, $x, $pos);
$n++;
echo "After insertion : ";
for ($i = 0; $i < $n; $i++) {
echo $arr[$i] . " ";
}
?>
Output
Before insertion : 2 4 1 8 5 After insertion : 2 4 10 1 8 5
Time complexity: O(N)
Auxiliary Space: O(1)
Inserting Elements in an Array | Array Operations
In this post, we will look into insertion operation in an Array, i.e., how to insert into an Array, such as:
- Inserting Elements in an Array at the End
- Inserting Elements in an Array at any Position in the Middle
- Inserting Elements in a Sorted Array