268. Missing Number
π© Easy
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.
Example 1
Input: nums = [3,0,1] Output: 2 Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.
Example 2
Input: nums = [0,1] Output: 2 Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.
Example 3
Input: nums = [9,6,4,2,3,5,7,0,1] Output: 8 Explanation: n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums.
Constraints
n == nums.length1 <= n <= 10^40 <= nums[i] <= nAll the numbers of
numsare unique.
Follow up: Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity?
Solution
My Solution (Gauss Formula)
Optimal Solution 1 (XOR)
Optimal Solution 2 (Cyclic Sort)
Approach Analysis
This problem showcases different mathematical and algorithmic approaches:
Gauss Formula (Your Solution):
Use mathematical formula for sum
Subtract array elements
Simple and elegant
No modification to input
XOR Approach:
Use XOR properties
Cancellation of pairs
Constant extra space
Bit manipulation technique
Cyclic Sort:
Place numbers in position
In-place modification
Index-based approach
Useful for similar problems
Visualization of Approaches
Gauss Formula Process (Your Solution)
XOR Process
Cyclic Sort Process
Complexity Analysis
Gauss Formula (Your Solution)
Time: O(n)
Single pass through array
Simple arithmetic
No sorting required
Space: O(1)
Only use sum variable
Constant extra space
No additional storage
XOR Solution
Time: O(n)
Single pass through array
Constant time operations
Bit manipulation
Space: O(1)
Only use result variable
In-place operations
Most space efficient
Cyclic Sort Solution
Time: O(n)
Two passes through array
Each element moved once
Index-based checking
Space: O(1)
In-place sorting
No extra storage
Modifies input array
Why Solutions Work
Gauss Formula:
Sum formula is exact
Difference shows missing
Works for any range
Mathematical proof
XOR Properties:
a^a = 0
a^0 = a
XOR is associative
Missing number remains
Cyclic Sort:
Numbers match indices
Missing creates gap
Natural ordering
Index-value relationship
When to Use
Gauss Formula When:
Can't modify input
Simple solution needed
Mathematical approach ok
Range is consecutive
XOR When:
Memory is critical
Bit operations allowed
No overflow concern
Performance critical
Cyclic Sort When:
Can modify array
Need sorted result
Similar problems follow
Index-based problems
Common Patterns & Applications
Related Problems:
Find All Numbers Disappeared
Single Number
First Missing Positive
Find the Duplicate Number
Key Techniques:
Mathematical formulas
Bit manipulation
In-place sorting
Index as hash key
Interview Tips
Solution Highlights:
Multiple approaches
Space-time tradeoffs
Bit manipulation
Mathematical insight
Common Pitfalls:
Integer overflow
Edge cases (0, n)
Modifying input
Off-by-one errors
Testing Strategy:
Missing first number
Missing last number
Single element array
Maximum size array
Sequential numbers
Follow-up Questions:
Multiple missing numbers?
Negative numbers?
Stream of numbers?
Memory constraints?

Leetcode: link
Last updated