php calculate,How to calculate Diagonal difference in PHP?

I have a N*N Matrix.Now i want to know the diagonal difference of this Matrix.What will be the best approach of this solution?

I am trying with given approach:

Such as it is 3*3 Matrix say it is:

11 15 85

66 72 21

14 21 47

the diagonal simple formula will be:

firstD= (11+72+47) = 130

secondD = (85+72+14)= 171

diagonalDiff = |firstD - secondD| = |130-171| = 41

If I count every row such as first to find out firstD (First row's first value + Sec row's Sec value + Third row's third value+..).This is my thinking.

Can anyone tell me best approaches?

# Answer 1

4d350fd91e33782268f371d7edaa8a76.png

Try this:

$arr = array(

array(11, 15, 85),

array(66, 72, 21),

array(14, 21, 47),

);

$arrDiag = count($arr);

$firstD = 0;

$secondD = 0;

$i = 0;

for($j = 0; $j < $arrDiag; $j++){

$firstD += $arr[$i++][$j];

$secondD += $arr[$arrDiag - $i][$j];

}

echo abs($firstD - $secondD);//41

# Answer 2

Model your matrix with a multi-dimensional array and iterate through it. The easiest way should be the following:

$matrix = array(array(1,2,3),array(4,5,6),array(7,8,9)); //Insert or define your matrix here..

$n = count($matrix); //Size of matrix, thanks to VolkerK

$firstD = 0;

$lastD = 0;

for($i = 0; $i < $n; $i++){

$firstD += $matrix[$i][$i];

$lastD += $matrix[$i][$n-$i-1];

}

echo $firstD."\n";

echo $lastD;

# Answer 3

Here is a pseudo code for your problem using one simple loop:

// $array - predefined 2 dimentional array with $N rows and $N columns

$result = 0;

for ($i=0;$i

$result += ($array[$i,$i] - &array[$i,$N-$i-1]);

}

return echo abs($result);

that way you can do the calculation in one pass, and do a diff between two elements in each row insead of calculation the sum of each diagonal

# Answer 4

Try this:

function diagonalDifference($arr) {

$left = 0;

$right = 0;

$i = 0;

foreach($arr as $ar){

$left+= $ar[0+$i];

$right+= $ar[count($ar) - (1+$i)];

$i++;

}

return abs($left - $right);

}

# Answer 5

Try this

$result=0;

for($i=0;$i<=count($arr)-1;$i++){

$result= $result+($arr[$i][$i])-($arr[(count($arr)-1-$i)] [$i]);

}

return abs($result);

# Answer 6

This is the code you need:

$first = 0;

$second = 0;

for($i = 0; $i < N; $i++) {

for($j = 0; $j < N; $j++) {

if($i == $j) {

$first += $matrix[$i][$j];

} else if($i + $j == N) {

$second += $matrix[$i][$j];

}

}

}

$diagonalDiff = abs($first - $second);

Where $matrix is a N*N array

# Answer 7

Just using the function array_reduce:

function diagonalDifference($arr) {

$i = 0;

$n = count($arr);

return abs(array_reduce($arr,

function ($c, $str) use (&$i, $n ) {

$i++;

return $c + $str[$i-1] - $str[$n-$i];

}, 0));

}

# Answer 8

you can try this :

$first_diag=$second_diag=0;

$matrix=array(array(11,15,85),array(66,72,21),array(14,21,47));

foreach($matrix as $index=>$sub_array){

$first_diag +=$sub_array[$index];

$second_diag +=$sub_array[count($matrix)-1-$index];

}

print abs ($first_diag-$second_diag);

# Answer 9

For one you can use a matrix library like Math_Matrix. But if this is the only operation you are gona need then it's best to write a generalized function for this using the same method you quoted yourself.

function diagonalDiff($n){

$firstD = 0;

$secondD = 0;

for($i=0;$i

for($j=0;$j

if($i == $j) {

$first += $matrix[$i][$j];

} else if($i + $j == $n) {

$secondD += $matrix[$i][$j];

}

}

}

return abs($firstD-$secondD);

}

Should give you the answer you need for a matrix of a given size $n. (Only square Matrixes ofcourse :) )

# Answer 10

Another better solution and it's easy to understand:

$arr = array(

array(11, 15, 85),

array(66, 72, 21),

array(14, 21, 47),

);

$arrDiag = count($arr);

$firstD = 0;

$secondD = 0;

$i = 0;

for($j = 0; $j < $arrDiag; $j++){

$i++;

$firstD += $arr[$j][$j];

$secondD += $arr[$arrDiag - $i][$j];

}

echo abs($firstD - $secondD);

?>

Recently solved this question in Hacker Rank.

# Answer 11

$m=array(array(3,4,-1,11,2),

array(-3,2,1,6,9),

array(3,4,6,5,-2),

array(1,9,9,7,-3),

array(12,9,16,7,-3));

echo count($m)."
";

$i=0;

$j=0;

$ek=0;

$k=0;

$n=1;

$es=count($m)-1;

for($i=0;$i

{

for($j=0;$j

{

echo $m[$i][$j]." ";

}

echo "
";

}

echo "
";

for($k=0;$k

{

for($n=0;$n

{

if($k==$n){

$ek=$ek+$m[$k][$n];

}

}

echo "
";

}

echo "


";

$p=0;

for($k=0;$k

{

echo $m[$k][$es-$p]."
";

$p++;

}

?>

# Answer 12

Sorry I used different variable names, I had to take this to my vs code.

$b = array(

array(1,2,5),

array(3,4,5),

array(3,4,5)

);

//So for each diag

echo $b[0][0] + $b[1][1] + $b[2][2]; //to sum the first diag

echo $b[0][2] + $b[1][1] + $b[2][0]; //to sum the second diag

//notice the pattern 00, 11, 22 vs 02,11, 20. hence why I have written the function below

function difference($b){

$d1 = 0;

$d2 = 0;

$count = count($b);

for($i=0; $i

$d1 += $b[$i][$i];

$d2 += $b[$i][$count-1-$i];

}

return abs($d1 - $d2);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值