Missing Number

Read the lesson → understand the pattern → then code. Tutor works offline (built-in teacher); optional Cursor/Gemini keys upgrade chat quality.

Topic refresher: Arrays — Index Thinking

Treat every array problem as: What do I know at index i? What must I remember from the past?

// Running best pattern
int best = nums[0];
for (int i = 1; i < n; i++) {
    // update state using nums[i]
    best = max(best, candidate);
}
return best;
Arrays

Missing Number

Easy
LeetCode
8/14
EasyArraysLeetCode #268

Missing Number

Array of size n with numbers from 0..n missing exactly one.

Moves

  • Math: expected sum n*(n+1)/2 minus actual sum
  • XOR: xor all indices and values

Dry run

[3,0,1] n=3 → expect 6, sum=4 → missing 2.

Examples

Example 1

Input

3
3 0 1

Output

2
Example 2

Input

2
0 1

Output

2

Stuck? Take a nudge

C++17