Lv.3bit-manipulationtrie
Maximum XOR of Two Numbers
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Return the maximum of nums[i] XOR nums[j] over indices i ≠ j.
Examples
| nums | result |
|---|---|
[3, 10, 5, 25, 2, 8] | 28 |
[7, 7] | 0 |
[0, 1073741823] | 1073741823 |
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(nums: int[]) → int
Constraints
-
nums.length,nums[i]
Expected complexity
- Time
- O(N log C)
- Space
- O(N log C)
Examples
| nums | Expected |
|---|---|
[3,10,5,25,2,8] | 28 |
[7,7] | 0 |
[0,1073741823] | 1073741823 |
Hint
Hint
Higher bits dominate. Insert the numbers into a binary trie (bit 29 down to 0) and, for each x, walk towards the opposite
bit whenever possible; or fix the answer bit by bit with a hash set of prefixes. Checking all pairs is .
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 246 ms | estimated |
| C++ (GCC, gnu++23) | 246 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1842 ms | derived |
| Kotlin 2.4 (JVM) | 1492 ms | estimated |
| C# 14 (.NET 10) | 1492 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 8 | verdict only |
| performance | 60 | 4 | verdict only |