> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Triples with Bitwise AND Equal To Zero

> Tested Python solution for LeetCode 982 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 982, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Bit Manipulation](/catalog/topics/bit-manipulation). [View on LeetCode](https://leetcode.com/problems/triples-with-bitwise-and-equal-to-zero/description/).

Generate this problem as a practice environment: tested reference solution, 22 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 982   # by problem number
lcpy gen -s triples_with_bitwise_and_equal_to_zero   # by problem name
```

## Problem

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.

### Examples

```
Input: nums = [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
```

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

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/triples_with_bitwise_and_equal_to_zero/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/triples_with_bitwise_and_equal_to_zero/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter


class Solution:
    # Time: O(n^2 + n * d) where d is the number of distinct pair AND values
    # Space: O(d)
    def count_triplets(self, nums: list[int]) -> int:
        pair_counts: Counter[int] = Counter()
        for a in nums:
            for b in nums:
                pair_counts[a & b] += 1

        total = 0
        for x in nums:
            for pair_and, count in pair_counts.items():
                if pair_and & x == 0:
                    total += count
        return total
```

## Complexity

| Time | Space |
| - | - |
| O(n^2 + n \* d) where d is the number of distinct pair AND values | O(d) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.