aboutsummaryrefslogtreecommitdiff
path: root/triples_bitwise_and_zero.py
blob: 0d71669048985284af486e2bb7a1df444cddd6f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 982. Triples with Bitwise AND Equal To Zero

"""
given an integer array nums, return the number of AND triplets. 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
"""


class Solution(object):
    def countTriplets(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        n, ans, dict1 = len(nums), 0, collections.Counter(x&y for x in nums for y in nums)       
        for i in nums:
            for j in dict1:
                if i & j == 0:
                    ans += dict1[j]
        return ans;


if __name__ == "__main__":
    obj = Solution()
    print(obj.countTriplets([2,1,3])) # expect: 12
    print(obj.countTriplets([0,0,0])) # expect: 27