For example, consider the prices = [7, 1, 5, 3, 6, 4]. The optimal profit can be obtained by purchasing on the second day when the price is 1 and selling on the fifth day for 6, resulting in a profit of 5. Here’s a method to tackle this problem: Start by initializing two variables: minPrice = Infinity and maxProfit = 0. Loop through the price array from the beginning to the end. For each price, update minPrice to be the lesser of minPrice and the current_price, and update maxProfit to be the greater of maxProfit and (current_price - minPrice). Finally, return maxProfit. This approach ensures that you buy at the lowest price and sell at the highest price possible after the purchase.