Skip to main content
Back to problems
Leetcode
Medium
Arrays
Bit Manipulation
Combinatorics
Number Of Unique Xor Triplets I

Count how many distinct XOR values can be formed by choosing three elements from the array.

Acceptance 0%
Problem Statement

Given an integer array nums, choose any three indices i, j, and k with i < j < k. Compute the value nums[i] XOR nums[j] XOR nums[k] for each such triplet.

Your task is to return the number of distinct XOR results that can be obtained from all valid triplets.

A result is considered distinct by its value, not by the indices that produced it.

Input Format

  • An integer array nums.
  • You may assume the array contains at least three elements.

Output Format

  • Return an integer: the count of unique values produced by XOR-ing every triplet of distinct indices.

Constraints

  • 3n3 \le n.
  • Elements are integers.
  • Count each XOR value once, even if many triplets produce it.
  • Indices must be distinct and ordered as i < j < k.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [1, 2, 3]

Output

1

Explanation

Only one triplet exists: (1, 2, 3). Its XOR is 1 XOR 2 XOR 3 = 0, so there is exactly one distinct result.

Example 2

Input

nums = [1, 2, 3, 4]

Output

4

Explanation

The triplets produce XOR values {0, 1, 2, 7}. The number of unique results is 4.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.