Header Ad

HackerRank Alice and Bob's Silly Game problem solution

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

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("Bob\n");
            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("Bob\n");
        } else {
            printf("Alice\n");
        }
    }
}


Post a Comment

0 Comments