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 Bob and Ben problem solution

YASH PAL, 31 July 2024

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

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 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("BENn");
        }
        else
        {
            printf("BOBn");
        }
    }
    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