Skip to content

Instantly share code, notes, and snippets.

@elishaukpong
Last active August 10, 2021 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elishaukpong/0f394bc9c9c64b64884cc92a7c06e27d to your computer and use it in GitHub Desktop.
Save elishaukpong/0f394bc9c9c64b64884cc92a7c06e27d to your computer and use it in GitHub Desktop.
Problem Statement: 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. Link: https://www.hackerrank.com/challenges/mini-max-sum/problem
function miniMaxSum($arr) {
$sumValue = [];
foreach($arr as $key => $integer)
{
$sumValue[] = array_sum(array_filter($arr, function($innerKey) use ($key){
return $innerKey !== $key;
},ARRAY_FILTER_USE_KEY));
}
// You can also use array_walk, this was for demonstration purpose
//
// array_walk($arr, function($value, $key, $array) use(&$sumValue){
//
// $sumValue[] = array_sum(array_filter($array, function($innerKey) use ($key){
// return $innerKey !== $key;
// },ARRAY_FILTER_USE_KEY));
//
// },$arr);
echo min($sumValue) . " " . max($sumValue);
// one liner solution by little_foot on hackerrank, i am impressed so I had to add it here.
// echo(array_sum($arr)-max($arr))." ".(array_sum($arr)-min($arr));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment