Sunday, December 15, 2013

Programming Challenges Question 5

Programming Challenges Question 

Gen is an event organizer. He has received data regarding the participation of employees in two different events. 
Some employees have participated in only one event and others have participated in both events. Gen now needs to count the number of employees who have taken part in both events. The records received by Gen consist of employee ids, which are unique. 

Write a program that accepts the employee ids participating in each event (the first line relates to the first event and the second line relates to the second event). The program should print the number of common employee ids in both the events.

Suppose the following input is given to the program, where each line represents a different event:
1001,1002,1003,1004,1005
1106,1008,1005,1003,1016,1017,1112

Now the common employee ids are 1003 and 1005, so the program should give the output as:
2

/************************************************Code in JAVA********************************************************/
import java.io.*;
public class Main {
    public static void main(String[] args) {
    try{
       String data[]= new String[2];
       InputStreamReader sr= new InputStreamReader(System.in);
       BufferedReader br= new BufferedReader(sr);
       for(int i=0;i<2;i++)
       {
         data[i]=br.readLine();
       }

         String EventOneIds[]=data[0].split(",");
         String EventTwoIds[]=data[1].split(",");
         int totalCommonIds=0;
         for(int i=0;i<EventOneIds.length;i++)
         {
             for(int j=0;j<EventTwoIds.length;j++)
             {
                 if(EventOneIds[i].equals(EventTwoIds[j]))
                     totalCommonIds++;
             }
         }

         System.out.println( totalCommonIds);
        
        }
    catch(Exception ex)
    {
        System.out.println(ex.toString());
    }

 }

}




No comments:

Post a Comment