Chocolate in Box Discussions | Algorithms | HackerRank

Sort by

recency

|

28 Discussions

|

  • + 0 comments

    Here is Chocolate in Box problem solution in Python Java C++ and c programming. https://programs.programmingoneonone.com/2021/07/hackerrank-chocolate-in-box-problem-solution.html

  • + 1 comment

    Ok, the solution seems to be difficult but is reachable with few lines of code.

    1. Get the nimsum of the heaps
    2. Verifify that the heap^nimsum < heap this means the next player will lose if none of them make a mistake

    By the way you can find a better understanding of this problem in wiki: https://en.wikipedia.org/wiki/Nim

    function chocolateInBox(arr: number[]): number {
        var nimsum = 0;
        var winner = 0;
        for(let box of arr){ nimsum ^= box};
        for(let box of arr){ winner += ((box^nimsum) < box)?1:0;};
        return winner;
    }
    
  • + 0 comments

    C#:

    public static int chocolateInBox(List<int> arr)
    {
        int trs = arr.Aggregate(0, (xor, cur) => xor ^ cur);
        return arr.Where(a=>a > 0 && (a ^ trs) < a).Count();
    }
    

    ...actually 2 lines

  • + 0 comments

    How XOR operator works in these kind of questions??

  • + 0 comments

    How many copies of "solve NIM" do we need on this site?