Lisp Adjacent Elements Product
(defun adjacent-elements-product (input-array)
  (let ((vec (coerce input-array 'vector))
        (max-product most-negative-fixnum))
    (dotimes (i (1- (length vec)))
      (setf max-product (max max-product (* (aref vec i) (aref vec (1+ i))))))
    max-product))

This walks through neighboring values, multiplies each pair, and keeps the biggest product it finds.

PHP Adjacent Elements Product
function adjacentElementsProduct($inputArray)
{
    $max = PHP_INT_MIN;

    for ($i = 0, $c = count($inputArray); $i < $c - 1; $i++) {
        $max = max($max, $inputArray[$i] * $inputArray[$i + 1]);
    }

    return $max;

}

This walks through neighboring values, multiplies each pair, and keeps the biggest product it finds.

Python Adjacent Elements Product
def adjacent_elements_product(input_array: list[int]) -> int:
    return max(
        input_array[i] * input_array[i + 1] for i in range(len(input_array) - 1)
    )

This walks through neighboring values, multiplies each pair, and keeps the biggest product it finds.

Rust Adjacent Elements Product
fn adjacent_elements_product(input_array: &[i64]) -> i64 {
    input_array
        .windows(2)
        .map(|w| w[0] * w[1])
        .max()
        .unwrap_or(i64::MIN)
}

This walks through neighboring values, multiplies each pair, and keeps the biggest product it finds.

TypeScript Adjacent Elements Product
function adjacentElementsProduct(inputArray: number[]): number {
  let max = -Infinity;

  for (let i = 0; i < inputArray.length - 1; i++) {
    max = Math.max(max, inputArray[i] * inputArray[i + 1]);
  }

  return max;
}

This walks through neighboring values, multiplies each pair, and keeps the biggest product it finds.

Bash Almost Magic Square
almost_magic_square() {
    local -n _a="$1"
    local -n _out="$2"
    local -a _rowSum=(0 0 0) _colSum=(0 0 0)
    local -a _m
    local _i _j
    for ((_i = 0; _i < 3; _i++)); do
        for ((_j = 0; _j < 3; _j++)); do
            _m[_i*3+_j]=${_a[_i*3+_j]}
        done
    done
    for ((_i = 0; _i < 3; _i++)); do
        for ((_j = 0; _j < 3; _j++)); do
            _rowSum[_i]=$(( _rowSum[_i] + _m[_i*3+_j] ))
            _colSum[_i]=$(( _colSum[_i] + _m[_j*3+_i] ))
        done
    done
    local _maxSum=0
    for ((_i = 0; _i < 3; _i++)); do
        if (( _rowSum[_i] > _maxSum )); then _maxSum=${_rowSum[_i]}; fi
        if (( _colSum[_i] > _maxSum )); then _maxSum=${_colSum[_i]}; fi
    done
    _i=0; _j=0
    while (( _i < 3 && _j < 3 )); do
        local _diffR=$(( _maxSum - _rowSum[_i] ))
        local _diffC=$(( _maxSum - _colSum[_j] ))
        local _diff=$(( _diffR < _diffC ? _diffR : _diffC ))
        _m[_i*3+_j]=$(( _m[_i*3+_j] + _diff ))
        _rowSum[_i]=$(( _rowSum[_i] + _diff ))
        _colSum[_j]=$(( _colSum[_j] + _diff ))
        if (( _rowSum[_i] == _maxSum )); then ((_i++)); fi
        if (( _colSum[_j] == _maxSum )); then ((_j++)); fi
    done
    _out=("${_m[@]}")
}

This adjusts the matrix toward a matching target sum so the rows and columns line up more like a magic square.

C++ Almost Magic Square
#include <algorithm>
#include <array>
#include <vector>

std::vector<int> almostMagicSquare(const std::vector<int>& a)
{
    std::array<std::array<int, 3>, 3> grid{};
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            grid[i][j] = a[i * 3 + j];
        }
    }

    std::array<int, 3> rowSum{};
    std::array<int, 3> colSum{};
    int maxSum = 0;

    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            rowSum[i] += grid[i][j];
            colSum[i] += grid[j][i];
        }
    }

    for (int k = 0; k < 3; ++k) {
        maxSum = std::max(maxSum, rowSum[k]);
        maxSum = std::max(maxSum, colSum[k]);
    }

    for (int i = 0, j = 0; i < 3 && j < 3;) {
        int diff = std::min(maxSum - rowSum[i], maxSum - colSum[j]);
        grid[i][j] += diff;
        rowSum[i] += diff;
        colSum[j] += diff;

        if (rowSum[i] == maxSum) {
            ++i;
        }
        if (colSum[j] == maxSum) {
            ++j;
        }
    }

    std::vector<int> result;
    result.reserve(9);
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            result.push_back(grid[i][j]);
        }
    }

    return result;
}

This adjusts the matrix toward a matching target sum so the rows and columns line up more like a magic square.

C# Almost Magic Square
static int[] AlmostMagicSquare(int[] a)
{
    var rowSum = new int[3];
    var colSum = new int[3];
    var maxSum = 0;

    var grid = new int[3][];
    for (int i = 0; i < 3; i++)
    {
        grid[i] = new[] { a[i * 3], a[i * 3 + 1], a[i * 3 + 2] };
    }

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            rowSum[i] += grid[i][j];
            colSum[i] += grid[j][i];
        }
    }

    for (int k = 0; k < 3; k++)
    {
        maxSum = Math.Max(maxSum, rowSum[k]);
        maxSum = Math.Max(maxSum, colSum[k]);
    }

    for (int i = 0, j = 0; i < 3 && j < 3;)
    {
        var diff = Math.Min(maxSum - rowSum[i], maxSum - colSum[j]);
        grid[i][j] += diff;
        rowSum[i] += diff;
        colSum[j] += diff;

        if (rowSum[i] == maxSum)
        {
            i++;
        }
        if (colSum[j] == maxSum)
        {
            j++;
        }
    }

    var result = new int[9];
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            result[i * 3 + j] = grid[i][j];
        }
    }

    return result;
}

This adjusts the matrix toward a matching target sum so the rows and columns line up more like a magic square.

Elixir Almost Magic Square
defmodule AlmostMagicSquare do
  def almost_magic_square(a) do
    rows = Enum.chunk_every(a, 3)
    row_sums = Enum.map(rows, &Enum.sum/1)
    col_sums = for j <- 0..2, do: rows |> Enum.map(&Enum.at(&1, j)) |> Enum.sum()
    max_sum = Enum.max(row_sums ++ col_sums)

    {final_rows, _, _, _, _} = balance(rows, row_sums, col_sums, max_sum, 0, 0)
    List.flatten(final_rows)
  end

  defp balance(rows, row_sums, col_sums, max_sum, i, j) when i < 3 and j < 3 do
    diff = min(max_sum - Enum.at(row_sums, i), max_sum - Enum.at(col_sums, j))

    rows = List.update_at(rows, i, fn row -> List.update_at(row, j, &(&1 + diff)) end)
    row_sums = List.update_at(row_sums, i, &(&1 + diff))
    col_sums = List.update_at(col_sums, j, &(&1 + diff))

    next_i = if Enum.at(row_sums, i) == max_sum, do: i + 1, else: i
    next_j = if Enum.at(col_sums, j) == max_sum, do: j + 1, else: j

    balance(rows, row_sums, col_sums, max_sum, next_i, next_j)
  end

  defp balance(rows, row_sums, col_sums, _max_sum, i, j), do: {rows, row_sums, col_sums, i, j}
end

This adjusts the matrix toward a matching target sum so the rows and columns line up more like a magic square.

Erlang Almost Magic Square
-module(almost_magic_square).
-export([almost_magic_square/1]).

almost_magic_square(A) ->
    Rows = chunk3(A),
    RowSums = [lists:sum(R) || R <- Rows],
    ColSums = [lists:sum([lists:nth(C, R) || R <- Rows]) || C <- [1, 2, 3]],
    MaxSum = lists:max(RowSums ++ ColSums),
    FinalRows = balance(0, 0, RowSums, ColSums, Rows, MaxSum),
    lists:append(FinalRows).

chunk3([]) -> [];
chunk3(List) ->
    {Row, Rest} = lists:split(3, List),
    [Row | chunk3(Rest)].

balance(I, J, RowSums, ColSums, Rows, MaxSum) when I < 3, J < 3 ->
    RowSumI = lists:nth(I + 1, RowSums),
    ColSumJ = lists:nth(J + 1, ColSums),
    Diff = min(MaxSum - RowSumI, MaxSum - ColSumJ),
    Rows1 = add_at(Rows, I, J, Diff),
    RowSums1 = add_at_list(RowSums, I, Diff),
    ColSums1 = add_at_list(ColSums, J, Diff),
    NextI = case lists:nth(I + 1, RowSums1) of MaxSum -> I + 1; _ -> I end,
    NextJ = case lists:nth(J + 1, ColSums1) of MaxSum -> J + 1; _ -> J end,
    balance(NextI, NextJ, RowSums1, ColSums1, Rows1, MaxSum);
balance(_, _, _, _, Rows, _) ->
    Rows.

add_at(Rows, I, J, Diff) ->
    [case Idx of
         I -> [case Jdx of J -> V + Diff; _ -> V end
               || {Jdx, V} <- lists:zip(lists:seq(0, length(Row) - 1), Row)];
         _ -> Row
     end || {Idx, Row} <- lists:zip(lists:seq(0, length(Rows) - 1), Rows)].

add_at_list(List, Idx0, Diff) ->
    [case I of Idx0 -> V + Diff; _ -> V end
     || {I, V} <- lists:zip(lists:seq(0, length(List) - 1), List)].

This adjusts the matrix toward a matching target sum so the rows and columns line up more like a magic square.