Created
August 10, 2018 06:26
-
-
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 …
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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