forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInsertDeleteInArray.java
More file actions
173 lines (151 loc) · 6.28 KB
/
InsertDeleteInArray.java
File metadata and controls
173 lines (151 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.thealgorithms.others;
import java.util.Arrays;
import java.util.Scanner;
/**
* Utility class for performing insert and delete operations on arrays.
* <p>
* 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.
* </p>
*
* <p>
* <strong>Time Complexity:</strong>
* </p>
* <ul>
* <li>Insert: O(n) - requires copying elements to new array</li>
* <li>Delete: O(n) - requires shifting elements</li>
* </ul>
*
* <p>
* <strong>Space Complexity:</strong>
* </p>
* <ul>
* <li>Insert: O(n) - new array of size n+1</li>
* <li>Delete: O(1) - in-place modification (excluding result array)</li>
* </ul>
*
* @author TheAlgorithms community
* @see <a href="https://en.wikipedia.org/wiki/Array_(data_structure)">Array
* Data Structure</a>
*/
public final class InsertDeleteInArray {
private InsertDeleteInArray() {
}
/**
* Inserts an element at the specified position in the array.
* <p>
* 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.
* </p>
*
* @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.
* <p>
* Creates a new array with size = original array size - 1.
* Elements after the deletion position are shifted left by one position.
* </p>
*
* @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.
* <p>
* This method interactively:
* <ol>
* <li>Takes array size and elements as input</li>
* <li>Inserts a new element at a specified position</li>
* <li>Deletes an element from a specified position</li>
* <li>Displays the array after each operation</li>
* </ol>
* </p>
*
* @param args command line arguments (not used)
*/
public static void main(String[] args) {
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();
if (size <= 0) {
System.out.println("Array size must be positive");
return;
}
int[] array = new int[size];
System.out.println("Enter " + size + " elements:");
for (int i = 0; i < size; i++) {
array[i] = scanner.nextInt();
}
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;
}
// 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());
}
}
}
}