Number of Unique XOR Triplets I

MEDIUM

Description

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).

 

Example 1:

Input: nums = [1,2]

Output: 2

Explanation:

The possible XOR triplet values are:

  • (0, 0, 0) → 1 XOR 1 XOR 1 = 1
  • (0, 0, 1) → 1 XOR 1 XOR 2 = 2
  • (0, 1, 1) → 1 XOR 2 XOR 2 = 1
  • (1, 1, 1) → 2 XOR 2 XOR 2 = 2

The unique XOR values are {1, 2}, so the output is 2.

Example 2:

Input: nums = [3,1,2]

Output: 4

Explanation:

The possible XOR triplet values include:

  • (0, 0, 0) → 3 XOR 3 XOR 3 = 3
  • (0, 0, 1) → 3 XOR 3 XOR 1 = 1
  • (0, 0, 2) → 3 XOR 3 XOR 2 = 2
  • (0, 1, 2) → 3 XOR 1 XOR 2 = 0

The unique XOR values are {0, 1, 2, 3}, so the output is 4.

 

Constraints:

  • 1 <= n == nums.length <= 105
  • 1 <= nums[i] <= n
  • nums is a permutation of integers from 1 to n.

Approaches

Checkout 3 different approaches to solve Number of Unique XOR Triplets I. Click on different approaches to view the approach and algorithm in detail.

Mathematical Observation

The most efficient solution comes from a mathematical observation about the structure of XOR sums over the set of integers {1, ..., n}. By analyzing the problem for small values of n, a pattern emerges. This pattern can be proven to hold for all n, leading to a constant-time solution.

Algorithm

  1. Handle the base cases for small n.
    • If n = 1, the only possible triplet is (1,1,1) giving 1^1^1=1. The result is 1.
    • If n = 2, the unique values are {1, 2}. The result is 2.
  2. For n >= 3, observe the pattern of the unique values.
    • n=3: {1,2,3}. 1^2^3=0. Unique values are {0,1,2,3}. Size 4.
    • n=4: {1,2,3,4}. Triplets generate {0,5,6,7}. Unique values are {0,1,2,3,4,5,6,7}. Size 8.
  3. Generalize the pattern: For n >= 3, the set of unique XOR values is {0, 1, ..., p-1}, where p is the smallest power of 2 that is strictly greater than n.
  4. The size of this set is p. This can be calculated efficiently using bit manipulation.
  5. Implement the logic: check for n=1 and n=2, otherwise calculate and return p.

The key insight is that since nums is a permutation of {1, ..., n}, the problem is independent of the order of elements in nums and only depends on n. The set of unique values is {1, ..., n} igcup {a^b^c | a,b,c ext{ are distinct in } {1,...,n}}.

By analyzing small cases:

  • n=1: nums=[1]. Values: {1}. Size: 1.
  • n=2: nums is perm of {1,2}. Values: {1,2}. Size: 2.
  • n=3: nums is perm of {1,2,3}. Values: {1,2,3} and 1^2^3=0. Total: {0,1,2,3}. Size: 4.
  • n=4: nums is perm of {1,2,3,4}. Values: {1,2,3,4} and {1^2^3=0, 1^2^4=7, 1^3^4=6, 2^3^4=5}. Total: {0,1,2,3,4,5,6,7}. Size: 8.

For n >= 3, it can be shown that the set of all possible XOR triplet values forms a complete range of numbers from 0 to p-1, where p is the smallest power of 2 strictly greater than n. The size of this set is p.

This value p can be found efficiently. For a given n, p is 2^k where k is the smallest integer such that 2^k > n. This is equivalent to finding the next power of two after n.

For example, if n=5, the next power of two is 8. If n=8, the next power of two is 16.

This leads to a very simple and fast implementation.

class Solution {
    public int countUniqueXorTriplets(int[] nums) {
        int n = nums.length;

        if (n == 1) {
            return 1;
        }
        if (n == 2) {
            return 2;
        }

        // For n >= 3, the result is the smallest power of 2 strictly greater than n.
        // This can be found using bit manipulation.
        // Integer.highestOneBit(n) finds the largest power of 2 less than or equal to n.
        // If n is a power of 2, say n = 8, highestOneBit(8) is 8. We need 16. So 8 << 1.
        // If n is not a power of 2, say n = 7, highestOneBit(7) is 4. We need 8. So 4 << 1.
        // This logic holds for n >= 1, but we've handled n=1,2 separately for clarity.
        // Let's re-check the formula for n=3: highestOneBit(3) is 2. 2 << 1 = 4. Correct.
        return Integer.highestOneBit(n) << 1;
    }
}

Complexity Analysis

Time Complexity: O(1), as the solution involves a few comparisons and bitwise operations that do not depend on `n`.Space Complexity: O(1), as no extra space proportional to the input size is needed.

Pros and Cons

Pros:
  • Extremely efficient with O(1) time complexity.
  • Minimal space usage.
  • Very simple to implement once the mathematical property is known.
Cons:
  • Relies on a non-trivial mathematical property that may not be immediately obvious.
  • The correctness depends on number-theoretic results about XOR sums.

Video Solution

Watch the video walkthrough for Number of Unique XOR Triplets I



Patterns:

MathBit Manipulation

Data Structures:

Array

Subscribe to Scale Engineer newsletter

Learn about System Design, Software Engineering, and interview experiences every week.

No spam, unsubscribe at any time.

Number of Unique XOR Triplets I | Scale Engineer