Java Max Product Of Three
import java.util.Arrays;

public class Solution {
    public static long maxProductOfThree(int[] a) {
        int[] sorted = a.clone();
        Arrays.sort(sorted);
        int c = sorted.length;

        long p1 = (long) sorted[c - 1] * sorted[c - 2] * sorted[c - 3];
        long p2 = (long) sorted[0] * sorted[1] * sorted[c - 1];

        return Math.max(p1, p2);
    }
}

This checks the useful extremes, because the best product can come from either the three largest numbers or two negatives plus one large positive.

Lisp Max Product Of Three
(defun max-product-of-three (a)
  (let* ((sorted (sort (copy-list a) #'<))
         (vec (coerce sorted 'vector))
         (c (length vec)))
    (max (* (aref vec (- c 1)) (aref vec (- c 2)) (aref vec (- c 3)))
         (* (aref vec 0) (aref vec 1) (aref vec (- c 1))))))

This checks the useful extremes, because the best product can come from either the three largest numbers or two negatives plus one large positive.

PHP Max Product Of Three
function maxProductOfThree(array $a): int
{
    sort($a);
    $c = count($a);

    return max($a[$c - 1] * $a[$c - 2] * $a[$c - 3], $a[0] * $a[1] * $a[$c - 1]);
}

This checks the useful extremes, because the best product can come from either the three largest numbers or two negatives plus one large positive.

Python Max Product Of Three
def max_product_of_three(a: list[int]) -> int:
    a = sorted(a)
    c = len(a)

    return max(a[c - 1] * a[c - 2] * a[c - 3], a[0] * a[1] * a[c - 1])

This checks the useful extremes, because the best product can come from either the three largest numbers or two negatives plus one large positive.

Rust Max Product Of Three
fn max_product_of_three(mut a: Vec<i64>) -> i64 {
    a.sort();
    let c = a.len();

    (a[c - 1] * a[c - 2] * a[c - 3]).max(a[0] * a[1] * a[c - 1])
}

This checks the useful extremes, because the best product can come from either the three largest numbers or two negatives plus one large positive.

TypeScript Max Product Of Three
function maxProductOfThree(a: number[]): number {
  const sorted = [...a].sort((x, y) => x - y);
  const c = sorted.length;

  return Math.max(
    sorted[c - 1] * sorted[c - 2] * sorted[c - 3],
    sorted[0] * sorted[1] * sorted[c - 1]
  );
}

This checks the useful extremes, because the best product can come from either the three largest numbers or two negatives plus one large positive.

Bash Max Profit
max_profit() {
    local -n _a="$1"
    local _price=${_a[0]} _profit=0 _v
    for _v in "${_a[@]}"; do
        (( _v < _price )) && _price=$_v
        local _cur=$(( _v - _price ))
        (( _cur > _profit )) && _profit=$_cur
    done
    echo "$_profit"
}

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

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.

C# Max Profit
static int MaxProfit(int[] a)
{
    var price = a[0];
    var profit = 0;

    foreach (var v in 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.

Elixir Max Profit
defmodule MaxProfit do
  def max_profit([first | _] = a) do
    {_min_price, profit} =
      Enum.reduce(a, {first, 0}, fn v, {min_price, profit} ->
        min_price = min(min_price, v)
        {min_price, max(profit, v - min_price)}
      end)

    profit
  end
end

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