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

HackerRank Poker Nim problem solution

YASH PAL, 31 July 202426 January 2026

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

HackerRank Poker Nim 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")
        

Poker Nim 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 AlgorithmsHackerRank

Post navigation

Previous post
Next post

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