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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:05:53+00:00 2026-05-11T05:05:53+00:00

I’m looking for a way to generate combinations of objects ordered by a single

  • 0

I’m looking for a way to generate combinations of objects ordered by a single attribute. I don’t think lexicographical order is what I’m looking for… I’ll try to give an example. Let’s say I have a list of objects A,B,C,D with the attribute values I want to order by being 3,3,2,1. This gives A3, B3, C2, D1 objects. Now I want to generate combinations of 2 objects, but they need to be ordered in a descending way:

  • A3 B3
  • A3 C2
  • B3 C2
  • A3 D1
  • B3 D1
  • C2 D1

Generating all combinations and sorting them is not acceptable because the real world scenario involves large sets and millions of combinations. (set of 40, order of 8), and I need only combinations above the certain threshold.

Actually I need count of combinations above a threshold grouped by a sum of a given attribute, but I think it is far more difficult to do – so I’d settle for developing all combinations above a threshold and counting them. If that’s possible at all.

EDIT – My original question wasn’t very precise… I don’t actually need these combinations ordered, just thought it would help to isolate combinations above a threshold. To be more precise, in the above example, giving a threshold of 5, I’m looking for an information that the given set produces 1 combination with a sum of 6 ( A3 B3 ) and 2 with a sum of 5 ( A3 C2, B3 C2). I don’t actually need the combinations themselves.

I was looking into subset-sum problem, but if I understood correctly given dynamic solution it will only give you information is there a given sum or no, not count of the sums.

Thanks

  • 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. 2026-05-11T05:05:54+00:00Added an answer on May 11, 2026 at 5:05 am

    Actually, I think you do want lexicographic order, but descending rather than ascending. In addition:

    • It’s not clear to me from your description that A, B, … D play any role in your answer (except possibly as the container for the values).
    • I think your question example is simply ‘For each integer at least 5, up to the maximum possible total of two values, how many distinct pairs from the set {3, 3, 2, 1} have sums of that integer?’
    • The interesting part is the early bailout, once no possible solution can be reached (remaining achievable sums are too small).

    I’ll post sample code later.

    Here’s the sample code I promised, with a few remarks following:

    public class Combos {      /* permanent state for instance */     private int values[];     private int length;      /* transient state during single 'count' computation */     private int n;     private int limit;     private Tally<Integer> tally;     private int best[][];  // used for early-bail-out      private void initializeForCount(int n, int limit) {         this.n = n;         this.limit = limit;         best = new int[n+1][length+1];         for (int i = 1; i <= n; ++i) {             for (int j = 0; j <= length - i; ++j) {                 best[i][j] = values[j] + best[i-1][j+1];             }         }     }      private void countAt(int left, int start, int sum) {         if (left == 0) {             tally.inc(sum);         } else {             for (                 int i = start;                 i <= length - left                 && limit <= sum + best[left][i];  // bail-out-check                 ++i             ) {                 countAt(left - 1, i + 1, sum + values[i]);             }         }     }      public Tally<Integer> count(int n, int limit) {         tally = new Tally<Integer>();         if (n <= length) {             initializeForCount(n, limit);             countAt(n, 0, 0);         }         return tally;     }      public Combos(int[] values) {         this.values = values;         this.length = values.length;     }  } 

    Preface remarks:

    This uses a little helper class called Tally, that just isolates the tabulation (including initialization for never-before-seen keys). I’ll put it at the end.

    To keep this concise, I’ve taken some shortcuts that aren’t good practice for ‘real’ code:

    • This doesn’t check for a null value array, etc.
    • I assume that the value array is already sorted into descending order, required for the early-bail-out technique. (Good production code would include the sorting.)
    • I put transient data into instance variables instead of passing them as arguments among the private methods that support count. That makes this class non-thread-safe.

    Explanation:

    An instance of Combos is created with the (descending ordered) array of integers to combine. The value array is set up once per instance, but multiple calls to count can be made with varying population sizes and limits.

    The count method triggers a (mostly) standard recursive traversal of unique combinations of n integers from values. The limit argument gives the lower bound on sums of interest.

    The countAt method examines combinations of integers from values. The left argument is how many integers remain to make up n integers in a sum, start is the position in values from which to search, and sum is the partial sum.

    The early-bail-out mechanism is based on computing best, a two-dimensional array that specifies the ‘best’ sum reachable from a given state. The value in best[n][p] is the largest sum of n values beginning in position p of the original values.

    The recursion of countAt bottoms out when the correct population has been accumulated; this adds the current sum (of n values) to the tally. If countAt has not bottomed out, it sweeps the values from the start-ing position to increase the current partial sum, as long as:

    • enough positions remain in values to achieve the specified population, and
    • the best (largest) subtotal remaining is big enough to make the limit.

    A sample run with your question’s data:

        int[] values = {3, 3, 2, 1};     Combos mine = new Combos(values);     Tally<Integer> tally = mine.count(2, 5);     for (int i = 5; i < 9; ++i) {         int n = tally.get(i);         if (0 < n) {             System.out.println('found ' + tally.get(i) + ' sums of ' + i);         }     } 

    produces the results you specified:

    found 2 sums of 5 found 1 sums of 6 

    Here’s the Tally code:

    public static class Tally<T> {     private Map<T,Integer> tally = new HashMap<T,Integer>();     public Tally() {/* nothing */}     public void inc(T key) {         Integer value = tally.get(key);         if (value == null) {             value = Integer.valueOf(0);         }         tally.put(key, (value + 1));     }     public int get(T key) {         Integer result = tally.get(key);         return result == null ? 0 : result;     }     public Collection<T> keys() {         return tally.keySet();     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 109k
  • Answers 109k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer XSLT / XPath 1.0: <!-- a space-separated list of valid… May 11, 2026 at 9:21 pm
  • Editorial Team
    Editorial Team added an answer That's what the browser does when it doesn't know the… May 11, 2026 at 9:21 pm
  • Editorial Team
    Editorial Team added an answer Chris's answer is best if there are no lines with… May 11, 2026 at 9:21 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.