Stock Buy Sell to Maximize Profit

Given a list of stock prices, find the maximum profit with a single buy or sell activity.

Statement

We’re given an array of daily stock prices (integers for simplicity). Return the buying and selling prices for making the maximum profit.

The values in the array represent the cost of stock each day. As we can buy and sell the stock only once, we need to find the best buy and sell prices that maximize profit (or minimized loss) over a given span of time.

We need to maximize the profit from a single buy and sell. If we can’t make any profit, we’ll try to minimize the loss.

Example

For the below examples, the buying and selling prices for making a maximum profit are highlighted:

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.