LeetCode Entry

3514. Number of Unique XOR Triplets II

24.07.2026 medium 2026 kotlin rust

Uniq xor of tripplets in array

3514. Number of Unique XOR Triplets II medium substack youtube

https://dmitrysamoylenko.com/leetcode/

24.07.2026.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1430

Problem TLDR

Uniq xor of tripplets in array

Intuition

The number of uniq xors is very compressable, we can assume that converting n^2 pairwise to set gives a small colleciton

Approach

  • to speed up more use boolean array/bitset

Complexity

  • Time complexity: \(O(n^2)\)

  • Space complexity: \(O(n^2)\)

Code

    fun uniqueXorTriplets(n: IntArray) = buildSet {
        for (x in buildSet { for (x in n) for (y in n) add(x xor y) })
            for (y in n) add(x xor y) }.size
    pub fn unique_xor_triplets(mut n: Vec<i32>) -> i32 {
        n.sort_unstable(); n.dedup();
        let mut s = [false; 1 << 12]; let mut t = s.clone();
        for i in 0..n.len() { for j in i..n.len() { s[(n[i] ^ n[j]) as usize] = true }}
        for x in 0..s.len() { if s[x] { for &v in &n { t[x ^ v as usize] = true }}}
        t.iter().filter(|&&b| b).count() as _
    }

Comments