Math Fundamentals18 sections · 814 units
Open in Course

Practice - Missing Number

LeetCode 268

Given an array numsnums containing nn distinct numbers in the range [0,n][0, n], return the only number in the range that is missing from the array.

For example, if nums=[3,0,1]nums = [3, 0, 1], then n=3n = 3 and the missing number is 22.

Hint: use the sum formula 1+2+...+n=n(n+1)21 + 2 + ... + n = \frac{n(n+1)}{2}. The missing number equals the expected sum minus the actual sum.