34 · Bit Manipulation

#268 Missing Number

Easysolve on LeetCode ↗

frequently reported atAmazonBloombergNVIDIAMicrosoftGoogleMeta

the problem

In plain words

nums holds n distinct numbers taken from 0, 1, …, n. Exactly one number in that range is missing; return it. Aim for O(n) time and O(1) extra space.

in nums = [3,0,1]

out 2

in nums = [0,1]

out 2

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

out 8

  • n == nums.length
  • 1 ≤ n ≤ 10⁴
  • 0 ≤ nums[i] ≤ n, all distinct

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #41
      First Missing Positive
      Hardin Arrays & Strings
    2. #287
      Find the Duplicate Number
      Mediumin Linked Lists
    3. #136
      Single Number
      Easyin Bits