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 8678153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:38:28+00:00 2026-06-12T20:38:28+00:00

Possible Duplicate: Algorithm: Extract subset based on property sum in the simple case we

  • 0

Possible Duplicate:
Algorithm: Extract subset based on property sum

in the simple case we have an array:

{6, 1, 3, 11, 2, 5,12}

and we want to know all combinations the sum of elements contained in that array to getting 12.

in this case we will get four combinations:

  • 12
  • 1 + 11
  • 6 + 5 + 1
  • 1 + 3 + 2 + 6

so, how can we do this in BASIC or PHP?. maybe pseudo-code first ;-).

I’ve been looking but there just got a combination with a predetermined number of elements.

  • 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-12T20:38:32+00:00Added an answer on June 12, 2026 at 8:38 pm

    The problem is NP-Hard. Even determining if there is ANY subset of the problem that sums to the desired number is NP-Hard (known as the subset sum problem), and there is no known polynomial solution to it.

    Thus, you should look for an exponantial solution, such as a backtracking one – generate all possible combinations, and check if they are valid.

    You can use trimming to get your search faster (for example, if you generate a partial subset of sum 13, no need to check other subsets which are supersets of this subset, since they will definetly won’t lead to a solution.

    pseudo code:

    findValidSubsets(sum,arr,idx,currSolution):
       if (sum == 0):
           print currSolution
       if (sum < 0): //trim the search, it won't be succesful
           return
       //one possibility: don't take the current candidate
       findPermutations(sum,arr,idx+1,currSolution) 
    
       //second poassibility: take the current candidate
       currSolution.add(arr[idx])
       findPermutations(sum-arr[idx],arr,idx+1,currSolution)
    
       //clean up before returning: 
       currSolution.removeLast()
    

    Complexity is O(2^n) – need to generate at worst case all 2^n possible subsets

    Invoke with findValidSubsets(desiredSum,myArray,0,[]), where [] is an empty initial list

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

Sidebar

Related Questions

Possible Duplicate: Does anyone have a good Proper Case algorithm I am currently in
Possible Duplicate: Python k-means algorithm I want to cluster 10000 indexed points based on
Possible Duplicate: optimal algorithm for finding unique divisors I have asked this question before,
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: Generating all Possible Combinations I'm trying to do a little algorithm in
Possible Duplicate: C#: Is using Random and OrderBy a good shuffle algorithm? I want
Possible Duplicate: Getting the submatrix with maximum sum? Given a 2-dimensional array of positive
Possible Duplicate: Akima interpolation of an array of doubles I'm searching for an algorithm
Possible Duplicate: An algorithm to "spacify" CamelCased strings I have a string like this:
Possible Duplicate: How to apply DBSCAN algorithm on grouping of similar url I have

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.