Calculate digonal difference in php array matirx
$arr = array(
array(11, 2, 4),
array(4, 5, 6),
array(10, 8, -12),
);
echo diagonalDifference($arr);
function diagonalDifference($arr) {
$n= count($arr);
$firstdsum= 0;
$seconddsum = 0;
$i = 0;
for($j = 0; $j < $n; $j++){
$firstdsum+= $arr[$i++][$j];
$seconddsum += $arr[$n- $i][$j];
}
return abs($firstdsum- $seconddsum );
}
array(11, 2, 4),
array(4, 5, 6),
array(10, 8, -12),
);
echo diagonalDifference($arr);
function diagonalDifference($arr) {
$n= count($arr);
$firstdsum= 0;
$seconddsum = 0;
$i = 0;
for($j = 0; $j < $n; $j++){
$firstdsum+= $arr[$i++][$j];
$seconddsum += $arr[$n- $i][$j];
}
return abs($firstdsum- $seconddsum );
}
Comments
Post a Comment