Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • 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
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Poker Nim problem solution

YASH PAL, 31 July 2024

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("Firstn");
    else
        printf("Secondn");
}

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("Firstn");
        } else {
           printf("Secondn");
        }
        T--;
    }
    return 0;
}

Algorithms coding problems solutions

Post navigation

Previous post
Next post

Related website

The Computer Science

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes