Preview:
// TC - O(n) || SC - O(1)
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n = prices.size() - 1, ans = 0, minSoFar = INT_MAX;
        for(int i = 0; i < n; ++i)  {
            minSoFar = min(minSoFar, prices[i]);
            ans = max(ans, (prices[i] - minSoFar));
        }
        return ans;
    }
};

// we can use auxilary array and store maxSoFar value
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter