Array Deletion at Beginning in DSA C - Time & Space Complexity
When we delete an element from the start of an array, the computer must adjust the rest of the elements.
We want to know how the time needed changes as the array gets bigger.
Analyze the time complexity of the following code snippet.
void deleteAtBeginning(int arr[], int *size) {
for (int i = 0; i < *size - 1; i++) {
arr[i] = arr[i + 1];
}
(*size)--;
}
This code removes the first element by shifting all other elements one position left.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that shifts elements left.
- How many times: It runs once for each element except the first, so roughly n-1 times.
As the array size grows, the number of shifts grows almost the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 9 shifts |
| 100 | 99 shifts |
| 1000 | 999 shifts |
Pattern observation: The work grows roughly in a straight line with the number of elements.
Time Complexity: O(n)
This means the time to delete the first element grows directly with the array size.
[X] Wrong: "Deleting the first element is always fast and takes constant time."
[OK] Correct: Because the array needs to move all other elements to fill the gap, which takes time proportional to the array size.
Understanding this helps you explain why some data structures are better for certain operations, showing your grasp of efficiency.
"What if we deleted the last element instead of the first? How would the time complexity change?"
Practice
Solution
Step 1: Understand 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.Final Answer:
All elements shift one position to the left, and the first element is removed. -> Option BQuick 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 from index 0 and assign arr[i] = arr[i+1] up to n-2.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 from 0 to n-2, then reduce size [OK]
- Looping backwards instead of forwards
- Increasing size instead of decreasing
- Incorrect loop bounds causing overwrite
int arr[] = {10, 20, 30, 40, 50}; using the correct deletion method?Solution
Step 1: Remove first element 10
Deleting first element removes 10 from the array.Step 2: Shift remaining elements left
Elements 20, 30, 40, 50 shift left to fill index 0 to 3.Final Answer:
[20, 30, 40, 50] -> Option DQuick Check:
First element removed, others shifted left [OK]
- Leaving original first element in array
- Removing last element instead
- Reversing array order
for(int i = 1; i < n; i++) { arr[i-1] = arr[i]; } n--;What is the error in this code?
Solution
Step 1: Analyze loop start and assignments
The loop starts at i=1 and assigns arr[i-1] = arr[i], shifting elements left correctly.Step 2: Check array size update
Decreasing n by 1 after shifting is correct to reflect deletion.Final Answer:
The loop correctly shifts elements; no error present. -> Option AQuick Check:
Loop from 1 with arr[i-1]=arr[i] shifts left [OK]
- Thinking loop must start at 0 for shifting left
- Confusing assignment direction
- Incorrectly changing array size
int arr[] = {5, 10, 15, 20, 25, 30}; and size n = 6. You delete the first element twice using the correct deletion method. What is the resulting array and size?Solution
Step 1: First deletion removes 5
After first deletion, array becomes [10, 15, 20, 25, 30], size 5.Step 2: Second deletion removes 10
After second deletion, array becomes [15, 20, 25, 30], size 4.Final Answer:
Array: [15, 20, 25, 30], Size: 4 -> Option AQuick Check:
Two deletions remove first two elements, size reduces by 2 [OK]
- Not updating size after each deletion
- Deleting wrong elements
- Confusing array indexes after shifts
Ready to prove it?
Pass the test and this pattern moves unknown
