AlgoMaster Logo
AlgoMasterPrint Zero Even Oddmedium

Print Zero Even Odd

medium

Three threads share one ZeroEvenOdd instance and one supplied printNumber callback:

  • Thread A calls zero(printNumber) and may output only 0.
  • Thread B calls even(printNumber) and may output only even numbers.
  • Thread C calls odd(printNumber) and may output only odd numbers.

Coordinate the threads so the callback sequence is 0, 1, 0, 2, 0, 3, ... through n. In other words, each number from 1 through n must be preceded by a zero.

The judge creates the shared object, starts all three threads, and calls each method once. Your implementation should coordinate those method calls rather than create threads itself.

The judge also preloads the standard concurrency and callback APIs for each supported language. You do not need to add import, include, or using statements.

Example 1:

Input:

Output:

Explanation: The callback sequence is 0, 1, 0, 2.

Example 2:

Input:

Output:

Constraints

  • 1 <= n <= 1000
  • zero, even, and odd are called concurrently on the same ZeroEvenOdd instance.
  • printNumber(x) outputs the integer x and is thread-safe.
Loading...

Input

n = 2

Output

0102

Run is a quick check against the first couple of scenarios, which is roughly what these examples describe. Submit puts your class under the full set, which stays hidden.