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
Objective:
To determine the maximum profit that can be achieved from buying and selling stock on given days, where the price of the stock is provided as an array.
Use a for loop to iterate through the array of stock prices.
minSoFar from the current stock price.maxProfit, update maxProfit with the new potential profit.Additionally, update minSoFar to be the minimum of its current value and the current stock price. This ensures that you always have the lowest price encountered up to the current day.
maxProfit will hold the maximum profit achievable by buying and selling the stock.Input:
prices = [7, 1, 5, 3, 6, 4]
Output:
Maximum Profit = 5
Explanation:
Input:
prices = [7, 6, 4, 3, 1]
Output:
Maximum Profit = 0
Explanation:
Input:
prices = [1, 2, 3, 4, 5]
Output:
Maximum Profit = 4
Explanation:
Input:
prices = [3, 2, 6, 5, 0, 3]
Output:
Maximum Profit = 4
Explanation:
def maxProfit(prices):
maxProfit = 0
minSoFar = prices[0]
for price in prices:
maxProfit = max(maxProfit, price - minSoFar)
minSoFar = min(minSoFar, price)
return maxProfit
🎯 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.