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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:29:05+00:00 2026-06-10T13:29:05+00:00

I have a set of objects. I also have a set of required objects,

  • 0

I have a set of objects. I also have a set of required objects, containing between 0 and 3 objects. I’m trying to find all combinations of 3 objects from the initial set that include the required objects.

Edit: Example —

objects: {A,B,C,D,E}
required: {A}
output: {A,B,C}, {A,B,D}, {A,B,E}, {A,C,D}, {A,C,E}, {A,D,E}

objects: {A,B,C,D,E}
required: {A,B}
output: {A,B,C}, {A,B,D}, {A,B,E}

The obvious, but slow, solution is something like:

for(int i = 0; i < objects.size()-2 ; i++){
  for(int j = i; j < objects.size()-1 ; j++){
    for(int k = j; k < objects.size() ; k++){
      if(required.contains(i) || required.contains (j) || required.contains(k)){
        results.add(new Result(i,j,k));
      }
    }
  }
}

This solution traverses the entire search space regardless of required objects.

Another approach I came up with was to write custom code for each number of required objects:

switch (required.size()){
case 3:
  results.add(new Result(required));
  break;
case 2:
  Object a = requiredIngredients.toArray()[0];
  Object b = requiredIngredients.toArray()[1];
  for(Object o : objects){
    if(!required.contains(i)){
      results.add(new Result(EnumSet.of(o, a, b)));
    }
  }
  break;

etc..

This works, but is obnoxious and not generalizable.

A third approach is to make three separate sets, shrinking sets to include only a required value if requirements are present, then iterate over them normally:

  for(Object i : firstObjects){
    for(Object j : secondObjects){
      if(i == j) continue;
      for(Object k : thirdObjects){
        if(j == k) continue;
        results.add(new Result(i,j,k));
      }
    }
  }

This seems somewhat better, but produces duplicate combinations. Eg. It would return both ABC and ACB as two separate results. I could have the result set recognize duplicates, but I’d prefer not to calculate it to begin with.

Hopefully these examples make the problem clear. I feel like someone has figured out how to solve this kind of problem already, but I’m having a hard time identifying the generalized problem type.

  • 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-10T13:29:07+00:00Added an answer on June 10, 2026 at 1:29 pm

    Here is a simple and general way, but maybe not the most efficient (in python):

    from itertools import combinations
    
    def combinations_including(n,objects,required):
      extra=objects-required
      return sorted([sorted(tuple(required)+x) for x in combinations(extra,n-len(required))])
    
    print combinations_including(3,set('ABCDE'),set('A'))
    print combinations_including(3,set('ABCDE'),set('AB'))
    

    outputs

    [['A', 'B', 'C'], ['A', 'B', 'D'], ['A', 'B', 'E'], ['A', 'C', 'D'], ['A', 'C', 'E'], ['A', 'D', 'E']]
    [['A', 'B', 'C'], ['A', 'B', 'D'], ['A', 'B', 'E']]
    

    If the required set is small, then it is probably worse than brute force, but as the size of the required set increases, it will become much more efficient than brute force. For example, if the required set is the same as the input set, then it finds the answer in constant time.

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

Sidebar

Related Questions

I have a set of objects that I want to conform to an interface,
I have an odd problem with Django. I have a set of objects that
Say I have a set myset of custom objects that may be equal although
I have a class that serializes a set of objects (using XML serialization) that
I have an API that filters a set of objects based on the ID
I have a requirement to graphically depict relationships between a set of objects as
I have a set of JPA POJO's that contain annotations required for mapping to
I have a set of objects I'd like to do some operations on, in
I have several objects set up in Core Data, one of which is Deck
I have set up a Core Data model where I have two objects, say

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.