Skip to main content
← Problem catalogue Understand the pattern. Try it yourself. Review the solution. Practice this pattern →
Easy Bit Manipulation High frequency Solution Available Practice Mode

Missing Number LeetCode Solution

Tags: #Array #Math #Bit Manipulation #Sorting

Approach Summary

XOR of all indices (0..n) XOR all array values. The missing number is what remains. Or use Gauss sum formula.

Practice Mode 2 Progressive Approaches

Solve it yourself before revealing the code

DSA interviews test reasoning from brute force to optimal. Think through the constraints first.

Algorithmic Pattern

Master the Bit Manipulation Pattern

View Pattern Guide →

Study the core invariant, step-by-step decision framework, and common interview traps for the Bit Manipulation pattern.

Framework: Bit Manipulation • Interview Prep: Tested at FAANG • Explore all Bit Manipulation problems & invariants →

How to Recognize This Pattern

  • Find missing number in 0..n
  • XOR or math sum

Complexity Analysis

Time Complexity

O(n)

Space Complexity

O(1)

Tags

Array Math Bit Manipulation Sorting

Frequently Asked Questions

What is the optimal time and space complexity for Missing Number?
The optimal solution for Missing Number achieves O(n) runtime complexity and requires O(1) auxiliary space. Time complexity is O(n) as we scan the array once. Space complexity is O(1) because both Gauss summation and XOR accumulation operate using a single accumulator register.
How do I identify that Missing Number requires the Bit Manipulation pattern?
In technical interviews, look for key problem signals: Find missing number in 0..n; XOR or math sum. Recognizing these invariants allows you to bypass naive brute-force exhaustive search in favor of Bit Manipulation strategy.
What critical edge cases must be handled in Missing Number?
Essential edge cases to verify include: Missing number is 0 (array contains [1..n]); Missing number is n (array contains [0..n-1]); Array has length 1 (e.g. [0] -> missing 1, [1] -> missing 0); Large array with up to 10^4 numbers. Always walk through these scenarios with your interviewer before coding.
Which tech companies frequently ask Missing Number?
Missing Number is a Easy-difficulty question tagged with high recurrence. It is actively asked in technical screening and onsite interview loops at Microsoft, Amazon.
Buy me a coffee
Daily Practice
0/0