The missing band
- O(n) · Medium
- Full plan
- Python
- JavaScript
- lists
- operations
Problem
At dawn, the biologist walks around the lagoon with her binoculars. A month ago she put a leg band on each heron, numbered from 1 to n, a different one for each bird, and today all of them came back but one. Write a function that takes seen, the list of bands she counted today (integers with no repeats, in any order), and returns an integer: the missing number. Since exactly one heron is missing, n is the length of the list plus one. The missing one can be any of them, including band 1 or band n. If the list is empty, there was only one heron: 1 is missing. With [3, 1, 5, 2], n is 5. The bands from 1 to 5 add up to 15, the ones seen add up to 11, so 4 is missing. There is no need to look for each number one by one: compare what they should add up to with what they do.
Examples
The example
[3, 1, 5, 2] → 4
One from the middle is missing
[2, 4, 1] → 3
Band 1 is missing
[2] → 1
Seven herons
[6, 2, 7, 1, 3, 4] → 5
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def missing_band(seen):
passJavaScript
function missingBand(seen) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.