Write a function which return a list of all of the n element
Write a function which return a list of all of the n element subset of a given set
Implement in JAVA
Question 3 Write a function that returns a list of all of the n-element subsets of a given set. For example, if the given set is [1,2,3] and n is 2, then the returned list should contain [1,2], [1,3], and [2,3]. The order of the elements is not important.Solution
The program for this problem is as follows:
Comments are included in the program for explaining the steps.
A sample output is attached below the program.
package com.temp;
import java.util.ArrayList;
 import java.util.List;
 import java.util.Scanner;
 /**
 * Program to find sublists of a list.
 * @author Rohit Phatak
 *
 */
 public class subset {
    /**
    * This list will contain the resultant sublists.
    */
    static List<String[]> list = new ArrayList<>();
   
    public static void main(String arg[]){
        /**
        * Input a list comma separated.
        */
        System.out.println(\"Enter a list : \");
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String[] sArr = s.split(\",\");
        /**
        * Input a value of n.
        */
        System.out.println(\"Enter value of n : \");
        Integer n = scanner.nextInt();
        /**
        * Check if value of n is greater than entered list.
        */
        if(sArr.length < n){
            System.out.println(\"Length of list is less than value of n\");
        } else if(sArr.length > 0){
            getSubsetList(sArr,n);
            System.out.println(\"List is as :\");
            /**
            * Printing the sublists.
            */
            for(String[] str : list){
                String printVar = \"\";
                for(String temps : str)
                    printVar+=temps+\",\";
                System.out.println(\"[\"+printVar.substring(0, printVar.length()-1)+\"]\");
            }
        } else {
            System.out.println(\"Please enter atleast one element:\");
        }
        scanner.close();
    }
    /**
    * This method calls the recursive function which is used to obtain the sublists.
    * @param sArr
    * @param n
    * @return
    */
    public static List<String[]> getSubsetList(String[] sArr,Integer n){
        List<String[]> lista = new ArrayList<>();
        sublist(sArr, 0, n, new String[n]);
        return lista;
    }
    /**
    * This is a recursive function.
    * This function will iterate over itself for finding the sublist.
    *
    * Parameters are as follows:
    *
    * @param arr -> List entered by the user.
    * @param n -> Value of sublist entered by user.
    * @param startPosition -> index to start creating the sublist.
    * @param result -> resultant sublist
    */
    public static void sublist(String[] arr, int startPosition, int n, String[] result){
        /**
        * Check for the value of n ==0.
        * If n==0 break from the recursion.
        */
         if (n == 0){
            /**
            * Add the sublist \'result\' in the list.
            */
            list.add(result.clone());
             return;
         }
         /**
          * Iterate through for loop starting from startPosition till length of list minus entered value n.
          */
         for (int i = startPosition; i <= arr.length-n; i++){
            /**
            * Here result is a temporary sublist.
            * This for loop adds an element to the temporary sublist in each iteration.
            * The element is added from the complete list entered by user.
            * Location of element is iTh index.
            */
             result[result.length - n] = arr[i];
             /**
              * After adding the element to the temporary sublist, the sublist and the updated start and end positions are recurssed to itself.
              */
             sublist(arr, i+1, n-1, result);
         }
     }
 }
Sample output of the above program :
Enter a list :
 1,2,3,5
 Enter value of n :
 2
 List is as :
 [1,2]
 [1,3]
 [1,5]
 [2,3]
 [2,5]
 [3,5]



