Skip to content

Instantly share code, notes, and snippets.

@AlloVince
Created April 3, 2013 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlloVince/5298390 to your computer and use it in GitHub Desktop.
Save AlloVince/5298390 to your computer and use it in GitHub Desktop.
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