Array Deletion at Middle Index in DSA Java - Time & Space Complexity
When we delete an element from the middle of an array, the time it takes depends on how many elements we need to move.
We want to know how the work grows as the array gets bigger.
Analyze the time complexity of the following code snippet.
int[] arr = {1, 2, 3, 4, 5};
int deleteIndex = arr.length / 2; // middle index
for (int i = deleteIndex; i < arr.length - 1; i++) {
arr[i] = arr[i + 1];
}
arr[arr.length - 1] = 0; // optional: clear last element
This code deletes the middle element by shifting all elements after it one step left.
- Primary operation: The for-loop that shifts elements left after the deleted index.
- How many times: It runs from the middle index to the end, about half the array size.
As the array gets bigger, the number of elements to shift grows roughly in proportion to the size of the array.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 5 shifts |
| 100 | About 50 shifts |
| 1000 | About 500 shifts |
Pattern observation: The work grows linearly with the size of the array.
Time Complexity: O(n)
This means if the array doubles in size, the time to delete the middle element roughly doubles too.
[X] Wrong: "Deleting an element from an array is always fast and takes constant time."
[OK] Correct: Because after deleting, elements must be shifted to fill the gap, which takes time proportional to how many elements are after the deleted one.
Understanding how array operations scale helps you explain trade-offs in data structures and choose the right one for your needs.
"What if we deleted the last element instead of the middle? How would the time complexity change?"
Practice
int[] arr = {10, 20, 30, 40, 50};Delete element at index 2 (middle). What is the resulting array?
Solution
Step 1: Identify the middle index and element
The array size is 5, so middle index is 2 (0-based). Element at index 2 is 30.Step 2: Remove the element and shift remaining
Remove 30, then shift elements after index 2 left: 40 moves to index 2, 50 moves to index 3.Final Answer:
[10, 20, 40, 50] -> Option AQuick Check:
Middle element 30 removed, array shrinks by one [OK]
- Not shifting elements after deletion
- Deleting wrong index (not middle)
- Leaving array size unchanged
arr of odd length n?Solution
Step 1: Understand the deletion index
Middle index for odd length n is n/2 (integer division). We must shift elements after this index left.Step 2: Check loop correctness
Loop from n/2 to n-2 shifts elements left by copying arr[i+1] to arr[i]. This correctly removes middle element.Final Answer:
for (int i = n/2; i < n - 1; i++) arr[i] = arr[i + 1]; -> Option CQuick Check:
Shift elements after middle left [OK]
- Looping from 0 instead of middle index
- Shifting elements right instead of left
- Loop running out of array bounds
int[] arr = {5, 10, 15, 20, 25, 30, 35};
int n = arr.length;
int mid = n / 2;
for (int i = mid; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
n--;
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}Solution
Step 1: Identify middle index and element
Array length n=7, middle index mid=3, element at index 3 is 20.Step 2: Shift elements left from middle
Loop shifts elements from index 3 to 5 left by one: arr[3]=arr[4]=25, arr[4]=arr[5]=30, arr[5]=arr[6]=35.Step 3: Decrease size and print
n is decremented to 6, so print first 6 elements: 5 10 15 25 30 35.Final Answer:
5 10 15 25 30 35 -> Option BQuick Check:
Middle element 20 removed, array shifted left [OK]
- Printing full original array length
- Not decrementing array size after deletion
- Confusing middle index calculation
arr of length n:int mid = n / 2;
for (int i = mid; i < n; i++) {
arr[i] = arr[i + 1];
}
n--;Solution
Step 1: Analyze loop range and array access
Loop runs from i = mid to i = n-1. At i = n-1, arr[i+1] = arr[n] is accessed, which is out of bounds.Step 2: Correct loop boundary
Loop should run until i < n-1 to avoid out-of-bounds access when shifting elements left.Final Answer:
Loop runs out of array bounds accessing arr[i+1] when i = n-1 -> Option DQuick Check:
Array index out of bounds error [OK]
- Using wrong loop end condition
- Ignoring array index limits
- Incorrect middle index for even length arrays
arr = {2, 4, 6, 8, 10, 12} of even length 6, how do you delete the middle element(s) to keep the array balanced? Choose the correct resulting array after deleting the middle element(s).Solution
Step 1: Identify middle elements for even length
For length 6, middle indices are 2 and 3 (elements 6 and 8).Step 2: Delete one or both middle elements to keep balance
Usually, delete one middle element to keep array balanced. Deleting element at index 2 (6) shifts elements left.Step 3: Resulting array after deleting element at index 2
Array becomes [2, 4, 8, 10, 12] after shifting elements left and reducing size by one.Final Answer:
[2, 4, 8, 10, 12] -> Option AQuick Check:
Delete one middle element, shift left, size reduces [OK]
- Deleting both middle elements instead of one
- Deleting wrong middle index
- Not shifting elements after deletion
Ready to prove it?
Pass the test and this pattern moves unknown
