Pairs of Songs With Total Durations Divisible by 60 (LeetCode 1010): Solution and Explanation
Given song lengths in seconds, count the pairs of songs whose combined length is a whole number of minutes.
Intuition
Whether a sum is a multiple of 60 depends only on the remainders of its parts. A 150 second song behaves exactly like a 30 second song, so every duration can be replaced by t % 60.
Two remainders work together when they add to 60, or when both are 0. So a song with remainder r needs a partner with remainder (60 - r) % 60. The extra % 60 turns the partner of 0 into 0 instead of 60.
This is Two Sum with a modulus. Walk the songs left to right and keep a count of the remainders already seen. Each new song pairs with every earlier song in its partner slot, so add that count, then record the song itself.
Approach: Count remainders in one pass
- Create count[0..59] filled with zeros and set pairs = 0.
- For each duration t, compute r = t % 60.
- Add count[(60 - r) % 60] to pairs: those are the earlier songs that complete a full minute with this one.
- Increment count[r] so later songs can pair with this one.
- Return pairs.
Step-by-step example
time = [30,20,150,100,40]
- 30 has remainder 30 and needs slot 30, which is empty. count[30] = 1.
- 20 has remainder 20 and needs slot 40, which is empty. count[20] = 1.
- 150 has remainder 30 and needs slot 30, which holds 1 song. pairs = 1. count[30] = 2.
- 100 has remainder 40 and needs slot 20, which holds 1 song. pairs = 2. count[40] = 1.
- 40 has remainder 40 and needs slot 20, which holds 1 song. pairs = 3. Return 3.
Code: Python, Java, C++
1class Solution:
2 def numPairsDivisibleBy60(self, time: List[int]) -> int:
3 count = [0] * 60
4 pairs = 0
5 for t in time:
6 r = t % 60
7 pairs += count[(60 - r) % 60]
8 count[r] += 1
9 return pairs1class Solution {
2 public int numPairsDivisibleBy60(int[] time) {
3 int[] count = new int[60];
4 int pairs = 0;
5 for (int t : time) {
6 int r = t % 60;
7 pairs += count[(60 - r) % 60];
8 count[r]++;
9 }
10 return pairs;
11 }
12}1class Solution {
2public:
3 int numPairsDivisibleBy60(vector<int>& time) {
4 vector<int> count(60, 0);
5 int pairs = 0;
6 for (int t : time) {
7 int r = t % 60;
8 pairs += count[(60 - r) % 60];
9 count[r]++;
10 }
11 return pairs;
12 }
13};Time and space complexity
O(n)
Each song does one lookup and one increment.
O(1)
The count array always has 60 entries, no matter how many songs there are.
Other approaches
Check every pair (time O(n^2), space O(1))
Two nested loops test (time[i] + time[j]) % 60 for every i < j. It is correct but makes about n squared over 2 checks, which is far too slow for 60,000 songs.
Count first, then combine buckets (time O(n), space O(1))
Count every remainder first, then add count[r] * count[60 - r] for r from 1 to 29, plus C(count[0], 2) and C(count[30], 2). It gives the same answer with arithmetic instead of a running sum.
Common mistakes
- Looking up count[60 - r] when r = 0. Index 60 is out of range; the partner of 0 is 0, which is why the formula uses (60 - r) % 60.
- Incrementing count[r] before the lookup. For remainders 0 and 30 that pairs a song with itself.
- Using a 32-bit counter in a language where the pair count could overflow. With at most 60,000 songs the answer is below 1.8 billion, which still fits in a signed 32-bit int.
Frequently asked questions
Why are remainders 0 and 30 special?
They are their own partners: 0 + 0 and 30 + 30 are both multiples of 60. Every other remainder r pairs with a different slot, 60 - r.
Why does the running count avoid double counting?
Each pair is counted exactly once, at the moment its later song is processed, because only earlier songs are in the counts at that point.
How is this related to Two Sum?
Two Sum looks up target - x in a hash map of values seen so far. Here the target is 0 modulo 60, so the lookup key is (60 - r) % 60, and because there are only 60 possible keys a fixed array replaces the hash map.