Best Time to Buy and Sell Stock Problem Statement

Given an array prices representing the prices of a stock where each element indicates the price at a given minute, determine the maximum profit you can achieve by buying and selling the stock once.

Example:

Input:
prices = [2, 100, 150, 120]
Output:
148
Explanation:

The maximum profit can be achieved by buying at minute 0 for Rs. 2 and selling at minute 2 for Rs. 150, resulting in a profit of 150 - 2 = 148.

Constraints:

  • 1 <= T <= 10
  • 2 <= N <= 10^4
  • 1 <= prices[i] <= 10^9

Note: You cannot sell before buying.

If the maximum profit is negative, consider it as 0.

chamktasuraj
4mo
Why not sort and return the difference between first and last.
Help your peers!
Add answer anonymously...
Tower Research Capital LLC Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter