Go Almost Magic Square
func almostMagicSquare(a []int) []int {
var grid [3][3]int
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
grid[i][j] = a[i*3+j]
}
}
rowSum := make([]int, 3)
colSum := make([]int, 3)
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
rowSum[i] += grid[i][j]
colSum[i] += grid[j][i]
}
}
maxSum := 0
for k, v := range rowSum {
maxSum = max(maxSum, v, colSum[k])
}
for i, j := 0, 0; i < 3 && j < 3; {
diff := 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++
}
}
result := make([]int, 0, 9)
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
result = append(result, 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.
Haskell Almost Magic Square
almostMagicSquare :: [Int] -> [Int]
almostMagicSquare a = concat (go rows rowSums colSums 0 0)
where
chunk3 [] = []
chunk3 xs = take 3 xs : chunk3 (drop 3 xs)
updateAt :: Int -> (b -> b) -> [b] -> [b]
updateAt idx f xs = [if i == idx then f x else x | (i, x) <- zip [0 ..] xs]
rows = chunk3 a
rowSums = map sum rows
colSums = [sum [rows !! r !! c | r <- [0 .. 2]] | c <- [0 .. 2]]
maxSum = maximum (rowSums ++ colSums)
go grid rs cs i j
| i >= 3 || j >= 3 = grid
| otherwise =
let diff = min (maxSum - rs !! i) (maxSum - cs !! j)
grid' = updateAt i (updateAt j (+ diff)) grid
rs' = updateAt i (+ diff) rs
cs' = updateAt j (+ diff) cs
i' = if rs' !! i == maxSum then i + 1 else i
j' = if cs' !! j == maxSum then j + 1 else j
in go grid' rs' cs' i' j'
This adjusts the matrix toward a matching target sum so the rows and columns line up more like a magic square.
Java Almost Magic Square
public class Solution {
public static int[] almostMagicSquare(int[] a) {
int[][] grid = new int[3][3];
for (int i = 0; i < 9; i++) {
grid[i / 3][i % 3] = a[i];
}
int[] rowSum = new int[3];
int[] colSum = new int[3];
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 = Math.max(maxSum, rowSum[k]);
maxSum = Math.max(maxSum, colSum[k]);
}
for (int i = 0, j = 0; i < 3 && j < 3; ) {
int 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++;
}
}
int[] result = new int[9];
for (int i = 0; i < 9; i++) {
result[i] = grid[i / 3][i % 3];
}
return result;
}
}
This adjusts the matrix toward a matching target sum so the rows and columns line up more like a magic square.
Lisp Almost Magic Square
(defun almost-magic-square (a)
(let ((m (make-array '(3 3)))
(row-sum (make-array 3 :initial-element 0))
(col-sum (make-array 3 :initial-element 0))
(max-sum 0))
(loop for idx from 0 below 9
for val in a
do (setf (aref m (floor idx 3) (mod idx 3)) val))
(dotimes (i 3)
(dotimes (j 3)
(incf (aref row-sum i) (aref m i j))
(incf (aref col-sum i) (aref m j i))))
(dotimes (k 3)
(setf max-sum (max max-sum (aref row-sum k) (aref col-sum k))))
(let ((i 0) (j 0))
(loop while (and (< i 3) (< j 3))
do (let ((diff (min (- max-sum (aref row-sum i))
(- max-sum (aref col-sum j)))))
(incf (aref m i j) diff)
(incf (aref row-sum i) diff)
(incf (aref col-sum j) diff)
(when (= (aref row-sum i) max-sum) (incf i))
(when (and (< j 3) (= (aref col-sum j) max-sum)) (incf j)))))
(loop for idx from 0 below 9
collect (aref m (floor idx 3) (mod idx 3)))))
This adjusts the matrix toward a matching target sum so the rows and columns line up more like a magic square.
PHP Almost Magic Square
function almostMagicSquare(array $a): array
{
$rowSum = $colSum = array_fill(0, 3, 0);
$maxSum = 0;
$a = array_chunk($a, 3);
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
$rowSum[$i] += $a[$i][$j];
$colSum[$i] += $a[$j][$i];
}
}
foreach ($rowSum as $k => $v) {
$maxSum = max($maxSum, $v);
$maxSum = max($maxSum, $colSum[$k]);
}
for ($i = 0, $j = 0; $i < 3 && $j < 3;) {
$diff = min($maxSum - $rowSum[$i], $maxSum - $colSum[$j]);
$a[$i][$j] += $diff;
$rowSum[$i] += $diff;
$colSum[$j] += $diff;
if ($rowSum[$i] === $maxSum) {
$i++;
}
if ($colSum[$j] === $maxSum) {
$j++;
}
}
return array_merge([], ...$a);
}
This adjusts the matrix toward a matching target sum so the rows and columns line up more like a magic square.
Python Almost Magic Square
def almost_magic_square(a: list[int]) -> list[int]:
grid = [a[i:i + 3] for i in range(0, 9, 3)]
row_sum = [0, 0, 0]
col_sum = [0, 0, 0]
for i in range(3):
for j in range(3):
row_sum[i] += grid[i][j]
col_sum[i] += grid[j][i]
max_sum = 0
for k in range(3):
max_sum = max(max_sum, row_sum[k])
max_sum = max(max_sum, col_sum[k])
i = j = 0
while i < 3 and j < 3:
diff = min(max_sum - row_sum[i], max_sum - col_sum[j])
grid[i][j] += diff
row_sum[i] += diff
col_sum[j] += diff
if row_sum[i] == max_sum:
i += 1
if col_sum[j] == max_sum:
j += 1
return [v for row in grid for v in row]
This adjusts the matrix toward a matching target sum so the rows and columns line up more like a magic square.
Rust Almost Magic Square
fn almost_magic_square(a: &[i64]) -> Vec<i64> {
let mut grid: Vec<Vec<i64>> = a.chunks(3).map(|c| c.to_vec()).collect();
let mut row_sum = [0i64; 3];
let mut col_sum = [0i64; 3];
for i in 0..3 {
for j in 0..3 {
row_sum[i] += grid[i][j];
col_sum[i] += grid[j][i];
}
}
let mut max_sum = 0;
for k in 0..3 {
max_sum = max_sum.max(row_sum[k]);
max_sum = max_sum.max(col_sum[k]);
}
let (mut i, mut j) = (0usize, 0usize);
while i < 3 && j < 3 {
let diff = (max_sum - row_sum[i]).min(max_sum - col_sum[j]);
grid[i][j] += diff;
row_sum[i] += diff;
col_sum[j] += diff;
if row_sum[i] == max_sum {
i += 1;
}
if col_sum[j] == max_sum {
j += 1;
}
}
grid.into_iter().flatten().collect()
}
This adjusts the matrix toward a matching target sum so the rows and columns line up more like a magic square.
TypeScript Almost Magic Square
function almostMagicSquare(a: number[]): number[] {
const rowSum = [0, 0, 0];
const colSum = [0, 0, 0];
let maxSum = 0;
const grid: number[][] = [a.slice(0, 3), a.slice(3, 6), a.slice(6, 9)];
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
rowSum[i] += grid[i][j];
colSum[i] += grid[j][i];
}
}
for (let k = 0; k < 3; k++) {
maxSum = Math.max(maxSum, rowSum[k]);
maxSum = Math.max(maxSum, colSum[k]);
}
for (let i = 0, j = 0; i < 3 && j < 3; ) {
const 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++;
}
}
return grid.flat();
}
This adjusts the matrix toward a matching target sum so the rows and columns line up more like a magic square.
Bash Are Equally Strong
are_equally_strong() {
local _yourLeft=$1 _yourRight=$2 _friendsLeft=$3 _friendsRight=$4
local _maxYou=$(( _yourRight > _yourLeft ? _yourRight : _yourLeft ))
local _maxFriend=$(( _friendsLeft > _friendsRight ? _friendsLeft : _friendsRight ))
local _minYou=$(( _yourLeft < _yourRight ? _yourLeft : _yourRight ))
local _minFriend=$(( _friendsRight < _friendsLeft ? _friendsRight : _friendsLeft ))
if (( _maxYou == _maxFriend && _minYou == _minFriend )); then
echo true
else
echo false
fi
}
This compares each person’s strongest and weakest arm. If both pairs match, the result is true.
C++ Are Equally Strong
#include <algorithm>
bool areEquallyStrong(int yourLeft, int yourRight, int friendsLeft, int friendsRight)
{
return std::max(yourRight, yourLeft) == std::max(friendsLeft, friendsRight)
&& std::min(yourLeft, yourRight) == std::min(friendsRight, friendsLeft);
}
This compares each person’s strongest and weakest arm. If both pairs match, the result is true.