/** * PROBLEM STATEMENT * In a social network, a person can invite friends of his/her friend. John wants to invite and add new friends. Complete the program below so that it prints the names of the persons to whom John can send a friend request. * Input Format: The first line contains the value of the N which represent the number of friends. Next N lines contain the name of the friend F followed by the number of friends of F and finally their names separated by space. * Boundary Conditions: 2 <= N <= 10 * Output Format: The names to which John can send a friend request. * Example Input/Output 1: * Input: 3 Mani 3 Ram Raj Guna Ram 2 Kumar Kishore Mughil 3 Praveen Naveen Ramesh * Output:Raj Guna Kumar Kishore Praveen Naveen Ramesh * * Explanation:Ram is not present in the output as Ram is already John's friend. * * Example Input/Output 2: * Input: 4 Arjun 3 Bhuvana Ramya Rajesh Naveen 2 Arjun Sangeetha Rajesh 3 Narmada Madan Suresh Suresh 2 Pawan Adhil * Output:Bhuvana Ramya Sangeetha Narmada Madan Pawan Adhil **/ import java.util.HashSet; import java.util.Scanner; import java.util.StringTokenizer; class Ideone { int N = sc.nextInt(); // No of friends sc.nextLine(); HashSet<String> hs = new HashSet<>(); // to store name of the John's friends for(int i =0; i < N; i++) { int j =0; while(str.charAt(j)!= ' ') { friend = friend + str.charAt(j++); hs.add(friend); // add the name before the number to HashSet } j= j+2; invitation=invitation+str.substring(j)+" "; // remove name of John's friend from the string and store back remaining string } int j =0; while(st.hasMoreTokens()) { if(!hs.contains(str)) { /* check if token(string) is already in hashset ie if the person already friend with John or not if he is not then print str */ } } } }
3 Mani 3 Ram Raj Guna Ram 2 Kumar Kishore Mughil 3 Praveen Naveen Ramesh