Created
April 20, 2020 04:44
-
-
Save ThienTranDuy/d3632d5665a40d492fcbbbbb5999d716 to your computer and use it in GitHub Desktop.
Hackerrank challenge - Mini-Max Sum - https://www.hackerrank.com/challenges/mini-max-sum/problem
This file contains 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) { | |
$minArr = min($arr); | |
$maxArr = max($arr); | |
if ($minArr !== $maxArr) { | |
$miniSum = array_sum(array_diff($arr, [$maxArr])); | |
$maxSum = array_sum(array_diff($arr, [$minArr])); | |
} else { | |
// Case 13: 5 elements are the sames | |
unset($arr[0]); | |
$maxSum = $miniSum = array_sum($arr); | |
} | |
echo $miniSum . " " . $maxSum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment