AlgoLounge

Missing Number

medium

Given an array nums containing n integers in the range [0, n] without any duplicates, return the single number in the range that is missing from nums.

Follow-up: Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity?

Examples

Example 1

Input:  nums = [1,2,3]
Output: 0

Since there are 3 numbers, the range is [0,3]. The missing number is 0 since it does not appear in nums.

Example 2

Input:  nums = [0,2]
Output: 1

Constraints

  • 1 <= nums.length <= 1000
TypeScript
Loading editor…
List of test cases (5)
Case 1
nums = [1,2,3]
Case 2
nums = [0,2]
Case 3
nums = [0,1]
Case 4
nums = [3,0,1]
Case 5
nums = [9,6,4,2,3,5,7,0,1]