समस्या
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 के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।