findMedian

HackerRank 問題: findMedian

Question

HackerRank 問題: findMedian

Answer

<?php

function findMedian($number_list) {
    // medium index odd or even
    sort($number_list);
    $total_number_list = count($number_list);

    if ($total_number_list % 2 == 0) {
        // even
        $medium_number_index = (int) floor($total_number_list / 2);
    } else {
        // odd
        $medium_number_index = (int) floor($total_number_list / 2);
    }

    $medium_number = $number_list[$medium_number_index];

    return $medium_number;
}