Mathematics Problems at HackerRank
Problem 3 : Handshake Probelm
At the annual meeting of Board of Directors of Acme Inc, every one starts shaking hands with everyone else in the room. Given the fact that any two persons shake hand exactly once, Can you tell the total count of handshakes?
Say there are n people. The first person shakes his hand with the other n-1
people.
The second person then shakes his hand with the other n-2
people.
And so on until the (n-1)th
person shakes his hand with the nth person.
So the number of handshakes is (n-1) + (n-2)... + 3 + 2 + 1
which equals (n-1)*(n)/2.
def handshake(n):
return int(n*(n-1)/2)