Pairs of Songs With Total Durations Divisible by 60 - Problem

You are given a list of songs where the i-th song has a duration of time[i] seconds.

Return the number of pairs of songs for which their total duration in seconds is divisible by 60.

Formally, we want the number of indices i, j such that i < j with (time[i] + time[j]) % 60 == 0.

Input & Output

Example 1 — Basic Case
$ Input: time = [30,20,150,100,40]
Output: 3
💡 Note: Three pairs sum to multiples of 60: (30,150)=180, (20,40)=60, (100,40)=140. Wait, let me recalculate: (30,150)=180≡0, (20,40)=60≡0, (30,30) from positions would be invalid since we need i
Example 2 — Multiple Zeros
$ Input: time = [60,60,60]
Output: 3
💡 Note: All songs are 60 seconds (remainder 0). Every pair sums to 120≡0 (mod 60). Pairs: (0,1), (0,2), (1,2) = 3 pairs total.
Example 3 — No Valid Pairs
$ Input: time = [10,50,90,30]
Output: 1
💡 Note: Remainders: 10, 50, 30, 30. Only 10+50=60≡0 (mod 60) forms a valid pair.

Constraints

  • 1 ≤ time.length ≤ 6 × 104
  • 1 ≤ time[i] ≤ 500

Visualization

Tap to expand
Pairs of Songs With Total Durations Divisible by 60Input: Song Durations302015010040Valid Pairs (Sum ≡ 0 mod 60)30 + 150 = 180 ≡ 0 (mod 60) ✓20 + 40 = 60 ≡ 0 (mod 60) ✓100 + 40 = 140 ≡ 20 (mod 60) ❌Result3 pairs
Understanding the Visualization
1
Input
Array of song durations in seconds
2
Process
Find pairs where sum is divisible by 60
3
Output
Count of valid pairs
Key Takeaway
🎯 Key Insight: Use modular arithmetic - remainders that sum to 60 (or both 0/30) form valid pairs
Asked in
Amazon 15 Facebook 8
60.4K Views
Medium Frequency
~15 min Avg. Time
2.2K 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