Don't miss your Year-End Offer: Up to 20% OFF

Program to Insert Element in Array

13 February 2024
import java.util.Arrays;

public class HackerAlgo {

    // Insert Element in Array at a Specified Position
    public static int[] insertElement(int[] arr, int element, int position) {
        int n = arr.length;

        if (position < 0 || position > n) {
System.out.println("Invalid position. Element not inserted.");
            return arr;
        }

int[] resultArr = new int[n + 1];

        for (int i = 0, j = 0; i< n + 1; i++) {
            if (i == position) {
resultArr[i] = element;
            } else {
resultArr[i] = arr[j++];
            }
        }

        return resultArr;
    }

    public static void main(String[] args) {
        // Example for Insert Element in Array
int[] arr = {1, 3, 5, 7, 9};
        int elementToInsert = 4;
        int insertPosition = 2;

int[] resultArr = insertElement(arr, elementToInsert, insertPosition);
System.out.println(Arrays.toString(resultArr)
# Insert Element in Array at a Specified Position
def insert_element(arr, element, position):
    if position < 0 or position >len(arr):
print("Invalid position. Element not inserted.")
        return arr

result_arr = arr[:position] + [element] + arr[position:]
    return result_arr

# Example usage
arr = [1, 3, 5, 7, 9]
element_to_insert = 4
insert_position = 2

result_arr = insert_element(arr, element_to_insert, insert_position)
print(result_arr)
#include <iostream>
#include <vector>

class HackerAlgo {
public:
    // Insert Element in Array at a Specified Position
    static std::vector<int>insertElement(const std::vector<int>&arr, int element, int position) {
        int n = arr.size();

        if (position < 0 || position > n) {
std::cout<< "Invalid position. Element not inserted." <<std::endl;
            return arr;
        }

std::vector<int>resultArr;
resultArr.reserve(n + 1);

        for (int i = 0, j = 0; i< n + 1; i++) {
            if (i == position) {
resultArr.push_back(element);
            } else {
resultArr.push_back(arr[j++]);
            }
        }

        return resultArr;
    }
};

int main() {
    // Example for Insert Element in Array
std::vector<int>arr = {1, 3, 5, 7, 9};
    int elementToInsert = 4;
    int insertPosition = 2;

std::vector<int>resultArr = HackerAlgo::insertElement(arr, elementToInsert, insertPosition);
    for (int num :resultArr) {
std::cout<<num<< " ";
    }
std::cout<< std::endl;

    return 0
class HackerAlgo {
    // Insert Element in Array at a Specified Position
    static insertElement(arr, element, position) {
        if (position < 0 || position >arr.length) {
console.log("Invalid position. Element not inserted.");
            return arr;
        }

constresultArr = [...arr.slice(0, position), element, ...arr.slice(position)];
        return resultArr;
    }
}

// Example usage
constarr = [1, 3, 5, 7, 9];
constelementToInsert = 4;
constinsertPosition = 2;

constresultArr = HackerAlgo.insertElement(arr, elementToInsert, insertPosition);
console.log(resultArr)

Leave a Comment