Skip to main content
Back to problems
Leetcode
Easy
Arrays
Math
Bit Manipulation
Missing Number

Find the one number missing from an array containing distinct values from a consecutive range.

Acceptance 0%
Problem Statement

Missing Number

You are given an array nums containing n distinct integers. The values are drawn from the range [0, n], and exactly one number from that range does not appear in the array.

Your task is to return the missing number.

A solution should run efficiently in linear time and use constant extra space if possible.

Input Format

  • An integer array nums
  • nums contains distinct integers
  • Each value is in the range [0, n], where n = nums.length
  • Exactly one number in [0, n] is absent

Output Format

Return the single missing integer.

Constraints

  • 1 <= nums.length <= $10^{5}$
  • 0 <= nums[i] <= nums.length
  • All values in nums are distinct
  • Exactly one number in the inclusive range [0, nums.length] is missing
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [3,0,1]

Output

2

Explanation

The numbers from 0 to 3 are {0,1,2,3}. The value 2 does not appear in the array.

Example 2

Input

nums = [0,1]

Output

2

Explanation

The range is {0,1,2}, so 2 is missing.

Show 1 more example

Example 3

Input

nums = [9,6,4,2,3,5,7,0,1]

Output

8

Explanation

The array contains every number from 0 to 9 except 8.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.