Mini-Max Sum

HackerRank 問題: Mini-Max Sum

Question

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.

HackerRank 問題: Mini-Max Sum

Answer

<?php

/*
 * Complete the 'miniMaxSum' function below.
 *
 * The function accepts INTEGER_ARRAY arr as parameter.
 */

function miniMaxSum($number_list) {
    // find minimum number in array
    $min_number = min($number_list);
    // find maximum number in array
    $max_number = max($number_list);
    // sum all number
    $all_sum_number = array_sum($number_list);

    // find minimum sum number
    $minimum_sum = $all_sum_number - $max_number;
    // find maximum sum number
    $maximum_sum = $all_sum_number - $min_number;

    echo sprintf("%d %d", $minimum_sum, $maximum_sum);

}

$arr_temp = rtrim(fgets(STDIN));

$arr = array_map('intval', preg_split('/ /', $arr_temp, -1, PREG_SPLIT_NO_EMPTY));

miniMaxSum($arr);

Reference