Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
nums = [0,1,3]2nums = [0,1,2,3,4,5,6,7,9]8nums = [1,2,3]0lo = 0, hi = nums.length.mid = lo + (hi - lo) / 2.nums[mid] > mid: gap is in the LEFT half → set hi = mid.lo = mid + 1.lo == hi, that index is the missing number — return lo.After sorting, if no number is missing before index i, then nums[i] == i. The missing number creates a shift: every element at or after the gap satisfies nums[i] == i + 1. This is a monotone predicate — perfect for binary search. nums[mid] > mid tells us the gap happened at or before mid, so we shrink the search space left. Otherwise it is strictly to the right.