Given an array of integers arr
.
We want to select three indices i
, j
and k
where (0 <= i < j <= k < arr.length)
.
Let’s define a
and b
as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i
, j
and k
) Where a == b
.
Example 1:
Input: arr = [2,3,1,6,7] Output: 4 Explanation: 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 3:
Input: arr = [2,3] Output: 0
Example 4:
Input: arr = [1,3,5,7,9] Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22] Output: 8
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8
Solution 1: Brute Force (TLE)
Time complexity: O(n^4)
Space complexity: O(1)
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// Author: Huahua class Solution { public: int countTriplets(vector<int>& arr) { const int n = arr.size(); int ans = 0; for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) for (int k = j; k < n; ++k) { int a = 0; int b = 0; for (int t = i; t < j; ++t) a ^= arr[t]; for (int t = j; t <= k; ++t) b ^= arr[t]; if (a == b) ++ans; } return ans; } }; |
Solution 2: Prefix XORs
Let xors[i] = arr[0] ^ arr[1] ^ … ^ arr[i-1]
arr[i] ^ arr[i + 1] ^ … ^ arr[j – 1] = (arr[0] ^ … ^ arr[j – 1]) ^ (arr[0] ^ … ^ arr[i-1]) = xors[j] ^ xors[i]
We then can compute a and b in O(1) time.
Time complexity: O(n^3)
Space complexity: O(n)
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Author: Huahua, 232 ms class Solution { public: int countTriplets(vector<int>& arr) { const int n = arr.size(); int ans = 0; vector<int> xors(n + 1); for (int i = 0; i < n; ++i) xors[i + 1] = xors[i] ^ arr[i]; for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) for (int k = j; k < n; ++k) { const int a = xors[j] ^ xors[i]; const int b = xors[k + 1] ^ xors[j]; if (a == b) ++ans; } return ans; } }; |
Solution 3: Prefix XORs II
a = arr[i] ^ arr[i + 1] ^ … ^ arr[j – 1]
b = arr[j] ^ arr[j + 1] ^ … ^ arr[k]
a == b => a ^ b == 0
XORs(i ~ k) == 0
XORS(0 ~ k) ^ XORs(0 ~ i – 1) = 0
Problem => find all pairs of (i – 1, k) such that xors[k+1] == xors[i]
For each pair (i – 1, k), there are k – i positions we can insert j.
Time complexity: O(n^2)
Space complexity: O(1)
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// Author: Huahua class Solution { public: int countTriplets(vector<int>& arr) { const int n = arr.size(); int ans = 0; vector<int> xors(n + 1); for (int i = 0; i < n; ++i) xors[i + 1] = xors[i] ^ arr[i]; for (int i = 0; i < n; ++i) for (int k = i + 1; k < n; ++k) if (xors[k + 1] == xors[i]) ans += k - i; return ans; } }; |
Solution 3: HashTable
Similar to target sum, use a hashtable to store the frequency of each prefix xors.
Time complexity: O(n)
Space complexity: O(n)
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Author: Huahua // Author: Huahua 4 ms class Solution { public: int countTriplets(vector<int>& arr) { const int n = arr.size(); int ans = 0; unordered_map<int, int> freq{{0, 1}}; unordered_map<int, int> sum; int X = 0; for (int i = 0; i < n; ++i) { X ^= arr[i]; ans += freq[X] * i - sum[X]; ++freq[X]; sum[X] += i + 1; } return ans; } }; |