Code
Loading editor...
Tap Analyze to see visualization
Click Analyze to visualize
See step-by-step execution, variables, and output
Variables
Run code to see variables
Output
Console output will appear here
Click Analyze to visualize
See step-by-step execution, variables, and output
Run code to see variables
Console output will appear here
Sort people by weight. Use two pointers: pair the lightest person with the heaviest. If they fit together within the limit, both board one boat and both pointers move inward. Otherwise, the heaviest person takes a boat alone and only the right pointer moves.
Greedy pairing of lightest with heaviest maximizes space utilization. If the lightest cannot pair with the heaviest, no one else can pair with that heavy person either.
Updated Feb 2026