Step 1: i=0: Push index 0 (height 0) onto stack
Stack: [0]
Water trapped: 0
Why: Start with first bar index to compare with next bars
Step 2: i=1 (height 1) > height[0]=0, pop index 0
Popped index 0, stack now empty
Why: Current bar taller than top of stack
Step 3: Stack empty after pop, no water calculation, push index 1
Stack: [1]
Water trapped: 0
Why: No left boundary for water trapping
Step 4: i=2 (height 0) < height[1]=1, push index 2
Stack: [1,2]
Water trapped: 0
Why: Current bar shorter, keep for possible water trapping
Step 5: i=3 (height 2) > height[2]=0, pop index 2
Popped index 2, stack now [1]
Why: Found right boundary taller than top bar
Step 6: Calculate distance = 3 - 1 - 1 = 1, bounded height = min(2,1) - 0 = 1, water += 1
Water trapped: 1
Why: Water trapped between bars at indices 1 and 3 above index 2
Step 7: Still height[3]=2 > height[1]=1, pop index 1
Popped index 1, stack now empty
Why: Continue popping while current is taller
Step 8: Stack empty, no more calculation, push index 3
Stack: [3]
Water trapped: 1
Why: Add new taller bar index for future trapping
Step 9: i=4 (height 1) < height[3]=2, push index 4
Stack: [3,4]
Water trapped: 1
Why: Keep shorter bar for possible trapping
Step 10: i=5 (height 0) < height[4]=1, push index 5
Stack: [3,4,5]
Water trapped: 1
Why: Keep shorter bar for possible trapping
Step 11: i=6 (height 1) > height[5]=0, pop index 5
Popped index 5, stack now [3,4]
Why: Found right boundary taller than top bar
Step 12: Calculate distance = 6 - 4 - 1 = 1, bounded height = min(1,1) - 0 = 1, water += 1
Water trapped: 2
Why: Water trapped between bars at indices 4 and 6 above index 5
Step 13: height[6]=1 == height[4]=1, no more pop, push index 6
Stack: [3,4,6]
Water trapped: 2
Why: Condition not met for popping index 4
Step 14: i=7 (height 3) > height[6]=1, pop index 6
Popped index 6, stack now [3,4]
Why: Current bar taller than top
Step 15: Calculate distance = 7 - 4 - 1 = 2, bounded height = min(3,1) - 1 = 0, water += 0
Water trapped: 2
Why: No water as bounded height is zero
Step 16: Still 3 > height[4]=1, pop index 4
Popped index 4, stack now [3]
Why: Continue popping smaller bars
Step 17: Calculate distance = 7 - 3 - 1 = 3, bounded height = min(3,2) - 1 = 1, water += 3
Water trapped: 5
Why: Water trapped between bars at indices 3 and 7 above indices 4 and 6
Step 18: Still 3 > height[3]=2, pop index 3
Popped index 3, stack now empty
Step 19: Stack empty, no calculation, push index 7
Stack: [7]
Water trapped: 5
Why: Add current tall bar
Step 20: i=8 (height 2) < height[7]=3, push index 8
Stack: [7,8]
Water trapped: 5
Why: Shorter bar, push for potential trapping
Step 21: i=9 (height 1) < height[8]=2, push index 9
Stack: [7,8,9]
Water trapped: 5
Step 22: i=10 (height 2) > height[9]=1, pop index 9
Popped index 9, stack now [7,8]
Step 23: Calculate distance = 10 - 8 - 1 = 1, bounded height = min(2,2) - 1 = 1, water += 1
Water trapped: 6
Why: Water trapped between 8 and 10 above 9
Step 24: height[10]=2 == height[8]=2, no pop, push index 10
Stack: [7,8,10]
Water trapped: 6
Step 25: i=11 (height 1) < height[10]=2, push index 11
Stack: [7,8,10,11]
Water trapped: 6
Why: End of array, final state
Result: Final trapped water = 6 units