Arithmetic · Easy
An upload batch should contain every sequence number from 0 through n exactly once, but one record is missing.
Return the missing sequence number without sorting the batch.
Input
Call
- Implement
solve(input)and return its result. Do not print fromsolve.
Input object
{ received:integer[] }
Input rules
- For n=received.length, values are distinct members of [0,n].
What each field means
received— distinct sequence numbers in arbitrary order.- If there are
nreceived values, exactly one number from0throughnis missing.
Output
Return value and rules
- Return the missing integer.
- Do not sort or mutate the batch.
- An empty batch returns
0.
Constraints
- 0 <= received.length <= 100000
- Input may be in any order and must not be mutated.
Examples
Example 1
{
"received": [
3,
0,
1
]
}Returns
2Example 2
{
"received": []
}Returns
0Required complexity
O(n) time and O(1) auxiliary space