LeetCode 1116: Print Zero Even Odd Solution

Master LeetCode problem 1116 (Print Zero Even Odd), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1116. Print Zero Even Odd

Problem Explanation

Explanation:

To achieve the desired output of alternating zeros, even numbers, and odd numbers in a synchronized manner, we can utilize three semaphores to control the flow of execution between the three threads. We will use the following approach:

  • Initialize three semaphores: zeroSemaphore, evenSemaphore, and oddSemaphore to control the flow of zero, even, and odd threads respectively.
  • Each semaphore starts at 0 and will be incremented by 1 when its corresponding method is called.
  • The ZeroEvenOdd class will have three methods: zero, even, and odd, which will print the required numbers based on the current state.
  • Threads A, B, and C will be responsible for calling zero(), even(), and odd() methods respectively in a synchronized manner.
  • Use a mutex lock to ensure only one thread is executing at a time.

:

Solution Code

import java.util.concurrent.Semaphore;

class ZeroEvenOdd {
    private int n;
    private Semaphore zeroSemaphore, evenSemaphore, oddSemaphore;
    
    public ZeroEvenOdd(int n) {
        this.n = n;
        zeroSemaphore = new Semaphore(1);
        evenSemaphore = new Semaphore(0);
        oddSemaphore = new Semaphore(0);
    }

    public void zero(IntConsumer printNumber) throws InterruptedException {
        for (int i = 1; i <= n; i++) {
            zeroSemaphore.acquire();
            printNumber.accept(0);
            if (i % 2 == 0) {
                evenSemaphore.release();
            } else {
                oddSemaphore.release();
            }
        }
    }

    public void even(IntConsumer printNumber) throws InterruptedException {
        for (int i = 2; i <= n; i += 2) {
            evenSemaphore.acquire();
            printNumber.accept(i);
            zeroSemaphore.release();
        }
    }

    public void odd(IntConsumer printNumber) throws InterruptedException {
        for (int i = 1; i <= n; i += 2) {
            oddSemaphore.acquire();
            printNumber.accept(i);
            zeroSemaphore.release();
        }
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1116 (Print Zero Even Odd)?

This page provides optimized solutions for LeetCode problem 1116 (Print Zero Even Odd) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1116 (Print Zero Even Odd)?

The time complexity for LeetCode 1116 (Print Zero Even Odd) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1116 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1116 in Java, C++, or Python.

Back to LeetCode Solutions