Search⌘ K
AI Features

DIY: Best Time to Buy and Sell Stock

Explore how to design an algorithm that calculates the maximum profit from one buy and one sell stock transaction. This lesson helps you implement solutions that track stock prices daily and determine the best times to buy and sell for profit using Kotlin.

Problem statement

You have an array for which the ith element is the price of a given stock on day i.

If you are only permitted to complete one transaction at most (e.g, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note: You cannot sell a stock before you buy one. ...

Input

{7,1,5,3,6,4}