Open In App

Maximize the Median of Array formed from adjacent maximum of a rearrangement

Last Updated : 24 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the maximum median
static int maxValue(int X[], int n)
{
    // Sorting X[] by in-built sort
    // function of Arrays class
    sort(X, X + 2 * n);
  
    // Start point of right half X[]
    int start = (2 * n) / 2;
  
    // Loop for printing arrangement
    for (int i = 1; i <= n; i++) {
        cout << X[i - 1] << " " << X[start] << " ";
        start++;
    }
  
    cout << "\n";
  
    // Returning maximum possible
    // median value
    return (X[(start + ((2 * n) - 1)) / 2]);
}
  
// Driver Code
int main()
{
    int N = 5;
    int X[] = { 3, 5, 7, 1, 3, 5, 9, 0, 1, 3 };
  
    // Function call
    cout << maxValue(X, N);
    return 0;
}
  
// This code is contributed by Rohit Pradhan


Java




// Java code to implement the approach
  
import java.io.*;
import java.lang.*;
import java.util.*;
  
class GFG {
  
    // Driver function
    public static void main(String[] args)
    {
        int N = 5;
        int[] X = { 3, 5, 7, 1, 3, 5, 9, 0, 1, 3 };
  
        // Function call
        System.out.println(maxValue(X, N));
    }
  
    // Function to find the maximum median
    static int maxValue(int X[], int n)
    {
        // Sorting X[] by in-built sort
        // function of Arrays class
        Arrays.sort(X);
  
        // Start point of right half X[]
        int start = (2 * n) / 2;
  
        // Loop for printing arrangement
        for (int i = 1; i <= n; i++) {
            System.out.print(X[i - 1] + " " + X[start]
                             + " ");
            start++;
        }
  
        System.out.println();
  
        // Returning maximum possible
        // median value
        return (X[(start + ((2 * n) - 1)) / 2]);
    }
}


Python3




# Python3 code to implement the approach
  
# Function to find the maximum median
def maxValue(X, n) :
  
    # Sorting X[] by in-built sort
    # function of Arrays class
    X.sort()
  
    # Start point of right half X[]
    start = (2 * n) // 2;
  
    # Loop for printing arrangement
    for i in range(1, n + 1) :
        print(X[i - 1], " ", X[start], " ", end ="");
        start += 1;
  
    print()
  
    # Returning maximum possible
    # median value
    return (X[(start + ((2 * n) - 1)) // 2]);
  
# Driver Code
if __name__ ==  "__main__" :
  
    N = 5;
    X = [ 3, 5, 7, 1, 3, 5, 9, 0, 1, 3 ];
  
    # Function call
    print(maxValue(X, N));
  
    # This code is contributed by AnkThon


C#




// C# code for the above approach
  
using System;
using System.Collections;
  
public class GFG {
  
    static public void Main()
    {
  
        // Code
        int N = 5;
        int[] X = { 3, 5, 7, 1, 3, 5, 9, 0, 1, 3 };
  
        // Function call
        Console.WriteLine(maxValue(X, N));
    }
  
    // Function to find the maximum median
    static int maxValue(int[] X, int n)
    {
        // Sorting X[] by in-built sort
        // function of Arrays class
        Array.Sort(X);
  
        // Start point of right half X[]
        int start = (2 * n) / 2;
  
        // Loop for printing arrangement
        for (int i = 1; i <= n; i++) {
            Console.Write(X[i - 1] + " " + X[start] + " ");
            start++;
        }
  
        Console.WriteLine();
  
        // Returning maximum possible
        // median value
        return (X[(start + ((2 * n) - 1)) / 2]);
    }
}
  
// This code is contributed by lokeshmvs21.


Javascript




// JavaScript code to implement the approach
  
// Function to find the maximum median
function maxValue(X,  n)
{
    // Sorting X[] by in-built sort
    // function of Arrays class
    X.sort()
  
    // Start point of right half X[]
    let start = (2 * n) / 2;
    let string = "";
    // Loop for printing arrangement
    for (let i = 1; i <= n; i++) {
        string +=  X[i - 1] + " " + X[start] + " ";
        start++;
    }
    console.log(string);
  
    // Returning maximum possible
    // median value
    let y = Math.floor((start + ((2 * n) - 1)) / 2);
    return (X[y]);
}
  
// Driver Code
  
let N = 5;
let X = [ 3, 5, 7, 1, 3, 5, 9, 0, 1, 3 ];
  
// Function call
console.log(maxValue(X, N));
  
// This code is contributed by AnkThon


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

Similar Reads