Simplest C++ solution 1 mutex, 1 condition_variable(No Strange loop incrementing, Modulus etc)
\n\tclass ZeroEvenOdd {\n\tprivate:\n\t\tstd::mutex m;\n\t\tstd::condition_variable cv;\n\t\tint n;\n\t\tint flag; // 0 -> zero, 1 -> even, 2 -> odd\n\t\tint pr
\n\tclass ZeroEvenOdd {\n\tprivate:\n\t\tstd::mutex m;\n\t\tstd::condition_variable cv;\n\t\tint n;\n\t\tint flag; // 0 -> zero, 1 -> even, 2 -> odd\n\t\tint prev_flag; //  1 -> even, 2 -> odd\n\t\tint count;\n\tpublic:\n\t\tZeroEvenOdd(int n) {\n\t\t\tthis->n = n;\n\t\t\tflag = 0;\n\t\t\tprev_flag = 0;\n\t\t\tcount = 1;\n\t\t}\n\n    // printNumber(x) outputs "x", where x is an integer.\n    void zero(std::function<void(int)> printNumber) {\n        std::unique_lock<std::mutex> lock(m);\n        for(int i = 0; i < n; ++i) // if you look at the examples there are n zeroes\n        {           \n            cv.wait(lock, [this](){return flag == 0;});\n            printNumber(0);\n            if(prev_flag == 1)\n                flag = 2;\n            else if(prev_flag == 2)\n                flag = 1;\n\t\t\telse if(prev_flag == 0) // only happens in the first iteration\n\t\t\t  flag = 2;\n            cv.notify_all();\n        }\n        \n    }\n\n    void even(std::function<void(int)> printNumber) {\n        std::unique_lock<std::mutex> lock(m);\n        for(int i = 0; i < n/2; ++i)// if you look at the example there are n/2 even numbers\n        {\n            cv.wait(lock, [this](){return flag == 1;});\n            printNumber(count);\n            prev_flag = flag;\n            flag = 0;\n            ++count;\n            cv.notify_all();\n        }\n        \n    }\n\n    void odd(std::function<void(int)> printNumber) {\n        std::unique_lock<std::mutex> lock(m);\n        for(int i = 0; i < (n + 1)/2; ++i)// if you look at the examples there are (n+1)/2 odd numbers\n        {\n            cv.wait(lock, [this](){return flag == 2;});\n            printNumber(count);\n            prev_flag = flag;\n            flag = 0;\n            ++count;\n            cv.notify_all();\n\n        }\n        \n    }\n};\n
[]