Find the largest element in the array and set the lengths of the permutations accordingly
l1 = * max_element ( arr , arr + n ) ; l2 = n - l1 ; set < int > s1 , s2 ; for ( int i = 0 ; i < l1 ; i ++ ) s1 . insert ( arr [ i ] ) ; for ( int i = l1 ; i < n ; i ++ ) s2 . insert ( arr [ i ] ) ; if ( s1 . size ( ) == l1 && s2 . size ( ) == l2 ) printPermutations ( arr , n , l1 , l2 ) ; else { swap ( l1 , l2 ) ; printPermutations ( arr , n , l1 , l2 ) ; } }