Bird
Raised Fist0
DSA C++programming~10 mins

Array Deletion at Beginning in DSA C++ - Execution Trace

Choose your learning style10 modes available
Concept Flow - Array Deletion at Beginning
Start with array
↓
Delete first element
↓
Shift all elements left by one
↓
Reduce array size by one
↓
End with updated array
The process removes the first element, shifts all others left, and reduces the array size.
Execution Sample
DSA C++
int arr[] = {10, 20, 30, 40};
int n = 4;
// Delete first element
for (int i = 1; i < n; i++) {
  arr[i-1] = arr[i];
}
n--;
// Now arr = {20, 30, 40}, n = 3
This code deletes the first element of the array by shifting all elements left and decreasing size.
Execution Table
StepOperationArray StatePointer ChangesVisual State
0Start[10, 20, 30, 40]n=410 -> 20 -> 30 -> 40 -> null
1Delete first element: shift arr[1] to arr[0][20, 20, 30, 40]i=120 -> 20 -> 30 -> 40 -> null
2Shift arr[2] to arr[1][20, 30, 30, 40]i=220 -> 30 -> 30 -> 40 -> null
3Shift arr[3] to arr[2][20, 30, 40, 40]i=320 -> 30 -> 40 -> 40 -> null
4Decrease size n by 1[20, 30, 40]n=320 -> 30 -> 40 -> null
5End[20, 30, 40]n=320 -> 30 -> 40 -> null
💡 Loop ends when i reaches n; size reduced to 3 to exclude last duplicate element
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
arr[10, 20, 30, 40][20, 20, 30, 40][20, 30, 30, 40][20, 30, 40, 40][20, 30, 40, 40][20, 30, 40]
n444433
iundefined123loop endsloop ends
Key Moments - 3 Insights
Why do we shift elements left instead of just removing the first element?
Because arrays have fixed positions, removing the first element leaves a gap. Shifting left fills that gap, as shown in steps 1 to 3 in the execution_table.
Why do we reduce the size variable 'n' after shifting?
The size 'n' tracks how many valid elements are in the array. After shifting, the last element is duplicated, so reducing 'n' excludes the extra copy, as in step 4.
What happens if the array is empty or size is zero?
The loop does not run because the condition i < n fails immediately, so no shifting occurs and the array remains empty.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 2, what is the array state?
A[10, 20, 30, 40]
B[20, 20, 30, 40]
C[20, 30, 30, 40]
D[20, 30, 40, 40]
💡 Hint
Check the 'Array State' column at Step 2 in the execution_table.
At which step does the size 'n' decrease?
AStep 4
BStep 3
CStep 1
DStep 5
💡 Hint
Look at the 'Pointer Changes' column for 'n' value changes in the execution_table.
If the initial array was empty (n=0), what would happen in the execution_table?
AThe loop runs once and shifts elements
BThe loop does not run and array stays empty
CThe size 'n' increases
DThe array gets filled with zeros
💡 Hint
Refer to the key_moments explanation about empty arrays and loop conditions.
Concept Snapshot
Array Deletion at Beginning:
- Remove first element by shifting all elements left
- Use a loop from i=1 to n-1
- Assign arr[i-1] = arr[i]
- Decrease size n by 1
- Result: array size shrinks, first element removed
Full Transcript
This visual trace shows how to delete the first element of an array by shifting all elements left one position and reducing the size. Starting with array [10, 20, 30, 40] and size 4, each step moves elements left: arr[1] to arr[0], arr[2] to arr[1], arr[3] to arr[2]. After shifting, size n is decreased to 3 to exclude the duplicate last element. The final array is [20, 30, 40]. Key points include why shifting is needed to avoid gaps and why size must be reduced. If the array is empty, no shifting occurs. The quiz questions check understanding of array states at steps, size changes, and empty array behavior.

Practice

(1/5)
1. What happens to the elements of an array when you delete the first element (at the beginning)?
easy
A. All elements shift one position to the left, and the first element is removed.
B. All elements shift one position to the right, and the last element is removed.
C. The array size increases by one.
D. The array remains unchanged.

Solution

  1. Step 1: Understand deletion at beginning

    Deleting the first element means removing the element at index 0.
  2. Step 2: Effect on array elements

    All other elements move one position left to fill the gap.
  3. Final Answer:

    All elements shift one position to the left, and the first element is removed. -> Option A
  4. Quick Check:

    Deletion at start shifts elements left [OK]
Hint: Deleting start means shift left all elements [OK]
Common Mistakes:
  • Thinking elements shift right instead of left
  • Assuming array size increases
  • Believing array stays unchanged
2. Which of the following C++ code snippets correctly deletes the first element of an array arr of size n by shifting elements left?
easy
A. for (int i = 1; i < n; i++) arr[i] = arr[i - 1]; n--;
B. for (int i = n - 1; i > 0; i--) arr[i] = arr[i - 1]; n++;
C. for (int i = 0; i < n - 1; i++) arr[i] = arr[i + 1]; n--;
D. for (int i = 0; i < n; i++) arr[i] = arr[i]; n--;

Solution

  1. Step 1: Identify correct loop for shifting left

    The loop must start at 0 and assign arr[i] = arr[i+1] to shift left.
  2. Step 2: Check array size update

    After shifting, size decreases by 1 (n--).
  3. Final Answer:

    for (int i = 0; i < n - 1; i++) arr[i] = arr[i + 1]; n--; -> Option C
  4. Quick Check:

    Shift left loop from 0 to n-2, then n-- [OK]
Hint: Shift left from i=0 to n-2, then reduce size [OK]
Common Mistakes:
  • Looping backwards instead of forwards
  • Increasing size instead of decreasing
  • Copying elements incorrectly
3. What is the output of the following C++ code?
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] << " ";
}
medium
A. 50 40 30 20
B. 20 30 40 50
C. 10 20 30 40 50
D. 30 40 50

Solution

  1. Step 1: Perform element shifting

    Elements shift left: arr[0]=20, arr[1]=30, arr[2]=40, arr[3]=50.
  2. Step 2: Update size and print

    Size n becomes 4, so print arr[0] to arr[3]: 20 30 40 50.
  3. Final Answer:

    20 30 40 50 -> Option B
  4. Quick Check:

    First element removed, rest shifted left [OK]
Hint: After deletion, print first n-1 elements shifted left [OK]
Common Mistakes:
  • Printing original array without shift
  • Printing one extra element
  • Confusing order of elements
4. Identify the error in the following code that attempts to delete the first element of an array arr of size n:
for (int i = 1; i < n; i++) {
    arr[i] = arr[i - 1];
}
n--;
medium
A. The loop should start from i = 0 and go to n - 1.
B. The loop correctly shifts elements left, no error.
C. The array size should increase, not decrease.
D. The loop shifts elements right instead of left, causing data overwrite.

Solution

  1. 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.
  2. Step 2: Understand correct shifting for deletion

    Deletion requires shifting elements left: arr[i] = arr[i + 1].
  3. Final Answer:

    The loop shifts elements right instead of left, causing data overwrite. -> Option D
  4. Quick Check:

    Deletion needs left shift, not right [OK]
Hint: Deletion shifts left; right shift is wrong [OK]
Common Mistakes:
  • Shifting elements in wrong direction
  • Not updating array size
  • Starting loop at wrong index
5. Given an array 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?
hard
A. Use a variable to track the start index and increment it on each deletion.
B. Shift all elements left on every deletion and decrease size.
C. Create a new array each time with one less element.
D. Reverse the array and delete from the end.

Solution

  1. Step 1: Understand cost of shifting on repeated deletions

    Shifting elements left every time costs O(n) per deletion, inefficient for many deletions.
  2. Step 2: Use a start index pointer

    Keep a start index that moves forward on each deletion, avoiding shifting.
  3. Step 3: Adjust size accordingly

    Decrease size or track remaining elements using start index and size.
  4. Final Answer:

    Use a variable to track the start index and increment it on each deletion. -> Option A
  5. Quick Check:

    Track start index to avoid shifting [OK]
Hint: Track start index to delete efficiently [OK]
Common Mistakes:
  • Shifting elements every time causing slow code
  • Creating new arrays repeatedly wasting memory
  • Reversing array unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard