human
Write me a code to permutations of an array having sum of Bitwise AND of adjacent elements at least K and the other steps required given the following:
Permutations of an array having sum of Bitwise AND of adjacent elements at least K | Python3 program for the above approach.
Function to print all permutations of arr [ ] such that the sum of Bitwise AND of all adjacent element is at least K.
To check if any permutation exists or not.
Sort the given array.
Find all possible permutations.
Stores the sum of bitwise AND of adjacent elements of the current permutation.
Traverse the current permutation of arr [ ].
Update the sum.
If the sum is at least K , then print the current permutation.
Set the flag variable.
Print the current permutation.
If flag is unset , then print - 1.
Driver Code.
Function Call"
Solution in Python:
assistant
#Here is code to permutations of an array having sum of Bitwise AND of adjacent elements at least K and the other steps required.
 def next_permutation ( a ) :
	for i in reversed ( range ( len ( a ) - 1 ) ) :
		if a [ i ] < a [ i + 1 ] :
			break
	else :
		return False
	j = next ( j for j in reversed ( range ( i + 1 , len ( a ) ) ) if a [ i ] < a [ j ] )
	a [ i ] , a [ j ] = a [ j ] , a [ i ]
	a [ i + 1 : ] = reversed ( a [ i + 1 : ] )
	return True
def printPermutation ( arr , n , k ) :
	flag = False
	arr . sort ( )
	while True :
		sum = 0
		for i in range ( n - 1 ) :
			sum += arr [ i ] & arr [ i + 1 ]
		if ( sum >= k ) :
			flag = True
			for i in range ( n ) :
				print ( arr [ i ] , end = " ▁ " )
			print ( )
		if ( next_permutation ( arr ) == False ) :
			break
	if ( flag == False ) :
		print ( " - 1" )
arr = [ 1 , 2 , 3 , 4 , 5 ]
K = 8
N = len ( arr )
printPermutation ( arr , N , K )