C++ Max Profit
#include <algorithm>
#include <vector>

long long maxProfit(const std::vector<int>& a)
{
    long long price = a[0];
    long long profit = 0;

    for (int v : a) {
        price = std::min(price, static_cast<long long>(v));
        profit = std::max(profit, v - price);
    }

    return profit;
}

This tracks the lowest buy price seen so far and updates the best profit as it scans the prices once.