-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagonal Difference.php
More file actions
52 lines (37 loc) · 1023 Bytes
/
Diagonal Difference.php
File metadata and controls
52 lines (37 loc) · 1023 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
/*
PROBLEM:
Given a square matrix, calculate the absolute difference between the sums of its diagonals.
For example, the square matrix is shown below:
1 2 3
4 5 6
9 8 9
The left-to-right diagonal = 1 + 5 + 9 = 15. The right to left diagonal = 3 + 5 + 9 = 17. Their absolute difference is (15 - 17) = 2.
Function description
Complete the "Diagonal Difference" function in the editor below.
diagonalDifference takes the following parameter:
- int arr[n][m]: an array of integers
Return
- int: the absolute diagonal difference
*/
// Solution
function diagonalDifference($arr) {
$counter1 = $counter2 = 0;
for( $i = 0; $i < count($arr); $i++ ) {
$counter1 += $arr[$i][$i];
}
for( $i = 0; $i < count($arr); $i++ ){
for( $j = count($arr)-1; $j > 0; $j-- ) {
$counter2 += $arr[$i][$j-$i];
break;
}
}
return abs($counter1 - $counter2);
}
$arr = [
[11, 2, 4],
[4, 5, 6],
[10, 8, -12]
];
echo diagonalDifference($arr);
?>