|
1 | 1 | package com.thealgorithms.others; |
2 | 2 |
|
| 3 | +import java.util.Arrays; |
3 | 4 | import java.util.Scanner; |
4 | 5 |
|
| 6 | +/** |
| 7 | + * Utility class for performing insert and delete operations on arrays. |
| 8 | + * <p> |
| 9 | + * This class demonstrates how to insert an element at a specific position and |
| 10 | + * delete an element from a specific position in an integer array. Since arrays |
| 11 | + * in Java have fixed size, insertion creates a new array with increased size, |
| 12 | + * and deletion shifts elements to fill the gap. |
| 13 | + * </p> |
| 14 | + * |
| 15 | + * <p> |
| 16 | + * <strong>Time Complexity:</strong> |
| 17 | + * </p> |
| 18 | + * <ul> |
| 19 | + * <li>Insert: O(n) - requires copying elements to new array</li> |
| 20 | + * <li>Delete: O(n) - requires shifting elements</li> |
| 21 | + * </ul> |
| 22 | + * |
| 23 | + * <p> |
| 24 | + * <strong>Space Complexity:</strong> |
| 25 | + * </p> |
| 26 | + * <ul> |
| 27 | + * <li>Insert: O(n) - new array of size n+1</li> |
| 28 | + * <li>Delete: O(1) - in-place modification (excluding result array)</li> |
| 29 | + * </ul> |
| 30 | + * |
| 31 | + * @author TheAlgorithms community |
| 32 | + * @see <a href="https://en.wikipedia.org/wiki/Array_(data_structure)">Array |
| 33 | + * Data Structure</a> |
| 34 | + */ |
5 | 35 | public final class InsertDeleteInArray { |
6 | 36 | private InsertDeleteInArray() { |
7 | 37 | } |
8 | 38 |
|
| 39 | + /** |
| 40 | + * Inserts an element at the specified position in the array. |
| 41 | + * <p> |
| 42 | + * Creates a new array with size = original array size + 1. |
| 43 | + * Elements at positions <= insertPos retain their positions, |
| 44 | + * while elements at positions > insertPos are shifted right by one position. |
| 45 | + * </p> |
| 46 | + * |
| 47 | + * @param array the original array |
| 48 | + * @param element the element to be inserted |
| 49 | + * @param position the index at which the element should be inserted (0-based) |
| 50 | + * @return a new array with the element inserted at the specified position |
| 51 | + * @throws IllegalArgumentException if position is negative or greater than |
| 52 | + * array length |
| 53 | + * @throws IllegalArgumentException if array is null |
| 54 | + */ |
| 55 | + public static int[] insertElement(int[] array, int element, int position) { |
| 56 | + if (array == null) { |
| 57 | + throw new IllegalArgumentException("Array cannot be null"); |
| 58 | + } |
| 59 | + if (position < 0 || position > array.length) { |
| 60 | + throw new IllegalArgumentException("Position must be between 0 and " + array.length); |
| 61 | + } |
| 62 | + |
| 63 | + int[] newArray = new int[array.length + 1]; |
| 64 | + |
| 65 | + // Copy elements before insertion position |
| 66 | + System.arraycopy(array, 0, newArray, 0, position); |
| 67 | + |
| 68 | + // Insert the new element |
| 69 | + newArray[position] = element; |
| 70 | + |
| 71 | + // Copy remaining elements after insertion position |
| 72 | + System.arraycopy(array, position, newArray, position + 1, array.length - position); |
| 73 | + |
| 74 | + return newArray; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Deletes an element at the specified position from the array. |
| 79 | + * <p> |
| 80 | + * Creates a new array with size = original array size - 1. |
| 81 | + * Elements after the deletion position are shifted left by one position. |
| 82 | + * </p> |
| 83 | + * |
| 84 | + * @param array the original array |
| 85 | + * @param position the index of the element to be deleted (0-based) |
| 86 | + * @return a new array with the element at the specified position removed |
| 87 | + * @throws IllegalArgumentException if position is negative or greater than or |
| 88 | + * equal to array length |
| 89 | + * @throws IllegalArgumentException if array is null or empty |
| 90 | + */ |
| 91 | + public static int[] deleteElement(int[] array, int position) { |
| 92 | + if (array == null) { |
| 93 | + throw new IllegalArgumentException("Array cannot be null"); |
| 94 | + } |
| 95 | + if (array.length == 0) { |
| 96 | + throw new IllegalArgumentException("Array is empty"); |
| 97 | + } |
| 98 | + if (position < 0 || position >= array.length) { |
| 99 | + throw new IllegalArgumentException("Position must be between 0 and " + (array.length - 1)); |
| 100 | + } |
| 101 | + |
| 102 | + int[] newArray = new int[array.length - 1]; |
| 103 | + |
| 104 | + // Copy elements before deletion position |
| 105 | + System.arraycopy(array, 0, newArray, 0, position); |
| 106 | + |
| 107 | + // Copy elements after deletion position |
| 108 | + System.arraycopy(array, position + 1, newArray, position, array.length - position - 1); |
| 109 | + |
| 110 | + return newArray; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Main method demonstrating insert and delete operations on an array. |
| 115 | + * <p> |
| 116 | + * This method interactively: |
| 117 | + * <ol> |
| 118 | + * <li>Takes array size and elements as input</li> |
| 119 | + * <li>Inserts a new element at a specified position</li> |
| 120 | + * <li>Deletes an element from a specified position</li> |
| 121 | + * <li>Displays the array after each operation</li> |
| 122 | + * </ol> |
| 123 | + * </p> |
| 124 | + * |
| 125 | + * @param args command line arguments (not used) |
| 126 | + */ |
9 | 127 | public static void main(String[] args) { |
10 | | - try (Scanner s = new Scanner(System.in)) { |
11 | | - System.out.println("Enter the size of the array"); |
12 | | - int size = s.nextInt(); |
13 | | - int[] a = new int[size]; |
14 | | - int i; |
15 | | - |
16 | | - // To enter the initial elements |
17 | | - for (i = 0; i < size; i++) { |
18 | | - System.out.println("Enter the element"); |
19 | | - a[i] = s.nextInt(); |
20 | | - } |
| 128 | + try (Scanner scanner = new Scanner(System.in)) { |
| 129 | + // Input: array size and elements |
| 130 | + System.out.println("Enter the size of the array:"); |
| 131 | + int size = scanner.nextInt(); |
21 | 132 |
|
22 | | - // To insert a new element(we are creating a new array) |
23 | | - System.out.println("Enter the index at which the element should be inserted"); |
24 | | - int insertPos = s.nextInt(); |
25 | | - System.out.println("Enter the element to be inserted"); |
26 | | - int ins = s.nextInt(); |
27 | | - int size2 = size + 1; |
28 | | - int[] b = new int[size2]; |
29 | | - for (i = 0; i < size2; i++) { |
30 | | - if (i <= insertPos) { |
31 | | - b[i] = a[i]; |
32 | | - } else { |
33 | | - b[i] = a[i - 1]; |
34 | | - } |
| 133 | + if (size <= 0) { |
| 134 | + System.out.println("Array size must be positive"); |
| 135 | + return; |
35 | 136 | } |
36 | | - b[insertPos] = ins; |
37 | | - for (i = 0; i < size2; i++) { |
38 | | - System.out.println(b[i]); |
| 137 | + |
| 138 | + int[] array = new int[size]; |
| 139 | + |
| 140 | + System.out.println("Enter " + size + " elements:"); |
| 141 | + for (int i = 0; i < size; i++) { |
| 142 | + array[i] = scanner.nextInt(); |
39 | 143 | } |
40 | 144 |
|
41 | | - // To delete an element given the index |
42 | | - System.out.println("Enter the index at which element is to be deleted"); |
43 | | - int delPos = s.nextInt(); |
44 | | - for (i = delPos; i < size2 - 1; i++) { |
45 | | - b[i] = b[i + 1]; |
| 145 | + System.out.println("Original array: " + Arrays.toString(array)); |
| 146 | + |
| 147 | + // Insert operation |
| 148 | + System.out.println("\nEnter the index at which the element should be inserted (0-" + size + "):"); |
| 149 | + int insertPos = scanner.nextInt(); |
| 150 | + System.out.println("Enter the element to be inserted:"); |
| 151 | + int elementToInsert = scanner.nextInt(); |
| 152 | + |
| 153 | + try { |
| 154 | + array = insertElement(array, elementToInsert, insertPos); |
| 155 | + System.out.println("Array after insertion: " + Arrays.toString(array)); |
| 156 | + } catch (IllegalArgumentException e) { |
| 157 | + System.out.println("Error during insertion: " + e.getMessage()); |
| 158 | + return; |
46 | 159 | } |
47 | | - for (i = 0; i < size2 - 1; i++) { |
48 | | - System.out.println(b[i]); |
| 160 | + |
| 161 | + // Delete operation |
| 162 | + System.out.println("\nEnter the index at which element is to be deleted (0-" + (array.length - 1) + "):"); |
| 163 | + int deletePos = scanner.nextInt(); |
| 164 | + |
| 165 | + try { |
| 166 | + array = deleteElement(array, deletePos); |
| 167 | + System.out.println("Array after deletion: " + Arrays.toString(array)); |
| 168 | + } catch (IllegalArgumentException e) { |
| 169 | + System.out.println("Error during deletion: " + e.getMessage()); |
49 | 170 | } |
50 | 171 | } |
51 | 172 | } |
|
0 commit comments