80. Best Day to Buy and Sell Gold

Acceptance — 0 accepted 0 submissions

Easy
ArraysGreedy
No companies listed
Ritu tracked the price of 1 gram of gold for n days. She can buy once and sell once later (selling must happen after buying).

Find the maximum profit she can make. If no profit is possible, print 0.

Input Format
The first line has n.
The second line has n prices.

Output Format
Print the maximum profit.

Constraints
• 1 ≤ n ≤ 10^5
• 1 ≤ price ≤ 10^4

Example 1

Input
6
7 1 5 3 6 4
Output
5

Explanation Buy at 1 (day 2) and sell at 6 (day 5): profit 5.

Example 2

Input
5
7 6 4 3 1
Output
0

Explanation Prices only fall, so don't trade: 0.

Example 3

Input
2
1 2
Output
1

Explanation Buy at 1, sell at 2.

Acceptance —
Accepted 0
Submissions 0
Auto-save
Saved Ln 1, Col 1
Submit a solution to see its status here.
Input
Expected Output
5
No results yet
Run your code to test it against the sample cases.
Problem List