Array Deletion at Beginning in DSA C++ - Time & Space Complexity
When we delete an element from the start of an array, the time it takes depends on how many elements 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.
void deleteAtBeginning(int arr[], int &n) {
for (int i = 1; i < n; i++) {
arr[i - 1] = arr[i];
}
n--;
}
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.
- How many times: It runs (n - 1) times, where n is the array size.
As the array size grows, the number of shifts grows almost the same way.
| Input Size (n) | Approx. Operations (shifts) |
|---|---|
| 10 | 9 |
| 100 | 99 |
| 1000 | 999 |
Pattern observation: The operations increase roughly in a straight line with input 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 always fast and takes constant time."
[OK] Correct: Because all other elements must move to fill the gap, the work depends on the array size, not just one step.
Understanding this helps you explain why some data structures are better for front deletions, showing your grasp of efficiency.
"What if we used a linked list instead of an array? How would the time complexity of deleting the first element change?"
Practice
Solution
Step 1: Understand deletion at beginning
Deleting the first element means removing the element at index 0.Step 2: Effect on array elements
All other elements move one position left to fill the gap.Final Answer:
All elements shift one position to the left, and the first element is removed. -> Option AQuick Check:
Deletion at start shifts elements left [OK]
- Thinking elements shift right instead of left
- Assuming array size increases
- Believing array stays unchanged
arr of size n by shifting elements left?Solution
Step 1: Identify correct loop for shifting left
The loop must start at 0 and assign arr[i] = arr[i+1] to shift left.Step 2: Check array size update
After shifting, size decreases by 1 (n--).Final Answer:
for (int i = 0; i < n - 1; i++) arr[i] = arr[i + 1]; n--; -> Option CQuick Check:
Shift left loop from 0 to n-2, then n-- [OK]
- Looping backwards instead of forwards
- Increasing size instead of decreasing
- Copying elements incorrectly
int arr[5] = {10, 20, 30, 40, 50};
int n = 5;
for (int i = 0; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
n--;
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}Solution
Step 1: Perform element shifting
Elements shift left: arr[0]=20, arr[1]=30, arr[2]=40, arr[3]=50.Step 2: Update size and print
Size n becomes 4, so print arr[0] to arr[3]: 20 30 40 50.Final Answer:
20 30 40 50 -> Option BQuick Check:
First element removed, rest shifted left [OK]
- Printing original array without shift
- Printing one extra element
- Confusing order of elements
arr of size n:for (int i = 1; i < n; i++) {
arr[i] = arr[i - 1];
}
n--;Solution
Step 1: Analyze loop direction and assignment
The loop assigns arr[i] = arr[i - 1], shifting elements right, which is wrong for deletion at start.Step 2: Understand correct shifting for deletion
Deletion requires shifting elements left: arr[i] = arr[i + 1].Final Answer:
The loop shifts elements right instead of left, causing data overwrite. -> Option DQuick Check:
Deletion needs left shift, not right [OK]
- Shifting elements in wrong direction
- Not updating array size
- Starting loop at wrong index
arr of size n, you want to delete the first element multiple times until the array is empty. Which approach is the most efficient to handle this repeated deletion without shifting elements every time?Solution
Step 1: Understand cost of shifting on repeated deletions
Shifting elements left every time costs O(n) per deletion, inefficient for many deletions.Step 2: Use a start index pointer
Keep a start index that moves forward on each deletion, avoiding shifting.Step 3: Adjust size accordingly
Decrease size or track remaining elements using start index and size.Final Answer:
Use a variable to track the start index and increment it on each deletion. -> Option AQuick Check:
Track start index to avoid shifting [OK]
- Shifting elements every time causing slow code
- Creating new arrays repeatedly wasting memory
- Reversing array unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
