Given an array arr[] of size N, the task is to rearrange the array elements such that the count of local minima in the array is maximum.
Note:An element arr[x] is said to be a local minimum if it is less than or equal to both its adjacent elements. The first and last array elements can’t be considered as local minima.
Examples:
Input: arr[]= {1, 2, 3, 4, 5}
Output: 3 1 4 2 5
Explanation:
Rearranging array elements to {3, 1, 4, 2, 5}. The count of local minima in the array is 2, i.e. {arr[1], arr[3]}, which is the maximum possible count of local minima that can be obtained from the array. Therefore, the required output is 3 1 4 2 5.Input: arr[]= {1, 2, 3, 4, 5, 6}
Output: 4 1 5 2 6 3
Approach: The idea is to use sorting algorithms and two pointer technique to solve this problem. Follow the steps below to solve this problem:
- Sort the array in ascending order.
- Initialize two variables, say left = 0 and right = N / 2 to store the index of the left and right pointers respectively.
- Traverse the array and in each traversal, first print the value of arr[right] and then print the value of arr[left] and increment the value of left and right by 1.
Below is the implementation for the above approach:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to rearrange array elements to // maximize count of local minima in the array void rearrangeArrMaxcntMinima(int arr[], int N) { // Sort the array in // ascending order sort(arr, arr + N); // Stores index of // left pointer int left = 0; // Stores index of // right pointer int right = N / 2; // Traverse the array elements while (left < N / 2 || right < N) { // if right is less than N if (right < N) { // Print array element cout << arr[right] << " "; // Update right right++; } if (left < N / 2) { // Print array element cout << arr[left] << " "; // Update left left++; } } } // Driver Code int main() { int arr[] = { 1, 2, 3, 4, 5 }; int N = sizeof(arr) / sizeof(arr[0]); rearrangeArrMaxcntMinima(arr, N); return 0; } |
3 1 4 2 5
Time Complexity: O(N log(N))
Auxiliary Space:O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find indices of all local maxima and local minima in an Array
- Find a local minima in an array
- Rearrange array to maximize count of triplets (i, j, k) such that arr[i] > arr[j] < arr[k] and i < j < k
- Rearrange the array to maximize the number of primes in prefix sum of the array
- Rearrange Array to maximize number having Array elements as digits based on given conditions
- Rearrange an array to maximize i*arr[i]
- Rearrange given binary strings to maximize their Bitwise XOR value
- Rearrange two given arrays to maximize sum of same indexed elements
- Number of local extrema in an array
- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Rearrange array such that all even-indexed elements in the Array is even
- Rearrange an array to make similar indexed elements different from that of another array
- Rearrange array to make it non-decreasing by swapping pairs having GCD equal to minimum array element
- Rearrange array to make product of prefix sum array non zero
- Rearrange array by interchanging positions of even and odd elements in the given array
- Minimum cost required to rearrange a given array to make it equal to another given array
- Local and Global Optimum in Uni-variate Optimization
- N-Queen Problem | Local Search using Hill climbing with random neighbour
- Maximize count of pairs (i, j) from two arrays having element from first array not exceeding that from second array
- Maximize product of array by replacing array elements with its sum or product with element from another array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

