Einfach 0 gelöst 0% Erfolgsquote

Count Handshakes

Count handshakes when every pair of people shakes hands once.

Das lernst du

  • Count pairs with n × (n - 1) / 2
  • Reason about counting each pair once
  • Replace a loop with a formula

Problem

Übersetzung anzeigen

Every person shakes hands once with every other person. A handshake between A and B is the same event as one between B and A. Nobody shakes their own hand.

Implement handshake_count(people).

Input

people: The number of people.

Output

Return the total number of distinct handshakes.

Einschränkungen

0 ≤ people ≤ 1,000,000,000. The answer fits a signed 64-bit integer.

Beispiele

Beispiel 1
Eingabe:
people = 4

Erwartete Ausgabe:
expected = 6
Eingabe:
long long people = 4;

Erwartete Ausgabe:
long long expected = 6;
Eingabe:
long people = 4;

Erwartete Ausgabe:
long expected = 6;

Erläuterung

The pairs are AB, AC, AD, BC, BD, and CD: six handshakes.

Beispiel 2
Eingabe:
people = 1

Erwartete Ausgabe:
expected = 0
Eingabe:
long long people = 1;

Erwartete Ausgabe:
long long expected = 0;
Eingabe:
long people = 1;

Erwartete Ausgabe:
long expected = 0;

Erläuterung

One person has nobody else to greet.

So gehst du vor

Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.

Hinweis 1

Each handshake is just an unordered pair of distinct people.

Hinweis 2

Count how many pairs can be formed from all people without double-counting.

Hinweis 3

Be careful that zero or one person means no handshakes at all.

Erwartete Komplexität

O(1) time, O(1) extra space

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.