Rearrange array to maximize count of local minima | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void rearrangeArrMaxcntMinima ( int arr [ ] , int N ) { sort ( arr , arr + N ) ; int left = 0 ; int right = N / 2 ; while ( left < N / 2 right < N ) { if ( right < N ) { cout << arr [ right ] << " ▁ " ; right ++ ; } if ( left < N / 2 ) { cout << arr [ left ] << " ▁ " ; left ++ ; } } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; rearrangeArrMaxcntMinima ( arr , N ) ; return 0 ; }