C# with mutex
Intuition\n Describe your first thoughts on how to solve this problem. \nIntiution is to have a while loop and coordinate the different functions through a lock
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIntiution is to have a while loop and coordinate the different functions through a lock. There should be a running variable indicating the current number to print. Accessing and operating on this class level variable would be the critical section (inside lock)\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. The Odd function needs to print when the current number is odd\n2. The Even function needs to print when the current number is even\n3. Zero should be printed before printing any number\n\nWithout modifying the abosulte value of the current number, the Even and Odd functions can increment the current number and just negate it to inform Zero function to print "0".\nOnce Zero is printed, the Zero function can negate back the current number for the corresponding Even/Odd function to pick up and print.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(N) where N is the total number of numbers to be printed.\nNote that due to the race condition, the individual functions can sometimes just enter the critical section and not do anything - these are wasted cycles. O(N) might not necessarily be the right Time complexity\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1) -> 1 mutex and 2 integers\n\n# Code\n```\nusing System.Threading;\n\npublic class ZeroEvenOdd {\n    private int n;\n\n    private int i;\n\n    private Mutex mutex;\n\n    public ZeroEvenOdd(int n) {\n        this.n = n;\n        this.i = -1;\n        this.mutex = new Mutex(false);\n    }\n\n    // printNumber(x) outputs "x", where x is an integer.\n    public void Zero(Action<int> printNumber) {\n        while(true){\n            if (mutex.WaitOne()){\n                if (this.i < 0 && this.i*-1 <= n){\n                    printNumber(0);\n                    this.i *= -1;\n                }\n\n                mutex.ReleaseMutex();\n                if (Math.Abs(this.i) > n){\n                    break;\n                }\n            }\n        }\n    }\n\n    public void Even(Action<int> printNumber) {\n        while(true){\n            if (mutex.WaitOne()){\n                if (this.i %2 == 0 && this.i<=n && this.i > 0){\n                    printNumber(this.i);\n                    this.i++;\n                    this.i *= -1;\n                }\n\n                mutex.ReleaseMutex();\n                if (Math.Abs(this.i) > n){\n                    break;\n                }\n            }\n        }\n    }\n\n    public void Odd(Action<int> printNumber) {\n        while(true){\n            if (mutex.WaitOne()){\n                if (this.i %2 == 1 && this.i<=n && this.i > 0){\n                    printNumber(this.i);\n                    this.i++;\n                    this.i *= -1;\n                }\n\n                mutex.ReleaseMutex();\n                if (Math.Abs(this.i) > n){\n                    break;\n                }\n            }\n        }\n    }\n}\n```
['C#']