Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8938971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:44:31+00:00 2026-06-15T10:44:31+00:00

Possible Duplicate: Algorithm to find which number in a list sum up to a

  • 0

Possible Duplicate:
Algorithm to find which number in a list sum up to a certain number

question:

there is a list: [1,2,3,4,6,8,10,12], i want to use these numbers to sum up to a new number 16.

rules:

1) don’t need to use all of the numbers, 6 + 10 will be ok.

2) a number can use multi times, 12+2+1+1 will be ok.

3) order matters, 12 + 6 and 6 + 12 are two different combinations.

i have see algorithm to sum up a list of numbers for all combinations, but this is not the same.

don’t know much about algorithm, if this fit certain algorithm, please let me know, or some python code / pseudo-code will be much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:44:32+00:00Added an answer on June 15, 2026 at 10:44 am

    First – note that even finding if there is any subset that sums to the desired number is NP-Complete and is known as the subset sum problem, so there is no known polynomial solution for it.

    Now, regarding the specific issue, here are some options:

    First there is of course the obvious “generate all subsets and check the sum” way. Note that if your elements are all non-negative, you can use branch and bound and terminate a large portion of the possibilities before you actually develop them (If you found a subset X with sum(X) == s, and you are looking for the number n < s – you can be sure any set containing X will NOT find the solution). Something along the lines of:

    findSubsets(list,sol,n):
      if (list.empty() and n == 0): #found a feasible subset!
         print sol 
         return
      else if (n < 0): #bounding non feasible solutions
         return 
      else if (list.empty()): #a solution that sums to a smaller number then n
         return
      e <- list.removeAndReturnFirst()
      sol <- sol.add(e)
      findSubsets(list,sol,n-e)
      sol <- sol.removeLast()
      findSubsets(list,sol,n)
      list.addFirst(e) #cleanup, return the list to original state
    

    Invoke with findSubsets(list,[],n) where list is your list of items, n is the desired number and [] is an empty list.

    Note that it can be easily parallelized if needed, there is no real syncrhonization needed between two subsets explored.


    Another alternative if the list contains only integers is using Dynamic Programming for solving the subset sum problem. Once you have the matrix you can re-create all the elements from the table by going back in the table. This similar question discusses how to get a list from the knapsack DP solution. The principles of the two problems are pretty much the same.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Algorithm to find Lucky Numbers I came across this question.A number is
Possible Duplicate: Find the maximum interval sum in a list of real numbers. I
Possible Duplicate: Easy interview question got harder: given numbers 1..100, find the missing number(s)
Possible Duplicate: Algorithm to find the total number of connected sets in a matrix
Possible Duplicate: Algorithm to find minimum number of weighings required to find defective ball
Possible Duplicate: Finding a single number in a list Given an array of numbers,
Possible Duplicate: Quickest way to find missing number in an array of numbers Input:
Possible Duplicate: Algorithm to find the maximum sum in a sequence of overlapping intervals
Possible Duplicate: substring algorithm Given two strings, A and B, how to find the
Possible Duplicate: Algorithm for generating a random number is posible to generate a random

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.