LeetCode Entry
3289. The Two Sneaky Numbers of Digitville
Two extra numbers from 0..n
3289. The Two Sneaky Numbers of Digitville easy blog post substack youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1159
Problem TLDR
Two extra numbers from 0..n #easy
Intuition
Use any of:
- HashSet for visited
- bitmask for visited
- array itself for visited
The clever solution with bit manipulation:
- total xor, no extras: a^b^c – can compute as x1
- total xor for single extra: a^b^c^a
- total xor for two extras: a^b^c^a^b – can compute as x2
- xor of x1^x2: a^b^c ^ a^b^c^a^b = a^b – can compute as x1^x2
Now we have a^b, each bits is a different between a and b.
Split all given numbers by have or have-nots of this bit. xor(have_bit) = xx1 xor(have_not_bit) == xx2
Then split range numbers similarly: xor(have_bit) == yy1 xor(have_not_bit) == yy2
Then a = xx1 ^ yy1, b = xx2 ^ yy2
Approach
- just brute-force
Complexity
-
Time complexity: \(O()\)
-
Space complexity: \(O()\)
Code
// 17ms
fun getSneakyNumbers(n: IntArray) =
n.indices.filter { x -> n.count { it == x } > 1 }
// 0ms
pub fn get_sneaky_numbers(n: Vec<i32>) -> Vec<i32> {
let mut m = 0u128;
n.into_iter().filter(|x| { let u = (1 << x) & m > 0; m |= 1<<x; u}).collect()
}