Skip to content

Latest commit

 

History

History
55 lines (44 loc) · 1.37 KB

Print Zero Even Odd.md

File metadata and controls

55 lines (44 loc) · 1.37 KB

Hints

Use Semaphores. Here is a good explanation of Semaphores (read their explanation, skip their code).

Solution

class ZeroEvenOdd {
    private final int n;
    private final Semaphore zeroLock = new Semaphore(1);
    private final Semaphore  oddLock = new Semaphore(0);
    private final Semaphore evenLock = new Semaphore(0);

    public ZeroEvenOdd(int n) {
        this.n = n;
    }

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

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

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

Time/Space Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Links