You get an array nums of n different integers, each one between 0 and n inclusive. That range holds n + 1 values but the array only has room for n of them, so exactly one value from 0 to n is absent. Return that value.
Four numbers, so the range is 0 to 4. Every value in it shows up except 3.
The range is 0 to 2. Both 1 and 2 are present, so the gap is at the bottom.
Seven numbers cover 0 through 6 with nothing skipped, so the absent value is the top of the range, 7.
Constraints
n == nums.length1 <= n <= 10⁴0 <= nums[i] <= nnums are all different.The numbers 0 through n add up to a total you can work out without looking at the array. The array holds all of them except one.
Before you read a single number, you already know almost everything about the array. It has n slots, and the values 0 to n are n + 1 candidates. Every slot holds a different candidate, so all of them are present but one.
Time O(n)Space O(1)
The full walkthrough and code are part of Premium
3