Skip to main content
Back to problems
#3215
Medium Algorithms

Count triplets with even xor set bits ii

Array Bit Manipulation
61.2% acceptance
Mar 31, 2026
17
3
Given three integer arrays a, b, and c, return the number of triplets (a[i], b[j], c[k]), such that the bitwise XOR between the elements of each triplet has an even number of set bits.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn triplet_count(a: Vec<i32>, b: Vec<i32>, c: Vec<i32>) -> i64 {
    let count_parity = |arr: &Vec<i32>| -> (i64, i64) {
      let even = arr.iter().filter(|&&x| x.count_ones() % 2 == 0).count() as i64;
      let odd = arr.len() as i64 - even;
      (even, odd)
    };
    let (ea, oa) = count_parity(&a);
    let (eb, ob) = count_parity(&b);
    let (ec, oc) = count_parity(&c);
    ea * eb * ec + ea * ob * oc + oa * eb * oc + oa * ob * ec
  }
}