InferenceAI

Enter at least two characters to search.

Start free

The Missing Number

easyArrays & Strings15 min10 XP

nums contains nn distinct numbers taken from 0,1,…,n0, 1, \dots, n. Exactly one is missing; return it.

Requirements

  • O(n)O(n) time and O(1)O(1) extra space. Use ∑i=0ni=n(n+1)2\sum_{i=0}^{n} i = \frac{n(n+1)}{2} or XOR.

Examples

EXAMPLE WALKTHROUGHmiddle missing

A guided view of the published example, not a trace of your code.

INPUT[[3,0,1]]
TASKthe_missing_number()
EXPECTED OUTPUT2

Start with the input values shown in this example.

Example 1 of 3

Input: nums = [3,0,1]

Output: 2

Explanation: middle missing

Input: nums = [0,1]

Output: 2

Explanation: last missing

Input: nums = [1]

Output: 0

Explanation: zero missing

Hint 1

Subtract the actual sum from the expected sum.

Topics

DSAArrays & StringsPython