Construct two arrays satisfying the given conditions
Last Updated :
15 Mar, 2023
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++
#include <bits/stdc++.h>
using namespace std;
void findMedian( int x[], int y[], int n)
{
if (n == 1) {
cout << x[0] + y[0];
}
else {
int i = (n - 1) / 2;
int j = n - 1;
int Med = INT_MAX;
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);
}
cout << Med << endl;
}
}
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++;
}
findMedian(X, Y, n);
}
int main() {
int n = 5;
Printer(n);
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
public static void main(String[] args)
throws java.lang.Exception
{
int n = 5 ;
Printer(n);
}
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++;
}
findMedian(X, Y, n);
}
static void findMedian( int [] x, int [] y, int n)
{
if (n == 1 ) {
System.out.print(x[ 0 ] + y[ 0 ]);
}
else {
int i = (n - 1 ) / 2 ;
int j = n - 1 ;
int Med = Integer.MAX_VALUE;
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 );
}
System.out.print(Med);
}
}
}
|
Python3
import math
def find_median(x, y, n):
if n = = 1 :
print (x[ 0 ] + y[ 0 ])
else :
i = (n - 1 ) / / 2
j = n - 1
med = math.inf
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
print (med)
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
find_median(X, Y, n)
if __name__ = = '__main__' :
n = 5
printer(n)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static public void Main()
{
int n = 5;
Printer(n);
}
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++;
}
findMedian(X, Y, n);
}
static void findMedian( int [] x, int [] y, int n)
{
if (n == 1) {
Console.Write(x[0] + y[0]);
}
else {
int i = (n - 1) / 2;
int j = n - 1;
int Med = int .MaxValue;
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);
}
Console.Write(Med);
}
}
}
|
Javascript
function findMedian( x, y, n)
{
if (n == 1) {
console.log(x[0] + y[0]);
}
else {
let i = Math.floor((n - 1) / 2);
let j = n - 1;
let Med = Number.MAX_SAFE_INTEGER;
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));
}
console.log(Med);
}
}
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++;
}
findMedian(X, Y, n);
}
let n = 5;
Printer(n);
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...