Skip to the code
cf.
+25 XP

#30 Animation Loops That Sync

medium

A motion designer plays two looping animations back to back. The pair looks seamless when their total length is a multiple of the beat length k. The loop lengths are in durations.

Write syncedPairs(durations, k) that returns how many pairs of indexes i < j have (durations[i] + durations[j]) % k === 0.

Example 1

Input: durations = [45,15,75,30,90], k = 60
Output: 3

45 + 15 = 60, 45 + 75 = 120, and 30 + 90 = 120 are multiples of 60.

Example 2

Input: durations = [60,60,60], k = 60
Output: 3

Every pair adds to 120, a multiple of 60.

Constraints

  • 0 <= durations.length <= 10^5
  • 1 <= durations[i] <= 10^9
  • 1 <= k <= 10^5
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).