Distinct XOR Results from Three Array Choices

Quick Overview

Count distinct XOR results from three array choices with repeated indices allowed. Preserve nondecreasing index semantics and support arbitrary input values and duplicates within the stated bounds.

Distinct XOR Results from Three Array Choices

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Given an integer array `nums`, consider every index triple satisfying `0 <= i <= j <= k < nums.length`. Its XOR value is: ```text nums[i] XOR nums[j] XOR nums[k] ``` Return the number of distinct values produced by these triples. Equal indices are allowed, so an array element may be used more than once. Different triples producing the same value contribute only once to the answer. ### C++ Interface ```cpp class Solution { public: int uniqueXorTriplets(vector<int>& nums) { } }; ``` ### Examples **Example 1** ```text Input: nums = [2,2] Output: 1 ``` Every permitted triple uses the value `2` three times, giving `2 XOR 2 XOR 2 = 2`. Different index choices do not produce different values. **Example 2** ```text Input: nums = [1,2,4] Output: 4 ``` The distinct results are `1`, `2`, `4`, and `7`. If a triple repeats a value, two equal operands cancel and the result is one of the input values. Using all three different values gives `1 XOR 2 XOR 4 = 7`. ### Constraints - `1 <= nums.length <= 1500`. - `1 <= nums[i] <= 1500`. ### Clarifying Questions - Must the indices be distinct? No. The inequalities are non-strict. - Is the array a permutation or are its values unique? Neither property is promised. - Can a result be absent from the input? Yes, as the result `7` in the second example shows. - Does the answer count triples or resulting values? It counts each distinct XOR value once. ### What a Strong Answer Covers - All permitted index triples, including repeated indices. - Distinct result values despite repeated inputs or multiple producing triples. - A justification that no possible result is missed or counted spuriously. - Resource bounds for `1500` elements and the full value range. ### Follow-up Questions - How would requiring strictly increasing indices change which triples are allowed? - Can several distinct index triples contribute just one value to the requested answer? ```hint Reuse results after each choice Values up to 1500 occupy at most 11 bits, so every XOR result is below 2048. Track which values are reachable after one, two, and three choices. Explain why reordering the chosen indices does not change their XOR. ```

Overview: Count distinct XOR results from three array choices with repeated indices allowed. Preserve nondecreasing index semantics and support arbitrary input values and duplicates within the stated bounds.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Given an integer array nums, consider every index triple satisfying 0 <= i <= j <= k < nums.length. Its XOR value is:

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

Return the number of distinct values produced by these triples. Equal indices are allowed, so an array element may be used more than once. Different triples producing the same value contribute only once to the answer.

C++ Interface

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

Examples

Example 1

Input: nums = [2,2]
Output: 1

Every permitted triple uses the value 2 three times, giving 2 XOR 2 XOR 2 = 2. Different index choices do not produce different values.

Example 2

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

The distinct results are 1, 2, 4, and 7. If a triple repeats a value, two equal operands cancel and the result is one of the input values. Using all three different values gives 1 XOR 2 XOR 4 = 7.

Constraints

  • 1 <= nums.length <= 1500 .
  • 1 <= nums[i] <= 1500 .

Clarifying Questions Guidance

  • Must the indices be distinct? No. The inequalities are non-strict.
  • Is the array a permutation or are its values unique? Neither property is promised.
  • Can a result be absent from the input? Yes, as the result 7 in the second example shows.
  • Does the answer count triples or resulting values? It counts each distinct XOR value once.

What a Strong Answer Covers Guidance

  • All permitted index triples, including repeated indices.
  • Distinct result values despite repeated inputs or multiple producing triples.
  • A justification that no possible result is missed or counted spuriously.
  • Resource bounds for 1500 elements and the full value range.

Follow-up Questions Guidance

  • How would requiring strictly increasing indices change which triples are allowed?
  • Can several distinct index triples contribute just one value to the requested answer?