Show problems as
Classic problem #3513MediumArrayMathBit Manipulation

Number of Unique XOR Triplets I

Real-world version: Relay Panel Fault Codes
Tracked in this browser only
Read Number of Unique XOR Triplets I on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A cabinet holds n relay panels in a row. Panel i carries the stamp stamps[i], and the stamps are exactly the whole numbers from 1 to n, each used once, in some order.

A technician raises a fault code by selecting three panels and combining their stamps with the bitwise XOR operation. The three selections are made independently, so the same panel may be selected two or three times, and the order of the selections does not matter.

Return how many different fault codes can be raised.

Examples

Example 1

Input
stamps = [3, 4, 2, 5, 1]
Output
8

Selecting the panels stamped 1, 2 and 3 raises the code 0, and selecting the panels stamped 4, 5 and 3 raises the code 2. Over all selections 8 different codes come up.

Example 2

Input
stamps = [7, 5, 6, 8, 2, 1, 4, 3]
Output
16

Selecting the panels stamped 8, 1 and 7 raises the code 14, and over all selections 16 different codes come up.

Example 3

Input
stamps = [3, 9, 5, 1, 6, 8, 4, 7, 2]
Output
16

Selecting the panel stamped 5 all three times raises the code 5, and selecting the panels stamped 9, 8 and 2 raises the code 3. In all 16 different codes come up.

Constraints

  • 1 <= stamps.length == n <= 10^5
  • 1 <= stamps[i] <= n
  • stamps is a permutation of the whole numbers from 1 to n.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def distinct_fault_codes(stamps: list[int]) -> int:
Java
public int distinctFaultCodes(int[] stamps)