Selecting values from list to make up totals in second list
-
Hi guys. My google foo seems to have left me this morning as well as my cognitive abilities. Lets say I have a list of 3 numbers, say (31549, 20468, 17625), that together add up to 69642. Say I have another list of numbers, (10211,8821,6894,6497,6482,5623,5594,5132,4471,4326,3245,2346), that also add up to 69642. I need to select the numbers from the second list that add up to the individual numbers in the first list. In my example the result I would expect would be 10211 + 5623 + 6894 + 8821 = 31549 3245 + 6497 + 5132 + 5594 = 20468 21346 + 6482 + 4326 + 4471 = 17625 Hope someone has some ideas to help. Thanks in advance
Everyone dies - but not everyone lives
-
Hi guys. My google foo seems to have left me this morning as well as my cognitive abilities. Lets say I have a list of 3 numbers, say (31549, 20468, 17625), that together add up to 69642. Say I have another list of numbers, (10211,8821,6894,6497,6482,5623,5594,5132,4471,4326,3245,2346), that also add up to 69642. I need to select the numbers from the second list that add up to the individual numbers in the first list. In my example the result I would expect would be 10211 + 5623 + 6894 + 8821 = 31549 3245 + 6497 + 5132 + 5594 = 20468 21346 + 6482 + 4326 + 4471 = 17625 Hope someone has some ideas to help. Thanks in advance
Everyone dies - but not everyone lives
Create an array of the second numbers. Take each of the first set in turn and loop through the second arraylooking for the combination that sums to the selected number. You will need to loop through the array taking each number in turn and adding one or more of the others looking for a combination that makes the sum.
-
Create an array of the second numbers. Take each of the first set in turn and loop through the second arraylooking for the combination that sums to the selected number. You will need to loop through the array taking each number in turn and adding one or more of the others looking for a combination that makes the sum.
Ah! the brute force approach. Sometimes the obvious escapes you. Many thanks for kickstarting my brain this morning. Much appreciated
Everyone dies - but not everyone lives
-
Hi guys. My google foo seems to have left me this morning as well as my cognitive abilities. Lets say I have a list of 3 numbers, say (31549, 20468, 17625), that together add up to 69642. Say I have another list of numbers, (10211,8821,6894,6497,6482,5623,5594,5132,4471,4326,3245,2346), that also add up to 69642. I need to select the numbers from the second list that add up to the individual numbers in the first list. In my example the result I would expect would be 10211 + 5623 + 6894 + 8821 = 31549 3245 + 6497 + 5132 + 5594 = 20468 21346 + 6482 + 4326 + 4471 = 17625 Hope someone has some ideas to help. Thanks in advance
Everyone dies - but not everyone lives
Might be worth having a look at this too Perfect Sum Problem (Print all subsets with given sum) - GeeksforGeeks[^]