This blog will soon be merged with

JavaByPatel

which explains each solution in detail, to visit new blog, click JavaByPatel

Thursday, 1 January 2015

Number Of Handshake Problem


Question :: In a party of N people, each person will shake her/his hand with each other person only once. 
   On total how many hand-shakes would happen?

Solution ::
  There are N persons. 
  Each person shake-hand with each other only once. 
  
  1st person = (n-1)
  2nd person = (n-2)
  3rd person = (n-3)
  nth person = (0)
  
  Consider example of 4 persons.
  1st 2nd 3rd 4th
  
  1st = 4-1 = 3
  2nd = 4-2 = 2
  3rd = 4-3 = 1
  4th = 4-4 = 0
  
  If we make a table, 

     Person    Number of Handshakes
     --------    ----------------------
        1            0
        2            1
        3            2
        4            3
        .            .
        .            .
        n           n-1

 So if we have N person, the number of handshake will be

   1 + 2 + 3 + ... + (N-1)

    
    Sum of Numbers 1-500
    ------------------------
                         N(N+1)
   1 + 2 + 3 + ... + N =  ------
                              2

 But we have to make one little adjustment. If you add the first N 
 numbers, you get
   
 which isn't quite the same formula. But we're adding, not the first N 
 numbers, but the first (N-1) numbers. So replace N with (N-1).
 
                             (N-1)((N-1)+1)      
 1 + 2 + 3 + ... + (N-1) =  --------------
                                   2

                             (N-1)N
                           = ------
                                  2


No comments:

Post a Comment