Hello codechef community , this is CodeNCode
Here are the video editorials.
Beside these video I have course teaching many concepts free of any cost.
Some of them are
And many more.
Thanks for your valuable time.
CodeNCode out.
9 Likes
tum bahut mast kaam karta hai @waqar_ahmad224 bhai, from your lectures i learn Basic graph theory bas ab contest me questions hone lag jaaye I will surely contribute
1 Like
do one for Doctor chef As well…this has bugged me a lot
haha thanks man.
and don’t worry about contribution just focus on your learning.
2 Likes
a bit busy but will work on that for sure.
1 Like
must be a difficult problem then
/* package codechef; // don't place package name! */
//package p1;
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 boolean EvenFrequency(List<Long> a ,List<Long> b ){
Map <Long,Integer> h = new HashMap<Long,Integer>();
for(int i=0;i<a.size();i++){
if(h.containsKey(a.get(i)))
h.put(a.get(i),h.get(a.get(i))+1);
else
h.put(a.get(i),1);
if(h.containsKey(b.get(i)))
h.put(b.get(i),h.get(b.get(i))+1);
else
h.put(b.get(i),1);
}
for(int i=0;i<a.size();i++){
if(h.get(a.get(i))%2!=0)
return false;
if(h.get(b.get(i))%2!=0)
return false;
}
return true;
}
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
int n;
long value,min,subMin;
while(t-->0){
n = input.nextInt();
long a [] = new long[n];
long b [] = new long[n];
for(int i=0;i<n;i++) a[i] = input.nextLong();
for(int i=0;i<n;i++) b[i] = input.nextLong();
Arrays.sort(a);
Arrays.sort(b);
for(int j=0,i=0;j<a.length&&i<a.length;){
if(a[i]==b[j]){
a[i++]=0;
b[j++]=0;
}
else if(a[i]>b[j]) j++;
else i++;
}
List <Long> la = new ArrayList<>();
List <Long> lb = new ArrayList<>();
for(int i=0;i<n;i++) {if(a[i]!=0) la.add(a[i]); if(b[i]!=0) lb.add(b[i]);}
if(la.size()==0&&lb.size()==0) System.out.println("0");
else if(!Codechef.EvenFrequency(la,lb))
System.out.println("-1");
else{
value=0;
subMin = la.get(0)<=lb.get(0)?la.get(0):lb.get(0);
List<Long> lla = new ArrayList<>();
List<Long> llb = new ArrayList<>();
for(int i=0;i<la.size();i+=2){
lla.add(la.get(i));
llb.add(lb.get(i));
}
for(int i=0;i<lla.size();i++) {
min = lla.get(i)<=llb.get(llb.size()-i-1)?lla.get(i):llb.get(llb.size()-i-1);
if(min<2*subMin)
value+=min;
else
value+=(2*subMin);
}
System.out.println(value);
}
}
input.close();
}
}