Bird
0
0
DSA Cprogramming~10 mins

Array Deletion at Middle Index in DSA C - Execution Trace

Choose your learning style9 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
This flow shows deleting an element by shifting all elements after it one step left, then reducing the array size.
Execution Sample
DSA C
int arr[] = {10, 20, 30, 40, 50};
int n = 5;
int del_index = 2;
for (int i = del_index; i < n - 1; i++) {
    arr[i] = arr[i + 1];
}
n--;
Deletes element at index 2 by shifting elements left and reducing size.
Execution Table
StepOperationIndex iArray StateArray Size
0Start-[10, 20, 30, 40, 50]5
1Delete at index 22[10, 20, 40, 40, 50]5
2Shift element from index 3 to 23[10, 20, 40, 50, 50]5
3Loop exits: i=4 >= n-14[10, 20, 40, 50, 50]5
4Reduce size by 1-[10, 20, 40, 50]4
💡 Array size reduced to 4, element at index 2 deleted by shifting elements left.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
arr[10, 20, 30, 40, 50][10, 20, 40, 40, 50][10, 20, 40, 50, 50][10, 20, 40, 50, 50][10, 20, 40, 50]
n55554
i-234-
Key Moments - 3 Insights
Why do we shift elements left after the deleted index?
Because arrays have fixed positions, to remove an element we move all elements after it one step left to fill the gap, as shown in steps 1 to 3 in the execution_table.
Why do we reduce the array size after shifting?
The last element becomes duplicated after shifting, so we reduce the size by 1 to ignore the extra element, as shown in step 4.
What happens if we delete the last element (index n-1)?
No shifting is needed; we just reduce the size by 1. This is a special case not shown here but follows the same logic.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the array state after step 2?
A[10, 20, 40, 40, 50]
B[10, 20, 30, 40, 50]
C[10, 20, 40, 50, 50]
D[10, 20, 30, 50, 50]
💡 Hint
Check the 'Array State' column at step 2 in the execution_table.
At which step does the array size reduce?
AStep 1
BStep 4
CStep 3
DStep 2
💡 Hint
Look at the 'Array Size' column in the execution_table.
If we delete index 4 instead of 2, how many shifts happen?
A0 shifts
B2 shifts
C1 shift
D3 shifts
💡 Hint
Deleting the last element requires no shifting, only size reduction.
Concept Snapshot
Array Deletion at Middle Index:
- Identify index to delete.
- Shift all elements after index one step left.
- Reduce array size by 1.
- No gaps remain; array stays contiguous.
- Deleting last element skips shifting.
Full Transcript
This concept shows how to delete an element from the middle of an array. We start with the array and the index to delete. Then we shift every element after that index one position to the left to fill the gap. Finally, we reduce the array size by one to remove the duplicate last element. This keeps the array contiguous without empty spaces. The execution table shows each step of shifting and the array state. Key moments explain why shifting is needed and what happens when deleting the last element. The quiz tests understanding of array states and size changes during deletion.