Last active
August 4, 2016 00:41
-
-
Save tuanphpvn/30de87226c6706576e9a to your computer and use it in GitHub Desktop.
Find permutation of an array with total #ALGORITHM
This file contains hidden or 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
<?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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment