14 June 2018

Find New Friends in Social Network

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. N lines contain the name of the friend F followed by the number of friends of F finally their names separated by space.
Input:

3
Mani 3 Ram Raj Guna
Ram 2 Kumar Kishore
Mughil 3 Praveen Naveen Mani
Output:

Raj Guna Kumar Kishore Praveen Naveen
Explanation:
Ram is not present in the output as Ram is already John's friend.


import java.util.*;
public class NewClass22 {
    void findFriend(int n,String a[],ArrayList<String> ar)
    {
     
        for(int i=0;i<ar.size();i++)
       {
                  int c=0;
           for(int j=0;j<a.length;j++)
           {
                if(ar.get(i).toLowerCase().equals(a[j].toLowerCase()))
                {
                    c=1;
                    break;
                }
           } if(c==0)
             System.out.print(ar.get(i)+" ");
       }
    }
   
public static void main(String args[])
    {
        Scanner s=new Scanner(System.in);
        ArrayList<String> ar=new ArrayList<String>();
        int n=s.nextInt();
        String a[]=new String[n];
        for(int i=0;i<n;i++)
        {
            a[i]=s.next();
            int l=s.nextInt();
            for(int j=0;j<l;j++)
            {
                ar.add(s.next());
            }
        }
        NewClass22 ob=new NewClass22();
        ob.findFriend(n,a,ar);
    }
}

No comments:

Post a Comment