Find the largest element in the array and set the lengths of the permutations accordingly
l1 = max ( arr ) NEW_LINE l2 = n - l1 NEW_LINE s1 = set ( ) NEW_LINE s2 = set ( ) NEW_LINE for i in range ( l1 ) : NEW_LINE INDENT s1 . add ( arr [ i ] ) NEW_LINE DEDENT for i in range ( l1 , n ) : NEW_LINE INDENT s2 . add ( arr [ i ] ) NEW_LINE DEDENT if ( len ( s1 ) == l1 and len ( s2 ) == l2 ) : NEW_LINE INDENT printPermutations ( arr , n , l1 , l2 ) NEW_LINE DEDENT else : NEW_LINE INDENT temp = l1 NEW_LINE l1 = l2 NEW_LINE l2 = temp NEW_LINE printPermutations ( arr , n , l1 , l2 ) NEW_LINE DEDENT