982. Triples with Bitwise AND Equal To Zero LeetCode Solution

In this guide, you will get 982. Triples with Bitwise AND Equal To Zero LeetCode Solution with the best time and space complexity. The solution to Triples with Bitwise AND Equal To Zero problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Triples with Bitwise AND Equal To Zero solution in C++
  4. Triples with Bitwise AND Equal To Zero solution in Java
  5. Triples with Bitwise AND Equal To Zero solution in Python
  6. Additional Resources
982. Triples with Bitwise AND Equal To Zero LeetCode Solution image

Problem Statement of Triples with Bitwise AND Equal To Zero

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.

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

See also  913. Cat and Mouse LeetCode Solution

Constraints:

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

Complexity Analysis

  • Time Complexity: O(n^2)
  • Space Complexity: O(2^{16}) = O(1)

982. Triples with Bitwise AND Equal To Zero LeetCode Solution in C++

class Solution {
 public:
  int countTriplets(vector<int>& nums) {
    constexpr int kMax = 1 << 16;
    int ans = 0;
    vector<int> count(kMax);  // {nums[i] & nums[j]: times}

    for (const int a : nums)
      for (const int b : nums)
        ++count[a & b];

    for (const int num : nums)
      for (int i = 0; i < kMax; ++i)
        if ((num & i) == 0)
          ans += count[i];

    return ans;
  }
};
/* code provided by PROGIEZ */

982. Triples with Bitwise AND Equal To Zero LeetCode Solution in Java

class Solution {
  public int countTriplets(int[] nums) {
    final int kMax = 1 << 16;
    int ans = 0;
    int[] count = new int[kMax]; // {nums[i] & nums[j]: times}

    for (final int a : nums)
      for (final int b : nums)
        ++count[a & b];

    for (final int num : nums)
      for (int i = 0; i < kMax; ++i)
        if ((num & i) == 0)
          ans += count[i];

    return ans;
  }
}
// code provided by PROGIEZ

982. Triples with Bitwise AND Equal To Zero LeetCode Solution in Python

class Solution:
  def countTriplets(self, nums: list[int]) -> int:
    kMax = 1 << 16
    ans = 0
    count = [0] * kMax  # {nums[i] & nums[j]: times}

    for a in nums:
      for b in nums:
        count[a & b] += 1

    for num in nums:
      for i in range(kMax):
        if (num & i) == 0:
          ans += count[i]

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.