Hint: Use strict less-than in loop to avoid double counting [OK]
Common Mistakes:
Using <= causes overlap and wrong water sum
Changing comparison operators unnecessarily
Moving water calculation outside conditions
5. Given the height array {5, 2, 1, 2, 1, 5}, what is the total trapped rain water? Explain why the trapped water forms in this case.
hard
A. 10
B. 8
C. 14
D. 12
Solution
Step 1: Calculate trapped water at each position
Left max array: [5,5,5,5,5,5], Right max array: [5,5,5,5,5,5]. Water trapped = min(leftMax, rightMax) - height at each index: positions 1=3, 2=4, 3=3, 4=4.
Step 2: Sum trapped water and explain formation
Total trapped water = 3+4+3+4 = 14 units. Water forms because tall bars at ends trap water over lower bars inside.
Final Answer:
14 -> Option C
Quick Check:
Sum trapped water = 14 units [OK]
Hint: Use leftMax and rightMax arrays to find trapped water [OK]
Common Mistakes:
Ignoring max heights on both sides
Calculating trapped water incorrectly per position
Forgetting to sum all trapped units
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.