Skip to content

Instantly share code, notes, and snippets.

@jayzeng
Created June 11, 2013 06:09
Show Gist options
  • Save jayzeng/5754772 to your computer and use it in GitHub Desktop.
Save jayzeng/5754772 to your computer and use it in GitHub Desktop.
Find max
<?php
function findMax(array $input, $range) {
$curSum = 0;
for($i=0;$i<count($input);$i++) {
$subArr = array_slice($input, $i, $range);
$sum = 0;
foreach($subArr as $arr) {
$sum += $arr;
}
if($curSum < $sum) {
$curSum = $sum;
}
}
return $curSum;
}
$input = array(1,2,1,3,4,5);
var_dump(findMax($input,2));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment