Python BasicsHard

Maximum number of handshakes

Detailed guide and Python implementation for the 'Maximum number of handshakes' problem.

Problem Statement

Hard

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.

Constraints
  • 1 <= n <= 10^4

Examples

Example 1
Input
max_handshakes(5)
Output
10
Explanation

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.

Example 2
Input
max_handshakes(2)
Output
1
Explanation

2 people can only have 1 handshake between them.

Example 3
Input
max_handshakes(10)
Output
45
Explanation

10 × 9 / 2 = 45 handshakes.

Need a Hint?
Consider using Numbers-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.