Skip to content
Programmingoneonone
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • 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
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

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

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

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
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

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