Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.
Example=a[1,2,3,4,5]
The minimum sum is[1,2,3,4] =1+2+3+4=10 and the maximum sum is [5,4,3,2]= 5+4+3+2=14. The function prints 10 14
// Complete the miniMaxSum function below.
function miniMaxSum($arr)
{
$res = [];
$count = count($arr) - 1;
for ($i = 0;$i < count($arr);$i++)
{
$sum = 0;
for ($j = 0;$j < count($arr);$j++)
{
if ($j != $count)
{
$sum = $sum + $arr[$j];
}
}
$res[$i] = $sum;
$count--;
}
sort($res);
echo $res[0] . " " . $res[count($arr) - 1];
}
$stdin = fopen("php://stdin", "r");
fscanf($stdin, "%[^\n]", $arr_temp);
$arr = array_map('intval', preg_split('/ /', $arr_temp, -1, PREG_SPLIT_NO_EMPTY));
miniMaxSum($arr);
fclose($stdin);
0 Response to "Mini-Max Sum"
Post a Comment