Design a ParallelMapper that applies a supplied transformation to an array using a fixed number of worker threads.
Implement map(values, workers, transform):
transform exactly once to every value.min(workers, values.length) workers.The transformation performs one unit of work and is safe to call concurrently. Do not create one thread per element, and do not use busy-waiting.
If a transformation fails, stop assigning new work after the failure is observed, wait for all workers that were already started, and propagate the first failure. Java, Python, C++, and C# propagate the callback exception. In Go, the transformation and Map return an error; return the first non-nil error and no result.
For an empty input, return an empty result without creating workers or invoking the transformation.
Standard thread, synchronization, collection, callback, and error APIs are preloaded, so you do not need import, include, package, or using statements.
Input:
Output:
Explanation: The callbacks may complete in any order, but each result is written to its input index.
Input:
Output:
Explanation: The mapper joins every started worker and then propagates the first transformation failure.
0 <= values.length <= 100001 <= workers <= 100-1_000_000 <= values[i] <= 1_000_000map calls on the same ParallelMapper may execute concurrently.Input
values = [4, 1, 3] workers = 2 transform(x) = x * x
Output
[16, 1, 9]
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.

