-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2D-Array-DS.php
More file actions
39 lines (30 loc) · 843 Bytes
/
2D-Array-DS.php
File metadata and controls
39 lines (30 loc) · 843 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
<?php
/*
* Complete the 'hourglassSum' function below.
*
* The function is expected to return an INTEGER.
* The function accepts 2D_INTEGER_ARRAY arr as parameter.
*/
function hourglassSum($arr) {
// Write your code here
$maxSum=-99999;
for($i=0;$i<count($arr)-2;$i++){
$sum=0;
for($j=0;$j<count($arr[$i])-2;$j++){
$sum=$arr[$i][$j]+ $arr[$i][$j+1]+ $arr[$i][$j+2]+ $arr[$i+1][$j+1]+ $arr[$i+2][$j]+$arr[$i+2][$j+1]+$arr[$i+2][$j+2];
if($sum>$maxSum){
$maxSum=$sum;
}
}
}
return $maxSum;
}
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$arr = array();
for ($i = 0; $i < 6; $i++) {
$arr_temp = rtrim(fgets(STDIN));
$arr[] = array_map('intval', preg_split('/ /', $arr_temp, -1, PREG_SPLIT_NO_EMPTY));
}
$result = hourglassSum($arr);
fwrite($fptr, $result . "\n");
fclose($fptr);