This is really a problem with a backpack, but I will try to give a complete solution that is not optimal, but illustrates a complete strategy for solving your problem.
First of all, you can do this with just one iteration over an array of numbers without recursion and without preliminary sorting. Dynamic programming is all you need, tracking all the previously possible paths of a partial sum. The idea is somewhat similar to your described recursive method, but we can do it iteratively and without pre-configuration.
Assuming the input array [84, 38, 12, 13, 17, 2, 78, 1, 1]
and target 29, we scroll the numbers like this:
* 84 - too big, move on * 38 - too big, move on * 12 - gives us a subtarget of 29-12 = 17 subtargets: 17 (paths: 12) * 13 - gives us a subtarget of 29-13=16 subtargets: 16 (paths: 13) 17 (paths: 12) * 17 - is a subtarget, fulfilling the '12' path; and gives us a subtarget of 29-17=12 subtargets: 12 (paths: 17) 16 (paths: 13) 17 (paths: 12) solutions: 12+17 etc.
The trick is that when we subTargets
numbers, we save the subTargets
lookup subTargets
, which is a number that will give us a solution using one or more combinations ("paths") of the numbers we saw before. If the new number is subTarget, we add to our list of solutions; if not, then add to the existing paths, where num<subTarget
and move on.
PHP quick and dirty function for this:
// Note: only positive non-zero integer values are supported // Also, we may return duplicate addend sets where the only difference is the order function findAddends($components, $target) { // A structure to hold our partial result paths // The integer key is the sub-target and the value is an array of string representations // of the 'paths' to get to that sub-target. Eg for target=29 // subTargets = { // 26: { '=3':true }, // 15: { '=12+2':true, '=13+1':true } // } // We are (mis)using associative arrays as HashSets $subTargets = array(); // And our found solutions, stored as string keys to avoid duplicates (again using associative array as a HashSet) $solutions = array(); // One loop to Rule Them All echo 'Looping over the array of values...' . PHP_EOL; foreach ($components as $num) { echo 'Processing number ' . $num . '...' . PHP_EOL; if ($num > $target) { echo $num . ' is too large, so we skip it' . PHP_EOL; continue; } if ($num == $target) { echo $num . ' is an exact match. Adding to solutions..' . PHP_EOL; $solutions['='.$num] = true; continue; } // For every subtarget that is larger than $num we get a new 'sub-subtarget' as well foreach ($subTargets as $subTarget => $paths) { if ($num > $subTarget) { continue; } if ($num == $subTarget) { echo 'Solution(s) found for ' . $num . ' with previous sub-target. Adding to solutions..' . PHP_EOL; foreach ($paths as $path => $bool) { $solutions[$path . '+' . $num] = true; } continue; } // Our new 'sub-sub-target' is: $subRemainder = $subTarget-$num; // Add the new sub-sub-target including the 'path' of addends to get there if ( ! isset($subTargets[$subRemainder])) { $subTargets[$subRemainder] = array(); } // For each path to the original sub-target, we add the $num which creates a new path to the subRemainder foreach ($paths as $path => $bool) { $subTargets[$subRemainder][$path.'+'.$num] = true; } } // Subtracting the number from our original target gives us a new sub-target $remainder = $target - $num; // Add the new sub-target including the 'path' of addends to get there if ( ! isset($subTargets[$remainder])) { $subTargets[$remainder] = array(); } $subTargets[$remainder]['='.$num] = true; } return $solutions; }
Run the code as follows:
$componentArr = array(84, 38, 12, 13, 17, 2, 78, 1, 1); $addends = findAddends($componentArr, 29); echo 'Result:'.PHP_EOL; foreach ($addends as $addendSet => $bool) { echo $addendSet . PHP_EOL; }
which outputs:
Looping over the array of values... Processing number 84... 84 is too large, so we skip it Processing number 38... 38 is too large, so we skip it Processing number 12... Processing number 13... Processing number 17... Solution(s) found for 17 with previous sub-target. Adding to solutions.. Processing number 2... Processing number 78... 78 is too large, so we skip it Processing number 1... Processing number 1... Solution(s) found for 1 with previous sub-target. Adding to solutions.. Result: =12+17 =12+13+2+1+1