Best Time to Buy and Sell Stock

Easy
ArrayGreedyDynamic ProgrammingAmazonMicrosoft
Given daily stock prices, choose one day to buy and a later day to sell. Print the maximum possible profit. If no profitable trade exists, print 0. Input format: - First line: n - Second line: n prices

Constraints

1 <= n <= 10^5
0 <= prices[i] <= 10^9

Sample tests

Sample 1

Sample 2

Sign in to submit