We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Game Theory
- Chocolate in Box
- Discussions
Chocolate in Box
Chocolate in Box
+ 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
+ 0 comments Ok, the solution seems to be difficult but is reachable with few lines of code.
- Get the nimsum of the heaps
- 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?
Load more conversations
Sort 28 Discussions, By:
Please Login in order to post a comment