Count of possible arrays from prefix | Function to find power of a number . ; Function to find factorial of a number . ; Function to print no of arrays ; c variable counts the no of pairs ; Map to store the frequency of each element ; Sum of all elements of the array ; Variable to check if it is possible to make any array ; First element of suffix array and the last element of prefix array ; Check if the element exists in the map ; If elements of any pair are equal and their frequency is not divisible by 2 update the isArrayPossible variable to false and break through the loop ; If elements of any pair are not equal and their frequency is not same update the isArrayPossible variable to false and break through the loop ; Check if frequency is greater than zero ; update the count of pairs ; Multiply the answer by 2 ^ ( frequency of pairs ) since the elements of the pair are not the same in this condition ; Divide the answer by the factorial of no of similar pairs ; Make frequency of both these elements 0 ; Update the count of pairs ; Divide the answer by the factorial of no . of similar pairs ; Make frequency of this element 0 ; Check if it is possible to make the array and there are n - 1 pairs whose sum will be equal to s1 ; Driver code ; Function calling
def power ( a , b ) : NEW_LINE INDENT result = 1 ; NEW_LINE while ( b > 0 ) : NEW_LINE INDENT if ( b % 2 == 1 ) : NEW_LINE INDENT result = result * a ; NEW_LINE DEDENT a = a * a ; NEW_LINE b = b // 2 ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT def factorial ( n ) : NEW_LINE INDENT fact = 1 ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT fact = fact * i ; NEW_LINE DEDENT return fact ; NEW_LINE DEDENT def findNoOfArrays ( a , n ) : NEW_LINE INDENT sum = 0 ; c = 0 ; NEW_LINE mp = dict . fromkeys ( a , 0 ) ; NEW_LINE for i in range ( 2 * n ) : NEW_LINE INDENT mp [ a [ i ] ] += 1 ; NEW_LINE sum = sum + a [ i ] ; NEW_LINE DEDENT isArrayPossible = True ; NEW_LINE ans = factorial ( n - 1 ) ; NEW_LINE s1 = sum // ( n + 1 ) ; NEW_LINE if ( mp [ s1 ] >= 2 ) : NEW_LINE INDENT mp [ s1 ] = mp [ s1 ] - 2 ; NEW_LINE DEDENT else : NEW_LINE INDENT isArrayPossible = False ; NEW_LINE DEDENT if ( isArrayPossible ) : NEW_LINE INDENT for first , second in mp . items ( ) : NEW_LINE INDENT if ( first == s1 - first ) : NEW_LINE INDENT if ( mp [ first ] % 2 != 0 ) : NEW_LINE INDENT isArrayPossible = False ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( first != s1 - first ) : NEW_LINE INDENT if s1 - first in mp : NEW_LINE INDENT if ( mp [ first ] != mp [ s1 - first ] ) : NEW_LINE INDENT isArrayPossible = False ; NEW_LINE break ; NEW_LINE DEDENT DEDENT DEDENT if ( second > 0 ) : NEW_LINE INDENT if ( first != s1 - first ) : NEW_LINE INDENT c = c + second ; NEW_LINE ans = ans * power ( 2 , second ) ; NEW_LINE ans = ans / factorial ( second ) ; NEW_LINE mp [ first ] = 0 ; NEW_LINE mp [ s1 - first ] = 0 ; NEW_LINE DEDENT if ( first == s1 - first ) : NEW_LINE INDENT c = c + second // 2 ; NEW_LINE ans = ans // factorial ( second // 2 ) ; NEW_LINE mp [ first ] = 0 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( c < n - 1 or isArrayPossible == False ) : NEW_LINE INDENT print ( "0" ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( ans ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr1 = [ 5 , 2 , 3 , 5 ] ; NEW_LINE n1 = len ( arr1 ) ; NEW_LINE findNoOfArrays ( arr1 , n1 // 2 ) ; NEW_LINE arr2 = [ - 1 , - 1 , - 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 0 , 0 ] ; NEW_LINE n2 = len ( arr2 ) ; NEW_LINE findNoOfArrays ( arr2 , n2 // 2 ) ; NEW_LINE DEDENT