Best Time to Buy & Sell Stock

00:00
EasyArraySliding WindowGreedy
AmazonInfosysWipro

You are given an array prices[i] = price on day i. Find the maximum profit from buying once and selling on a later day. Return 0 if no profit possible.

Examples

Input → [7,1,5,3,6,4]
Output → 5
Note: buy@1, sell@6
Input → [7,6,4,3,1]
Output → 0
Note: prices only fall
Input → [2,4,1,7]
Output → 6
Note: buy@1, sell@7