Count Distinct XOR Values from Index Triples

Quick Overview

Count distinct XOR results from nondecreasing index triples of a permutation. Allow repeated indices and preserve the distinction between counting triples and counting their resulting values.

Count Distinct XOR Values from Index Triples

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem You are given an array `nums` of length `n` that is a permutation of the integers `1` through `n`. For every index triple satisfying `0 <= i <= j <= k < n`, consider the value: ```text nums[i] XOR nums[j] XOR nums[k] ``` Return the number of distinct values obtainable from all such triples. Equal indices are allowed, so the same array element may be used more than once in a triple. Count resulting XOR values, rather than the number of triples producing them. ### C++ Interface ```cpp class Solution { public: int uniqueXorTriplets(vector<int>& nums) { } }; ``` ### Examples **Example 1** ```text Input: nums = [1] Output: 1 ``` The only index triple is `(0,0,0)`, whose value is `1 XOR 1 XOR 1 = 1`. **Example 2** ```text Input: nums = [4,2,1,3] Output: 8 ``` The distinct values are `0,1,2,3,4,5,6,7`. Each input value is obtained by using its own index three times. Additional witnesses include `1 XOR 2 XOR 3 = 0`, `4 XOR 2 XOR 3 = 5`, `4 XOR 1 XOR 3 = 6`, and `4 XOR 1 XOR 2 = 7`. Each witness can use indices in nondecreasing order. No larger value is possible for these inputs. ### Constraints - `1 <= n == nums.length <= 100000`. - `1 <= nums[i] <= n`. - `nums` contains every integer from `1` through `n` exactly once. ### Clarifying Questions - Must the three indices be different? No. The inequalities are non-strict. - Does the array have to be sorted? No. Any permutation is valid. - Is zero an eligible XOR result even though it is absent from the input? Yes, if a valid triple produces it. - Should duplicate XOR results be counted again for different triples? No. Each resulting value counts once. ### What a Strong Answer Covers - The permutation promise and the permitted repeated indices. - The distinction between triples and their distinct XOR results. - Completeness of the claimed result count for all valid triples. - Small arrays and resource and numeric bounds up to `100000` elements. ### Follow-up Questions - What would change in the definition if the three indices had to be strictly increasing? - How would the requested result differ if the task counted index triples instead of distinct XOR values? ```hint Use the full value set Every value from 1 through n is available, and XOR is unchanged when the three operands are reordered. Bound the possible result bits, then prove which values inside that bound can actually be formed; treat n = 1 and n = 2 separately. ```

Overview: Count distinct XOR results from nondecreasing index triples of a permutation. Allow repeated indices and preserve the distinction between counting triples and counting their resulting values.

Google logo
Google
May 2, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

You are given an array nums of length n that is a permutation of the integers 1 through n.

For every index triple satisfying 0 <= i <= j <= k < n, consider the value:

nums[i] XOR nums[j] XOR nums[k]

Return the number of distinct values obtainable from all such triples. Equal indices are allowed, so the same array element may be used more than once in a triple. Count resulting XOR values, rather than the number of triples producing them.

C++ Interface

class Solution {
public:
    int uniqueXorTriplets(vector<int>& nums) {
        
    }
};

Examples

Example 1

Input: nums = [1]
Output: 1

The only index triple is (0,0,0), whose value is 1 XOR 1 XOR 1 = 1.

Example 2

Input: nums = [4,2,1,3]
Output: 8

The distinct values are 0,1,2,3,4,5,6,7. Each input value is obtained by using its own index three times. Additional witnesses include 1 XOR 2 XOR 3 = 0, 4 XOR 2 XOR 3 = 5, 4 XOR 1 XOR 3 = 6, and 4 XOR 1 XOR 2 = 7. Each witness can use indices in nondecreasing order. No larger value is possible for these inputs.

Constraints

  • 1 <= n == nums.length <= 100000 .
  • 1 <= nums[i] <= n .
  • nums contains every integer from 1 through n exactly once.

Clarifying Questions Guidance

  • Must the three indices be different? No. The inequalities are non-strict.
  • Does the array have to be sorted? No. Any permutation is valid.
  • Is zero an eligible XOR result even though it is absent from the input? Yes, if a valid triple produces it.
  • Should duplicate XOR results be counted again for different triples? No. Each resulting value counts once.

What a Strong Answer Covers Guidance

  • The permutation promise and the permitted repeated indices.
  • The distinction between triples and their distinct XOR results.
  • Completeness of the claimed result count for all valid triples.
  • Small arrays and resource and numeric bounds up to 100000 elements.

Follow-up Questions Guidance

  • What would change in the definition if the three indices had to be strictly increasing?
  • How would the requested result differ if the task counted index triples instead of distinct XOR values?