Sort by

recency

|

34 Discussions

|

  • + 0 comments
    string nimGame(const vector<int>& pile) {
        int XOR = 0, twoOrMore = 0;
        for (int x : pile) {
            XOR = XOR ^ x;
            if (x > 1) twoOrMore++;
        }
        if (twoOrMore > 1) return (XOR == 0) ? "Second" : "First";
        else if (twoOrMore == 1) return "First";
        else return (pile.size() % 2 == 0) ? "First" : "Second";
    }
    
  • + 1 comment

    How did you know that an XOR operation solved the problem? What is the relationship?

  • + 0 comments

    C++ (more at https://github.com/IhorVodko/Hackerrank_solutions/tree/master , feel free to give a star :) )

    std::string misereNim(std::vector<int> const & _piles){
        int binSum = 0;
        int sum = 0;
        for(auto const & stones : _piles){
            binSum ^= stones;
            sum += stones;
        }
        if(_piles.size() == sum){
            return _piles.size() % 2 == 0 ? "First" : "Second";
        }
        return binSum ? "First" : "Second";
    }
    
  • + 1 comment

    here it is my python code : from functools import reduce def misereNim(s): if (set(s)=={1}) and n%2==1: return 'Second' elif (set(s)=={1}) and n%2==0: return 'First' elif reduce((lambda x,y:x^y),s): return 'First' else: return 'Second'

  • + 0 comments

    Here is Misere Nim problem solution in Python Java C++ and c programming - https://programs.programmingoneonone.com/2021/07/hackerrank-misere-nim-problem-solution.html