Sort by

recency

|

9 Discussions

|

  • + 0 comments

    This was a fun one — XOR problems always force you to think a bit differently. I’ve been into debugging game mechanics lately too, especially around how logic plays out in advance servers. Challenges like this help sharpen that mindset a lot.

  • + 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'