Number theory · Medium
A scheduler combines periodic maintenance cycles expressed in whole ticks. It needs the first shared period, not a simulation of every tick. A disabled cycle is represented by zero and makes the combined period zero.
Return the least common multiple of cycle magnitudes with exact integer arithmetic.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ nums: integer[] }
Fields
- nums is an array of signed integers in its original order. Equal values at different indexes are separate entries.
Output
Return value
- Empty input returns 1. Any zero returns 0. Otherwise return the smallest positive shared multiple.
- Signs do not change the period. Do not return an approximation.
Constraints
- 0 <= nums.length <= 100000; -1000000000 <= nums[i] <= 1000000000.
- All requested totals and counts fit exactly in a safe JSON integer.
- The mathematical LCM is at most 9007199254740991. Use exact intermediate arithmetic; intermediate products before division may exceed that.
Examples
Example 1
{
"nums": [
6,
-8
]
}Returns
24Example 2
{
"nums": [
6,
0,
8
]
}Returns
0Required complexity
O(n log M) greatest-divisor steps with exact integer intermediates and O(1) accumulators