diff --git a/src/main/java/com/thealgorithms/others/InsertDeleteInArray.java b/src/main/java/com/thealgorithms/others/InsertDeleteInArray.java
index 150935498..06a2539ee 100644
--- a/src/main/java/com/thealgorithms/others/InsertDeleteInArray.java
+++ b/src/main/java/com/thealgorithms/others/InsertDeleteInArray.java
@@ -1,51 +1,172 @@
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:
+ *
+ *
+ * - Insert: O(n) - requires copying elements to new array
+ * - Delete: O(n) - requires shifting elements
+ *
+ *
+ *
+ * Space Complexity:
+ *
+ *
+ * - Insert: O(n) - new array of size n+1
+ * - Delete: O(1) - in-place modification (excluding result array)
+ *
+ *
+ * @author TheAlgorithms community
+ * @see Array
+ * Data Structure
+ */
public final class InsertDeleteInArray {
private InsertDeleteInArray() {
}
+ /**
+ * Inserts an element at the specified position in the array.
+ *
+ * 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:
+ *
+ * - Takes array size and elements as input
+ * - Inserts a new element at a specified position
+ * - Deletes an element from a specified position
+ * - Displays the array after each operation
+ *
+ *
+ *
+ * @param args command line arguments (not used)
+ */
public static void main(String[] args) {
- try (Scanner s = new Scanner(System.in)) {
- System.out.println("Enter the size of the array");
- int size = s.nextInt();
- int[] a = new int[size];
- int i;
+ try (Scanner scanner = new Scanner(System.in)) {
+ // Input: array size and elements
+ System.out.println("Enter the size of the array:");
+ int size = scanner.nextInt();
- // To enter the initial elements
- for (i = 0; i < size; i++) {
- System.out.println("Enter the element");
- a[i] = s.nextInt();
+ if (size <= 0) {
+ System.out.println("Array size must be positive");
+ return;
}
- // To insert a new element(we are creating a new array)
- System.out.println("Enter the index at which the element should be inserted");
- int insertPos = s.nextInt();
- System.out.println("Enter the element to be inserted");
- int ins = s.nextInt();
- int size2 = size + 1;
- int[] b = new int[size2];
- for (i = 0; i < size2; i++) {
- if (i <= insertPos) {
- b[i] = a[i];
- } else {
- b[i] = a[i - 1];
- }
- }
- b[insertPos] = ins;
- for (i = 0; i < size2; i++) {
- System.out.println(b[i]);
+ int[] array = new int[size];
+
+ System.out.println("Enter " + size + " elements:");
+ for (int i = 0; i < size; i++) {
+ array[i] = scanner.nextInt();
}
- // To delete an element given the index
- System.out.println("Enter the index at which element is to be deleted");
- int delPos = s.nextInt();
- for (i = delPos; i < size2 - 1; i++) {
- b[i] = b[i + 1];
+ System.out.println("Original array: " + Arrays.toString(array));
+
+ // Insert operation
+ System.out.println("\nEnter the index at which the element should be inserted (0-" + size + "):");
+ int insertPos = scanner.nextInt();
+ System.out.println("Enter the element to be inserted:");
+ int elementToInsert = scanner.nextInt();
+
+ try {
+ array = insertElement(array, elementToInsert, insertPos);
+ System.out.println("Array after insertion: " + Arrays.toString(array));
+ } catch (IllegalArgumentException e) {
+ System.out.println("Error during insertion: " + e.getMessage());
+ return;
}
- for (i = 0; i < size2 - 1; i++) {
- System.out.println(b[i]);
+
+ // Delete operation
+ System.out.println("\nEnter the index at which element is to be deleted (0-" + (array.length - 1) + "):");
+ int deletePos = scanner.nextInt();
+
+ try {
+ array = deleteElement(array, deletePos);
+ System.out.println("Array after deletion: " + Arrays.toString(array));
+ } catch (IllegalArgumentException e) {
+ System.out.println("Error during deletion: " + e.getMessage());
}
}
}
diff --git a/src/test/java/com/thealgorithms/others/InsertDeleteInArrayTest.java b/src/test/java/com/thealgorithms/others/InsertDeleteInArrayTest.java
new file mode 100644
index 000000000..e8e49980d
--- /dev/null
+++ b/src/test/java/com/thealgorithms/others/InsertDeleteInArrayTest.java
@@ -0,0 +1,220 @@
+package com.thealgorithms.others;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test cases for {@link InsertDeleteInArray} class.
+ *
+ * Tests cover:
+ *
+ * - Insert operations at various positions
+ * - Delete operations at various positions
+ * - Edge cases (empty arrays, single element, boundary positions)
+ * - Error conditions (null arrays, invalid positions)
+ *
+ *
+ */
+class InsertDeleteInArrayTest {
+
+ // ========== Insert Element Tests ==========
+
+ @Test
+ void testInsertAtBeginning() {
+ int[] array = {2, 3, 4, 5};
+ int[] result = InsertDeleteInArray.insertElement(array, 1, 0);
+ assertArrayEquals(new int[] {1, 2, 3, 4, 5}, result);
+ }
+
+ @Test
+ void testInsertAtEnd() {
+ int[] array = {1, 2, 3, 4};
+ int[] result = InsertDeleteInArray.insertElement(array, 5, 4);
+ assertArrayEquals(new int[] {1, 2, 3, 4, 5}, result);
+ }
+
+ @Test
+ void testInsertInMiddle() {
+ int[] array = {1, 2, 4, 5};
+ int[] result = InsertDeleteInArray.insertElement(array, 3, 2);
+ assertArrayEquals(new int[] {1, 2, 3, 4, 5}, result);
+ }
+
+ @Test
+ void testInsertIntoEmptyArray() {
+ int[] array = {};
+ int[] result = InsertDeleteInArray.insertElement(array, 42, 0);
+ assertArrayEquals(new int[] {42}, result);
+ }
+
+ @Test
+ void testInsertIntoSingleElementArray() {
+ int[] array = {5};
+ int[] result = InsertDeleteInArray.insertElement(array, 3, 0);
+ assertArrayEquals(new int[] {3, 5}, result);
+ }
+
+ @Test
+ void testInsertNegativeNumber() {
+ int[] array = {1, 2, 3};
+ int[] result = InsertDeleteInArray.insertElement(array, -10, 1);
+ assertArrayEquals(new int[] {1, -10, 2, 3}, result);
+ }
+
+ @Test
+ void testInsertZero() {
+ int[] array = {1, 2, 3};
+ int[] result = InsertDeleteInArray.insertElement(array, 0, 1);
+ assertArrayEquals(new int[] {1, 0, 2, 3}, result);
+ }
+
+ @Test
+ void testInsertWithNullArray() {
+ IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> InsertDeleteInArray.insertElement(null, 5, 0));
+ assertEquals("Array cannot be null", exception.getMessage());
+ }
+
+ @Test
+ void testInsertWithNegativePosition() {
+ int[] array = {1, 2, 3};
+ IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> InsertDeleteInArray.insertElement(array, 5, -1));
+ assertEquals("Position must be between 0 and 3", exception.getMessage());
+ }
+
+ @Test
+ void testInsertWithPositionTooLarge() {
+ int[] array = {1, 2, 3};
+ IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> InsertDeleteInArray.insertElement(array, 5, 4));
+ assertEquals("Position must be between 0 and 3", exception.getMessage());
+ }
+
+ // ========== Delete Element Tests ==========
+
+ @Test
+ void testDeleteFromBeginning() {
+ int[] array = {1, 2, 3, 4, 5};
+ int[] result = InsertDeleteInArray.deleteElement(array, 0);
+ assertArrayEquals(new int[] {2, 3, 4, 5}, result);
+ }
+
+ @Test
+ void testDeleteFromEnd() {
+ int[] array = {1, 2, 3, 4, 5};
+ int[] result = InsertDeleteInArray.deleteElement(array, 4);
+ assertArrayEquals(new int[] {1, 2, 3, 4}, result);
+ }
+
+ @Test
+ void testDeleteFromMiddle() {
+ int[] array = {1, 2, 3, 4, 5};
+ int[] result = InsertDeleteInArray.deleteElement(array, 2);
+ assertArrayEquals(new int[] {1, 2, 4, 5}, result);
+ }
+
+ @Test
+ void testDeleteFromSingleElementArray() {
+ int[] array = {42};
+ int[] result = InsertDeleteInArray.deleteElement(array, 0);
+ assertArrayEquals(new int[] {}, result);
+ }
+
+ @Test
+ void testDeleteFromTwoElementArray() {
+ int[] array = {10, 20};
+ int[] result = InsertDeleteInArray.deleteElement(array, 1);
+ assertArrayEquals(new int[] {10}, result);
+ }
+
+ @Test
+ void testDeleteWithNullArray() {
+ IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> InsertDeleteInArray.deleteElement(null, 0));
+ assertEquals("Array cannot be null", exception.getMessage());
+ }
+
+ @Test
+ void testDeleteFromEmptyArray() {
+ int[] array = {};
+ IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> InsertDeleteInArray.deleteElement(array, 0));
+ assertEquals("Array is empty", exception.getMessage());
+ }
+
+ @Test
+ void testDeleteWithNegativePosition() {
+ int[] array = {1, 2, 3};
+ IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> InsertDeleteInArray.deleteElement(array, -1));
+ assertEquals("Position must be between 0 and 2", exception.getMessage());
+ }
+
+ @Test
+ void testDeleteWithPositionTooLarge() {
+ int[] array = {1, 2, 3};
+ IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> InsertDeleteInArray.deleteElement(array, 3));
+ assertEquals("Position must be between 0 and 2", exception.getMessage());
+ }
+
+ @Test
+ void testDeleteWithPositionEqualToLength() {
+ int[] array = {1, 2, 3, 4, 5};
+ IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> InsertDeleteInArray.deleteElement(array, 5));
+ assertEquals("Position must be between 0 and 4", exception.getMessage());
+ }
+
+ // ========== Combined Operations Tests ==========
+
+ @Test
+ void testInsertThenDelete() {
+ int[] array = {1, 2, 3};
+ int[] afterInsert = InsertDeleteInArray.insertElement(array, 99, 1);
+ assertArrayEquals(new int[] {1, 99, 2, 3}, afterInsert);
+ int[] afterDelete = InsertDeleteInArray.deleteElement(afterInsert, 1);
+ assertArrayEquals(new int[] {1, 2, 3}, afterDelete);
+ }
+
+ @Test
+ void testMultipleInsertions() {
+ int[] array = {1, 3, 5};
+ array = InsertDeleteInArray.insertElement(array, 2, 1);
+ assertArrayEquals(new int[] {1, 2, 3, 5}, array);
+ array = InsertDeleteInArray.insertElement(array, 4, 3);
+ assertArrayEquals(new int[] {1, 2, 3, 4, 5}, array);
+ }
+
+ @Test
+ void testMultipleDeletions() {
+ int[] array = {1, 2, 3, 4, 5};
+ array = InsertDeleteInArray.deleteElement(array, 2);
+ assertArrayEquals(new int[] {1, 2, 4, 5}, array);
+ array = InsertDeleteInArray.deleteElement(array, 0);
+ assertArrayEquals(new int[] {2, 4, 5}, array);
+ }
+
+ @Test
+ void testLargeArray() {
+ int[] array = new int[100];
+ for (int i = 0; i < 100; i++) {
+ array[i] = i;
+ }
+ int[] result = InsertDeleteInArray.insertElement(array, 999, 50);
+ assertEquals(101, result.length);
+ assertEquals(999, result[50]);
+ assertEquals(49, result[49]);
+ assertEquals(50, result[51]);
+ }
+
+ @Test
+ void testArrayWithDuplicates() {
+ int[] array = {1, 2, 2, 3, 2};
+ int[] result = InsertDeleteInArray.deleteElement(array, 1);
+ assertArrayEquals(new int[] {1, 2, 3, 2}, result);
+ }
+
+ @Test
+ void testNegativeNumbers() {
+ int[] array = {-5, -3, -1, 0, 1};
+ int[] result = InsertDeleteInArray.insertElement(array, -2, 2);
+ assertArrayEquals(new int[] {-5, -3, -2, -1, 0, 1}, result);
+ }
+}