H
H
Henry2017-04-12 19:11:02
PHP
Henry, 2017-04-12 19:11:02

How to split array values ​​into different parts?

I have an array of numeric values:

array(16,7,45,23124,45,12231,32345,45,45456,23,456,1212,450...n); // ещё миллион значений

From it you need to make:
array(16,7,45,23124,45,45656,23,456...n);
array(45,12311,32345...n);
array(45456,23,456,1212,450...n);

We need an easy resource-free solution or function that will split this array into arrays of the same size. That is, so that the sum of the values ​​of the final arrays is not more than 100000 and at the same time the MINIMUM allowable number of arrays should be obtained.
Well, or just output values ​​or keys in the right order.
The problem is more complicated than it seems at first glance!
What are some ideas?
upd: Values ​​cannot be broken.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2017-04-12
@sergiks

Similar to the bin packing problem. There are many algorithms. You can solve quickly and suboptimally (first fit), better (best fit) or very long and optimal ( MTP algorithm (8Mb pdf, in English)).
First fit - go sequentially through the numbers and put them in the current container as long as they fit. When filling / overflowing, go to the next container.
Your data is random, there are no regularities, patterns in the distribution of values?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question