Skip to content

Instantly share code, notes, and snippets.

@tanmay27vats
Created August 10, 2018 06:26
Show Gist options
  • Save tanmay27vats/c725d2d4bbd28ba09e7df50548bdc02a to your computer and use it in GitHub Desktop.
Save tanmay27vats/c725d2d4bbd28ba09e7df50548bdc02a to your computer and use it in GitHub Desktop.
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. For example,  arr = [1,3,5,7,9]. Our minimum sum is 1 + 3 + 5 + 7 = 16 and our maximum sum is 3 + 5 + 7 …
function miniMaxSum($arr) {
$sum = array_sum($arr);
$min = $sum;
$max = 0;
foreach($arr as $key => $val) {
$excluded_sum = $sum - $val;
if($max < $excluded_sum) {
$max = $excluded_sum;
}
if($min > $excluded_sum) {
$min = $excluded_sum;
}
}
echo $min.' '.$max;
}
$arr = [1,2,3,4,5];
miniMaxSum($arr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment