Chocolate in Box Discussions | Algorithms | HackerRank
  • + 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;
    }