I use this code
function maxSum($arr, $n, $k)
{
$show=[];
$max_sum = PHP_INT_MIN ;
for ( $i = 0; $i < $n - $k + 1; $i++)
{
$current_sum = 0;
for ( $j = 0; $j < $k; $j++)
$current_sum = $current_sum +
$arr[$i + $j];
$max_sum = max($current_sum, $max_sum );
array_push($show,$max_sum);
}
return $show;
}
$arr = array(1, 4, 2, 10, 2, 3, 1, 0, 2);
$k = 3;
$n = count($arr);
print_r(maxSum($arr, $n, $k));
It works fine to find sums of 3 array items. Now I would like to do the opposite - to find how many array items should I sum to get 10 or close to 10.
Help is appreciated.