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 Misère Nim problem solution

YASH PAL, 31 July 202426 January 2026

In this HackerRank Misère Nim problem solution we have given the value of N piles of stones indexed from 0 to n – 1 and the number of stones in each pile, determine whether the person who wins the game is the first or second person to move. If the first player to move wins, print First on a new line; otherwise, print Second. Assume both players move optimally.

HackerRank Misère Nim problem solution

HackerRank Misère Nim problem solution in Python.

g = int(input().strip())

for _ in range(g):
    n = int(input().strip())
    s = [int(x) for x in input().strip().split(' ')]
    x = 0
    for i in s:
        x ^= i
    if len(set(s))==1 and 1 in s:
        #here x=0 or 1
        if x:#odd no. of ones
            print("Second")
        else:#even no. of ones
            print("First")
    else:
        if x:
            print("First")
        else:
            print("Second")

Misère Nim problem solution in Java.

import java.util.Arrays;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) throws java.lang.Exception {

    Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();
    while (t-- > 0) {
      int k1 = sc.nextInt();
      int l1 = 0;
      int max = 0;
      while (k1-- > 0) {
        int p = sc.nextInt();
        l1 ^= p;
        max = Math.max(max, p);
      }
      System.out.println((l1 == 0 && max > 1 || l1 == 1 && max == 1) ? "Second" : "First");
    }
  }

}

Problem solution in C++.

#include <iostream>
using namespace std;

int main() {
  int T, n, s, result, count;
  cin >> T >> ws;
  for (int a0 = 0; a0 < T; a0++) {
    cin >> n >> ws;
    result = 0;
    count = 0;
    for (int i = 0; i < n; i++) {
      cin >> s >> ws;
      result ^= s;
      if (s <= 1) count++;
    }
    if ((count == n && result == 1) ||
        (count < n  && result == 0))
      cout << "Second" << endl;
    else
      cout << "First" << endl;
  }
  return 0;
}

Problem solution in C.

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

int main(void) {
    int t;
    scanf("%d", &t);

    while(t--) {
        int n;
        int result = 0;

        scanf("%d", &n);
        int counter_big = 0;
        int counter     = 0;
        int max         = 0;
        for(int i = 0; i < n; i++) {
            int temp;
            scanf("%d", &temp);
            result ^= temp;
            if(temp > 0) {
                if(temp > 1)
                    counter_big++;
                counter++;
            }

            if(temp > max)
                max = temp;
        }

        if(counter_big <= 1) {
            if(counter & 1 && max == 1)
                puts("Second");
            else
                puts("First");
        } else {
            if(result)
                puts("First");
            else
                puts("Second");
        }
    }

    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