Count Triplets That Can Form Two Arrays of Equal XOR

Medium Topics Company Tags

Count the index triples (i, j, k) satisfying 0 <= i < j <= k < arr.length for which the XOR of arr[i] through arr[j - 1] equals the XOR of arr[j] through arr[k]. Both ranges are nonempty. Return the number of such triples.

Example 1:

Input: arr = [4,4]

Output: 1

Explanation: The only eligible triple is (0, 1, 1). Its two ranges are [4] and [4], which have equal XOR.

Example 2:

Input: arr = [1,2,4]

Output: 0

Explanation: The eligible pairs of adjacent nonempty ranges have XORs (1,2), (1,6), (3,4), or (2,4). None are equal.

Example 3:

Input: arr = [3,3,3]

Output: 2

Explanation: The triples (0, 1, 1) and (1, 2, 2) each compare two single entries equal to 3. The other eligible triples do not have equal XOR.

Constraints:

  • 1 <= arr.length <= 300
  • 1 <= arr[i] <= 100000000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.