human
Rearrange an array such that product of every two consecutive elements is a multiple of 4 | Function to rearrange array elements such that the every two consecutive elements is a multiple of 4.
If element is odd.
Odd.
If element is divisible by 4.
Divisible by 4.
If element is not divisible by 4.
Even but not divisible by 4.
Condition for rearrangement to be possible.
Print ODD [ i ] and FOUR [ i ] consecutively.
Print the remaining FOUR [ i ] , if any.
Condition for rearrangement to be possible.
Print ODD [ i ] and FOUR [ i ] consecutively.
Print the remaining FOUR [ i ] , if any.
Print the NON_FOUR [ i ] elements at the end.
No possible configuration.
Driver Code"
Solution in Python:
assistant
#Here is code to rearrange an array such that product of every two consecutive elements is a multiple of 4 and the other steps required.
 def Permute ( arr , n ) :
	odd = 0
	four = 0
	non_four = 0
	ODD , FOUR , NON_FOUR = [ ] , [ ] , [ ]
	for x in arr :
		if ( x & 1 ) :
			odd += 1
			ODD . append ( x )
		elif ( x % 4 == 0 ) :
			four += 1
			FOUR . append ( x )
		else :
			non_four += 1
			NON_FOUR . append ( x )
	if ( non_four == 0 and four >= odd - 1 ) :
		x = len ( ODD )
		y = len ( FOUR )
		i = 0
		while i < x :
			print ( ODD [ i ] , end = " ▁ " )
			if ( i < y ) :
				print ( FOUR [ i ] , end = " ▁ " )
		while ( i < y ) :
			print ( FOUR [ i ] , end = " ▁ " )
			i += 1
		print ( )
	elif ( non_four > 0 and four >= odd ) :
		x = len ( ODD )
		y = len ( FOUR )
		i = 0
		while i < x :
			print ( ODD [ i ] , end = " ▁ " )
			if ( i < y ) :
				print ( FOUR [ i ] , end = " ▁ " )
			i += 1
		while ( i < y ) :
			print ( FOUR [ i ] , end = " ▁ " )
			i += 1
		for j in NON_FOUR :
			print ( j , end = " ▁ " )
	else :
		print ( " Not ▁ Possible " )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 2 , 7 , 1 , 8 , 2 , 8 ]
	N = len ( arr )
	Permute ( arr , N )