Java Max Profit
public class Solution {
    public static int maxProfit(int[] a) {
        int price = a[0];
        int profit = 0;

        for (int v : a) {
            price = Math.min(price, v);
            profit = Math.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.