Python Nozioni di baseDifficile

Numero massimo di strette di mano

Guida dettagliata e implementazione Python per il problema del "Numero massimo di strette di mano".

Dichiarazione del problema

Difficile

Scrivi una funzione max_handshakes(n) che accetta un intero positivo n che rappresenta il numero di persone in una stanza e restituisce il numero massimo di strette di mano che possono verificarsi se ogni persona stringe la mano a tutte le altre esattamente una volta.

La formula è: max_handshakes = n * (n - 1) / 2. Questo perché ogni coppia di persone si stringe la mano una volta.

Vincoli
  • 1 <= n <= 10^4

Esempi

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?
Prendi in considerazione l'utilizzo di strutture dati specifiche di Numbers come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

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

Apri nell'editor
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

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.