Created
April 3, 2013 04:10
-
-
Save AlloVince/5298390 to your computer and use it in GitHub Desktop.
This file contains 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
function testArray(array $array){ | |
sort($array); | |
$max = array_pop($array); | |
$arrLen = count($array); | |
$len = pow(2, $arrLen); | |
for($i = 0; $i < $len; $i++){ | |
$byte = sprintf('%0+'. $arrLen . 'b', $i); | |
$cache = array(); | |
for($j = 0; $j <$arrLen; $j++) { | |
if($byte{$j}==1 && $j!=$arrLen) { | |
$cache[] = $array[$j]; | |
} | |
} | |
if($max == array_sum($cache)){ | |
return true; | |
} | |
} | |
return false; | |
} | |
echo testArray(array(4, 6, 23, 10, 1, 3)); | |
echo '<br>'; | |
echo testArray(array(1, 22, 23, 24, 27, 29, 33)); | |
echo '<br>'; | |
echo testArray(array(1, 22, 23, 25, 26)); | |
echo '<br>'; | |
echo testArray(array(3, 5, -1, 8, 1, -2, 12)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment