tuan
9/6/2015 - 12:55 PM

Find permutation of an array with total #ALGORITHM

Find permutation of an array with total #ALGORITHM

<?php
$swapEqualWithResult = function($arr, $total) {
    $result = array();
    
    for($i = 0; $i < count($arr); $i++) {
        for($j = 0; $j < count($arr); $j++) {
            for($k = 0; $k < count($arr); $k++) {
                if($arr[$i] + $arr[$j] + $arr[$k] === $total)
                {
                    $result = array($arr[$i], $arr[$j], $arr[$k]);
                    goto end;
                }
            }
        }
    }
    end:

    return $result;
};

// $arr = array(1,3,5,7,9,11,13,15);
// $result = $swapEqualWithResult($arr, 30);