Python Basicsハード

Maximum number of handshakes

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

問題提起

ハード

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

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

解決する準備はできましたか?

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

エディタで開く
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推奨される Python リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。