There are N persons in a room. Find the maximum number of Handshakes possible. Given the fact that any two persons shake hand exactly once.
Input : N = 2 Output : 1. There are only 2 persons in the room. 1 handshake take place. Input : N = 10 Output : 45.
To maximize the number of handshakes, each person should shake hand with every other person in the room. For the first person, there would be N-1 handshakes. For second person there would N-1 person available but he had already shaken hand with the first person. So there would be N-2 handshakes and so on.
So, Total number of handshake = N-1 + N-2 +….+ 1 + 0, which is equivalent to ((N-1)*N)/2
(using the formula of sum of first N natural number).
Below is the implementation of this problem.
Time Complexity : O(1)
This article is contributed by Anuj Chauhan(anuj0503). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Write a program to reverse digits of a number
- Program to find whether a no is power of two
- Write an Efficient Method to Check if a Number is Multiple of 3
- Program to find parity
- Write an Efficient C Program to Reverse Bits of a Number
- Minimum number of jumps to reach end
- Find minimum number to be divided to make a number a perfect square
- Find whether a given number is a power of 4 or not
- Print all combinations of points that can compose a given number
- Find Union and Intersection of two unsorted arrays
- Check if a number is multiple of 5 without using / and % operators
- To find sum of two numbers without using any operator
- Find day of the week for a given date
- Given a number, find the next smallest palindrome
- Find the element that appears once
- Count the number of possible triangles
- Select a random number from stream, with O(1) space
- Find the largest multiple of 2, 3 and 5
- Program to find amount of water in a given glass
- Program to convert a given number to words