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
Insert all numbers into a Set for O(1) lookup. For each number, check if it is the start of a sequence (num - 1 not in Set). If so, count consecutive numbers upward. Track the longest sequence found across all starting points.
Only starting sequence exploration from true sequence starts (where num - 1 is absent) ensures each element is visited at most twice total, achieving O(n) despite the nested while loop.
Updated Feb 2026