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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:58:09+00:00 2026-05-18T06:58:09+00:00

Can someone please share a program in java which does the following. if given

  • 0

Can someone please share a program in java which does the following.
if given the following sets as inputs,

a={1,2,3,8,9,10}
b={1,2,3,4,5}
c={4,5,7}
d={5,6,7}
e={6,7,8,9,10}

and

U={1,2,3,4,5,6,7,8,9,10}

the program will find all the combinations of the set and find out the minimum number of sets that together has all the elements of U.

In the above example, the minimum number is 2. set b and e together covers all of U.
So basically, it is a set covering problem. In the Set Covering problem, we are given a universe U, such that |U|=n, and sets S1,……,Sk are subsets of U. A set cover is a collection C of some of the sets from S1,……,Sk whose union is the entire universe U. Additionally, we must minimize the cost of the sets.

  • 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-18T06:58:09+00:00Added an answer on May 18, 2026 at 6:58 am

    What you need is to test increasing sizes of combinations, like so

    interface Filter<T> {
        boolean matches(T t);
    }
    public static void main(String... args) throws IOException {
        Integer[][] arrayOfSets = {
                {1, 2, 3, 8, 9, 10},
                {1, 2, 3, 4, 5},
                {4, 5, 7},
                {5, 6, 7},
                {6, 7, 8, 9, 10},
        };
        Integer[] solution = {1,2,3,4,5,6,7,8,9,10};
    
        List<Set<Integer>> listOfSets = new ArrayList<Set<Integer>>();
        for (Integer[] array : arrayOfSets)
            listOfSets.add(new LinkedHashSet<Integer>(Arrays.asList(array)));
        final Set<Integer> solutionSet = new LinkedHashSet<Integer>(Arrays.asList(solution));
    
        Filter<Set<Set<Integer>>> filter = new Filter<Set<Set<Integer>>>() {
            public boolean matches(Set<Set<Integer>> integers) {
                Set<Integer> union = new LinkedHashSet<Integer>();
                for (Set<Integer> ints : integers)
                    union.addAll(ints);
                return union.equals(solutionSet);
            }
        };
    
        Set<Set<Integer>> firstSolution = shortestCombination(filter, listOfSets);
        System.out.println("The shortest combination was "+firstSolution);
    }
    
    private static <T> Set<T> shortestCombination(Filter<Set<T>> filter, List<T> listOfSets) {
        final int size = listOfSets.size();
        if (size > 20) throw new IllegalArgumentException("Too many combinations");
        int combinations = 1 << size;
        List<Set<T>> possibleSolutions = new ArrayList<Set<T>>();
        for(int l = 0;l<combinations;l++) {
            Set<T> combination = new LinkedHashSet<T>();
            for(int j=0;j<size;j++) {
                if (((l >> j) & 1) != 0)
                    combination.add(listOfSets.get(j));
            }
            possibleSolutions.add(combination);
        }
        // the possible solutions in order of size.
        Collections.sort(possibleSolutions, new Comparator<Set<T>>() {
            public int compare(Set<T> o1, Set<T> o2) {
                return o1.size()-o2.size();
            }
        });
        for (Set<T> possibleSolution : possibleSolutions) {
            if (filter.matches(possibleSolution))
                return possibleSolution;
        }
        return null;
    }
    

    Prints

    The shortest combination was [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please share a extremely simple version of JQuery Ajax with pagination??
Can someone please explain how Read/Show works.. I cannot find any tutorials on it.
Can someone please share a library/code to access a USB 3G dongle in C#..?
Can someone please explain how JAR files and the Java class loader make use
Can someone please elaborate the following related to strategy pattern: Strategies increase the number
Can someone please tell me what this means: 07-04 09:54:38.048: I/DetailActivity(15496): Title that is
Can someone please explain why ?___SID=U is appearing in some Magento URLs on my
Can someone please provide an example of how to store, and read xml data
Can someone please explain to me how this responsive approach works? This was done
Can someone please fill in the blanks for me, including a brief description of

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.