Array Deletion at End in DSA Java - Time & Space Complexity
We want to understand how long it takes to remove an item from the end of an array.
How does the time needed change as the array gets bigger?
Analyze the time complexity of the following code snippet.
// Remove last element from array
int[] arr = {1, 2, 3, 4, 5};
int size = arr.length;
// Deleting last element
size = size - 1;
// Now size is 4, last element ignored
This code reduces the size count to ignore the last element, effectively deleting it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single step to reduce size by one.
- How many times: Exactly once per deletion.
Removing the last element takes the same small step no matter how big the array is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The work stays the same even if the array grows larger.
Time Complexity: O(1)
This means deleting the last element takes a constant amount of time regardless of array size.
[X] Wrong: "Deleting an element always takes time proportional to the array size because we have to move elements."
[OK] Correct: When deleting at the end, no elements need to be moved; we just reduce the size count, so it takes constant time.
Knowing that deleting at the end is fast helps you choose the right data structure and operation in real problems.
"What if we changed deletion to remove the first element instead? How would the time complexity change?"
Practice
Solution
Step 1: Understand fixed-size array behavior
Java arrays have a fixed size once created, so their length cannot change.Step 2: Effect of deleting at end
Deleting means logically ignoring the last element or setting it to default (0 or null), but the array length remains unchanged.Final Answer:
The last element is set to zero or null but the array size stays the same -> Option BQuick Check:
Fixed-size array length unchanged on deletion [OK]
- Thinking array size shrinks automatically
- Assuming elements shift after deletion
- Believing array clears fully on one deletion
arr in Java by reducing its logical size stored in size?Solution
Step 1: Understand logical deletion
To delete last element, first reduce size by one, then clear that element.Step 2: Check syntax correctness
size--; arr[size] = 0; reduces size first (size--) then sets arr[size] to 0, correctly deleting last element.Final Answer:
size--; arr[size] = 0; -> Option CQuick Check:
Decrease size before clearing element [OK]
- Clearing element before decreasing size
- Using post-decrement incorrectly
- Increasing size instead of decreasing
int[] arr = {10, 20, 30, 40, 50};
int size = 5;
size--;
arr[size] = 0;
for(int i = 0; i < size; i++) {
System.out.print(arr[i] + " ");
}Solution
Step 1: Understand size decrement and element clearing
Size is reduced from 5 to 4, then arr[4] is set to 0, removing last element logically.Step 2: Loop prints elements from index 0 to 3
Loop runs i=0 to 3, printing arr[0]=10, arr[1]=20, arr[2]=30, arr[3]=40.Final Answer:
10 20 30 40 -> Option DQuick Check:
Last element removed, loop prints first 4 elements [OK]
- Printing all 5 elements ignoring size
- Not updating last element to zero
- Confusing array index with size
int[] arr = {1, 2, 3, 4};
int size = 4;
arr[size] = 0;
size--;Solution
Step 1: Check array indexing
Array indices go from 0 to size-1, so arr[size] is out of bounds.Step 2: Identify correct order
Must decrement size first, then access arr[size] to avoid error.Final Answer:
Array index out of bounds error at arr[size] = 0; -> Option AQuick Check:
Accessing arr[size] causes error [OK]
- Accessing arr[size] directly
- Not decrementing size before deletion
- Ignoring array index limits
int[] arr = {5, 10, 15, 20, 25} and a variable size = 5. You want to logically delete the last two elements without modifying the values in the array and print the remaining elements. Which code snippet correctly does this?Solution
Step 1: Reduce logical size by 2
Decreasing size by 2 means last two elements are ignored logically.Step 2: Print elements up to new size
Loop from 0 to size-1 prints remaining elements correctly.Final Answer:
size -= 2; for(int i = 0; i < size; i++) System.out.print(arr[i] + " "); -> Option AQuick Check:
Logical size controls printed elements [OK]
- Setting elements to zero before reducing size
- Printing without adjusting size
- Accessing invalid indices after deletion
Ready to prove it?
Pass the test and this pattern moves unknown
