Find the Number of Permutations that satisfy the given condition in an array | Function to calculate x ^ y recursively ; Function to return the number of permutations that satisfy the given condition in an array ; If there is only one element then only one permutation is available ; Sort the array for calculating the number of elements occurring twice ; If the maximum element is occurring twice , then the number of permutations satisfying the condition is 0 ; This variable will store the number of element occurring twice ; Loop to check the number of elements occurring twice ; Check if this element is occurring twice ; If this element is occurring twice then check if this number is occurring more than twice ; If element occurring thrice then no permutation will satisfy the given condition ; Since we have checked the next element as well , then we can increment the loop variable ; Driver code
def pow ( x , y ) : NEW_LINE INDENT if ( y == 1 ) : NEW_LINE INDENT return x NEW_LINE DEDENT if ( y == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT temp = pow ( x , y // 2 ) NEW_LINE temp *= temp NEW_LINE if ( y & 1 ) : NEW_LINE INDENT temp *= x NEW_LINE DEDENT return temp NEW_LINE DEDENT def noOfPermutations ( a , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT a . sort ( ) NEW_LINE if ( a [ n - 1 ] == a [ n - 2 ] ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT x = 0 NEW_LINE for i in range ( n - 2 ) : NEW_LINE INDENT if ( a [ i ] == a [ i + 1 ] ) : NEW_LINE INDENT if ( a [ i ] == a [ i + 2 ] ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT x += 1 NEW_LINE i += 1 NEW_LINE DEDENT DEDENT return pow ( 2 , n - 2 * x - 1 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 2 , 3 , 4 ] NEW_LINE n = len ( a ) NEW_LINE num = noOfPermutations ( a , n ) NEW_LINE print ( num ) NEW_LINE DEDENT