Skip to content
BytePatterns

Least Common Multiple of a List

EasyMath & Number Theory#gcd#lcm~15m

Problem

Several buses leave the same depot at time zero, and bus i returns every nums[i] minutes. Return the first positive minute at which all of them are back at the depot together, which is the least common multiple of the values. Every value is a positive integer, and the list holds at least one value.

Examples

Input:  nums = [4, 6, 10]
Output: 60
Why:    60 is the smallest number divisible by 4, 6 and 10
Input:  nums = [12, 18, 24]
Output: 72
Why:    the values share factors, so the answer is far below their product
Input:  nums = [9]
Output: 9
Why:    edge case, a single bus meets itself on its first return

Hints

0 / 3

Stuck on the idea rather than the code? GCD and Euclid covers it.