Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • 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
Programmingoneonone

Learn everything about programming

HackerRank Bob and Ben problem solution

YASH PAL, 31 July 202430 November 2025

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

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes