Skip to main content
Back to problems
#3513
Medium Algorithms

Number of unique xor triplets i

Array Math Bit Manipulation
26.5% acceptance
Feb 25, 2026
49
11
You are given an integer array nums of length n, where nums is a permutation of the numbers in the range [1, n]. A XOR triplet is defined as the XOR of three elements nums[i] XOR nums[j] XOR nums[k] where i <= j <= k. Return the number of unique XOR triplet values from all possible triplets (i, j, k).

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn unique_xor_triplets(nums: Vec<i32>) -> i32 {
    let n = nums.len() as i32;
    if n == 1 { return 1; }
    if n == 2 { return 2; }
    // For n >= 3: all values 0..(next power of 2 after n) are achievable
    // since nums is a permutation of [1..n].
    let mut p = 1i32;
    while p <= n { p <<= 1; }
    p
  }
}