[ create a new paste ] login | about

Link: http://codepad.org/5j5nl3dT    [ raw code | output | fork ]

PHP, pasted on Nov 18:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$a = array(8, 16, 16, 32, 8, 8, 4, 4);
$limit = 32;
rsort($a);
$b = array(array());
$index = 0;
foreach($a as $i){
    if($i+array_sum($b[$index]) > $limit){
        $b[++$index] = array();
    }
    $b[$index][] = $i;
}
$a = $b;
print_r($a);
?>


Output:
Array
(
    [0] => Array
        (
            [0] => 32
        )

    [1] => Array
        (
            [0] => 16
            [1] => 16
        )

    [2] => Array
        (
            [0] => 8
            [1] => 8
            [2] => 8
            [3] => 4
            [4] => 4
        )

)


Create a new paste based on this one


Comments: