LeetCode Problem

How to Solve Count Triplets That Can Form Two Arrays of Equal XOR

This problem requires identifying all index triplets (i, j, k) such that the XOR of arr[i..j-1] equals the XOR of arr[j..k]. By maintaining a running prefix XOR and using a hash map to track previously seen XOR values, you can efficiently count valid splits. Careful attention to indices and cumulative XOR ensures O(n) time complexity without missing overlapping subarrays or double-counting.

GhostInterview Help

Need help with Count Triplets That Can Form Two Arrays of Equal XOR without spending extra time grinding it?

GhostInterview can read Count Triplets That Can Form Two Arrays of Equal XOR from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #1442Array scanning plus hash lookupReviewed 2026-03-07
Difficulty
Medium
Primary pattern
Array scanning plus hash lookup
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

This problem requires identifying all index triplets (i, j, k) such that the XOR of arr[i..j-1] equals the XOR of arr[j..k]. By maintaining a running prefix XOR and using a hash map to track previously seen XOR values, you can efficiently count valid splits. Careful attention to indices and cumulative XOR ensures O(n) time complexity without missing overlapping subarrays or double-counting.

Problem Statement

Given an integer array arr, your task is to count the number of triplets (i, j, k) where 0 <= i < j <= k < arr.length such that splitting arr into two subarrays arr[i..j-1] and arr[j..k] results in both having equal XOR values. The goal is to efficiently identify these triplets using array scanning and hash lookup techniques.

For example, with arr = [2,3,1,6,7], the triplets satisfying the condition are (0,1,2), (0,2,2), (2,3,4), and (2,4,4). Your solution must handle arrays of up to length 300 and values up to 10^8 while leveraging prefix XOR computations and hash maps to optimize counting.

Examples

Example 1

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

Output: 4

The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)

Example 2

Input: arr = [1,1,1,1,1]

Output: 10

Example details omitted.

Constraints

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

Solution Approach

Compute Prefix XOR

Maintain an array of prefix XOR values where prefix[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]. This allows computing any subarray XOR in constant time, which is essential for efficiently comparing arr[i..j-1] and arr[j..k] without nested loops.

Use Hash Map to Track Prefix XOR Counts

Iterate through the array and store the frequency and cumulative indices of each prefix XOR value in a hash map. Whenever a repeated prefix XOR is encountered, it indicates that a subarray with zero XOR exists between previous occurrences and the current index, allowing multiple valid triplets to be counted efficiently.

Count Valid Triplets with Index Logic

For each repeated prefix XOR, calculate the number of triplets using the formula derived from previously stored counts and index sums. This approach avoids checking every triplet explicitly, leveraging the hash map to reduce complexity from O(n^3) to O(n).

Complexity Analysis

MetricValue
TimeO(n)
SpaceO(n)

The solution achieves O(n) time because each element is processed once for prefix XOR and hash map updates. Space complexity is O(n) due to storing counts and index sums in the hash map, which grows linearly with unique prefix XOR values.

What Interviewers Usually Probe

  • Focus on prefix XOR computation to compare subarrays quickly.
  • Consider hash map accumulation to avoid triple nested loops.
  • Watch for overlapping subarrays and proper index handling.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting that XOR of a subarray arr[i..j] can be computed using prefix XOR in O(1).
  • Miscounting triplets when multiple previous prefix XOR values exist at different indices.
  • Incorrectly handling i, j, k constraints leading to invalid subarrays.

Follow-up variants

  • Count quadruplets where splitting arr into three consecutive subarrays yields equal XORs.
  • Find all subarrays of length >= 2 with XOR zero without restricting to triplets.
  • Modify the array to allow negative numbers and still count valid triplets.

How GhostInterview Helps

  • Automatically computes prefix XOR arrays to quickly identify candidate subarrays for XOR equality.
  • Tracks XOR frequencies and cumulative indices to efficiently count all valid triplets in one pass.
  • Highlights potential index and overlap errors that commonly occur when applying hash map optimizations.

Topic Pages

FAQ

What is the best approach to solve Count Triplets That Can Form Two Arrays of Equal XOR?

Use prefix XOR and a hash map to track previous XOR values and their indices, allowing efficient O(n) counting of all valid triplets.

Can this problem be solved without a hash map?

Yes, but a naive solution would require triple nested loops with O(n^3) time, which is inefficient for larger arrays.

Why is prefix XOR important for this problem?

Prefix XOR allows constant-time computation of any subarray XOR, which is essential for comparing subarrays arr[i..j-1] and arr[j..k] efficiently.

How do overlapping subarrays affect counting triplets?

Overlapping subarrays can produce multiple valid triplets for the same XOR value, so tracking cumulative indices is crucial to avoid undercounting.

Is this approach applicable to arrays with negative numbers?

Yes, the prefix XOR and hash map method works for negative numbers, but care must be taken with bitwise operations in languages that handle signed integers differently.

GhostInterview Solver

Need direct help with Count Triplets That Can Form Two Arrays of Equal XOR instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Count Triplets That Can Form Two Arrays of Equal XOR from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.