In: Computer Science
IN JAVA PLEASE
Implement a recursive approach to showing all the teams that can be created from a group (n things taken k at a time). Write the recursive showTeams()method and a main() method to prompt the user for the group size and the team size to provide arguments for showTeam(), which then displays all the possible combinations.
Program code screenshot
Program Sample Input/Output Screenshot
Program Code to copy
import java.util.*;
public class Main {
//current_person refers the the current person in the group who will
//either be taken in the team or not taken in the team
//current_team_size refers to the number of person who are already a part of the team
public static void showAllTeams(int groupSize, int teamSize, int current_person, int current_team_size, String current_team){
//base case is if we have sufficient menber to make a team
if(current_team_size==teamSize){
//printthe team and return
System.out.println(current_team);
return;
}else if(current_person>groupSize){
//this means we have checked all the people in the group
return;
}
//if he is in the team then add him/her in team then check next person
//also increment current_team_size
showAllTeams(groupSize, teamSize, current_person+1, current_team_size+1, current_team+current_person+" ");
//here we have an option of either taking the current_person in the team or not
//if he/she is not in the team check next person
showAllTeams(groupSize, teamSize, current_person+1, current_team_size, current_team);
}
public static void main(String[] args) {
int groupSize, teamSize;
Scanner sc = new Scanner(System.in);
System.out.print("Enter group size: ");
groupSize=sc.nextInt();
System.out.print("Enter team size: ");
teamSize=sc.nextInt();
sc.close();
showAllTeams(groupSize, teamSize, 1, 0, "");
}
}