-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.php
More file actions
38 lines (28 loc) · 795 Bytes
/
3.php
File metadata and controls
38 lines (28 loc) · 795 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
<?php
/*
* Complete the 'diagonalDifference' function below.
*
* The function is expected to return an INTEGER.
* The function accepts 2D_INTEGER_ARRAY arr as parameter.
*/
function diagonalDifference($arr) {
// Write your code here
$lsum = 0;
$rsum = 0;
$length = count($arr);
for ($i = 0; $i < $length; $i++) {
$lsum += $arr[$i][$i];
$rsum += $arr[$i][$length - 1 - $i];
}
return abs($lsum - $rsum);
}
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$n = intval(trim(fgets(STDIN)));
$arr = array();
for ($i = 0; $i < $n; $i++) {
$arr_temp = rtrim(fgets(STDIN));
$arr[] = array_map('intval', preg_split('/ /', $arr_temp, -1, PREG_SPLIT_NO_EMPTY));
}
$result = diagonalDifference($arr);
fwrite($fptr, $result . "\n");
fclose($fptr);