Uber · Question 1 · Arrays
Find the missing number in an array of 1 to N
Problem
You are given an array containing n distinct numbers taken from 0, 1, 2, ..., n. Find the one number that is missing from the array.
Approach
Use the formula: n * (n + 1) / 2 Subtract the actual sum from this expected sum.
Time O(n)Space O(1)
How it works
- 1The complete range 0..n sums to n*(n+1)/2.
- 2Subtract the actual array sum from the expected sum.
- 3The difference is the missing number.
Sample input / output
Input: 3 3 0 1 Output: 2
Constraints
1 <= n <= 10^4; 0 <= nums[i] <= n; all numbers distinct; exactly one missing.
Solution — read it, then walk it
Time O(n)Space O(1)Branches 1Returns 1source · Python
Scroll sideways for wide charts — the view follows the active step.
- start / end
- statement
- condition
- loop
- returns
- definition
1/14
StartSTART
Interactive control-flow diagram and step-by-step code walkthrough.
Recommended resourcesAffiliate links · may earn us a commission
Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits