# Triples With Bitwise And Equal To Zero Problem

## Description

LeetCode Problem 982.

Given an integer array nums, return the number of AND triples.

An AND triple is a triple of indices (i, j, k) such that:

• 0 <= i < nums.length
• 0 <= j < nums.length
• 0 <= k < nums.length
• nums[i] & nums[j] & nums[k] == 0, where & represents the bitwise-AND operator.

Example 1:

``````1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Input: nums = [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 &amp; 2 &amp; 1
(i=0, j=1, k=0) : 2 &amp; 1 &amp; 2
(i=0, j=1, k=1) : 2 &amp; 1 &amp; 1
(i=0, j=1, k=2) : 2 &amp; 1 &amp; 3
(i=0, j=2, k=1) : 2 &amp; 3 &amp; 1
(i=1, j=0, k=0) : 1 &amp; 2 &amp; 2
(i=1, j=0, k=1) : 1 &amp; 2 &amp; 1
(i=1, j=0, k=2) : 1 &amp; 2 &amp; 3
(i=1, j=1, k=0) : 1 &amp; 1 &amp; 2
(i=1, j=2, k=0) : 1 &amp; 3 &amp; 2
(i=2, j=0, k=1) : 3 &amp; 2 &amp; 1
(i=2, j=1, k=0) : 3 &amp; 1 &amp; 2
``````

Example 2:

``````1
2
Input: nums = [0,0,0]
Output: 27
``````

Constraints:

• 1 <= nums.length <= 1000
• 0 <= nums[i] < 2^16

## Sample C++ Code

``````1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
int countTriplets(vector<int>& nums) {
unordered_map<int, int> freq;
for (auto& x : nums)
for (auto& y : nums)
++freq[x&y];
int ans = 0;
for (auto& x : nums) {
x ^= 0xffff;
for (int mask = x; x; x = mask & (x-1))
ans += freq[x];
}
return ans + size(nums) * freq[0];
}
};
``````