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 Alice and Bob’s Silly Game problem solution

YASH PAL, 31 July 202426 January 2026

In this HackerRank Alice and Bob’s Silly Game problem solution, Alice and Bob play G games. Given the value of N for each game, print the name of the game’s winner on a new line. If Alice wins, print Alice; otherwise, print Bob.

HackerRank Alice and Bob's Silly Game problem solution

HackerRank Alice and Bob’s Silly Game problem solution in Python.

#!/bin/python3

import sys

def isP(n):
    if n<=3:
        return True
    if ((n%2==0)|(n%3==0)):
        return False
    for d in range(5,int(n**.5)+1,6):
        if ((n%d==0)|(n%(d+2)==0)):
            return False
    return True

def getP(m):
    P=[1,2]
    for n in range(3,m+1,2):
        if isP(n):
            P.append(n)
    return P            

P=getP(int(10**5+1000))

g = int(input().strip())
for a0 in range(g):
    n = int(input().strip())
    for i in range(len(P)):
        if P[i]>n:
            c=i-1
            break
    if c%2==1:
        print("Alice")
    else:
        print("Bob")

Alice and Bob’s Silly Game problem solution in Java.

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class AliceAndBobSillyGame {
    private static final int MAX = 30;
    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        
        int g = scanner.nextInt();
        for (int i = 0; i < g; i++) {
            int n  = scanner.nextInt();
            System.out.println(new AliceAndBobSillyGame(n).win() ? "Alice" : "Bob");
        }
        scanner.close();
    }

    int n;
    public AliceAndBobSillyGame(int n) {
        this.n = n;
    }

    boolean[] composite;
    public boolean win() {
        return (sieveEratosthenes(n) % 2) == 1;
    }

    private int sieveEratosthenes(int n) {
        int primes = 0;
        composite = new boolean[n+1];
        for (int i = 2; i <= n; i++) {
            if (! composite[i]) {
                primes++;
                for (int j = 2*i; j <= n; j += i) {
                    composite[j] = true;
                }
            }
        }
        return primes;
    }

}

Problem solution in C++.

#include <bits/stdc++.h>
#define lli long long int


using namespace std;
bool prime[100010];
void SieveOfEratosthenes(int n)
{
    
    memset(prime, true, sizeof(prime));
 
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
        {
            for (int i=p*2; i<=n; i += p)
                prime[i] = false;
        }
    }
 
    
}
int main()
{
    lli t;
    cin>>t;
    SieveOfEratosthenes(100005);
    while(t--)
    {
        lli n,ans=0,i;
        cin>>n;
        for(i=2;i<=n;i++)
        {
            if(prime[i]==true)
            {
                ans++;
            }
        }
        if(ans%2==0)
        {
            cout<<"Bob"<<endl;
        }
        else
        {
            cout<<"Alice"<<endl;
        }
    }
    return 0;
}

Problem solution in C.

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

int  main ()
{
    int N, i, j, a0;
    int test_cases;
    scanf("%d", &test_cases);
    for (a0 = 0; a0 < test_cases; a0++) {
        scanf("%d", &N);
        char arr[N+1];
        int b = 1;
        if (N == 1) {
            printf("Bobn");
            continue;
        }

        for (i = 0; i <= N; i++)
             arr[i] = 1;

        for (i = 2; i <= sqrt(N); i++) {
             j = i+i;
             while (j <= N) {
                 arr[j] = 0;
                 j += i;
             }
        }
        int k = 0;
        for (i = 2; i <= N; i++) {
             if (arr[i])  {
                 ++k;
                 
                 b = abs(b-1);
             }
        } 
        
        if (b) {
            printf("Bobn");
        } else {
            printf("Alicen");
        }
    }
}

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