Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I split an array of numbers evenly

Suppose I have the array:

array(1,1,2,1,4,5,7,2,3);

What would be the fastest way to get these numbers into x arrays we'll use 3 and have the numbers be as equal as possible with the larger numbers at the end?

Example:

array(1, 1, 1, 5);
array(7, 2);
array(4, 2, 3);

I'm worried that this might be a p=np problem but it seems so simple that it shouldn't be. I just can't seem to figure it out.

Similar question: Algorithm to split an array into P subarrays of balanced sum

like image 559
Blakethepatton Avatar asked Sep 27 '22 04:09

Blakethepatton


People also ask

How do you separate data in an array?

The easiest way to extract a chunk of an array, or rather, to slice it up, is the slice() method: slice(start, end) - Returns a part of the invoked array, between the start and end indices.

How do you split an array by even or odd?

Logic To Split Even and Odd Elements of An Array Into Two Arrays. First we accept all the elements of an array from the user. Next we iterate through the array elements one by one using a for loop. Inside this for loop we check each individual element, if its even or odd.

How do you split an array into two parts?

To divide an array into two, we need at least three array variables. We shall take an array with continuous numbers and then shall store the values of it into two different variables based on even and odd values.


1 Answers

Not exactly what you're looking for, but this should help get you started:

$array = array(1,1,2,1,4,5,7,2,3);

asort($array);

$total = array_sum($array);

$array1 = array();
$array2 = array();
$array3 = array();

foreach($array as $number) {
    if(array_sum($array1) < round($total / 3)) {
        array_push($array1, $number);
    } elseif(array_sum($array2) < round($total / 3)) {
        array_push($array2, $number);
    } else {
        array_push($array3, $number);
    }
}

for($i = 1; $i <= 3; $i++) {

    switch($i) {
        case 1:
            $op1 = 2;
            $op2 = 1;
            break;
        case 2:
            $op1 = -1;
            $op2 = 1;
            break;
        case 3:
            $op1 = -2;
            $op2 = -1;
            break;
    }

    foreach(${'array' . $i} as $number) {
        if((array_sum(${'array' . ($i + $op1)}) + $number) == round($total / 3)) {
            unset(${'array' . $i}[array_search($number, ${'array' . $i})]);
            array_push(${'array' . ($i + $op1)}, $number);
        } elseif((array_sum(${'array' . ($i + $op2)}) + $number) == round($total / 3)) {
            unset(${'array' . $i}[array_search($number, ${'array' . $i})]);
            array_push(${'array' . ($i + $op2)}, $number);
        }
    }
}

print_r($array1);
print_r($array2);
print_r($array3);

New Output:

Array
(
    [0] => 1
    [1] => 1
    [2] => 1
    [4] => 2
    [5] => 3
)
Array
(
    [0] => 4
    [1] => 5
)
Array
(
    [0] => 7
    [1] => 2
)
like image 138
mattslone Avatar answered Sep 29 '22 00:09

mattslone