C++ Solution | TC O(N) & SC O(1)
Complexity\n- Time complexity : O(N)\n- Space complexity : O(1)\n\n# Code\n\nclass ZeroEvenOdd {\nprivate:\n    int n;\n    mutex m;\n    condition_variable cv;
# Complexity\n- Time complexity : O(N)\n- Space complexity : O(1)\n\n# Code\n```\nclass ZeroEvenOdd {\nprivate:\n    int n;\n    mutex m;\n    condition_variable cv;\n    int turn;\n    int i;\n\npublic:\n    ZeroEvenOdd(int n) {\n        this->n = n;\n        turn = 0;\n        i = 1;\n    }\n\n    // printNumber(x) outputs "x", where x is an integer.\n    void zero(function<void(int)> printNumber) {\n        while(i <= n) {\n            unique_lock<mutex> lock(m);\n            while(turn != 0 && i<=n)\n                cv.wait(lock);\n            if(i > n)\n                break;\n            printNumber(0);\n            turn = (i % 2) == 0 ? 2 : 1;\n            cv.notify_all();\n        }\n    }\n\n    void even(function<void(int)> printNumber) {\n        while(i <= n) {\n            unique_lock<mutex> lock(m);\n            while(turn != 2 && i<=n)\n                cv.wait(lock);\n            if(i > n)\n                break;\n            printNumber(i++);\n            turn = 0;\n            cv.notify_all();\n        }\n    }\n\n    void odd(function<void(int)> printNumber) {\n        while(i <= n) {\n            unique_lock<mutex> lock(m);\n            while(turn != 1 && i<=n)\n                cv.wait(lock);\n            if(i > n)\n                break;\n            printNumber(i++);\n            turn = 0;\n            cv.notify_all();\n        }\n    }\n};\n```
['Concurrency', 'C++']