Missing Number — Solution in Python, Java & C++
Missing Number solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem, asked at HCL and Mindtree.
Missing Number is an easy coding problem on arrays, math and bit manipulation, asked in HCL and Mindtree interviews.
- Difficulty: Easy
- Topics: Arrays, Math, Bit Manipulation
- Asked at: HCL, Mindtree
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Given an array nums containing n distinct numbers taken from the range [0, n], return the one number in the range that is missing.
Example 1
Input: nums = [3,0,1]
Output: 2
Explanation: n = 3, so the range is [0,3]; 2 is absent.
Example 2
Input: nums = [9,6,4,2,3,5,7,0,1]
Output: 8
Constraints
1 <= nums.length <= 30All numbers are distinct and within [0, nums.length]
Follow-up: can you do it with O(1) extra space using arithmetic or XOR?
Hints for Missing Number
Hint 1
The sum 0+1+…+n is n(n+1)/2 — compare it with the actual sum.
Hint 2
XOR of everything in [0,n] with everything in nums leaves exactly the missing number.
How to solve Missing Number
The array holds n distinct values drawn from 0 .. n, so exactly one is absent. The sum of the complete range is known in closed form — n(n+1)/2 — and subtracting the array's actual sum leaves precisely the missing value.
Approach
- Let
nbe the array's length; the full range is0 .. n. - Compute the expected total
n * (n + 1) / 2. - Sum the array.
- Return the difference.
Why it works
Every value except one contributes identically to both totals, so the difference cancels them all and isolates the absent one. XOR gives the same result by a similar cancellation — x ^ x == 0, so XOR-ing the array together with 0 .. n leaves only the unpaired value — and has the advantage of never overflowing, which matters when n is large enough for the triangular number to exceed the integer range.
Complexity
- Time —
O(n) - Space —
O(1)
Pitfalls
- The range is
0 .. ninclusive, which isn + 1values for an array of lengthn— usingn - 1in the formula is the usual slip. - Sorting and scanning works but is
O(n log n)for no gain. - The triangular number can overflow for large
n; the XOR variant avoids that entirely. - The missing value may be
0ornitself, so no interior assumption is safe.
Reference solution
Python
from typing import List
def missingNumber(nums: List[int]) -> int:
n = len(nums)
return n * (n + 1) // 2 - sum(nums)Java
public static int missingNumber(int[] nums) {
int n = nums.length;
int total = n * (n + 1) / 2;
for (int x : nums) total -= x;
return total;
}C++
int missingNumber(vector<int>& nums) {
int n = (int) nums.size();
int total = n * (n + 1) / 2;
for (int x : nums) total -= x;
return total;
}JavaScript
var missingNumber = function(nums) {
const n = nums.length;
return (n * (n + 1)) / 2 - nums.reduce((a, b) => a + b, 0);
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays · Bit Manipulation