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 Introduction to Nim Game problem solution

YASH PAL, 31 July 202426 January 2026

In this HackerRank Introduction to Nim Game problem solution we have given the value of N and the number of stones in each pile, determine the game’s winner if both players play optimally.

HackerRank Introduction to Nim Game problem solution

HackerRank Introduction to Nim Game problem solution in Python.

from functools import reduce

for _ in range(int(input())):
    n = int(input())
    piles = list(map(int, input().split()))
    if reduce(lambda x, y: x ^ y, piles) == 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) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        while (t > 0) {
            --t;
            int n = scanner.nextInt();
            int x = 0;
            for (int i = 0; i < n; ++i) {
                x ^= scanner.nextInt();
            }
            
            System.out.println(x > 0 ? "First" : "Second");
        }
    }
}

Introduction to Nim Game problem solution in C++.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int T, N, s;
int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
  cin >> T;
  for (int t = 0; t < T; t++) {
    cin >> N;
    int tot = 0;
    for (int i = 0; i < N; i++) {
      cin >> s;
      tot ^= s;
    }
    if (tot == 0)
      cout << "Second" << endl;
    else
      cout << "First" << endl;
  }
  return 0;
}

Problem solution in C.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int g;
    scanf("%d", &g);
    for (int x = 0; x < g; x++){
        int n, sum = 0;
        scanf("%d", &n);
        int s;
        for (int y = 0; y < n; y++){
            scanf("%d", &s);
            sum = sum ^ s;
        }
        if (sum == 0){
            printf("Secondn");
        }
        else {
            printf("Firstn");
        }
    }
    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