0K, here it the problem, I’ll try to make it as clear as possible.
So, we have an array of key-value pairs in the form unique id => number.
pseudo code array A= array(0 => 17, 1 => 26, 2 => 17, 3 => 2, 4 => 7, 5 => 8);
we see that the total of the numbers is 60. THIS IS ALWAYS CORRECT, SO THE TOTAL IN ARRAY A IS ALWAYS CORRECT.
now we have array B
in array B, also unique id => value pairs, BUT here some of the values are PHONY/INCORRECT, and total(array B)-total(array A) has to be 0.
i.e. for the upper example array A= array(6 => 22, 7 => 11, 8 => 8, 9 => 9, 10 => 5, 11 => 10, 12 => 7, 13 => 17, 14 => 10);
now, array B total is 99. 60-99=-39, which means we have to find all of the combinations of numbers that equal 39 from array B. in this case, just looking @ eye it seems the only 2 combinations of this are possible: that unique ids 10, 12 ,13, 14 are incorrect or that 10, 11, 12, 13 are PHONY/INCORRECT (5+7+17+10=39). now, with such small arrays it is easy to find these using loops and iterating through all possible options, but with arrays ranging in the thousands (of unique IDs). what is the best way to do this? how would the computer find the phony/incorrect pairs the fastest?
Also, I owe a beer to the person that gives the best (and good) answer.
This is only true if you know that all numbers that are wrong should have been zeros.
Any or all numbers in the array could contribute to the error by being off by such amounts that the total of the error amounts is 39.
That being said, the problem of finding a subset of a list of numbers that add to a particular sum is related to the Subset Sum problem, though the normal statement of the problem is only verifying the existence of such a subset, and you’re looking for a list of all such subsets.
I don’t believe an effective solution exists for large sets, and the brute force solution grows exponentially.