Skip to content
Programming101
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
Programming101
Programmingoneonone

Learn everything about programming

HackerRank Alice and Bob’s Silly Game problem solution

YASH PAL, 31 July 2024

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

Foundation course by prepbytes

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 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")

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

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