Sort by

recency

|

8 Discussions

|

  • + 0 comments

    Here is Bob and Ben problem solution in Python Java C++ and c programming - https://programs.programmingoneonone.com/2021/07/hackerrank-bob-and-ben-problem-solution.html

  • + 0 comments

    C# (as usually short - 1 line - and elegant, no indexing):

    public static string bobAndBen(List<List<int>> trees)
    {
        return trees.Aggregate(0,(xor,t)=>xor^(t[0]==0 || t[0]==2 ? 0 : (t[0]-1)%2+1))==0
        ? "BEN" : "BOB";
    }
    

    Python must be 1-ne as well........

  • + 0 comments

    simple java solution, O(1) space and O(number of trees) time

    import java.util.Scanner;
    
    class Solution{
        static void solve(){
    	Scanner sc=new Scanner(System.in);
    	int ngame=sc.nextInt();
    	for(int i=0;i<ngame;++i){
    	    int ntree=sc.nextInt(), xor=0;
    	    for(int j=0;j<ntree;++j){
    		int nnode=sc.nextInt();
    		sc.nextInt();
    		int nimber=0;
    	        if(nnode!=2) nimber=2-(nnode&1);
    		xor=(j==0?nimber: xor^nimber);
    	    }
    	    System.out.println(xor==0?"BEN":"BOB");
    	}
    	sc.close();
        }
        public static void main(String[] args){
    	solve();
        }
    }
    
  • + 1 comment
    def GrudyNumber(nodes):
        if(nodes == 0 or nodes == 2):
            return 0 
        else:
            return ((nodes-1)%2+1)
    
    def bobAndBen(trees): 
        GrudyNumers= []
        for i in range(len(trees)):
            GrudyNumers.append(GrudyNumber(trees[i][0]))
        re=GrudyNumers[0]
        print(GrudyNumers)
        if((len(GrudyNumers)-1)>1):
            for j in range(1,len(GrudyNumers)):
                re^=GrudyNumers[j]
        elif((len(GrudyNumers)-1)==1):
            re=re ^ GrudyNumers[1]
        if(re != 0): 
            return 'BOB' 
        else:
            return 'BEN'
    
  • + 1 comment

    This is good problem for learning game theory. Hope my comments help you:

    The key to this problem is Grundy Theorem wich sounds like:

    Any position of an impartial game is equivalent to a nim pile of a certain size.

    or in more simple way:

    Suppose there is a composite game (more than one sub-game) made up of N sub-games and two players, A and B. Then Sprague-Grundy Theorem says that if both A and B play optimally (i.e., they don’t make any mistakes), then the player starting first is guaranteed to win if the XOR(also known as nim-addition) of the grundy numbers(also known as nimbers) of position in each sub-games at the beginning of the game is non-zero. Otherwise, if the XOR evaluates to zero, then player A will lose definitely, no matter what.

    So, we need now find all Grundy nunbers (shortly nimbers) and XOR all of them.

    Grundy nunber (or nimber or Gn) definition:

    1. If from current game position there is no moves (Zero 0 stones in pile) then nimber Gn(0) = 0.
    2. Nimber of N-position equal to minimal nimber of positions not reachable from current position.

    For better understanding let's consiter term 'Mex': ‘Minimum excludant’ a.k.a ‘Mex’ of a set of numbers is the smallest non-negative number not present in the set. details here.

    Now we can say:

    Gn=Mex{Gn's of all possible positions we can reach from current}

    Let's now find nimbers of our game. First we need to devide or game into sub games. in this case each subgame is tree in given forest. The trees looks like this one where nuber of child nodes depend on given k:

                   1
                 /   \
                2     3
               / \    / \
              4   5  6   7
             / \ / \/ \ / \
            8   9..............  
    

    We need to highlight following properties of tree:

    -if number of nodes N>2, then we can remove either one leaf or whole tree N --> N-1 or N --> 0.
    -but when N=2, then both nodes become leafs, in this case we can remove only one of the leaf N --> N-1

    Back to our nimbers:

    if thre is N=0 nodes in tree

    Gn(0) = 0 (by definition)   
    

    if N=1, then we can move only to 0-position,

    Gn(1) = Mex{Gn(0)} = Mex{0} = 1 {by definition of Mex}
    

    if N=2, we can move only to 1 position

    Gn(2) = Mex{Gn(1)} = Mex{1} = 0
    

    if N=3 we can move to 0-position or to 2-position:

    Gn(3) = Mex{Gn(0), Gn(2)} = Mex{0,1} = 2
    

    if N=4 we can move to 0-position or to 3-position:

    Gn(4) = Mex{Gn(0), Gn(3)} = Mex{0,2} = 1
    

    if N=5 we can move to 0-position or to 4-position:

    Gn(5) = Mex{Gn(0), Gn(4)} = Mex{0,1} = 2
    

    if N=6 we can move to 0-position or to 5-position:

    Gn(6) = Mex{Gn(0), Gn(5)} = Mex{0,2} = 1
    

    ...and so on. We can see that:

    for n =0 and n =2: G(n) = 0

    for all other n: Gn(n) = (n-1)%2+1

    Now we can calculate Grundy numbers (nimbers) for a tree of any height. And we only need to find XOR of all trees in given forest.

    Below is my JavaScript solution passed all the tests:

    function GrudyNumber(tree){
        //function will return GrudyNumber (nimber) for given tree
        return (tree == 0 || tree == 2) ? 0 : (tree-1)%2+1
    }
    
    function bobAndBen(forest) {
        let GrudyNumers = []
        
        //find GrudyNumber(nimbers) for each tree
        for(let tree of forest){
            GrudyNumers.push(GrudyNumber(tree[0]))
        }
        
        //return XOR (also known as nim-sum) of all Grundy numbers
        return GrudyNumers.reduce((a,b)=>a^b) != 0 ? 'BOB' : 'BEN'
    }
    
    function main() {
        const g = parseInt(readLine(), 10);
    
        for (let gItr = 0; gItr < g; gItr++) {
            const n = parseInt(readLine(), 10);
            let forest = Array(n);
    
            for (let i = 0; i < n; i++) {
                forest[i] = readLine().split(' ').map(trees => parseInt(trees, 10));
            }
            console.log(bobAndBen(forest))
        }
    }