How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focus—or scroll down to preview all options.
Visualize the algorithm step-by-step with interactive animations in real time.
Read the full explanation, examples, and starter code at your own pace.
Drag and arrange the algorithm steps in the correct execution order.
🧠 Select Active to activate
Follow every state change, comparison, and transformation as the execution unfolds in real time.
📖 Select Passive to activate
Given an array height representing the height of blocks, calculate how much water can be trapped after rainfall. Water can only be trapped between blocks if there is a taller block on both the left and the right of the current block.
Find the total amount of water that can be trapped between the blocks.
For each block, the water it can trap is determined by:
min(leftMax, rightMax) - height[i]
Where:
leftMax is the maximum height of blocks to the left of the current block.rightMax is the maximum height of blocks to the right of the current block.leftMax.rightMax.i as:water[i] = min(leftMax[i], rightMax[i]) - height[i]height = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
[0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3]
[3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 1]
[0, 0, 1, 0, 1, 2, 1, 0, 0, 1, 0, 0]
6
Output:
6
Input:
height = [4, 2, 0, 3, 2, 5]
Output:
9
Explanation:
Water trapped at each block: [0, 2, 4, 1, 2, 0]
Total water trapped = 9.
Input:
height = [1, 0, 2]
Output:
1
Explanation:
Water trapped at each block: [0, 1, 0]
Total water trapped = 1.
Input:
height = [0, 1, 0, 1, 0]
Output:
0
Explanation:
No water is trapped because no block has taller blocks on both sides.
Input:
height = [3, 0, 0, 2, 0, 4]
Output:
10
Explanation:
Water trapped at each block: [0, 3, 3, 1, 3, 0]
Total water trapped = 10.
1 <= height.length <= 10^50 <= height[i] <= 10^4leftMax, rightMax, and total water trapped.leftMax and rightMax arrays.For an optimized version, we can reduce space complexity to O(1) using two-pointer technique.
— Written by Saurabh Patil • B.Tech CSE • Software Developer🎯 Select Challenge to activate
Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.
The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.
Understand Below AlgorithmGreen text means the instruction is placed in the correct position.
Red text means the instruction is in the wrong position.
Instructions with the same background color indicate particular blocks start and end.
A tick mark means the instruction is correct and locked.
🔒 Locked steps cannot be moved. Only unlocked steps are draggable.
🔊 Enable sound for swap feedback and completion effects.