Program to Insert an element in an Array at Given Location

Program to Insert an element in an Array at Given Location

  • Write a program to Insert an element in an Array at Given Location in C
  • Write a program to Insert an element in an Array at Given Location in C++
  • Write a program to Insert an element in an Array at Given Location in Python
  • Write a program to Insert an element in an Array at Given Location in PHP
  • Write a program to Insert an element in an Array at Given Location in Java
  • Write a program to Insert an element in an Array at Given Location in Java Script
  • Write a program to Insert an element in an Array at Given Location in C#

Explanation:

Logic:

  1. Input: The array, the element to insert, and the index where the element should be inserted.
  2. Check Boundaries: Ensure the index is within the valid range (0 to the length of the array).
  3. Shift Elements: Move all elements starting from the specified index one position to the right.
  4. Insert the Element: Place the new element at the desired index.
  5. Resize Array: For dynamic arrays, ensure the array size increases as necessary.

Program to Insert an element in an Array at Given Location

#include <stdio.h>

int main() {
    int arr[6] = {1, 2, 3, 4, 5};  // Example array (size 5, adding 1 element)
    int size = 5;  // Current size of the array
    int index = 2;  // Index where the element is to be inserted
    int value = 6;  // Element to insert

    // Check if the index is valid
    if (index >= 0 && index <= size) {
        // Shift elements to the right to make space for the new element
        for (int i = size; i > index; i--) {
            arr[i] = arr[i - 1];
        }
        arr[index] = value;  // Insert the new element at the specified index
        size++;  // Increase the size of the array
    }

    // Print the array after insertion
    printf("Array after insertion: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;
}

#include <iostream>
using namespace std;

int main() {
    int arr[6] = {1, 2, 3, 4, 5};  // Example array (size 5, adding 1 element)
    int size = 5;  // Current size of the array
    int index = 2;  // Index where the element is to be inserted
    int value = 6;  // Element to insert

    // Check if the index is valid
    if (index >= 0 && index <= size) {
        // Shift elements to the right to make space for the new element
        for (int i = size; i > index; i--) {
            arr[i] = arr[i - 1];
        }
        arr[index] = value;  // Insert the new element at the specified index
        size++;  // Increase the size of the array
    }

    cout << "Array after insertion: ";
    for (int i = 0; i < size; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;

    return 0;
}

arr = [1, 2, 3, 4, 5]  # Example array
index = 2  # Index where the element is to be inserted
value = 6  # Element to insert

# Check if the index is valid
if 0 <= index <= len(arr):
    arr.insert(index, value)  # Insert the element at the specified index

print("Array after insertion:", arr)

<?php
$arr = array(1, 2, 3, 4, 5);  // Example array
$index = 2;  // Index where the element is to be inserted
$value = 6;  // Element to insert

// Check if the index is valid
if ($index >= 0 && $index <= count($arr)) {
    array_splice($arr, $index, 0, $value);  // Insert the element at the specified index
}

echo "Array after insertion: ";
print_r($arr);
?>

import java.util.ArrayList;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        Integer[] arr = {1, 2, 3, 4, 5};  // Example array
        ArrayList<Integer> list = new ArrayList<>(Arrays.asList(arr)); // Convert array to ArrayList
        int index = 2;  // Index where the element is to be inserted
        int value = 6;  // Element to insert

        // Check if the index is valid
        if (index >= 0 && index <= list.size()) {
            list.add(index, value);  // Insert the element at the specified index
        }

        System.out.print("Array after insertion: ");
        for (int num : list) {
            System.out.print(num + " ");
        }
        System.out.println();
    }
}

let arr = [1, 2, 3, 4, 5];  // Example array
let index = 2;  // Index where the element is to be inserted
let value = 6;  // Element to insert

// Check if the index is valid
if (index >= 0 && index <= arr.length) {
    arr.splice(index, 0, value);  // Insert the element at the specified index
}

console.log("Array after insertion:", arr);

using System;
using System.Linq;

class Program {
    static void Main() {
        int[] arr = {1, 2, 3, 4, 5};  // Example array
        int index = 2;  // Index where the element is to be inserted
        int value = 6;  // Element to insert

        // Check if the index is valid
        if (index >= 0 && index <= arr.Length) {
            // Use LINQ to insert the element at the specified index
            arr = arr.Take(index).Concat(new[] {value}).Concat(arr.Skip(index)).ToArray();
        }

        Console.Write("Array after insertion: ");
        foreach (var num in arr) {
            Console.Write(num + " ");
        }
        Console.WriteLine();
    }
}

List of All Programs