Условие
The 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.
Ограничения
0 ≤ n ≤ 100,000
Примеры
Ввод:
numbers = [3, 0, 1]
Ожидаемый вывод:
expected = 2
Ввод:
std::vector<long long> numbers = {3, 0, 1};
Ожидаемый вывод:
long long expected = 2;
Ввод:
long[] numbers = new long[] {3, 0, 1};
Ожидаемый вывод:
long expected = 2;
Пояснение
n = 3, so the range is 0–3; 2 is missing.
Ввод:
numbers = [0]
Ожидаемый вывод:
expected = 1
Ввод:
std::vector<long long> numbers = {0};
Ожидаемый вывод:
long long expected = 1;
Ввод:
long[] numbers = new long[] {0};
Ожидаемый вывод:
long expected = 1;
Пояснение
n = 1 and the range is 0–1; 1 is missing.
Как подступиться
Сначала попробуйте сами, затем открывайте подсказки по одной.
Подсказка 1
The numbers should match the full range from 0 through n except for one hole.
Подсказка 2
Compare what the complete range would total against what the list actually totals.
Подсказка 3
Watch for the case where the missing value is 0 or n.
Ожидаемая сложность
O(n) time, O(1) extra space
Ссылки
Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.