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.length0 <= j < nums.length0 <= k < nums.lengthnums[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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code