Count triples with Bitwise AND equal to Zero
Given an array of integers A[] consisting of N integers, find the number of triples of indices (i, j, k) such that A[i] & A[j] & A[k] is 0(<0 ? i, j, k ? N and & denotes Bitwise AND operator.
Examples:
Input: A[]={2, 1, 3}
Output: 12
Explanation: The following i, j, k triples can be chosen whose bitwise AND is zero:
(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
Input: A[]={21, 15, 6}
Output: 0
Explanation: No such triplets exist.
Approach: The idea to solve this problem is to use a Map to process the array solving elements. Follow the steps below to solve the problem:
- Initialize a Map to store frequencies of every possible value of A[i] & A[j]. Also, initialize a variable answer with 0, to store the required count.
- Traverse the array and for each array element, traverse the map and check for each map if key, if it’s Bitwise AND with the current array element is 0 or not. For every array element for which it is found to be true, increase answer by frequency of the key.
- After completing the traversal of the array, print answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int countTriplets(vector< int >& A)
{
int cnt = 0;
unordered_map< int , int > tuples;
for ( auto a : A)
for ( auto b : A)
++tuples[a & b];
for ( auto a : A)
for ( auto t : tuples)
if ((t.first & a) == 0)
cnt += t.second;
return cnt;
}
int main()
{
vector< int > A = { 2, 1, 3 };
cout << countTriplets(A);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int countTriplets( int []A)
{
int cnt = 0 ;
HashMap<Integer,Integer> tuples = new HashMap<Integer,Integer>();
for ( int a : A)
for ( int b : A)
{
if (tuples.containsKey(a & b))
tuples.put(a & b, tuples.get(a & b) + 1 );
else
tuples.put(a & b, 1 );
}
for ( int a : A)
for (Map.Entry<Integer, Integer> t : tuples.entrySet())
if ((t.getKey() & a) == 0 )
cnt += t.getValue();
return cnt;
}
public static void main(String[] args)
{
int []A = { 2 , 1 , 3 };
System.out.print(countTriplets(A));
}
}
|
Python3
def countTriplets(A) :
cnt = 0 ;
tuples = {};
for a in A:
for b in A:
if (a & b) in tuples:
tuples[a & b] + = 1 ;
else :
tuples[a & b] = 1 ;
for a in A:
for t in tuples:
if ((t & a) = = 0 ):
cnt + = tuples[t];
return cnt;
if __name__ = = "__main__" :
A = [ 2 , 1 , 3 ];
print (countTriplets(A));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int countTriplets( int []A)
{
int cnt = 0;
Dictionary< int , int > tuples = new Dictionary< int , int >();
foreach ( int a in A)
foreach ( int b in A)
{
if (tuples.ContainsKey(a & b))
tuples[a & b] = tuples[a & b] + 1;
else
tuples.Add(a & b, 1);
}
foreach ( int a in A)
foreach (KeyValuePair< int , int > t in tuples)
if ((t.Key & a) == 0)
cnt += t.Value;
return cnt;
}
public static void Main(String[] args)
{
int []A = { 2, 1, 3 };
Console.Write(countTriplets(A));
}
}
|
Javascript
<script>
function countTriplets(A)
{
var cnt = 0;
var tuples = new Map();
A.forEach(a => {
A.forEach(b => {
if (tuples.has(a & b))
tuples.set(a & b, tuples.get(a & b)+1)
else
tuples.set(a & b, 1)
});
});
A.forEach(a => {
tuples.forEach((value, key) => {
if ((key & a) == 0)
cnt += value;
});
});
return cnt;
}
var A = [2, 1, 3];
document.write( countTriplets(A));
</script>
|
Time Complexity: O(max(M*N, N2)) where M is the maximum element present in the given array
Auxiliary Space: O(M)
Last Updated :
09 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...