Track pairs by remainder
A DJ tool suggests smooth crossfades between tracks whose combined duration lines up with a beat grid. Given a list of track durations in seconds and a grid length k (k >= 1), count the pairs of indices i < j such that durations[i] + durations[j] is divisible by k. For example, with durations [30, 20, 150, 100, 40] and k = 60, the qualifying pairs are (30, 150), (20, 100) and (20, 40), so the answer is 3. Checking every pair is O(n^2) — count by remainder instead.
Implement
divisible_pairs(durations: list[int], k: int) → intExamples
in
[[30,20,150,100,40],60]out3What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 16 min
solution.py
InputExpectedGot
[[30,20,150,100,40],60]3not run yetsample