Rearrange array to maximize count of triplets ( i , j , k ) such that arr [ i ] > arr [ j ] < arr [ k ] and i < j < k | Function to rearrange the array to maximize the count of triplets ; Sort the given array ; Stores the permutations of the given array ; Stores the index of the given array ; Place the first half of the given array ; Place the last half of the given array ; Stores the count of triplets ; Check the given conditions ; Driver Code
def ctTriplets ( arr , N ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE temp = [ 0 ] * N NEW_LINE index = 0 NEW_LINE for i in range ( 1 , N , 2 ) : NEW_LINE INDENT temp [ i ] = arr [ index ] NEW_LINE index += 1 NEW_LINE DEDENT for i in range ( 0 , N , 2 ) : NEW_LINE INDENT temp [ i ] = arr [ index ] NEW_LINE index += 1 NEW_LINE DEDENT ct = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( i > 0 and i + 1 < N ) : NEW_LINE INDENT if ( temp [ i ] < temp [ i + 1 ] and temp [ i ] < temp [ i - 1 ] ) : NEW_LINE INDENT ct += 1 NEW_LINE DEDENT DEDENT DEDENT print ( " Count ▁ of ▁ triplets : " , ct ) NEW_LINE print ( " Array : " , end = " " ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT print ( temp [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE ctTriplets ( arr , N ) NEW_LINE