Count Triplets With Bitwise AND Zero

You are given an integer array nums.

Return the total number of ordered triplets (i, j, k) such that:

  • 0 <= i < nums.length
  • 0 <= j < nums.length
  • 0 <= k < nums.length
  • (nums[i] & nums[j] & nums[k]) == 0

Here, & denotes the bitwise AND operator.

An ordered triplet means (i, j, k) and (j, i, k) are treated as different if their indices differ.

Example 1:

Input: nums = [1, 2]

Output: 6

Explanation:

All possible ordered triplets are:

  • (0, 0, 0) → 1 & 1 & 1 = 1
  • (0, 0, 1) → 1 & 1 & 2 = 0
  • (0, 1, 0) → 1 & 2 & 1 = 0
  • (0, 1, 1) → 1 & 2 & 2 = 0
  • (1, 0, 0) → 2 & 1 & 1 = 0
  • (1, 0, 1) → 2 & 1 & 2 = 0
  • (1, 1, 0) → 2 & 2 & 1 = 0
  • (1, 1, 1) → 2 & 2 & 2 = 2

Out of these 8 triplets, 6 satisfy the condition.

Example 2:

Input: nums = [0, 1, 2]

Output: 25

Now Your Turn!

Pick the correct output for the given input

Input: nums = [1, 3]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

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

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int countTriplets(vector<int>& nums) {
// Your code goes here
}
};
Test Case

Input:

Nums