Program to Insert Element in Array
Program to Insert Element in Array
Description:
Given an array arr[] of integers, write a program to insert an element at a specified position in the array.
Example 1:
Input: arr = {1, 3, 5, 7, 9}, Element to Insert: 4, Position: 2
Output: {1, 4, 3, 5, 7, 9}
Explanation: The element 4 is inserted at position 2 in the array.
Constraints:
The array length |arr| is at least 1 and at most 10^5.
Java Code:
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)
Python Code:
# 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)
C++ Code:
#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
JavaScript Code:
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)