Array Deletion at Beginning in DSA Java - Time & Space Complexity
When we delete an element from the start of an array, the rest of the elements need to move. We want to understand how this moving affects the time it takes.
How does the time to delete grow as the array gets bigger?
Analyze the time complexity of the following code snippet.
public void deleteAtBeginning(int[] arr, int size) {
for (int i = 1; i < size; i++) {
arr[i - 1] = arr[i];
}
size--;
}
This code removes the first element by shifting all other elements one position to the left.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that shifts elements left by one.
- How many times: It runs once for each element after the first, so roughly n - 1 times.
As the array size grows, the number of moves grows almost the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 9 moves |
| 100 | 99 moves |
| 1000 | 999 moves |
Pattern observation: The number of moves grows linearly with the array size.
Time Complexity: O(n)
This means the time to delete the first element grows directly with the number of elements in the array.
[X] Wrong: "Deleting the first element is quick because we just remove it."
[OK] Correct: Actually, all other elements must move to fill the gap, so the operation takes time proportional to the array size.
Understanding this helps you explain why some data structures are better for certain operations. It shows you can think about how code runs, not just what it does.
"What if we deleted the last element instead of the first? How would the time complexity change?"
Practice
Solution
Step 1: Understand array deletion at beginning
Deleting the first element means removing the element at index 0.Step 2: Effect on remaining elements
All other elements move one position left to fill the gap, reducing the array size by one.Final Answer:
All elements shift one position to the left, and the first element is removed. -> Option AQuick Check:
Delete first element = shift left [OK]
- Thinking elements shift right instead of left
- Assuming array size stays the same
- Believing the array remains unchanged
arr and shifts the remaining elements left?Solution
Step 1: Identify correct loop direction and indices
To shift elements left, start from index 0 to length-2, assigning arr[i] = arr[i+1].Step 2: Check each option
for (int i = 0; i < arr.length - 1; i++) { arr[i] = arr[i + 1]; } matches this logic; others shift right or use wrong indices causing errors.Final Answer:
for (int i = 0; i < arr.length - 1; i++) { arr[i] = arr[i + 1]; } -> Option CQuick Check:
Shift left loop: i from 0 to length-2 [OK]
- Looping backwards when shifting left
- Using wrong indices causing ArrayIndexOutOfBounds
- Assigning arr[i-1] instead of arr[i+1]
int[] arr = {10, 20, 30, 40, 50};
for (int i = 0; i < arr.length - 1; i++) {
arr[i] = arr[i + 1];
}
arr[arr.length - 1] = 0;
for (int num : arr) {
System.out.print(num + " ");
}Solution
Step 1: Trace the shifting loop
Each element moves left: arr[0]=20, arr[1]=30, arr[2]=40, arr[3]=50.Step 2: Set last element to 0
arr[4] is set to 0 explicitly.Final Answer:
20 30 40 50 0 -> Option AQuick Check:
Shift left + last zero = 20 30 40 50 0 [OK]
- Forgetting to set last element to zero
- Printing original array without shift
- Confusing shift direction
int[] arr = {1, 2, 3, 4};
for (int i = 0; i <= arr.length - 1; i++) {
arr[i] = arr[i + 1];
}Solution
Step 1: Analyze loop range
The loop runs i from 0 to arr.length - 1 (i.e., 0 to 3).Step 2: Identify out-of-bounds access
At i=3, arr[i+1] = arr[4] is accessed, which is outside array bounds causing exception.Final Answer:
ArrayIndexOutOfBoundsException at i = 3 -> Option BQuick Check:
Accessing arr[4] in length 4 array causes exception [OK]
- Using <= instead of < in loop condition
- Ignoring array length limits
- Assuming code runs without error
arr = {5, 10, 15, 20, 25}, write the output after deleting the first element twice using the standard shift-left method and setting the last element to zero each time.What is the final state of the array?
Solution
Step 1: First deletion
Shift left: arr becomes [10, 15, 20, 25, 0]Step 2: Second deletion
Shift left again: arr becomes [15, 20, 25, 0, 0]Final Answer:
[15, 20, 25, 0, 0] -> Option DQuick Check:
Two shifts left + zeros at end = [15, 20, 25, 0, 0] [OK]
- Deleting only once instead of twice
- Not setting last element to zero after shift
- Confusing order of elements after shifts
Ready to prove it?
Pass the test and this pattern moves unknown
