Header Ad

HackerRank Bob and Ben problem solution

In this HackerRank Bob and Ben problem solution we have given the values of Mi the number of nodes in the tree and a constant Ki for each tree in the forest, we need to determine who will win the game.

hackerrank bob and ben problem solution


Problem solution in Python.

#!/bin/python3

import os
import sys
import functools

#
# Complete the bobAndBen function below.
#
def bobAndBen(trees):
    #
    # Write your code here.
    #
    nims = []
    for tree in trees:
        nims.append(grundy(tree[0]))
    res = functools.reduce(lambda a,b: a^b, nims)
    if res == 0: return "BEN"
    return "BOB"

def grundy(m):
    res = 0 if m == 0 or m == 2 else ((m-1) % 2) + 1
    return res

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    g = int(input())

    for g_itr in range(g):
        n = int(input())

        trees = []

        for _ in range(n):
            trees.append(list(map(int, input().rstrip().split())))

        result = bobAndBen(trees)

        fptr.write(result + '\n')

    fptr.close()


Problem solution in Java.

import java.io.*;
import java.util.*;
public class Solution
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int G = input.nextInt();
        for(int g=0; g<G; g++)
        {
            int n = input.nextInt();
            int p = 0;
            for(int i=0; i<n; i++)
            {
                int m = input.nextInt();
                int k = input.nextInt();
                if(m%2 == 1)
                    p = p^1;
                else if(m > 2)
                    p = p^2;
            }
            if(p==0)
                System.out.println("BEN");
            else
                System.out.println("BOB");
        }
    }
}


Problem solution in C++.

#include<string>
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string.h>
#include<memory.h>
#include<math.h>
#include<vector>
#include<algorithm>
#include<map>
#include<numeric>
#include<deque>
#include<set>
#include<functional>
#include<queue>
#include<stack>
#include<unordered_set>
#include<unordered_map>



#define REP(i,s,n) for(int (i)=s; (i)<(int)(n);(i)++)
#define RIT(it,c) for(__typeof(c.begin()) it = c.begin();it!=c.end();it++)
#define RITT(it,v,c) for(__typeof(c.begin()) it = v;it!=c.end();it++)
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int)(x).size()
#define MSET(m,v) memset(m,v,sizeof(m))
#define auto(c) __typeof(c.begin())

using namespace std;
typedef vector<int> vi;
typedef pair<int,int>ii;
typedef long long LL;
typedef pair<ii,int> iiit;



int main(){
    int T;
    cin>>T;
    for(int k=0;k<T;k++){
        int n,sum=0;
        cin>>n;
        int cnt = n;
        for(int i=0;i<n;i++){
            int m,l;
            scanf("%d%d",&m,&l);
            if(m==2) cnt++;
            sum += m%2;
        }
        if((cnt%2==0 && sum%2==0)) cout<<"BEN"<<endl;
        else cout<<"BOB"<<endl;
    }
}


Problem solution in C.

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

int main()
{
    int test,t;
    scanf("%d",&test);
    for(t=1;t<=test;t++)
    {
        int n,i;
        scanf("%d",&n);
        int answer=0;
        for(i=0;i<n;i++)
        {
            int m,k;
            scanf("%d %d",&m,&k);
            if(m==2)
            {
                m=0;
            }
            else if(m==3)
            {
                m=1;
            }
            else if(m>3)
            {
                if(m%2==0)
                {
                    m=2;
                }
                else
                {
                    m=1;
                }
            }
            answer=answer^m;
        }
        if(answer==0)
        {
            printf("BEN\n");
        }
        else
        {
            printf("BOB\n");
        }
    }
    return 0;
}


Post a Comment

0 Comments