Header Ad

HackerRank Fun Game problem solution

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("First\n");
        else if (sum == 0) printf("Tie\n");
        else printf("Second\n");
    }
    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;
}


Post a Comment

0 Comments