Problem Name : Fair Elections Practice Coding Problem
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
// your code goes here
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
priority_queue < int > john; // max_heap
priority_queue < int, vector < int > , greater < int >> jack; // min_heap
int john_vote = 0;
int jack_vote = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
john.push(x);
john_vote += x;
}
for (int i = 0; i < m; i++) {
int x;
cin >> x;
jack.push(x);
jack_vote += x;
}
int ans = 0;
while (john_vote <= jack_vote) {
int john_ele = john.top();
int jack_ele = jack.top();
if (john_ele >= jack_ele) {
break;
}
// For swapping
john_vote = john_vote - john_ele + jack_ele;
jack_vote = jack_vote - jack_ele + john_ele;
john.pop();
john.push(jack_ele);
jack.pop();
jack.push(john_ele);
// for counting how much swaping need to win john
ans++;
}
if (john_vote > jack_vote) {
cout << ans << endl;
} else {
cout << -1 << endl;
}
}
return 0;
}