A
A
Alexey Vladimirov2014-02-05 11:14:46
PHP
Alexey Vladimirov, 2014-02-05 11:14:46

How to find a combination of any number of elements of the requests subarray, the sum of whose values ​​will be as close as possible to, or equal to, the value of max_sum?

Hello gentlemen.
There is an array like:

[test] => Array
                (
                    [max_sum] => 15
                    [requests] => Array
                        (
                            [0] => 1
                            [1] => 2
                            [2] => 3
                            [3] => 4
                            [4] => 5
                            [5] => 6
                            [6] => 7
                            [7] => 8
                            [8] => 9
                            [9] => 10
                            [10] => 3
                            [11] => 5
                            [12] => 5
                            [13] => 1
                            [14] => 7
                            [15] => 8
                            [16] => 5
                        )
                )

It is necessary to find a combination of any number of elements of the requests subarray (unique, i.e. without repetitions), the sum of whose values ​​will be maximally close to, or equal to, the value of max_sum.
The number of requests elements can be quite large, and this operation itself will have to be performed for a large number of such arrays, and it must be done "on the fly", i.e. speed matters.
I would be extremely grateful for any hints.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2014-02-05
@xvladimirov

Your task is nothing more than an interpretation of the " knapsack problem "
Look through, there are solution algorithms.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question