Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. Selecting values from list to make up totals in second list

Selecting values from list to make up totals in second list

Scheduled Pinned Locked Moved Algorithms
helptutorial
4 Posts 3 Posters 9 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wayne Gaylard
    wrote on last edited by
    #1

    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

    L F 2 Replies Last reply
    0
    • W Wayne Gaylard

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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.

      W 1 Reply Last reply
      0
      • L Lost User

        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.

        W Offline
        W Offline
        Wayne Gaylard
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • W Wayne Gaylard

          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

          F Offline
          F Offline
          F ES Sitecore
          wrote on last edited by
          #4

          Might be worth having a look at this too Perfect Sum Problem (Print all subsets with given sum) - GeeksforGeeks[^]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups