Pairs of Songs With Total Durations Divisible by 60
Pairs of Songs With Total Durations Divisible by 60 is a medium arrays & hashing problem solved with the two sum pattern.
The best approach, optimal (one pass, count the complements), runs in O(n) time and O(1) space.
Below are 3 approaches in Java, Python, C++ and JavaScript, from brute force (check every pair) up.
Problem
You are given an array time, where time[i] is the length of the i-th song in seconds.
Count the pairs of songs whose total length is a whole number of minutes: the number of index pairs (i, j) with i < j such that (time[i] + time[j]) % 60 == 0.
Return that count.
Examples
Example 1
- Input
time = [20, 40, 100, 80, 60]- Output
4- Why
- The pairs are 20 + 40 = 60, 20 + 100 = 120, 40 + 80 = 120 and 100 + 80 = 180. The song of 60 seconds has no partner, because no other song is a multiple of 60.
Example 2
- Input
time = [60, 60, 60]- Output
3- Why
- Any two of the three songs add up to 120, and there are 3 ways to choose two of them.
Example 3
- Input
time = [10, 25, 45]- Output
0- Why
- The sums are 35, 55 and 70, and none of them is a multiple of 60.
Constraints
1 <= time.length <= 6 * 10^41 <= time[i] <= 500
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (3 examples, or your own). Submit also runs 13 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Brute force (check every pair) | O(n²) | O(1) |
| Remainder buckets (combine the counts) | O(n) | O(1) |
| Optimal (one pass, count the complements) | O(n) | O(1) |
1Brute force (check every pair)
O(n²)There are n * (n - 1) / 2 pairs, each tested in O(1).O(1)Only the counter.Try every pair of positions i < j and test whether the two lengths add up to a multiple of 60.
- Set pairs = 0.
- For every i < j: if (time[i] + time[j]) % 60 == 0, add 1.
- Return pairs.
class Solution {
public int numPairsDivisibleBy60(int[] time) {
int n = time.length, pairs = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if ((time[i] + time[j]) % 60 == 0) pairs++;
}
}
return pairs;
}
}class Solution:
def numPairsDivisibleBy60(self, time: List[int]) -> int:
n = len(time)
pairs = 0
for i in range(n):
for j in range(i + 1, n):
if (time[i] + time[j]) % 60 == 0:
pairs += 1
return pairsclass Solution {
public:
int numPairsDivisibleBy60(vector<int>& time) {
int n = time.size(), pairs = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if ((time[i] + time[j]) % 60 == 0) pairs++;
}
}
return pairs;
}
};function numPairsDivisibleBy60(time) {
const n = time.length;
let pairs = 0;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if ((time[i] + time[j]) % 60 === 0) pairs++;
}
}
return pairs;
}2Remainder buckets (combine the counts)
O(n)One pass fills the buckets, then a fixed 30 steps combine them.O(1)60 counters, no matter how large n is.Put every song into one of 60 buckets by its remainder modulo 60. A pair works exactly when its two remainders add up to 0 or 60. So bucket r pairs with bucket 60 - r for r from 1 to 29, giving count[r] * count[60 - r] pairs. Buckets 0 and 30 pair with themselves, giving c * (c - 1) / 2 pairs each. Use a 64-bit type for the products, because c * (c - 1) can pass the int range before it is halved.
- Build count[r] = how many songs have time % 60 == r.
- pairs = C(count[0], 2) + C(count[30], 2), where C(c, 2) = c * (c - 1) / 2.
- For r from 1 to 29, add count[r] * count[60 - r].
- Return pairs.
class Solution {
public int numPairsDivisibleBy60(int[] time) {
long[] count = new long[60];
for (int t : time) count[t % 60]++;
long pairs = count[0] * (count[0] - 1) / 2 + count[30] * (count[30] - 1) / 2;
for (int r = 1; r < 30; r++) pairs += count[r] * count[60 - r];
return (int) pairs;
}
}class Solution:
def numPairsDivisibleBy60(self, time: List[int]) -> int:
count = [0] * 60
for t in time:
count[t % 60] += 1
pairs = count[0] * (count[0] - 1) // 2 + count[30] * (count[30] - 1) // 2
for r in range(1, 30):
pairs += count[r] * count[60 - r]
return pairsclass Solution {
public:
int numPairsDivisibleBy60(vector<int>& time) {
vector<long long> count(60, 0);
for (int t : time) count[t % 60]++;
long long pairs = count[0] * (count[0] - 1) / 2 + count[30] * (count[30] - 1) / 2;
for (int r = 1; r < 30; r++) pairs += count[r] * count[60 - r];
return (int) pairs;
}
};function numPairsDivisibleBy60(time) {
const count = new Array(60).fill(0);
for (const t of time) count[t % 60]++;
let pairs = count[0] * (count[0] - 1) / 2 + count[30] * (count[30] - 1) / 2;
for (let r = 1; r < 30; r++) pairs += count[r] * count[60 - r];
return pairs;
}3Optimal (one pass, count the complements)
O(n)One pass with O(1) work per song.O(1)60 counters, no matter how large n is.This is two-sum with remainders. Scan the songs from left to right and keep seen[r], the number of earlier songs with remainder r. A song with remainder r completes a pair with every earlier song whose remainder is (60 - r) % 60, so add seen[(60 - r) % 60], then record the song in seen[r]. The extra % 60 sends r = 0 to bucket 0 instead of 60. Each pair is counted once, at its later song.
- Create seen[0..59] = 0 and pairs = 0.
- For each t: r = t % 60.
- Add seen[(60 - r) % 60] to pairs: those earlier songs are partners.
- Then increase seen[r].
- Return pairs.
class Solution {
public int numPairsDivisibleBy60(int[] time) {
int[] seen = new int[60];
int pairs = 0;
for (int t : time) {
int r = t % 60;
pairs += seen[(60 - r) % 60];
seen[r]++;
}
return pairs;
}
}class Solution:
def numPairsDivisibleBy60(self, time: List[int]) -> int:
seen = [0] * 60
pairs = 0
for t in time:
r = t % 60
pairs += seen[(60 - r) % 60]
seen[r] += 1
return pairsclass Solution {
public:
int numPairsDivisibleBy60(vector<int>& time) {
vector<int> seen(60, 0);
int pairs = 0;
for (int t : time) {
int r = t % 60;
pairs += seen[(60 - r) % 60];
seen[r]++;
}
return pairs;
}
};function numPairsDivisibleBy60(time) {
const seen = new Array(60).fill(0);
let pairs = 0;
for (const t of time) {
const r = t % 60;
pairs += seen[(60 - r) % 60];
seen[r]++;
}
return pairs;
}Edge cases to test
- A single song: no pairs, the answer is 0
- Remainder 0 (60, 120, ...) pairs only with another remainder 0
- Remainder 30 (30, 90, 150, ...) pairs only with another remainder 30
- All songs are multiples of 60: every pair counts, n * (n - 1) / 2 pairs, almost 1.8 * 10^9 for n = 6 * 10^4
- count * (count - 1) can pass the int range even though the final answer fits, so multiply in a 64-bit type
- Songs longer than 60 seconds: only t % 60 matters, so 100 pairs with 20 or 80
Hints
Hint 1
Only the remainder of each length modulo 60 matters: (a + b) % 60 == 0 exactly when (a % 60 + b % 60) % 60 == 0.
Hint 2
For a remainder r, the partner needs remainder (60 - r) % 60. Note r = 0 and r = 30 are their own partners.
Hint 3
Scan left to right and keep how many songs you have seen with each remainder. For a song with remainder r, all earlier songs with remainder (60 - r) % 60 pair with it. This is two-sum with 60 buckets.
FAQ
What is the best time complexity for Pairs of Songs With Total Durations Divisible by 60?
Optimal (one pass, count the complements) runs in O(n) time and O(1) extra space. One pass with O(1) work per song.
Which pattern does Pairs of Songs With Total Durations Divisible by 60 use?
It is a arrays & hashing problem that uses the two sum pattern. Other problems with the same pattern: Two Sum, 3Sum, Count Good Meals.
Is there a brute force solution for Pairs of Songs With Total Durations Divisible by 60?
Yes. Brute force (check every pair) takes O(n²) time and O(1) space. Try every pair of positions i < j and test whether the two lengths add up to a multiple of 60.
Which edge cases should I test for Pairs of Songs With Total Durations Divisible by 60?
A single song: no pairs, the answer is 0; Remainder 0 (60, 120, ...) pairs only with another remainder 0; Remainder 30 (30, 90, 150, ...) pairs only with another remainder 30; All songs are multiples of 60: every pair counts, n (n - 1) / 2 pairs, almost 1.8 10^9 for n = 6 10^4; count (count - 1) can pass the int range even though the final answer fits, so multiply in a 64-bit type; Songs longer than 60 seconds: only t % 60 matters, so 100 pairs with 20 or 80.