Rearrange array to maximize count of local minima | Function to rearrange array elements to maximize count of local minima in the array ; Sort the array in ascending order ; Stores index of left pointer ; Stores index of right pointer ; Traverse the array elements ; if right is less than N ; Print array element ; Update right ; Print array element ; Update left ; Driver Code
def rearrangeArrMaxcntMinima ( arr , N ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE left = 0 NEW_LINE right = N // 2 NEW_LINE while ( left < N // 2 or right < N ) : NEW_LINE INDENT if ( right < N ) : NEW_LINE INDENT print ( arr [ right ] , end = " ▁ " ) NEW_LINE right += 1 NEW_LINE DEDENT if ( left < N // 2 ) : NEW_LINE INDENT print ( arr [ left ] , end = " ▁ " ) NEW_LINE left += 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE rearrangeArrMaxcntMinima ( arr , N ) NEW_LINE DEDENT