-
Notifications
You must be signed in to change notification settings - Fork 0
/
FairElection.java
64 lines (61 loc) · 951 Bytes
/
FairElection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package codeChef;
import java.util.*;
public class FairElection {
public static void main(String[] args) {
try{
Scanner sc= new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++)
{
int n=sc.nextInt();
int m=sc.nextInt();
int a[]=new int[n];
int b[]= new int[m];
int sum1=0;
int sum2=0;
int count=0;
for(int j=0;j<n;j++)
{
a[j]=sc.nextInt();
sum1+=a[j];
}
for(int k=0;k<m;k++)
{
b[k]=sc.nextInt();
sum2+=b[k];
}
if(sum1>sum2)
{
System.out.println(0);
continue;
}
Arrays.sort(a);
Arrays.sort(b);
int y=m-1;
for(int x=0;x<n;x++)
{
sum1=sum1-a[x];
sum2=sum2-b[y];
sum1=sum1+b[y];
sum2=sum2+a[x];
count++;
y--;
if(sum1>sum2 || y<0)
break;
}
if(sum1>sum2)
{
System.out.println(count);
}
else
{
System.out.println(-1);
}
}
sc.close();
}
catch(Exception e){
return;
}
}
}