Triples with Bitwise AND Equal To Zero - 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

The challenge is to efficiently count all possible combinations of three elements (with repetition allowed) whose bitwise AND equals zero. This problem tests your understanding of bit manipulation and optimization techniques for avoiding the naive O(n³) approach.

Key Insight: When three numbers AND to zero, it means that for every bit position, at least one of the three numbers must have a 0 in that position.

Input & Output

example_1.py — Basic Case
$ Input: [2,1,3]
Output: 12
💡 Note: The 12 triples are: (0,0,1), (0,1,0), (0,1,1), (0,1,2), (1,0,0), (1,0,1), (1,0,2), (1,1,0), (1,2,0), (2,0,1), (2,1,0), (2,1,1). For example: 2&1&3=0, 1&2&1=0, etc.
example_2.py — Single Element
$ Input: [0]
Output: 1
💡 Note: Only one possible triple: (0,0,0). Since 0&0&0 = 0, this counts as 1 valid triplet.
example_3.py — All Same Bits
$ Input: [7,7,7]
Output: 0
💡 Note: All numbers are 7 (binary: 111). Since 7&7&7 = 7 ≠ 0, there are no valid triplets where the AND equals zero.

Visualization

Tap to expand
Bitwise AND Triplet VisualizationExample: nums = [2, 1, 3]Binary representations:2 = 0101 = 0013 = 011Valid Triplet Example:010&001&011=000Each bit position has at least one 0Optimization Strategy1. Brute Force: Check all n³ combinations → O(n³)2. Two-Pass: Precompute pairs, then check thirds → O(n²)3. Key Insight: (a & b) & c = 0 ⟺ For each bit, at least one element has 0Result: 12 valid triplets for [2,1,3]Including: (0,0,1), (0,1,0), (0,1,1), (0,1,2), etc.
Understanding the Visualization
1
Understand AND Operation
For AND to be 0, each bit position needs at least one 0
2
Optimize with Pairs
Precompute pairwise results to avoid cubic complexity
3
Count Valid Combinations
For each third element, count compatible pairs
4
Sum Total Results
Add frequencies of all valid triplet combinations
Key Takeaway
🎯 Key Insight: Instead of checking all O(n³) triplets, we can optimize to O(n²) by precomputing pairwise AND results and then efficiently counting valid third elements that make the final result zero.

Time & Space Complexity

Time Complexity
⏱️
O(n³)

Three nested loops, each running n times

n
2n
Quadratic Growth
Space Complexity
O(1)

Only using a few variables for counting and iteration

n
2n
Linear Space

Constraints

  • 1 <= nums.length <= 1000
  • 0 <= nums[i] < 216
  • The same index can be used multiple times in a triplet
  • Time limit: Solutions must handle up to 109 operations efficiently
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
31.8K Views
Medium Frequency
~25 min Avg. Time
1.4K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen