Skip to content

Instantly share code, notes, and snippets.

@tuanphpvn
Last active August 4, 2016 00:41
Show Gist options
  • Save tuanphpvn/30de87226c6706576e9a to your computer and use it in GitHub Desktop.
Save tuanphpvn/30de87226c6706576e9a to your computer and use it in GitHub Desktop.
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment