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