Maximize the Median of Array formed from adjacent maximum of a rearrangement
Last Updated :
24 Mar, 2023
You are given X[] of length 2*N, Where N will always be odd, the task is to construct an array Y[] of length N from a rearrangement of X[] such that Y[]’s element is equal to max(Xi, Xi+1) and i is always an even index (0 based indexing), and the median of the new array is maximum possible.
Examples:
Input: N = 1, X[] = {1, 2}
Output: X[] = {2, 1}, Median = 2
Explanation: Y[] = { max(X[1], X[2]) }, Y[] = {2}.
Median of Y[] = 2, 2 is the maximum possible value of median that can achieve by arrange elements of X[].
Input: N = 3, X[] = {1, 2, 3, 4, 5, 6}
Output: X[] = {3, 1, 2, 5, 6, 4}, Median = 5
Explanation: Y[] = { max(3, 1), max(2, 5), max(6, 4) }, Y[] = {3, 5, 6}.
Median of Y[] = 5, 5 is the maximum possible value of median that can achieve by arrange elements of X[].
Approach: Implement the idea below to solve the problem:
Sort X[], put its left half part on even indices in arrangement and right half elements at odd indices of arrangement. For value of maximum value of median can be get by printing middle element of right half X[] after performing sorting.
Illustration of approach:
Consider N = 3, X[] = {1, 2, 3, 4, 5, 6}
After sorting X[] will be: {1, 2, 3, 4, 5, 6}. Let the new Arrangement: {A1, A2, A3, A4, A5, A6}
- Put left half X[] = {1, 2, 3} on odd indices of new Arrangement, Then Arrangement: {1, A2, 2, A4, 3, A6}
- Put right half X[] = {4, 5, 6} on even indices of new Arrangement. Then, Arrangement: {1, 4, 2, 5, 3, 6}
Now, It can be verified that Y[] will be = {4, 5, 6}, 5 is maximum possible median and value 5 is at middle of right half of X[] = {4, 5, 6}
Follow the below steps to implement the idea:
- Sort X[].
- Make a new Array of same length.
- Put left half of X[] on odd indices of new Arrangement.
- Put right half of X[] on even indices of new Arrangement.
- Print the arrangement of new Array.
- Return the middle element of the right half of X[] for the maximum possible value.
Below is the implementation of the approach.
C++
#include <bits/stdc++.h>
using namespace std;
static int maxValue( int X[], int n)
{
sort(X, X + 2 * n);
int start = (2 * n) / 2;
for ( int i = 1; i <= n; i++) {
cout << X[i - 1] << " " << X[start] << " " ;
start++;
}
cout << "\n" ;
return (X[(start + ((2 * n) - 1)) / 2]);
}
int main()
{
int N = 5;
int X[] = { 3, 5, 7, 1, 3, 5, 9, 0, 1, 3 };
cout << maxValue(X, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
int N = 5 ;
int [] X = { 3 , 5 , 7 , 1 , 3 , 5 , 9 , 0 , 1 , 3 };
System.out.println(maxValue(X, N));
}
static int maxValue( int X[], int n)
{
Arrays.sort(X);
int start = ( 2 * n) / 2 ;
for ( int i = 1 ; i <= n; i++) {
System.out.print(X[i - 1 ] + " " + X[start]
+ " " );
start++;
}
System.out.println();
return (X[(start + (( 2 * n) - 1 )) / 2 ]);
}
}
|
Python3
def maxValue(X, n) :
X.sort()
start = ( 2 * n) / / 2 ;
for i in range ( 1 , n + 1 ) :
print (X[i - 1 ], " " , X[start], " " , end = "");
start + = 1 ;
print ()
return (X[(start + (( 2 * n) - 1 )) / / 2 ]);
if __name__ = = "__main__" :
N = 5 ;
X = [ 3 , 5 , 7 , 1 , 3 , 5 , 9 , 0 , 1 , 3 ];
print (maxValue(X, N));
|
C#
using System;
using System.Collections;
public class GFG {
static public void Main()
{
int N = 5;
int [] X = { 3, 5, 7, 1, 3, 5, 9, 0, 1, 3 };
Console.WriteLine(maxValue(X, N));
}
static int maxValue( int [] X, int n)
{
Array.Sort(X);
int start = (2 * n) / 2;
for ( int i = 1; i <= n; i++) {
Console.Write(X[i - 1] + " " + X[start] + " " );
start++;
}
Console.WriteLine();
return (X[(start + ((2 * n) - 1)) / 2]);
}
}
|
Javascript
function maxValue(X, n)
{
X.sort()
let start = (2 * n) / 2;
let string = "" ;
for (let i = 1; i <= n; i++) {
string += X[i - 1] + " " + X[start] + " " ;
start++;
}
console.log(string);
let y = Math.floor((start + ((2 * n) - 1)) / 2);
return (X[y]);
}
let N = 5;
let X = [ 3, 5, 7, 1, 3, 5, 9, 0, 1, 3 ];
console.log(maxValue(X, N));
|
Output
0 3 1 5 1 5 3 7 3 9
9
Time Complexity: O(N * log(N))
Auxiliary Space: O(1)
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...