Array Deletion at End in DSA C - Time & Space Complexity
We want to understand how fast it is to remove an element 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.
void deleteAtEnd(int arr[], int *size) {
if (*size == 0) return; // no elements to delete
(*size)--;
}
This code removes the last element by reducing the size count of the array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Decreasing the size variable by one.
- How many times: Happens once per deletion call, no loops or recursion.
Removing the last element always 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 time stays constant regardless of array size.
Time Complexity: O(1)
This means deleting the last element takes the same short time no matter how big the array is.
[X] Wrong: "Deleting from the end takes longer as the array grows because we have to move many elements."
[OK] Correct: We do not move any elements when deleting at the end; we just reduce the size count, so time stays the same.
Knowing that deleting at the end is very fast helps you choose the right data structure and shows you understand how arrays work under the hood.
"What if we changed deletion to remove the first element instead? How would the time complexity change?"
Practice
Solution
Step 1: Understand array size after deletion
Deleting an element from the end means removing the last element, so the total number of elements reduces by one.Step 2: Effect on array size
The size variable or count of elements decreases by one to reflect the removal.Final Answer:
The size decreases by one -> Option AQuick Check:
Deleting last element reduces size by 1 [OK]
- Thinking size stays same after deletion
- Assuming size increases after deletion
- Confusing deletion with insertion
arr of size n in C?Solution
Step 1: Understand array deletion in C
In static arrays, deleting means reducing the size count. So, decreasingnby 1 removes last element logically.Step 2: Analyze options
n = n - 1; correctly reduces size. arr[n] = 0; tries to set element but doesn't change size. free(arr[n-1]); is invalid asfreeis for dynamic memory. arr = arr - 1; is invalid pointer arithmetic.Final Answer:
n = n - 1; -> Option CQuick Check:
Decrease size variable to delete last element [OK]
- Using free() on static array elements
- Trying to set last element to zero without changing size
- Incorrect pointer arithmetic
int arr[5] = {10, 20, 30, 40, 50};
int n = 5;
n = n - 1;
for(int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}Solution
Step 1: Understand size after deletion
Initially,nis 5. Aftern = n - 1;,nbecomes 4, so loop runs from 0 to 3.Step 2: Loop prints elements from index 0 to 3
Elements printed are arr[0]=10, arr[1]=20, arr[2]=30, arr[3]=40.Final Answer:
10 20 30 40 -> Option BQuick Check:
Loop runs n=4 times, prints first 4 elements [OK]
- Printing all 5 elements ignoring updated size
- Starting loop from 1 instead of 0
- Confusing array indices
arr of size n.void deleteEnd(int arr[], int n) {
arr[n] = 0;
n = n - 1;
}Solution
Step 1: Check array index usage
Array indices go from 0 to n-1. Accessing arr[n] is out of bounds and causes undefined behavior.Step 2: Understand deletion logic
To delete last element, reduce sizenby 1. Setting arr[n] to 0 is invalid and does not delete element.Final Answer:
Accessing arr[n] is out of bounds -> Option AQuick Check:
Array index must be less than size [OK]
- Using arr[n] instead of arr[n-1]
- Assuming setting element to zero deletes it
- Not updating size correctly
int *arr and current size n, which of the following correctly deletes the last element and updates the array size safely in C?Solution
Step 1: Understand dynamic array deletion
To delete last element, reduce sizenby 1 and resize memory usingreallocto avoid wasted space.Step 2: Analyze options
n = n - 1; arr = realloc(arr, n * sizeof(int)); correctly reduces size and resizes memory. arr[n-1] = 0; n = n - 1; sets element to zero but wastes memory. free(arr[n-1]); n = n - 1; incorrectly uses free on element. arr = arr - 1; n = n - 1; does invalid pointer arithmetic.Final Answer:
n = n - 1; arr = realloc(arr, n * sizeof(int)); -> Option DQuick Check:
Reduce size and resize memory with realloc [OK]
- Not resizing memory after deletion
- Using free() on array elements
- Incorrect pointer arithmetic
Ready to prove it?
Pass the test and this pattern moves unknown
