Python Max Double Slice Sum
def max_double_slice_sum(a: list[int]) -> int:
size = len(a)
if size < 3:
return 0
p1 = {1: 0}
p2 = {size - 2: 0}
for i in range(2, size - 1):
p1[i] = max(0, p1[i - 1] + a[i - 1])
p2[size - i - 1] = max(0, p2[size - i] + a[size - i])
total = p1[1] + p2[1]
for i in range(1, size - 1):
total = max(total, p1[i] + p2[i])
return total
This keeps the best sum ending on the left and starting on the right, then combines them around each middle position.
Rust Max Double Slice Sum
fn max_double_slice_sum(a: &[i64]) -> i64 {
let size = a.len();
if size < 3 {
return 0;
}
let mut p1 = vec![0i64; size];
let mut p2 = vec![0i64; size];
for i in 2..size - 1 {
p1[i] = 0.max(p1[i - 1] + a[i - 1]);
p2[size - i - 1] = 0.max(p2[size - i] + a[size - i]);
}
let mut sum = p1[1] + p2[1];
for i in 1..size - 1 {
sum = sum.max(p1[i] + p2[i]);
}
sum
}
This keeps the best sum ending on the left and starting on the right, then combines them around each middle position.
TypeScript Max Double Slice Sum
function maxDoubleSliceSum(a: number[]): number {
const size = a.length;
if (size < 3) {
return 0;
}
const p1: number[] = new Array(size).fill(0);
const p2: number[] = new Array(size).fill(0);
p1[1] = 0;
p2[size - 2] = 0;
for (let i = 2; i < size - 1; i++) {
p1[i] = Math.max(0, p1[i - 1] + a[i - 1]);
p2[size - i - 1] = Math.max(0, p2[size - i] + a[size - i]);
}
let sum = p1[1] + p2[1];
for (let i = 1; i < size - 1; i++) {
sum = Math.max(sum, p1[i] + p2[i]);
}
return sum;
}
This keeps the best sum ending on the left and starting on the right, then combines them around each middle position.
Bash Max Product Of Three
max_product_of_three() {
local -n _a="$1"
local -a _sorted=($(printf '%s\n' "${_a[@]}" | sort -n))
local _c=${#_sorted[@]}
local _p1=$(( _sorted[_c-1] * _sorted[_c-2] * _sorted[_c-3] ))
local _p2=$(( _sorted[0] * _sorted[1] * _sorted[_c-1] ))
if (( _p1 > _p2 )); then echo "$_p1"; else echo "$_p2"; fi
}
This checks the useful extremes, because the best product can come from either the three largest numbers or two negatives plus one large positive.
C++ Max Product Of Three
#include <algorithm>
#include <vector>
long long maxProductOfThree(std::vector<int> a)
{
std::sort(a.begin(), a.end());
int c = static_cast<int>(a.size());
long long topThree = static_cast<long long>(a[c - 1]) * a[c - 2] * a[c - 3];
long long twoLowestAndTop = static_cast<long long>(a[0]) * a[1] * a[c - 1];
return std::max(topThree, twoLowestAndTop);
}
This checks the useful extremes, because the best product can come from either the three largest numbers or two negatives plus one large positive.
C# Max Product Of Three
static long MaxProductOfThree(int[] a)
{
var sorted = (int[])a.Clone();
Array.Sort(sorted);
var c = sorted.Length;
return Math.Max(
(long)sorted[c - 1] * sorted[c - 2] * sorted[c - 3],
(long)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.
Elixir Max Product Of Three
defmodule MaxProductOfThree do
def max_product_of_three(a) do
sorted = Enum.sort(a)
c = length(sorted)
max(
Enum.at(sorted, c - 1) * Enum.at(sorted, c - 2) * Enum.at(sorted, c - 3),
Enum.at(sorted, 0) * Enum.at(sorted, 1) * Enum.at(sorted, c - 1)
)
end
end
This checks the useful extremes, because the best product can come from either the three largest numbers or two negatives plus one large positive.
Erlang Max Product Of Three
-module(max_product_of_three).
-export([max_product_of_three/1]).
max_product_of_three(A) ->
Sorted = lists:sort(A),
N = length(Sorted),
Top3 = lists:nth(N, Sorted) * lists:nth(N - 1, Sorted) * lists:nth(N - 2, Sorted),
TwoLowOneHigh = lists:nth(1, Sorted) * lists:nth(2, Sorted) * lists:nth(N, Sorted),
max(Top3, TwoLowOneHigh).
This checks the useful extremes, because the best product can come from either the three largest numbers or two negatives plus one large positive.
Go Max Product Of Three
func maxProductOfThree(a []int) int {
sorted := append([]int(nil), a...)
sort.Ints(sorted)
c := len(sorted)
return 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.
Haskell Max Product Of Three
import Data.List (sort)
maxProductOfThree :: [Int] -> Int
maxProductOfThree a =
max (s !! (n - 1) * s !! (n - 2) * s !! (n - 3)) (s !! 0 * s !! 1 * s !! (n - 1))
where
s = sort a
n = length s
This checks the useful extremes, because the best product can come from either the three largest numbers or two negatives plus one large positive.