Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth The score game problem solution

YASH PAL, 31 July 202415 February 2026
In this HackerEarth The score game problem solution You are given an array A of N elements where each element is a pair represented by (X[i], Y[i]). 
 
Bob and Alice play a game where both of them have to pick some elements from the array A. Suppose, Bob picked the elements at index i1,i2,…,ik where K <= N.
 
Score(Bob) = Sigma(X[j] + Y[j]), where j belongs to (i1,i2,….,ik).
Score(Alice) = Sigma(X[j]), where j belongs to (1,2,….,N) excluding (i1,i2,….,ik).
Find the minimum number of elements Bob must pick such that the score of Bob is greater than Alice.
 
 
HackerEarth The score game problem solution

 

 

HackerEarth The score game problem solution.

#include<bits/stdc++.h>
#define int long long int
using namespace std;

bool cmp(pair<int,int> a, pair<int,int> b)
{
int gaina = 2*a.first + a.second;
int gainb = 2*b.first + b.second;

if(gaina != gainb) return gaina < gainb;
return a.first < b.first;
}

void solve(){
int n;
cin >> n;

int x[n+1], y[n+1];

for(int i = 1 ; i <= n ; i++) cin >> x[i];

for(int i = 1 ; i <= n ; i++) cin >> y[i];

int bob = 0;
int alice = 0;

vector<pair<int,int> >m;

for(int i = 1 ; i <= n ; i++){
m.push_back(make_pair(x[i], y[i]));
alice += x[i];
}

sort(m.begin(), m.end(), cmp);
int answer = 0;
for(int i = n - 1 ; i >= 0 ; i--)
{
if(bob > alice) break;
answer++;
bob += (m[i].first + m[i].second);
alice -= (m[i].first);
}

cout << answer << endl;
}

signed main(){
int t;
cin >> t;
while(t--){
solve();
}
}
 

Second solution

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int MAX_N = 1e5;
int x[MAX_N], y[MAX_N];

int main() {
ios::sync_with_stdio(0), cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
ll alice = 0;
for (int i = 0; i < n; ++i) {
cin >> x[i];
alice += x[i];
}
for (int i = 0; i < n; ++i)
cin >> y[i];
int per[n];
iota(per, per + n, 0);
sort(per, per + n, [](int i, int j) { return 2 * x[i] + y[i] > 2 * x[j] + y[j]; });
ll me = 0;
int ans = 0;
while (me <= alice) {
me += x[per[ans]] + y[per[ans]];
alice -= x[per[ans++]];
}
cout << ans << 'n';
}
}
 
 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

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

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes