Skip to main content
Back to problems
Leetcode
Medium
Arrays
Hash Maps
Math
Google
Meta
Count Triplets That Can Form Two Arrays Of Equal Xor

Count triplets of indices where two adjacent subarrays have the same XOR.

Acceptance 0%
Problem Statement

Given an array of integers, count the number of triplets (i,j,k)(i, j, k) such that:

  • 0i<jk<n0 \le i < j \le k < n
  • the XOR of the subarray from index ii to j1j - 1 is equal to the XOR of the subarray from index jj to kk

In other words, split a contiguous segment into two non-empty parts with the same XOR and count all valid choices of (i,j,k)(i, j, k).

Input Format

  • A single integer array arr of length nn.
  • Each value is an integer.

Output Format

  • Return the total number of valid triplets (i,j,k)(i, j, k) satisfying the XOR condition.

Constraints

  • 1n3001 \le n \le 300 for the brute-force friendly interview formulation, though the standard optimized solution works for larger nn as well.
  • Elements are integers in a range that supports XOR operations normally.
  • Count all valid index triplets.
Examples
Sample cases returned by the problem API.

Example 1

Input

arr = [2,3,1,6,7]

Output

4

Explanation

The valid triplets are (0,1,2), (0,2,2), (2,3,4), and (2,4,4). Each one splits a subarray into two parts with equal XOR.

Example 2

Input

arr = [1,1,1,1,1]

Output

10

Explanation

Many segments have equal XOR on both sides. Counting all valid index triplets gives 10.

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.