Back to Practice Dashboard
Python BasicsHard

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

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?
Use simple arithmetic operators (like modulo `%`, division `//`), conditional checks, or loops to inspect number properties.
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.

Open in Editor