Skip to the code
cf.
+25 XP

#636 When Animations Line Up

medium

A loading screen runs several looping CSS animations at once. Their durations in milliseconds are in durations. They all start together at time 0.

Write loopsLineUp(durations) that returns the first time after 0 when every animation is at the start of a loop again: the least common multiple of the durations.

Example 1

Input: durations = [400,600]
Output: 1200

400 loops 3 times and 600 loops 2 times in 1200 ms, and no earlier time works for both.

Example 2

Input: durations = [300]
Output: 300

One animation lines up with itself after one loop.

Constraints

  • 1 <= durations.length <= 20
  • 1 <= durations[i] <= 10^6
  • The answer is at most 10^15.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).