Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Maximum Median Matching CodeChef Solution

Problem -Maximum Median Matching CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.

Maximum Median Matching CodeChef Solution in C++17

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

void solve(){
    int n;
    cin>>n;
    
    vector<int> a(n);
    vector<int> b(n);
    
    for(int i = 0 ; i < n ; i++) cin>>a[i];
    for(int i = 0 ; i < n ; i++) cin>>b[i];
    
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    
    ll ans = INT_MAX;
    
    int x = (n - 1)/2;
    for(int i = x ; i < n ; i++){
        ll tmp = a[i] + b[n + x - 1 - i];
        ans = min(ans, tmp);
    }
    
    cout<<ans<<endl;
    
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);    
    int t;
    cin >> t;
    while(t--){
        solve();
    }
}

Maximum Median Matching CodeChef Solution in C++14

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int i,j,k,l,m,n,p,q,r,s,t,u,v,x,y,z;
    cin>>t;
    while(t--)
    {
      cin>>n;
      long long int a[n],b[n];
      for(i=0;i<n;i++)
      {
          cin>>a[i];
      }
      for(i=0;i<n;i++)
      {
          cin>>b[i];
      }
      sort(a,a+n);
      sort(b,b+n);
      p=n/2;
      q=n-1;
      long long int c[n];
      r=0;
      x=100000000000;
      for(i=p;i<n;i++)
      {
        c[r]=a[i]+b[q];
        if(c[r]<x)
        x=c[r];
        r++;
        q--;
      }
      cout<<x<<endl;
    }
}

Maximum Median Matching CodeChef Solution in PYTH 3


# cook your dish here

t=int(input())
for _ in range(t):
    
    n=int(input())
    
    boys=list(map(int,input().split()))
    girls=list(map(int,input().split()))
    
    boys.sort()
    girls.sort()
    
    b=[]
    g=[]
    ans=[]
    for i in range(n//2,n):
        b.append(boys[i])
        g.append(girls[i])
        
    g.reverse()
    for i in range(len(g)):
        ans.append(b[i]+g[i])

    print(min(ans))
  
    

Maximum Median Matching CodeChef Solution in C

#include<stdio.h>
void swap(int* x,int* y)
     {
      int temp=*x;
      *x=*y;
      *y=temp;
      return;
     }
void heapify(int* arr,int i,int n)
    { 
      int l_i=(2*i+1),r_i=l_i+1,max_i;
      if(l_i>=n)return;
      if(r_i<n && arr[r_i]>arr[l_i])max_i=r_i;
      else max_i=l_i;
      if(arr[max_i]<arr[i])return;
      swap(&arr[max_i],&arr[i]);
      heapify(arr,max_i,n);
    }
void n_max(int *arr,int siz,int n)
   {
      for(int i=(siz-2)/2;i>=0;i--)
      heapify(arr,i,siz);
      int j=0,i=siz-1;
      while(j<n)
      {
       swap(&arr[0],&arr[i]);
       heapify(arr,0,i);
       j++;
       i--;
      }
   }
int main()
{
  int t;
  scanf("%d",&t);
  for(int itr=0;itr<t;itr++)
  {
      
   int siz;
   scanf("%d",&siz);
   
   int a[siz],b[siz];
   for(int i=0;i<siz;i++)scanf("%d",&a[i]);
   for(int i=0;i<siz;i++)scanf("%d",&b[i]);
   
   int n=(siz/2)+1;
   n_max(a,siz,n);
   n_max(b,siz,n);
   
   int i=siz/2,j=siz-1,min=a[i]+b[j];
   while(i<siz)
   {
    if(a[i]+b[j]<min)min=a[i]+b[j];
    i++;j--; 
   }

   printf("%d\n",min);
  }
}

Maximum Median Matching CodeChef Solution in JAVA

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		 Scanner sc=new Scanner(System.in);
	    int t=sc.nextInt();
	      while(t-->0){
		int n=sc.nextInt();
	        int a[]=new int[n];
	        int b[]=new int[n];
	        for(int i=0;i<n;i++)
	        {
	            a[i]=sc.nextInt();
	        }
	        for(int i=0;i<n;i++)
	        {
	            b[i]=sc.nextInt();
	        }
	         if(n==1)
	         {
	             System.out.println(a[0]+b[0]);
	             continue;
	         }
	        Arrays.sort(a);
	        Arrays.sort(b);
	        ArrayList<Long>ans=new ArrayList<Long>();
	        int start=(n/2);
	          for(int i=start,j=n-1;i<n;i++,j--)
	          {
	              ans.add((long)a[i]+b[j]);
	          }
	          Collections.sort(ans);
	          System.out.println(ans.get(0));
	      }
	}
}

Maximum Median Matching CodeChef Solution in PYPY 3

for i in range(int(input())):
    n=int(input())
    a,b=list(map(int,input().split())),list(map(int,input().split()))
    a.sort(),b.sort()
    i=(n-1)//2
    e=n-1
    med=1e18;
    while i<n :
        med=min(med,a[i]+b[e])
        i +=1 
        e -=1 
    print(med)
        

Maximum Median Matching CodeChef Solution in C#

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;
using System.Numerics;

public class Test
{
    private static readonly Random r = new Random();
    public static void Main()
    {
        var t = GetInt();
        for (int i = 0; i < t; i++)
            Solve();
    }

    public static bool IsVowel(char c) => c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';

    public static void Solve()
    {
        int n = GetInt();
        var a = GetIntArray();
        var b = GetIntArray();
        a = a.OrderBy(_ => _).Skip((n - 1)/2).ToArray();
        b = b.OrderBy(_ => _).Skip((n - 1)/2).Reverse().ToArray();
        //Console.WriteLine("a is " + String.Join(" ",a));
        //Console.WriteLine("b is " + String.Join(" ",b));
        int min = int.MaxValue;
        for(int i=0; i<a.Length; i++)
        {
            min = Math.Min(min, a[i] + b[i]);
        }
        long ans = min;
#if DEBUG
        Console.Write("Answer is --------------> ");
#endif
        Console.WriteLine(ans);
    }

    public static int GetInt() => int.Parse(Console.ReadLine());
    public static long GetLong() => long.Parse(Console.ReadLine());

    public static int[] GetIntArray() => Console.ReadLine().Trim().Split(' ').Select(int.Parse).ToArray();
    public static long[] GetLongArray() => Console.ReadLine().Trim().Split(' ').Select(long.Parse).ToArray();



    public static int Gcd(int a, int b) => b == 0 ? a : Gcd(b, a % b);
    public static int Gcd(int[] n) => n.Aggregate((a, b) => Gcd(a, b));

    public static bool IsBitSet(long a, int bit)
    {
        return (a & (1L << bit)) != 0;
    }
}

Maximum Median Matching CodeChef Solution in NODEJS

process.stdin.resume();
process.stdin.setEncoding("utf-8");
var arr = "";
process.stdin.on("data", function (chunk) {
  arr += chunk;
});

process.stdin.on("end", function () {
  arr = arr.split("\n");
  const testcases = parseInt(arr.shift());
  for (let t = 0; t < 3 * testcases; ) {
    const n = parseInt(arr[t++]);
    const numsA = arr[t++].split(" ").map((el) => parseInt(el));
    let numsB = arr[t++].split(" ").map((el) => parseInt(el));

    numsA.sort((a, b) => a - b);
    numsB.sort((a, b) => a - b);

    let dummy = [];
    for (let i = Math.floor(n / 2); i < n; i++) {
      dummy.push(numsA[i]);
    }

    dummy.reverse();
    let j = 0;
    for (let i = Math.floor(n / 2); i < n; i++) {
      numsA[i] = dummy[j];
      j++;
    }
    
    // console.log(numsA,numsB,dummy);

    let ans = Array(n).fill(0);

    for (let i = 0 ; i < n;i++) {
      ans[i] = numsA[i] + numsB[i];
    }

    ans.sort((a, b) => a - b);
    // console.log(ans);
    console.log(ans[Math.floor(n / 2)]);
  }
});

Maximum Median Matching CodeChef Solution in GO

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
	"sort"
)

func main() {
	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)
	var buf bytes.Buffer
	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		B := readNNums(reader, n)
		res := solve(A, B)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}
	fmt.Print(buf.String())
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readString(reader *bufio.Reader) string {
	s, _ := reader.ReadString('\n')
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' || s[i] == '\r' {
			return s[:i]
		}
	}
	return s
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func solve(A []int, B []int) int {
	sort.Ints(A)
	sort.Ints(B)

	n := len(A)

	h := n / 2

	best := A[n-1] + B[h]

	for i, j := h, n-1; i < n; i, j = i+1, j-1 {
		best = min(best, A[i]+B[j])
	}

	return best
}

func min(a, b int) int {
	if a <= b {
		return a
	}
	return b
}
Maximum Median Matching CodeChef Solution Review:

In our experience, we suggest you solve this Maximum Median Matching CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Maximum Median Matching CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Maximum Median Matching CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

Your email address will not be published. Required fields are marked *