- var_dump: documentation ( source)
- array_sum: documentation ( source)
- abs: documentation ( source)
- array_splice: documentation ( source)
<?php
// you can use print for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
$B = array_splice($A, 1);
$length = count($B);
$t1 = $A[0];
$t2 = array_sum($B);
$i = 0;
$diff = abs($t1 - $t2);
echo "Diff:" . $diff;
do {
$newDiff = abs (($t1 + $B[$i]) - ($t2 + $B[$i]));
var_dump($newDiff);
if ($newDiff < $diff) {
$diff = $newDif;
$i++;
$length --;
} else {
break;
}
} while ($length > 1);
return $diff;
}
echo solution([3,1,2,4,3]);