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
Scan prices left to right, tracking the minimum price seen so far. At each day, calculate the profit if selling at the current price. Update the maximum profit whenever a better profit is found. The minimum price resets whenever a new low is encountered.
The optimal buy day is always the lowest price before the sell day. By tracking the running minimum, each potential sell day is compared against the best possible buy price.
Updated Feb 2026