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.

Input & Output

Example 1 — Basic Case
$ Input: nums = [2,1,3]
Output: 12
💡 Note: The 12 triples with bitwise AND equal to 0 are: (0,0,1), (0,1,0), (0,1,1), (0,1,2), (1,0,0), (1,0,2), (1,1,0), (1,2,0), (1,2,1), (1,2,2), (2,1,0), (2,1,1)
Example 2 — All Zeros
$ Input: nums = [0,0,0]
Output: 27
💡 Note: All possible 3³ = 27 triples have AND equal to 0 since 0 & 0 & 0 = 0
Example 3 — No Valid Triples
$ Input: nums = [7,7,7]
Output: 0
💡 Note: 7 & 7 & 7 = 7 ≠ 0, so no triples satisfy the condition

Constraints

  • 1 ≤ nums.length ≤ 1000
  • 0 ≤ nums[i] < 216

Visualization

Tap to expand
Find Triples with AND = 0213010001011Input: [2, 1, 3] with bit representationsCheck all triples (i,j,k):010 & 001 & 011 = 000 ✓010 & 010 & 011 = 010 ✗Output: Count = 12 valid triples
Understanding the Visualization
1
Input
Array with integer values having different bit patterns
2
Process
Check all possible triples (i,j,k) for bitwise AND = 0
3
Output
Count of valid triples
Key Takeaway
🎯 Key Insight: Use bitwise AND properties to find combinations where all bits are covered by at least one zero across the three numbers
Asked in
Google 15 Facebook 12 Microsoft 8
28.0K Views
Medium Frequency
~25 min Avg. Time
850 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