Skip to content

Instantly share code, notes, and snippets.

@mario
Created June 23, 2014 11:43
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 mario/b8f8893b8eeb63cb1dc2 to your computer and use it in GitHub Desktop.
Save mario/b8f8893b8eeb63cb1dc2 to your computer and use it in GitHub Desktop.
<?php
$A = array(
72, 79, 84, 46, 14, 4, 62, 88, 58, 21, 16, 27, 13, 52, 96, 70, 28, 75,
76, 45, 5, 71, 43, 87, 64, 95, 72, 22, 43, 15, 25, 70, 64, 93, 56, 90,
84, 31, 49, 71, 29, 46, 6, 17, 81, 21, 75, 44, 2, 45, 67, 57, 93, 72,
39, 74, 43, 53, 40, 45, 87, 23, 44, 52, 1, 56, 32, 26, 32, 67, 41
);
$B = array();
$C = array();
rsort($A);
for($i = 0; $i<count($A); $i++) {
if (array_sum($B) <= array_sum($C)){
array_push($B, $A[$i]);
} else {
array_push($C, $A[$i]);
}
}
print_r($B);
print_r($C);
printf("Sum of first is %d\n", array_sum($B));
printf("Sum of second is %d\n", array_sum($C));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment