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

  • Home
  • SEARCH
  • 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 6901175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:38:25+00:00 2026-05-27T07:38:25+00:00

I am trying to develop an optimization function that will determine which elements in

  • 0

I am trying to develop an optimization function that will determine which elements in list of doubles when added together will be less than a specified threshold values. The elements can be used multiple times.

For example if my list of elements is

{1,3,7,10}  

and my threshold is 20 I would expect my result to be

1
3
7
10
10, 10
10, 7
10, 7, 3
10,7,1
10,7,1,1
10,7,1,1,1
7,7
7,7,3
7,7,1
7,7,1,1
7,7,1,1,1
...

I expect that the answer to this question will probably be a recursive call and probably could be found in a textbook, but I don’t know how to properly phrase the question to find the answer. Help from this group of experts would be 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-05-27T07:38:26+00:00Added an answer on May 27, 2026 at 7:38 am

    This program works, and seems to be the simplest solution. All results are sorted ascending.

      private static final HashSet<ArrayList<Double>> lists = 
           new HashSet<ArrayList<Double>>(); // all of the combinations generated
    
      private static final double[] elements = {10, 7, 3, 1};
    
      public static void main(String[] args) {
        combine(20, new ArrayList<Double>());
        for (ArrayList<Double> set : lists) {
          System.out.println(set);
        }
      }
    
      private static void combine(final double limit, ArrayList<Double> stack) {
        // iterates through the elements that fit in the threshold
        for (double item : elements) {
          if (item < limit) {
            final ArrayList<Double> nextStack = new ArrayList<Double>(stack);
            nextStack.add(item);
            // a sort is necessary to let the HashSet de-dup properly
            Collections.sort(nextStack);
            lists.add(nextStack);
            combine(limit - item, nextStack);
          }
        }
      }
    

    This type of combinatoric problem, though, generates many results. If you are more concerned with performance than code readability and simplicity, I can optimize further.

    c#:

    static void Main(string[] args)
        {
            Run();
        }
    
        static public void Run()
        {
            Combine(20, new List<Double>());
            foreach (List<Double> set in lists)
            {
    
                Debug.Print(set.ToString());
            }
        }
        private static HashSet<List<Double>> lists =
            new HashSet<List<Double>>(); // all of the combinations generated
    
        private static double[] elements = { 10, 7, 3, 1 };
    
        private static void Combine(double limit, List<Double> stack)
        {
            // iterates through the elements that fit in the threshold
            foreach (double item in elements)
            {
                if (item < limit)
                {
                    List<Double> nextStack = new List<Double>(stack);
                    nextStack.Add(item);
                    // a sort is necessary to let the HashSet de-dup properly
                    nextStack.Sort();
                    lists.Add(nextStack);
                    Combine(limit - item, nextStack);
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to develop an application that will use getImageData in javascript in Firefox
I am trying to develop a BlackBerry application that will show data from an
I'm trying to develop a JS function that creates a new row each time
I am trying to develop a simple map application, which will display a map
I am trying to develop a concept for a simple algorithm which will turn
I'm trying to develop a XMPP Proxy which will be in the middle of
I am trying to develop a menu which will: Check to see if /tmp
I am trying develop a basic referrer system to my Django website, system will
I'm trying to develop a firefox extension that inserts additional HTTP header fields into
I am trying develop an Android app which uses Google maps. So for the

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.