Lv.1arraybit-manipulationmath
Missing Number
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Tickets numbered 0 to n (that is, n + 1 tickets) were handed out, and exactly one is missing. Given the n remaining
distinct ticket numbers tickets in any order (n is the length of tickets), return the missing number.
Examples
| tickets | result |
|---|---|
[3, 0, 1] | 2 |
[0, 1] | 2 |
[9, 6, 4, 2, 3, 5, 7, 0, 1] | 8 |
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(tickets: int[]) → int
Constraints
-
tickets.length -
tickets[i], all values are distinct
Expected complexity
- Time
- O(N)
- Space
- O(1)
Examples
| tickets | Expected |
|---|---|
[3,0,1] | 2 |
[0,1] | 2 |
[9,6,4,2,3,5,7,0,1] | 8 |
Hint
Hint
The sum 0 + 1 + … + n is ; subtract the sum of the tickets (use 64-bit integers). Alternatively XOR 0..n with all
tickets: paired values cancel and the missing one remains. Searching the array for every number is and exceeds the time limit.
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1026 ms | derived |
| Kotlin 2.4 (JVM) | 1400 ms | estimated |
| C# 14 (.NET 10) | 1400 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 10 | verdict only |
| performance | 60 | 4 | verdict only |