Skip to content

Commit acc52cc

Browse files
authored
Merge pull request kothariji#563 from karan0805/karan
Create FAIRELCT.cpp
2 parents dc10eb2 + 345f322 commit acc52cc

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

Codechef solutions/FAIRELCT.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
main()
4+
{
5+
int t;
6+
cin >> t;
7+
while (t--)
8+
{
9+
int n, m, i, f = 0;
10+
cin >> n >> m;
11+
int a[n], b[m], sum1 = 0, sum2 = 0;
12+
for (i = 0; i < n; i++)
13+
{
14+
cin >> a[i];
15+
sum1 = sum1 + a[i];
16+
}
17+
for (int i = 0; i < m; i++)
18+
{
19+
cin >> b[i];
20+
sum2 = sum2 + b[i];
21+
}
22+
if (sum1 > sum2)
23+
{
24+
f = 1;
25+
cout << "0";
26+
}
27+
else
28+
{
29+
sort(a, a + n);
30+
sort(b, b + m);
31+
reverse(b, b + m);
32+
for (int i = 0; i < n; i++)
33+
{
34+
sum1 = sum1 - a[i] + b[i];
35+
sum2 = sum2 + a[i] - b[i];
36+
swap(a[i], b[i]);
37+
if (sum1 > sum2)
38+
{
39+
f = 1;
40+
cout << i + 1;
41+
break;
42+
}
43+
}
44+
if (f == 0)
45+
cout << "-1";
46+
}
47+
cout << endl;
48+
}
49+
}

0 commit comments

Comments
 (0)