LeetCode solutions

1024. Triples with Bitwise AND Equal To Zero

My accepted Python solution to LeetCode problem 1024, Triples with Bitwise AND Equal To Zero, running in 2865ms.

  • Difficulty: Hard
  • Python
  • Runtime 2865ms
  • Memory 20.2MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 2865ms, memory 20.2MB, accepted 2026-01-01.

python
class Solution:
    def countTriplets(self, nums: List[int]) -> int:
        from collections import Counter
        
        # Count pairs (nums[i] & nums[j])
        pair_and = Counter()
        for x in nums:
            for y in nums:
                pair_and[x & y] += 1
        
        # Count triplets where (pair_and) & nums[k] == 0
        result = 0
        for k in nums:
            for pair_val, count in pair_and.items():
                if pair_val & k == 0:
                    result += count
        
        return result

Source