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 The Prime Game problem solution

YASH PAL, 31 July 2024

In this HackerRank The Prime Game problem solution Manasa loves the nim game, in which there are N buckets, each having Ai balls. Two players play alternately. Each turn consists of removing some non-zero number of balls from one of the buckets. A player with a lack of moves loses. But, Manasa having played it so many times, gets bored one day. So she wants to change the rules of the game. She loves prime numbers, so she makes a new rule: any player can only remove a prime number of balls from a bucket. But there are infinite number prime numbers. So to keep the game simple, a player can only remove X balls from a bucket if X belongs to the set

The whole game can now be described as follows:

There are N buckets, and the Kth bucket contains Ak balls. A player can choose a bucket and remove X balls from that bucket where X belongs to S. A player loses if there are no more available moves.

Manasa plays the first move against Sandy. Who will win if both of them play optimally?

HackerRank The Prime Game 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.

#!/bin/python3

import math
import os
import random
import re
import sys

# 2, 3, 5, 7, 11, 13
values = [0, 0, 1, 1, 2, 2, 3, 3, 4]
    
def winner(A):
    nimber = 0  # or grundy number  
    for ak in A:
        nimber ^= values[ak % len(values)];

    return "Manasa" if nimber > 0 else "Sandy"

for _ in range(int(input())):
    input(), print(winner(list(map(int, input().split()))))

Problem solution in Java.

import java.io.*;  
import java.util.*;  
import java.text.*;  
import java.math.*;  
import java.util.regex.*;  

public class Solution {  

    public static void main(String[] args) {  
        int t = ni();  
        for(int i=0; i<t; i++){  
            System.out.println(solve());  
        }  
    }  

    public static int[] values = new int[]{0, 0, 1, 1, 2, 2, 3, 3, 4};  
    public static String solve(){  
        int n = ni();  
        long ak;  
        long nimber = 0; //or grundy number  
        for(int i=0; i<n; i++){  
            ak = nl();  
            nimber ^= values[(int) (ak % values.length)];
        }  
        if(nimber > 0) return "Manasa";  
        return "Sandy";  
    }  

    public static Scanner sc = new Scanner(System.in);  
    public static int ni(){  
        return sc.nextInt();  
    }  
    public static long nl(){  
        return sc.nextLong();  
    }  
}  

Problem solution in C++.

#include <cstdio>

using namespace std;

using int64 = long long;

int ans[9] = {0, 0, 1, 1, 2, 2, 3, 3, 4};

int main() {
  int cas;
  scanf("%d", &cas);
  while (cas--) {
    int n;
    scanf("%d", &n);
    int ret = 0;
    for (int i = 0; i < n; ++i) {
      int64 x;
      scanf("%lld", &x);
      ret ^= ans[x % 9];
    }
    puts(ret ? "Manasa" : "Sandy");
  }
  return 0;
}

Problem solution in C.

#include<stdio.h>
int main()
{    
    int t, n;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        int val = 0;
        long long tmp;
        for( int i = 0 ; i < n ; i++ )
        {
            scanf("%lld", &tmp);
            val = val ^ ( ( tmp % 9 ) / 2 );
        }
        printf("%sn", val ? "Manasa" : "Sandy");
    }
    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