Rearrange the Array to maximize the elements which is smaller than both its adjacent elements
Last Updated :
15 Jul, 2021
Given an array arr[] consisting of N distinct integers, the task is to rearrange the array elements such that the count of elements that are smaller than their adjacent elements is maximum.
Note: The elements left of index 0 and right of index N-1 are considered as -INF.
Examples:
Input: arr[] = {1, 2, 3, 4}
Output: 2 1 3 4
Explanation:
One possible way to rearrange is as {2, 1, 3, 4}.
- For arr[0](= 2), is greater than the elements on both sides of it.
- For arr[1](= 1), is less than the elements on both sides of it.
- For arr[2](= 3), is less than the elements on its right and greater than the elements on its left.
- For arr[4](= 1), is greater than the elements on both sides of it.
Therefore, there is a total of 1 array element satisfying the condition in the above arrangement. And it is the maximum possible.
Input: arr[] = {2, 7}
Output: 2 7
Approach: The given problem can be solved based on the following observations:
- Consider the maximum number of indices that can satisfy the conditions being X.
- Then, at least (X + 1) larger elements are required to make it possible as each element requires 2 greater elements.
- Therefore, the maximum number of elements that is smaller than their adjacent is given by (N – 1)/2.
Follow the steps below to solve the problem:
- Initialize an array, say temp[] of size N, that stores the rearranged array.
- Sort the given array arr[] in the increasing order.
- Choose the first (N – 1)/2 elements from the array arr[] and place them at the odd indices in the array temp[].
- Choose the rest of the (N + 1)/2 elements from the array arr[] and place them in the remaining indices in the array temp[].
- Finally, after completing the above steps, print the array temp[] as the resultant array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maximumIndices( int arr[], int N)
{
int temp[N] = { 0 };
int maxIndices = (N - 1) / 2;
sort(arr, arr + N);
for ( int i = 0; i < maxIndices; i++) {
temp[2 * i + 1] = arr[i];
}
int j = 0;
for ( int i = maxIndices; i < N;) {
if (temp[j] == 0) {
temp[j] = arr[i];
i++;
}
j++;
}
for ( int i = 0; i < N; i++) {
cout << temp[i] << " " ;
}
}
int main()
{
int arr[] = { 1, 2, 3, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
maximumIndices(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static void maximumIndices( int arr[], int N)
{
int [] temp = new int [N];
int maxIndices = (N - 1 ) / 2 ;
Arrays.sort(arr);
for ( int i = 0 ; i < maxIndices; i++) {
temp[ 2 * i + 1 ] = arr[i];
}
int j = 0 ;
for ( int i = maxIndices; i < N;) {
if (temp[j] == 0 ) {
temp[j] = arr[i];
i++;
}
j++;
}
for ( int i = 0 ; i < N; i++) {
System.out.print(temp[i] + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 };
int N = 4 ;
maximumIndices(arr, N);
}
}
|
Python3
def maximumIndices(arr, N):
temp = [ 0 ] * N
maxIndices = (N - 1 ) / / 2
arr.sort()
for i in range (maxIndices):
temp[ 2 * i + 1 ] = arr[i]
j = 0
i = maxIndices
while (i < N):
if (temp[j] = = 0 ):
temp[j] = arr[i]
i + = 1
j + = 1
for i in range (N):
print (temp[i], end = " " )
arr = [ 1 , 2 , 3 , 4 ]
N = len (arr)
maximumIndices(arr, N)
|
C#
using System;
class GFG{
public static void maximumIndices( int []arr, int N)
{
int [] temp = new int [N];
int maxIndices = (N - 1) / 2;
Array.Sort(arr);
for ( int i = 0; i < maxIndices; i++)
{
temp[2 * i + 1] = arr[i];
}
int j = 0;
for ( int i = maxIndices; i < N;)
{
if (temp[j] == 0)
{
temp[j] = arr[i];
i++;
}
j++;
}
for ( int i = 0; i < N; i++)
{
Console.Write(temp[i] + " " );
}
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4 };
int N = 4;
maximumIndices(arr, N);
}
}
|
Javascript
<script>
function maximumIndices(arr, N) {
let temp = new Array(N).fill(0);
let maxIndices = parseInt((N - 1) / 2);
arr.sort( function (a, b) { return a - b; })
for (let i = 0; i < maxIndices; i++) {
temp[2 * i + 1] = arr[i];
}
let j = 0;
for (let i = maxIndices; i < N;) {
if (temp[j] == 0) {
temp[j] = arr[i];
i++;
}
j++;
}
for (let i = 0; i < N; i++) {
document.write(temp[i] + " " );
}
}
let arr = [1, 2, 3, 4];
let N = arr.length;
maximumIndices(arr, N);
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...