package com.thealgorithms.others; import java.util.Arrays; import java.util.Scanner; /** * Utility class for performing insert and delete operations on arrays. *
* This class demonstrates how to insert an element at a specific position and * delete an element from a specific position in an integer array. Since arrays * in Java have fixed size, insertion creates a new array with increased size, * and deletion shifts elements to fill the gap. *
* ** Time Complexity: *
** Space Complexity: *
** Creates a new array with size = original array size + 1. * Elements at positions <= insertPos retain their positions, * while elements at positions > insertPos are shifted right by one position. *
* * @param array the original array * @param element the element to be inserted * @param position the index at which the element should be inserted (0-based) * @return a new array with the element inserted at the specified position * @throws IllegalArgumentException if position is negative or greater than * array length * @throws IllegalArgumentException if array is null */ public static int[] insertElement(int[] array, int element, int position) { if (array == null) { throw new IllegalArgumentException("Array cannot be null"); } if (position < 0 || position > array.length) { throw new IllegalArgumentException("Position must be between 0 and " + array.length); } int[] newArray = new int[array.length + 1]; // Copy elements before insertion position System.arraycopy(array, 0, newArray, 0, position); // Insert the new element newArray[position] = element; // Copy remaining elements after insertion position System.arraycopy(array, position, newArray, position + 1, array.length - position); return newArray; } /** * Deletes an element at the specified position from the array. ** Creates a new array with size = original array size - 1. * Elements after the deletion position are shifted left by one position. *
* * @param array the original array * @param position the index of the element to be deleted (0-based) * @return a new array with the element at the specified position removed * @throws IllegalArgumentException if position is negative or greater than or * equal to array length * @throws IllegalArgumentException if array is null or empty */ public static int[] deleteElement(int[] array, int position) { if (array == null) { throw new IllegalArgumentException("Array cannot be null"); } if (array.length == 0) { throw new IllegalArgumentException("Array is empty"); } if (position < 0 || position >= array.length) { throw new IllegalArgumentException("Position must be between 0 and " + (array.length - 1)); } int[] newArray = new int[array.length - 1]; // Copy elements before deletion position System.arraycopy(array, 0, newArray, 0, position); // Copy elements after deletion position System.arraycopy(array, position + 1, newArray, position, array.length - position - 1); return newArray; } /** * Main method demonstrating insert and delete operations on an array. ** This method interactively: *