Best Time to Buy and Sell Stock
Easyjavapythonccppjavascript
Given daily stock prices, choose one day to buy and a later day to sell so that the profit is as large as possible.
Input Format
The first line contains an integer N. The second line contains N space-separated integers, where the i-th value is the price on day i.
Output Format
Print the maximum possible profit, or 0 if no profit can be made.
Example 1
Input
6 7 1 5 3 6 4
Output
5
Explanation: Buy at 1 and sell at 6 for a profit of 5.
- 1 <= N <= 100000
- 0 <= A[i] <= 1000000000