LeetCode Entry
3513. Number of Unique XOR Triplets I
Uniq xor of tripplets of 1..n
3513. Number of Unique XOR Triplets I medium substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1429
Problem TLDR
Uniq xor of tripplets of 1..n
Intuition
// for xor order doesnt matter
// nums are the same 1..n
// 1 xor 1 = 0
// 1 ^ 0 = 1
// 1 ^ 0 ^ 1 = 0
// 1 ^ 0 ^ 0 = 1
// 1 ^ 1 ^ 1 = 1
// 1 ^ 1 ^ 0 = 0
// uniq?
Order doesnt matter. The maximum value 0b1xxxxx allows to construct any intermediate value by taking 1 xor 1 xor n[i] plus tail bits can construct any value of up to 2*max.
Approach
- simulate and see the results, then derive whats happening
Complexity
-
Time complexity: \(O(logn)\)
-
Space complexity: \(O(1)\)
Code
fun uniqueXorTriplets(n: IntArray) =
if (n.size <3) n.size else 2*n.size.takeHighestOneBit()
pub fn unique_xor_triplets(n: Vec<i32>) -> i32 {
let l = n.len() as i32; if l < 3 { l } else { 1 << (32 - l.leading_zeros()) }
}
Comments