Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Fun Game problem solution

YASH PAL, 31 July 202430 November 2025

In this HackerRank Fun Game problem solution we have given the value of two N-sized integer arrays, A and B., and is played by two players P1 and P2. can you determine the outcome of the game? Print First if P1 will win, Second if P2 will win, or Tie if they will tie. Assume both players always move optimally.

HackerRank Fun Game problem solution

Problem solution in Python.

import sys

# Process input
data = sys.stdin.readlines()
T = int(data[0])


def run_game(N, a, b):
    c = [a[i] + b[i] for i in range(N)]  # a-b
    cs = [i for i, x in sorted(
        enumerate(c), key=(lambda y : y[1]))] # keys that sort c

    # Play game
    p1, p2 = (0, 0)  # payoffs to the two players
    for i in range(N):
        if (i % 2)==0:  # P1's turn
            p1 += a[cs.pop(-1)]
        else:  # P2's turn
            p2 += b[cs.pop(-1)]

    # Print results
    if p1 > p2:
        print("First")
    elif p1==p2:
        print("Tie")
    else:
        print("Second")
        

for k in range(T):
    N = int(data[3*k + 1])
    a = list(map(int, data[3*k + 2].split()))
    b = list(map(int, data[3*k + 3].split()))
    run_game(N, a, b)

Problem solution in Java.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int T = input.nextInt();
        for(int t=0; t<T; t++){
            int n = input.nextInt();
            int[] a = new int[n];
            int[] b = new int[n];
            int aScore = 0, bScore = 0;
            TreeSet<Pair> c = new TreeSet<>();
            
            for(int i=0; i<n; i++){
                a[i] = input.nextInt();
            }
            
            for(int i=0; i<n; i++){
                b[i] = input.nextInt();
                c.add(new Pair(a[i]+b[i], i));
            }
            
            int turn = 0;
            for(Pair p:c){
                if(turn == 0)
                    aScore += a[p.index];
                else
                    bScore += b[p.index];
                turn = 1 - turn;
            }
            if(aScore > bScore){
                System.out.println("First");
            }else if(aScore == bScore){
                System.out.println("Tie");
            }else{
                System.out.println("Second");
            }
            
        }
    }
    
    
    public static class Pair implements Comparable<Pair>{
        int sum;
        int index;
        
        Pair(int s, int i){
            this.sum = s;
            this.index = i;
        }
        
        @Override
        public int compareTo(Pair p){
            if(this.sum < p.sum){
                return 1;
            }else{
                return -1;
            }
        }
        
        public String toString(){
            return "{ s = "+sum+", i = "+index+" }";
        }
    }
}

Problem solution in C++.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

const int Maxn = 1005;

int t;
int n;
int sum;
int a[Maxn];

int main() {
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        sum = 0;
        for (int i = 0; i < n; i++) {
            scanf("%d", &a[i]); sum += a[i];
        }
        for (int i = 0; i < n; i++) {
            int tmp; scanf("%d", &tmp);
            a[i] += tmp; sum -= tmp;
        }
        sort(a, a + n);
        for (int i = n - 1; i >= 0; i--)
            if ((n - 1 - i) % 2 == 0) sum += a[i];
            else sum -= a[i];
        if (sum > 0) printf("Firstn");
        else if (sum == 0) printf("Tien");
        else printf("Secondn");
    }
    return 0;
}

Problem solution in C.

#include<stdio.h>
int solve(int *a, int *b, int n, int max)
{
    int temp = 0, loss, ret = a[0], i;
    loss = b[0] - ( max - a[0] );
    for( i = 1 ; i < n ; i++ )
    {
        if( ( b[i] - ( max - a[i] ) ) > loss )
        {
            loss = b[i] - ( max - a[i] );
            ret = a[i];
            temp = i;
        }
    }
    a[temp] = b[temp] = 0;
    return ret;
}
int main()
{
    int t, n, start, i;
    long P1, P2;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        int a[n], b[n], max = -1;
        for( i = 0 ; i < n ; i++ )
        {
            scanf("%d", a + i);
            if( max < a[i] )
            {
                max = a[i];
            }
        }
        for( i = 0 ; i < n ; i++ )
        {
            scanf("%d", b + i);
        }
        start = 1, P1 = P2 = 0;
        for( i = 0 ; i < n ; i++ )
        {
            start > 0 ? ( P1 += solve(a, b, n, max) ): ( P2 += solve(b, a, n, max) );
            start = !start;
        }
        puts(P1 > P2 ? "First" : P1 < P2 ? "Second" : "Tie");
    }
    return 0;
}

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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