human
What is a code to number of possible permutations when absolute difference between number of elements to the right and left are given and the other steps required given the following:
Number of possible permutations when absolute difference between number of elements to the right and left are given | Function to find the number of permutations possible of the original array to satisfy the given absolute differences.
To store the count of each a [ i ] in a map.
if n is odd.
check the count of each whether it satisfy the given criteria or not.
there is only 1 way for middle element ..
for others there are 2 ways ..
now find total ways.
When n is even ..
there will be no middle element so for each a [ i ] there will be 2 ways.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to number of possible permutations when absolute difference between number of elements to the right and left are given and the other steps required.
 def totalways ( arr , n ) :
	cnt = dict ( )
	for i in range ( n ) :
		cnt [ arr [ i ] ] = cnt . get ( arr [ i ] , 0 ) + 1
	if ( n % 2 == 1 ) :
		start , endd = 0 , n - 1
		for i in range ( start , endd + 1 , 2 ) :
			if ( i == 0 ) :
				if ( cnt [ i ] != 1 ) :
					return 0
			else :
				if ( cnt [ i ] != 2 ) :
					return 0
		ways = 1
		start = 2
		endd = n - 1
		for i in range ( start , endd + 1 , 2 ) :
			ways = ways * 2
		return ways
	elif ( n % 2 == 0 ) :
		start = 1
		endd = n - 1
		for i in range ( 1 , endd + 1 , 2 ) :
			if ( cnt [ i ] != 2 ) :
				return 0
		ways = 1
		for i in range ( start , endd + 1 , 2 ) :
			ways = ways * 2
		return ways
N = 5
arr = [ 2 , 4 , 4 , 0 , 2 ]
print ( totalways ( arr , N ) )