human
Find triplets in an array whose AND is maximum | Python3 program to find triplet with maximum bitwise AND ..
Flag Array initially set to true for all numbers.
2D array for bit representation of all the numbers . Initially all bits are set to 0..
Finding bit representation of every number and storing it in bits array ..
Checking last bit of the number.
Dividing number by 2..
maximum And number initially 0..
Traversing the 2d binary representation . 0 th index represents 32 th bits while 32 th index represents 0 th bit ..
If cnt greater than 3 then ( 32 - i ) th bits of the number will be set ..
Setting flags of the numbers whose ith bit is not set ..
Counting the numbers whose flag are true ..
Driver Code"
Solution in Python:
 ... What is a code to find triplets in an array whose AND is maximum and the other steps required?
assistant
#Here is code to find triplets in an array whose AND is maximum and the other steps required.
 def maxTriplet ( a , n ) :
	f = [ True for i in range ( n ) ]
	bits = [ [ 0 for i in range ( 33 ) ] for i in range ( n ) ]
	for i in range ( n ) :
		num = a [ i ]
		j = 32
		while ( num ) :
			if ( num & 1 ) :
				bits [ i ] [ j ] = 1
			j -= 1
			num >>= 1
	ans = 0
	for i in range ( 33 ) :
		cnt = 0
		for j in range ( n ) :
			if ( bits [ j ] [ i ] and f [ j ] ) :
				cnt += 1
		if ( cnt >= 3 ) :
			ans += pow ( 2 , 32 - i )
			for j in range ( n ) :
				if ( bits [ j ] [ i ] == False ) :
					f [ j ] = False
	cnt = 0
	for i in range ( n ) :
		if ( f [ i ] ) :
			cnt += 1
	NumberOfTriplets = ( cnt * ( cnt - 1 ) * ( cnt - 2 ) ) // 6
	print ( NumberOfTriplets , ans )
a = [ 4 , 11 , 10 , 15 , 26 ]
n = len ( a )
maxTriplet ( a , n )