The Missing Exam Paper
Before an exam you print one paper per student, numbered 1, 2, 3 and so on. Afterwards the stack is one paper short: somebody walked out with theirs.
The papers came back in drop-off order. Find the missing number without sorting the pile.
Your task
Write missingPaper(papers). You printed papers.length + 1 papers, numbered 1 to papers.length + 1. The list holds every number except one, each at most once, in any order. Return the missing number.
If the list is empty, you printed only paper 1, and it is missing.
missingPaper(papers: int[]) → int
Examples
papers = [4, 2, 5, 1]
3
Four papers came back, so five were printed: 1 to 5. Every number except 3 is on the desk.
papers = [1, 2, 3]
4
Four papers were printed. The pile 1, 2, 3 has no gap inside, so the missing one is the last, 4. Looking only for a gap between neighbours would miss this.
papers = []
1
Nothing came back, so only paper 1 was printed, and it is missing.
Constraints
- 0 ≤ papers.length ≤ 100,000
- 1 ≤ papers[i] ≤ papers.length + 1
- All numbers in papers are different, and exactly one number from the range is absent.
Start with brute force
For each number from 1 to n + 1, scan the pile for it. The first one you cannot find is the answer.
That is up to n + 1 scans of n papers: about 10 billion reads for 100,000 papers. Sorting and looking for the gap is O(n log n), still more work than needed.
Hints
0 of 4 shown- Hint 1
Take
[4, 2, 5, 1]. With no paper missing, what would 1 to 5 add up to? What do the four papers add up to? - Hint 2
The full set adds to 15 and the pile to 12. Which paper does the difference stand for? Does the order change either total?
- Hint 3
No sorting or storing needed. The total of 1 to
mism × (m + 1) / 2, and one running sum gives the rest. - Hint 4
Let
m = papers.length + 1. Start fromm × (m + 1) / 2and subtract each paper in one pass. What is left is the missing number.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
The full set is every number from 1 to m, so you know its total before looking at a paper. Each paper you hold accounts for part of it; the part nobody accounts for is the missing paper.
Trace [4, 2, 5, 1]. m = 5, and 1 + 2 + 3 + 4 + 5 = 5 × 6 / 2 = 15.
- Subtract 4, which leaves 11.
- Subtract 2, which leaves 9.
- Subtract 5, which leaves 4.
- Subtract 1, which leaves 3. Return 3.
A hash set also works in O(n) time but costs O(n) memory for a question one number answers. With 100,000 papers the total is about 5 billion: fine for JavaScript numbers and Python integers, but with 32-bit integers you would need a 64-bit type.
function missingPaper(papers) {
const m = papers.length + 1; // how many were printed
let left = (m * (m + 1)) / 2; // 1 + 2 + ... + m
for (const p of papers) left -= p; // remove each paper we have
return left;
}def missing_paper(papers):
m = len(papers) + 1 # how many were printed
left = m * (m + 1) // 2 # 1 + 2 + ... + m
for p in papers: # remove each paper we have
left -= p
return leftRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.