Complete the code to initialize the total trapped water variable.
int totalWater = [1];We start counting trapped water from zero because initially no water is trapped.
Complete the code to create an array to store the maximum height to the left of each bar.
int[] leftMax = new int[[1]];The leftMax array must have the same length as the height array to store max heights for each position.
Fix the error in the loop that fills the leftMax array with the maximum height to the left of each bar.
for (int i = 1; i < height.Length; i++) { leftMax[i] = Math.Max(leftMax[i - 1], [1]); }
We compare the previous max with the current bar's height to update leftMax.
Fill both blanks to calculate trapped water at each bar using leftMax and rightMax arrays.
for (int i = 0; i < height.Length; i++) { int waterLevel = Math.Min([1], [2]); if (waterLevel > height[i]) { totalWater += waterLevel - height[i]; } }
The water trapped depends on the smaller of the max heights on left and right sides.
Fill all three blanks to create the rightMax array by iterating from the end and updating max heights.
int[] rightMax = new int[height.Length]; rightMax[height.Length - 1] = height[height.Length - 1]; for (int i = height.Length - 2; i >= 0; i--) { rightMax[i] = Math.[1](rightMax[i + 1], [2]); } // Return the total trapped water return [3];
We use Math.Max to update rightMax with the max height seen so far from the right. Finally, we return totalWater.
