Given an integer N (N ? 2), the task is to find the maximum median that can be created by forming optimal sum pairs from X[] and Y[] such that these arrays should follow the given conditions:
- Length of array X[] and Y[] should be N.
- All the elements in X[] and Y[] should be distinct.
- No element should be common between both arrays.
- Sub-array having even length and starting from the first index, Then sub-arrays AND must be even.
- The absolute difference between the max element of X[] and the max elements of Y[] should not be greater than 2.
Examples:
Input: N = 2
Output: X[] = {2, 3}, Y[] = {4, 5}, Max median = 7
Explanation: Even values of i between 1 to N = 2 can be: 2 only. The maximum median from 2 arrays X[] = {2, 3}, Y[] = {4, 5}
- AND till index i = 2:
- X[1] & X[2] = 2 & 3 = 2
- Y[1] & Y[2] = 4 & 5 = 4
The median can be maximized by forming pairs such as {X[1] + Y[2], X[2] + Y[1]} = {2 + 5, 3 + 4} = {7, 7}. Median will be average of middle two elements = (7+7)/2 =3
Input: N = 5
Output: 16
Explanation: It can be verified that the maximum median that can be created by using X[] and Y[]’s optimal pairs will be: 16.
Approach: Implement the idea below to solve the problem:
The problem is based on observation based and can be solved by using some simple observations. For the concept of maximizing the median read here.
Steps were taken to solve the problem:
- Initialize an integer let’s say X = 2.
- Run a loop from i = 0 to less than n and print the value of X then increment X at each iteration.
- If (N % 2 == 1) then increment X.
- Run a loop from i = 0 to less than n and print the value of X then increment X at each iteration.
- Then for Maximizing the median by forming optimal pairs sum follow the below-mentioned steps:
- As both X[] and Y[] are already sorted therefore no need to sort them, Then For the first half (before the median positions) the new array must have the smallest possible elements to just discard this half of the elements from both arrays.
- Then from median positions onwards, traverse from the right end of 1st array, and from the median position of 2nd array, moving in opposite directions,
Below is the code to implement the approach:
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std;
void findMedian( int x[], int y[], int n)
{ // If both arrays contains single
// element
if (n == 1) {
cout << x[0] + y[0];
}
else {
// Initialize counters
int i = (n - 1) / 2;
int j = n - 1;
int Med = INT_MAX;
// Find maximum median
if ((n & 1)) {
while (i < n) {
Med = min(Med, (x[i++] + y[j--]));
}
}
else {
vector < int > v;
while (i < n) {
v.push_back(x[i++] + y[j--]);
}
sort(begin(v), end(v));
Med = ((v[0] + v[1]) / 2);
}
// Printing the maximum median
cout << Med << endl;
}
} // Method for printing Arrays void Printer( int n)
{ int X[n], Y[n];
int x = 2;
for ( int i = 0; i < n; i++) {
X[i] = x;
x++;
}
if (n % 2 == 1)
x++;
for ( int i = 0; i < n; i++) {
Y[i] = x;
x++;
}
// Function call for maximized
// median of X[] and Y[]
findMedian(X, Y, n);
} int main() {
// Input value of N
int n = 5;
// function call
Printer(n);
} |
// 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)
throws java.lang.Exception
{
// Input value of N
int n = 5 ;
// function call
Printer(n);
}
// Method for printing Arrays
static void Printer( int n)
{
int [] X = new int [n];
int [] Y = new int [n];
int x = 2 ;
for ( int i = 0 ; i < n; i++) {
X[i] = x;
x++;
}
if (n % 2 == 1 )
x++;
for ( int i = 0 ; i < n; i++) {
Y[i] = x;
x++;
}
// Function call for maximized
// median of X[] and Y[]
findMedian(X, Y, n);
}
static void findMedian( int [] x, int [] y, int n)
{
// If both arrays contains single
// element
if (n == 1 ) {
System.out.print(x[ 0 ] + y[ 0 ]);
}
else {
// Initialize counters
int i = (n - 1 ) / 2 ;
int j = n - 1 ;
int Med = Integer.MAX_VALUE;
// Find maximum median
if ((n & 1 ) != 0 ) {
while (i < n) {
Med = Math.min(Med, (x[i++] + y[j--]));
}
}
else {
ArrayList<Integer> v
= new ArrayList<Integer>(n);
while (i < n) {
v.add(x[i++] + y[j--]);
}
Collections.sort(v);
Med = ((v.get( 0 ) + v.get( 1 )) / 2 );
}
// Printing the maximum median
System.out.print(Med);
}
}
} |
# Python code to implement the approach import math
def find_median(x, y, n):
# If both arrays contains single element
if n = = 1 :
print (x[ 0 ] + y[ 0 ])
else :
# Initialize counters
i = (n - 1 ) / / 2
j = n - 1
med = math.inf
# Find maximum median
if n % 2 ! = 0 :
while i < n:
med = min (med, x[i] + y[j])
i + = 1
j - = 1
else :
v = []
while i < n:
v.append(x[i] + y[j])
i + = 1
j - = 1
v.sort()
med = (v[ 0 ] + v[ 1 ]) / / 2
# Printing the maximum median
print (med)
# Method for printing Arrays def printer(n):
X = [ 0 ] * n
Y = [ 0 ] * n
x = 2
for i in range (n):
X[i] = x
x + = 1
if (n % 2 = = 1 ):
x + = 1
for i in range (n):
Y[i] = x
x + = 1
# Function call for maximized median of X[] and Y[]
find_median(X, Y, n)
if __name__ = = '__main__' :
n = 5
printer(n)
# This code is contributed by sankar |
// C# code to implement the approach using System;
using System.Collections.Generic;
public class GFG {
static public void Main()
{
// Code
// Input value of N
int n = 5;
// function call
Printer(n);
}
// Method for printing Arrays
static void Printer( int n)
{
int [] X = new int [n];
int [] Y = new int [n];
int x = 2;
for ( int i = 0; i < n; i++) {
X[i] = x;
x++;
}
if (n % 2 == 1)
x++;
for ( int i = 0; i < n; i++) {
Y[i] = x;
x++;
}
// Function call for maximized median of X[] and Y[]
findMedian(X, Y, n);
}
static void findMedian( int [] x, int [] y, int n)
{
// If both arrays contains single element
if (n == 1) {
Console.Write(x[0] + y[0]);
}
else {
// Initialize counters
int i = (n - 1) / 2;
int j = n - 1;
int Med = int .MaxValue;
// Find maximum median
if ((n & 1) != 0) {
while (i < n) {
Med = Math.Min(Med, (x[i++] + y[j--]));
}
}
else {
List< int > v = new List< int >(n);
while (i < n) {
v.Add(x[i++] + y[j--]);
}
v.Sort();
Med = ((v[0] + v[1]) / 2);
}
// Printing the maximum median
Console.Write(Med);
}
}
} // This code is contributed by karthik |
// JS code to implement the approach function findMedian( x, y, n)
{ // If both arrays contains single
// element
if (n == 1) {
console.log(x[0] + y[0]);
}
else {
// Initialize counters
let i = Math.floor((n - 1) / 2);
let j = n - 1;
let Med = Number.MAX_SAFE_INTEGER;
// Find maximum median
if ((n & 1)) {
while (i < n) {
Med = Math.min(Med, (x[i++] + y[j--]));
}
}
else {
let v=[];
while (i < n) {
v.push(x[i++] + y[j--]);
}
v.sort();
Med = (Math.floor((v[0] + v[1]) / 2));
}
// Printing the maximum median
console.log(Med);
}
} // Method for printing Arrays function Printer( n)
{ let X= new Array(n), Y= new Array(n);
let x = 2;
for (let i = 0; i < n; i++) {
X[i] = x;
x++;
}
if (n % 2 == 1)
x++;
for (let i = 0; i < n; i++) {
Y[i] = x;
x++;
}
// Function call for maximized
// median of X[] and Y[]
findMedian(X, Y, n);
} // Input value of N let n = 5; // function call Printer(n); |
16
Time Complexity: O(N)
Auxiliary Space: O(N)