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; // nothing to delete
size = size - 1; // reduce size by one
}
This code removes the last element by simply 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: This operation happens once per deletion.
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 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 at the end takes longer as the array grows because we have to move elements."
[OK] Correct: We do not move any elements when deleting at the end; we just reduce the size count, so time stays constant.
Knowing that deleting at the end is very fast helps you choose the right data structure and operation in real projects and interviews.
"What if we changed to deleting an element at the start of the array? How would the time complexity change?"
Practice
Solution
Step 1: Understand dynamic array size behavior
When an element is deleted from the end, the array size reduces by one because one element is removed.Step 2: Confirm size change after deletion
Deleting from the end does not affect other elements, only reduces size by one.Final Answer:
The size decreases by one -> Option AQuick Check:
Size after deletion = original size - 1 [OK]
- Thinking size remains same after deletion
- Assuming size increases after deletion
- Confusing deletion at end with deletion at start
vec in C++?Solution
Step 1: Recall vector deletion syntax
The standard method to remove the last element from a vector in C++ ispop_back().Step 2: Verify other options
Other methods likedelete_last(),remove_end(), anderase_end()do not exist in C++ STL vector.Final Answer:
vec.pop_back(); -> Option DQuick Check:
Use pop_back() to delete last element [OK]
- Using non-existent methods like delete_last()
- Confusing erase() with erase_end()
- Trying to use remove_end() which is invalid
std::vector<int> v = {10, 20, 30, 40};
v.pop_back();
for(int x : v) std::cout << x << " ";Solution
Step 1: Understand pop_back effect on vector
pop_back() removes the last element, which is 40 in this vector.Step 2: Print remaining elements
After removal, vector contains 10, 20, 30. Loop prints these with spaces.Final Answer:
10 20 30 -> Option CQuick Check:
Last element removed, output = 10 20 30 [OK]
- Printing original vector without deletion
- Confusing which element is removed
- Forgetting pop_back() modifies vector
int arr[3] = {1, 2, 3};
arr.pop_back();Solution
Step 1: Check array type and methods
Built-in arrays in C++ do not have member functions like pop_back().Step 2: Understand correct usage
pop_back() is a method of std::vector, not raw arrays.Final Answer:
pop_back() is not a member of built-in arrays -> Option BQuick Check:
pop_back() only works with vectors, not arrays [OK]
- Assuming pop_back() works on raw arrays
- Ignoring that arrays have fixed size
- Thinking pop_back() deletes first element
v with elements {5, 10, 15, 20, 25}. You want to delete the last two elements and then print the vector. Which code snippet correctly does this?Solution
Step 1: Understand methods to delete multiple elements
pop_back()removes only one element at a time. To remove two, it must be called twice.erase()can remove a range efficiently.Step 2: Analyze the snippets
The snippetv.erase(v.end()-2, v.end()); for(auto x : v) std::cout << x << ' ';correctly removes the last two elements. The snippet with a singlev.pop_back();removes only one element.remove_end()does not exist.pop_back(2)uses invalid syntax.Final Answer:
v.erase(v.end()-2, v.end()); for(auto x : v) std::cout << x << ' '; -> Option AQuick Check:
erase() with range removes last two elements efficiently [OK]
- Using pop_back() only once instead of twice
- Using non-existent remove_end() method
- Not knowing erase() can remove ranges
Ready to prove it?
Pass the test and this pattern moves unknown
