Skip to main content
Back to problems
Leetcode
Medium
Arrays
Bit Manipulation
Hash Maps
Combinatorics
3514. Number of Unique XOR Triplets II

Count how many distinct XOR values can be formed by choosing three indices from an array.

Acceptance 100%
Also Available On
Other platform versions and source mappings for the same problem.

Number of Unique XOR Triplets II

gfg
Problem Statement

Problem

Given an integer array nums, consider every triplet of indices (i, j, k) with i < j < k. For each triplet, compute the XOR value nums[i] ^ nums[j] ^ nums[k].

Return the number of distinct XOR values that can appear among all such triplets.

In other words, different index triplets may produce the same XOR value; count each resulting value only once.

Input Format

  • An integer array nums.
  • Each element of nums is used as an array value.
  • A valid triplet must choose three different indices i < j < k.

Output Format

  • Return an integer representing the number of unique XOR results over all valid triplets.

Constraints

  • 3 <= nums.length
  • Values are integers in a range suitable for bitwise XOR operations.
  • The exact official constraints are not provided here; an efficient solution should avoid enumerating all triplets when possible.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [1, 2, 3]

Output

1

Explanation

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

Example 2

Input

nums = [1, 1, 2, 2]

Output

2

Explanation

The possible triplets can produce XOR values 1 or 2, but no other distinct result appears.

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.