Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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.Example 1:
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
Example 2:
Input: nums = [0,0,0] Output: 27
Constraints:
1 <= nums.length <= 10000 <= nums[i] < 216Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Bit Manipulation
[2,1,3]
[0,0,0]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #982: Triples with Bitwise AND Equal To Zero
class Solution {
public int countTriplets(int[] nums) {
int mx = 0;
for (int x : nums) {
mx = Math.max(mx, x);
}
int[] cnt = new int[mx + 1];
for (int x : nums) {
for (int y : nums) {
cnt[x & y]++;
}
}
int ans = 0;
for (int xy = 0; xy <= mx; ++xy) {
for (int z : nums) {
if ((xy & z) == 0) {
ans += cnt[xy];
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #982: Triples with Bitwise AND Equal To Zero
func countTriplets(nums []int) (ans int) {
mx := slices.Max(nums)
cnt := make([]int, mx+1)
for _, x := range nums {
for _, y := range nums {
cnt[x&y]++
}
}
for xy := 0; xy <= mx; xy++ {
for _, z := range nums {
if xy&z == 0 {
ans += cnt[xy]
}
}
}
return
}
# Accepted solution for LeetCode #982: Triples with Bitwise AND Equal To Zero
class Solution:
def countTriplets(self, nums: List[int]) -> int:
cnt = Counter(x & y for x in nums for y in nums)
return sum(v for xy, v in cnt.items() for z in nums if xy & z == 0)
// Accepted solution for LeetCode #982: Triples with Bitwise AND Equal To Zero
struct Solution;
use std::collections::HashMap;
impl Solution {
fn count_triplets(a: Vec<i32>) -> i32 {
let n = a.len();
let mut hm: HashMap<i32, usize> = HashMap::new();
for i in 0..n {
for j in 0..n {
*hm.entry(a[i] & a[j]).or_default() += 1;
}
}
let mut res = 0;
for i in 0..n {
for (&k, &v) in hm.iter() {
if a[i] & k == 0 {
res += v;
}
}
}
res as i32
}
}
#[test]
fn test() {
let a = vec![2, 1, 3];
let res = 12;
assert_eq!(Solution::count_triplets(a), res);
}
// Accepted solution for LeetCode #982: Triples with Bitwise AND Equal To Zero
function countTriplets(nums: number[]): number {
const mx = Math.max(...nums);
const cnt: number[] = Array(mx + 1).fill(0);
for (const x of nums) {
for (const y of nums) {
cnt[x & y]++;
}
}
let ans = 0;
for (let xy = 0; xy <= mx; ++xy) {
for (const z of nums) {
if ((xy & z) === 0) {
ans += cnt[xy];
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.