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
function rearrangeArrMaxcntMinima ( arr , N ) { arr . sort ( function ( a , b ) { return a - b ; } ) ; let left = 0 ; let right = Math . floor ( N / 2 ) ; while ( left < Math . floor ( N / 2 ) right < N ) { if ( right < N ) { document . write ( arr [ right ] + " " ) ; right ++ ; } if ( left < Math . floor ( N / 2 ) ) { document . write ( arr [ left ] + " " ) ; left ++ ; } } } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let N = arr . length ; rearrangeArrMaxcntMinima ( arr , N ) ;