Rearrange array to maximize count of local minima | Java program to implement the above approach ; 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
import java . util . * ; class GFG { static void rearrangeArrMaxcntMinima ( int arr [ ] , int N ) { Arrays . sort ( arr ) ; int left = 0 ; int right = N / 2 ; while ( left < N / 2 right < N ) { if ( right < N ) { System . out . print ( arr [ right ] + " ▁ " ) ; right ++ ; } if ( left < N / 2 ) { System . out . print ( arr [ left ] + " ▁ " ) ; left ++ ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . length ; rearrangeArrMaxcntMinima ( arr , N ) ; } }