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++
#include <bits/stdc++.h>
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;
}
|
Java
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);
}
}
|
Python3
def rearrangeArrMaxcntMinima(arr, N):
arr.sort()
left = 0
right = N / / 2
while (left < N / / 2 or
right < N):
if (right < N):
print (arr[right],
end = " " )
right + = 1
if (left < N / / 2 ):
print (arr[left],
end = " " )
left + = 1
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 , 5 ]
N = len (arr)
rearrangeArrMaxcntMinima(arr, N)
|
C#
using System;
class GFG{
static void rearrangeArrMaxcntMinima( int []arr,
int N)
{
Array.Sort(arr);
int left = 0;
int right = N / 2;
while (left < N / 2 || right < N)
{
if (right < N)
{
Console.Write(arr[right] + " " );
right++;
}
if (left < N / 2)
{
Console.Write(arr[left] + " " );
left++;
}
}
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5 };
int N = arr.Length;
rearrangeArrMaxcntMinima(arr, N);
}
}
|
Javascript
<script>
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);
</script>
|
Time Complexity: O(N log(N))
Auxiliary Space:O(1)