# Find the maximum number of handshakes

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.

**Examples :**

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.

## C++

`// C++ program to find maximum number of ` `// handshakes. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Calculating the maximum number of handshake ` `// using derived formula. ` `int` `maxHandshake(` `int` `n) ` `{ ` ` ` `return` `(n * (n - 1))/ 2; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 10; ` ` ` `cout << maxHandshake(n) <<endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find maximum number of ` `// handshakes. ` ` ` `class` `GFG ` `{ ` ` ` `// Calculating the maximum number of ` ` ` `// handshake using derived formula. ` ` ` `static` `int` `maxHandshake(` `int` `n) ` ` ` `{ ` ` ` `return` `(n * (n - ` `1` `)) / ` `2` `; ` ` ` `} ` ` ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `10` `; ` ` ` `System.out.println( maxHandshake(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find maximum ` `# number of handshakes. ` ` ` `# Calculating the maximum number ` `# of handshake using derived formula. ` `def` `maxHandshake(n): ` ` ` ` ` `return` `int` `((n ` `*` `(n ` `-` `1` `)) ` `/` `2` `) ` ` ` `# Driver Code ` `n ` `=` `10` `print` `(maxHandshake(n)) ` ` ` `# This code is contributed by Smitha Dinesh Semwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find maximum number of ` `// handshakes. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Calculating the maximum number of ` ` ` `// handshake using derived formula. ` ` ` `static` `int` `maxHandshake(` `int` `n) ` ` ` `{ ` ` ` `return` `(n * (n - 1)) / 2; ` ` ` `} ` ` ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 10; ` ` ` `Console.Write( maxHandshake(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to ` `// find maximum number ` `// of handshakes. ` ` ` `// Calculating the maximum ` `// number of handshake ` `// using derived formula. ` `function` `maxHandshake(` `$n` `) ` `{ ` ` ` `return` `(` `$n` `* (` `$n` `- 1))/ 2; ` `} ` ` ` `// Driver Code ` `$n` `= 10; ` `echo` `maxHandshake(` `$n` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

Output:

45

**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 contribute@geeksforgeeks.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.

## Recommended Posts:

- Find maximum number that can be formed using digits of a given number
- Find the maximum number of composite summands of a number
- Find a positive number M such that gcd(N^M, N&M) is maximum
- Find prime number K in an array such that (A[i] % K) is maximum
- Find the Number of Maximum Product Quadruples
- Find sum of a number and its maximum prime factor
- Find the maximum number of elements divisible by 3
- Find a number that divides maximum array elements
- Find maximum power of a number that divides a factorial
- Find the number in a range having maximum product of the digits
- Find integers that divides maximum number of elements of the array
- Querying maximum number of divisors that a number in a given range has
- Number with maximum number of prime factors
- Find maximum among x^(y^2) or y^(x^2) where x and y are given
- Find maximum value of x such that n! % (k^x) = 0