code in java Write a program to do the following It will pro

*code in java*

Write a program to do the following. It will prompt the user to enter a positive integer n. It will then prompt the user to enter a non-negative integer r such that 0 lessthanorequalto r lessthanorequalto n. You do not have to check for the integrity of the entered data, i.e., assume they satisfy the desired properties. Your program then must output all size r subsets of {1, 2, ..., n} in lexicographic increasing order. For example if I enter 4 for n and 2 for r the program should output (one subset on each line) the following: {1, 2} {1, 3} {1, 4} {2, 3} {2, 4} {3, 4}

Solution

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
public class subSets {
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       List<Integer> set = new ArrayList<>();
       Scanner scan = new Scanner(System.in);
       System.out.print(\"Enter a positive integer:\");
       int n = scan.nextInt();
       System.out.print(\"Enter a non-negative integer:\");
       int r = scan.nextInt();
       for(int i=1;i<=n;i++)
       {
           set.add(i);
       }
       get(set,r,0,new HashSet<Integer>());
   }

   private static void get(List<Integer> set, int r, int pnt, Set<Integer> temp) {
       // TODO Auto-generated method stub
       if(temp.size()==r)
       {
           int i=0;
           System.out.print(\'{\');
           for (Integer s : temp) {
               if(i>0)
               {
                   System.out.print(\',\');
               }
           System.out.print(s);
           i++;
           }
           System.out.println(\'}\');  
       }
       if(pnt==set.size())
       {
           return;
       }
       temp.add(set.get(pnt));
       get(set,r,pnt+1,temp);
       temp.remove(set.get(pnt));
       get(set,r,pnt+1,temp);
   }

}

*code in java* Write a program to do the following. It will prompt the user to enter a positive integer n. It will then prompt the user to enter a non-negative

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site