Maximum number of handshakes
Learn how to solve the 'Maximum number of handshakes' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
Write a function max_handshakes(n) that takes a positive integer n representing the number of people in a room and returns the maximum number of handshakes that can happen if every person shakes hands with every other person exactly once.
The formula is: max_handshakes = n * (n - 1) / 2. This is because each pair of people shakes hands once.
- •1 <= n <= 10^4
Examples
max_handshakes(5)
10
5 people: 5 × 4 / 2 = 10 handshakes. Each of the 5 people shakes hands with 4 others, divided by 2 since each handshake is counted twice.
max_handshakes(2)
1
2 people can only have 1 handshake between them.
max_handshakes(10)
45
10 × 9 / 2 = 45 handshakes.
Need a Hint?
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.