Skip to content

Least Common Multiple of a List

Easy#gcd#lcm~15 min

The mission

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

Target time
O(n log(max(nums)))
Target space
O(1)

Constraints

  • every value is a positive integer
  • the list holds at least one value

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

Python 3Ctrl+Enter runs
Custom test case