Rearrange the Array to maximize the elements which is smaller than both its adjacent elements | Function to rearrange array such that count of element that are smaller than their adjacent elements is maximum ; Stores the rearranged array ; Stores the maximum count of elements ; Sort the given array ; Place the smallest ( N - 1 ) / 2 elements at odd indices ; Placing the rest of the elements at remaining indices ; If no element of the array has been placed here ; Print the resultant array ; Input ; Function call
def maximumIndices ( arr , N ) : NEW_LINE INDENT temp = [ 0 ] * N NEW_LINE maxIndices = ( N - 1 ) // 2 NEW_LINE arr . sort ( ) NEW_LINE for i in range ( maxIndices ) : NEW_LINE INDENT temp [ 2 * i + 1 ] = arr [ i ] NEW_LINE DEDENT j = 0 NEW_LINE i = maxIndices NEW_LINE while ( i < N ) : NEW_LINE INDENT if ( temp [ j ] == 0 ) : NEW_LINE INDENT temp [ j ] = arr [ i ] NEW_LINE i += 1 NEW_LINE DEDENT j += 1 NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT print ( temp [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE maximumIndices ( arr , N ) NEW_LINE