Header Ad

HackerRank Poker Nim problem solution

In this HackerRank Poker Nim problem solution, we have given the values of N piles of chips indexed from 0 to n -1 and an integer K to ensure that the games end infinite time and the number of chips in each of the N piles, determine whether the person who wins the game is the first or second person to move. assume both players move optimally.

HackerRank Poker Nim problem solution


Problem solution in Python.

t = int(input().strip())
for i in range(t):
    n,k = [int(temp) for temp in input().strip().split(' ')]
    c = [int(temp) for temp in input().strip().split(' ')]
    
    #k = 5 c = 1 2
    score = 0 
    for ctemp in c: 
        score ^= ctemp
        
    if score==0: 
        print("Second")
    else:
        print("First")
        


Problem solution in Java.

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

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        int T = in.nextInt();
        for (int t = 0; t < T; t++) {
            int n = in.nextInt();
            in.nextInt(); // k is irrelevant
            int res = in.nextInt();
            for (int i = 1; i < n; i++) {
                res ^= in.nextInt();
            }
            if (res == 0) {
                System.out.println("Second");
            } else {
                System.out.println("First");
            }
        }
    }
}


Problem solution in C++.

#include <stdio.h>

using namespace std;

int N, K;

inline void solve()
{
    int res = 0, num;
    
    scanf("%d %d", &N, &K);
    
    while(N--)
        {
        scanf("%d", &num);
        res ^= num;
    }
    
    if(res)
        printf("First\n");
    else
        printf("Second\n");
}

int main()
{
    int T;
    
    scanf("%d", &T);
    
    while(T--)
    solve();
    return 0;
}


Problem solution in C.

#include <stdio.h>

int main() {
    int T;
   scanf ("%d",&T);
    while(T > 0) {
        int n, k;
        int nimSum, tmp;
        scanf ("%d %d %d",&n,&k,&nimSum);
        for(int i = 1; i < n; i++) {
           scanf ("%d",&tmp);
            nimSum ^= tmp;
        }
        if(nimSum != 0) {
            printf("First\n");
        } else {
           printf("Second\n");
        }
        T--;
    }
    return 0;
}


Post a Comment

0 Comments