Bird
Raised Fist0
DSA C++programming~10 mins

Array Deletion at Middle Index in DSA C++ - Execution Trace

Choose your learning style10 modes available
Concept Flow - Array Deletion at Middle Index
Start with array
↓
Identify index to delete
↓
Shift elements left from index+1 to end
↓
Reduce array size by 1
↓
Resulting array without deleted element
We start with an array, find the index to delete, shift all elements after it one step left, then reduce the array size by one.
Execution Sample
DSA C++
int arr[] = {10, 20, 30, 40, 50};
int n = 5;
int delIndex = 2;
for (int i = delIndex; i < n - 1; i++) {
    arr[i] = arr[i + 1];
}
n--;
This code deletes the element at index 2 (value 30) by shifting elements left and reducing size.
Execution Table
StepOperationIndex iArray StatePointer ChangesVisual State
1Start-[10, 20, 30, 40, 50]delIndex=2, n=510 -> 20 -> 30 -> 40 -> 50 -> null
2Shift element at i=22[10, 20, 40, 40, 50]arr[2] = arr[3]10 -> 20 -> 40 -> 40 -> 50 -> null
3Shift element at i=33[10, 20, 40, 50, 50]arr[3] = arr[4]10 -> 20 -> 40 -> 50 -> 50 -> null
4Reduce size-[10, 20, 40, 50]n = 410 -> 20 -> 40 -> 50 -> null
5End-[10, 20, 40, 50]n=410 -> 20 -> 40 -> 50 -> null
💡 Loop ends when i reaches n-1; array size reduced by 1 to exclude last duplicate.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
arr[10,20,30,40,50][10,20,40,40,50][10,20,40,50,50][10,20,40,50][10,20,40,50]
n55544
delIndex22222
i-23--
Key Moments - 3 Insights
Why do we shift elements starting from the deletion index?
Because we overwrite the element at the deletion index with the next element to remove the gap, as shown in execution_table steps 2 and 3.
Why do we reduce the array size after shifting?
Because the last element becomes duplicated after shifting, so reducing size excludes the duplicate, as shown in step 4.
What happens if we delete the last element (index n-1)?
No shifting occurs because the loop condition i < n-1 fails immediately; we just reduce size by 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the array state after step 3?
A[10, 20, 30, 40, 50]
B[10, 20, 40, 50, 50]
C[10, 20, 40, 40, 50]
D[10, 20, 40, 50]
💡 Hint
Check the 'Array State' column at step 3 in execution_table.
At which step is the array size reduced?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for 'Reduce size' operation in execution_table.
If delIndex was 4 (last element), how many shift operations occur?
A2
B1
C0
D3
💡 Hint
Refer to the loop condition i < n-1 and what happens when i equals n-1.
Concept Snapshot
Array Deletion at Middle Index:
- Identify index to delete.
- Shift elements left from index+1 to end.
- Reduce array size by 1.
- No shifting if deleting last element.
- Result: array without deleted element.
Full Transcript
This concept shows how to delete an element from the middle of an array. We find the index to delete, then shift all elements after it one position to the left to overwrite the deleted element. After shifting, we reduce the array size by one to remove the duplicate last element. If the deleted element is the last one, no shifting happens, only size reduction. The execution table traces each step, showing how the array changes and how pointers move. The variable tracker follows the array and index values through the process. Key moments clarify why shifting is needed and when size is reduced. The visual quiz tests understanding of array states and operations during deletion.

Practice

(1/5)
1. What happens when you delete the middle element from an array of size 5?
int arr[5] = {10, 20, 30, 40, 50};
Delete the element at index 2 (middle). What is the new array?
easy
A. [20, 30, 40, 50]
B. [10, 20, 30, 40, 50]
C. [10, 20, 30, 50]
D. [10, 20, 40, 50]

Solution

  1. Step 1: Identify the middle index and element

    The array size is 5, so middle index is 2. The element at index 2 is 30.
  2. Step 2: Delete the middle element and shift remaining elements

    Remove 30, then shift elements after index 2 left by one: 40 moves to index 2, 50 moves to index 3.
  3. Final Answer:

    [10, 20, 40, 50] -> Option D
  4. Quick Check:

    Deleted middle element 30, shifted left [OK]
Hint: Remove middle, shift right elements left by one [OK]
Common Mistakes:
  • Not shifting elements after deletion
  • Deleting wrong index
  • Leaving array size unchanged
2. Which of the following C++ code snippets correctly deletes the middle element from an array arr of size n?
easy
A. for(int i = n/2; i < n-1; i++) arr[i] = arr[i+1]; n--;
B. for(int i = 0; i < n/2; i++) arr[i] = arr[i+1]; n--;
C. arr[n/2] = 0; n--;
D. for(int i = n-1; i > n/2; i--) arr[i] = arr[i-1]; n--;

Solution

  1. Step 1: Understand deletion logic

    To delete middle element at index n/2, shift all elements after it one position left.
  2. Step 2: Check each code snippet

    for(int i = n/2; i < n-1; i++) arr[i] = arr[i+1]; n--; correctly shifts elements from middle to end left by one and reduces size. Others either shift wrong elements or just set zero without shifting.
  3. Final Answer:

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

    Shift elements after middle left [OK]
Hint: Shift elements from middle index to end left [OK]
Common Mistakes:
  • Shifting elements before middle index
  • Not reducing array size
  • Assigning zero instead of shifting
3. What is the output of the following C++ code?
int arr[6] = {5, 10, 15, 20, 25, 30};
int n = 6;
int mid = n / 2;
for(int i = mid; i < n - 1; i++) {
  arr[i] = arr[i + 1];
}
n--;
for(int i = 0; i < n; i++) {
  std::cout << arr[i] << " ";
}
medium
A. 5 10 15 25 30
B. 5 10 15 20 25 30
C. 5 10 15 20 30
D. 5 10 20 25 30

Solution

  1. Step 1: Calculate middle index and delete element

    n=6, mid=3. Element at index 3 is 20. We delete 20 by shifting elements after index 3 left.
  2. Step 2: Shift elements and print array

    Shift arr[3]=arr[4]=25, arr[4]=arr[5]=30. New array: 5,10,15,25,30. Size reduced to 5.
  3. Final Answer:

    5 10 15 25 30 -> Option A
  4. Quick Check:

    Deleted 20, shifted left [OK]
Hint: Shift elements after middle left, reduce size by one [OK]
Common Mistakes:
  • Not reducing array size
  • Printing old array size
  • Not shifting elements correctly
4. Find the error in this code that tries to delete the middle element from array arr of size n:
int mid = n / 2;
for(int i = mid; i <= n - 1; i++) {
  arr[i] = arr[i + 1];
}
n--;
medium
A. Array size n should not be decremented
B. Middle index calculation is wrong
C. Loop condition should be i < n - 1, not i <= n - 1
D. Loop should start from 0, not mid

Solution

  1. Step 1: Analyze loop range for shifting elements

    The loop runs from mid to n-1 inclusive, so arr[i+1] accesses arr[n], which is out of bounds.
  2. Step 2: Correct loop condition

    Loop should run while i < n-1 to avoid accessing beyond array size.
  3. Final Answer:

    Loop condition should be i < n - 1, not i <= n - 1 -> Option C
  4. Quick Check:

    Prevent out-of-bounds by correct loop limit [OK]
Hint: Loop till one before last index to avoid overflow [OK]
Common Mistakes:
  • Using <= instead of < in loop condition
  • Not decrementing array size
  • Wrong middle index calculation
5. Given an array arr of size n, write the logic to delete the middle element and return the new array size. What will be the size after deletion if n is even?
Choose the correct approach.
hard
A. Delete element at index (n-1)/2, shift elements left, new size = n - 1
B. Delete element at index n/2, shift elements left, new size = n - 1
C. Delete element at index n/2, no shifting needed, new size = n - 1
D. Delete element at index n/2 - 1, shift elements left, new size = n - 1

Solution

  1. Step 1: Understand middle index for even-sized array

    In standard array operations, middle index is n/2 (integer division), consistent with odd sizes.
  2. Step 2: Delete element at n/2 and shift elements left

    Remove element at index n/2, shift all elements after it left by one, reduce size by 1.
  3. Final Answer:

    Delete element at index n/2, shift elements left, new size = n - 1 -> Option B
  4. Quick Check:

    Middle index = n/2 for even n [OK]
Hint: Use n/2 as middle index regardless of parity [OK]
Common Mistakes:
  • Deleting lower middle index (n/2 - 1) for even n
  • Not shifting elements after deletion
  • Not reducing array size
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