Skip to main content
Back to problems
#2527
Medium Algorithms

Find xor beauty of array

Array Math Bit Manipulation
70.5% acceptance
Feb 25, 2026
394
57
You are given a 0-indexed integer array nums. The effective value of three indices i, j, and k is defined as ((nums[i] | nums[j]) & nums[k]). The xor-beauty of the array is the XORing of the effective values of all the possible triplets of indices (i, j, k) where 0 <= i, j, k < n. Return the xor-beauty of nums. Note that: val1 | val2 is bitwise OR of val1 and val2. val1 & val2 is bitwise AND of val1 and val2.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn xor_beauty(nums: Vec<i32>) -> i32 {
    // Proof: for each bit b, the result bit = parity of count of (i,j,k) with
    // (bit_b(nums[i]) | bit_b(nums[j])) & bit_b(nums[k]) = 1.
    // This equals parity of cnt1 (count of nums with bit b = 1) = XOR of all nums.
    nums.iter().fold(0, |acc, &x| acc ^ x)
  }
}