Pairs of Songs With Total Durations Divisible by 60 - Problem

You're managing a music streaming service and need to create perfect playlist pairs for your users. Given an array of song durations (in seconds), you need to find all pairs of songs where the combined duration is divisible by 60 seconds (i.e., forms a complete minute multiple).

More formally, given an array time where time[i] represents the duration of the i-th song in seconds, return the number of pairs (i, j) where:

  • i < j (to avoid counting the same pair twice)
  • (time[i] + time[j]) % 60 == 0 (sum is divisible by 60)

Example: If you have songs with durations [30, 20, 150, 100, 40], the pairs (30, 150) and (20, 100) both sum to 180 seconds (3 minutes), and (20, 40) sums to 60 seconds (1 minute).

Input & Output

example_1.py โ€” Basic Case
$ Input: [30, 20, 150, 100, 40]
โ€บ Output: 3
๐Ÿ’ก Note: Three pairs sum to multiples of 60: (30+150=180), (20+100=120), (20+40=60)
example_2.py โ€” Edge Case with Zeros
$ Input: [60, 60, 60]
โ€บ Output: 3
๐Ÿ’ก Note: All songs have remainder 0, so any pair works: (60+60=120), (60+60=120), (60+60=120) - total 3 unique pairs
example_3.py โ€” No Valid Pairs
$ Input: [269, 230, 175]
โ€บ Output: 0
๐Ÿ’ก Note: Remainders are 29, 50, 55. No two remainders sum to 0 or 60, so no valid pairs exist

Visualization

Tap to expand
Clock Visualization: Song Duration Remainders01530453045100s โ†’ 40150s โ†’ 30Complement pairs sum to 60Song Mapping30s โ†’ remainder 3020s โ†’ remainder 20150s โ†’ remainder 30100s โ†’ remainder 4040s โ†’ remainder 40Valid Pairs:20 + 40 = 60 โœ“30 + 30 = 60 โœ“
Understanding the Visualization
1
Map to Clock
Each song duration gets mapped to a position 0-59 on a 60-second clock
2
Find Complements
For each position, find what other position would complete a full rotation (sum to 0 mod 60)
3
Count Pairs
Count how many songs are at complementary positions
4
Handle Special Cases
Positions 0 and 30 pair with themselves, requiring special combination counting
Key Takeaway
๐ŸŽฏ Key Insight: Use modular arithmetic to transform the problem from checking all pairs to counting complement remainders, reducing complexity from O(nยฒ) to O(n).

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the array with constant time operations

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Fixed-size array of 60 elements regardless of input size

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค time.length โ‰ค 6 ร— 104
  • 1 โ‰ค time[i] โ‰ค 500
  • Follow-up: Can you solve it in O(n) time?
Asked in
Amazon 45 Google 38 Meta 32 Microsoft 28
68.2K Views
High Frequency
~18 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen