Problema
Mostra traduzioneThe list holds n distinct numbers taken from 0, 1, …, n, so exactly one number in that range is missing. Find it. (Hint: compare the expected total with the actual total.)
Implement missing_number(numbers).
Input
numbers: n distinct numbers from 0 to n.
Output
Return the missing number.
Vincoli
0 ≤ n ≤ 100,000
Esempi
Esempio 1
Input:
numbers = [3, 0, 1]
Output atteso:
expected = 2
Input:
std::vector<long long> numbers = {3, 0, 1};
Output atteso:
long long expected = 2;
Input:
long[] numbers = new long[] {3, 0, 1};
Output atteso:
long expected = 2;
Spiegazione
n = 3, so the range is 0–3; 2 is missing.
Esempio 2
Input:
numbers = [0]
Output atteso:
expected = 1
Input:
std::vector<long long> numbers = {0};
Output atteso:
long long expected = 1;
Input:
long[] numbers = new long[] {0};
Output atteso:
long expected = 1;
Spiegazione
n = 1 and the range is 0–1; 1 is missing.
Riferimenti
Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.