1010. Pairs of Songs With Total Durations Divisible by 60
Asked at Atlassian
Difficulty
Medium
Time
O(n)
Space
O(1)
Pattern
Array
Companies
1
Problem
You are given an array time, where time[i] is the length in seconds of the i-th song. Count how many pairs of songs (i, j) have i < j and a total length that is a multiple of 60.
A pair is just two different songs, and the order does not matter: the pair (i, j) with i < j is counted once. Two songs with the same length are still different songs, so they can form a pair.
Return the number of such pairs. The array can be large, so you need something faster than checking every pair by hand.
Which pairs sum to a multiple of 60
Constraints
- ●
1 <= time.length <= 6 * 10^4 - ●
1 <= time[i] <= 500
Examples
time = [30,20,150,100,40]3time = [60,60,60]3Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Atlassian | Medium | View all Atlassian questions → |
How to Think About It
Compute remainder of each duration when divided by 60
Use hash map to count occurrences of each remainder
For remainder r, find complement (60 - r) % 60 in the map
Handle special case where remainder is 0 (pairs with other 0s)
Count pairs incrementally while iterating for O(n) solution
Optimal Approach
Use modular arithmetic to track remainders when dividing durations by 60. Maintain a count of each remainder seen so far. For each new duration with remainder r, the complement is (60 - r) % 60. Add the count of that complement to the result. Increment the count of the current remainder. This yields O(n) time with O(1) space since remainders range from 0 to 59.
Count remainders, not pairs
What Trips People Up in Real Interviews
Clarify that remainder 0 pairs with remainder 0, not 60
Explain why (60 - r) % 60 handles both r=0 and r=30 cases
Discuss the O(n) time and O(1) space solution using counting
Mention edge case: all songs exactly 60 minutes long
Consider how to extend to divisibility by arbitrary number
Solution Code
O(n) timedef numPairsDivisibleBy60(time):
count = [0] * 60
result = 0
for t in time:
r = t % 60
complement = (60 - r) % 60
result += count[complement]
count[r] += 1
return resultFrequently Asked Questions
What is the Pairs of Songs With Total Durations Divisible by 60 problem?
Given a list of song durations, find how many pairs of songs have total duration divisible by 60. Each pair (i, j) where i < j counts once. The solution must handle large inputs efficiently using modular arithmetic.
How do you solve Pairs of Songs With Total Durations Divisible by 60?
The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Pairs of Songs With Total Durations Divisible by 60?
Pairs of Songs With Total Durations Divisible by 60 is asked at Atlassian. It is a medium difficulty problem.
What are common mistakes on Pairs of Songs With Total Durations Divisible by 60?
- Clarify that remainder 0 pairs with remainder 0, not 60
- Explain why (60 - r) % 60 handles both r=0 and r=30 cases
- Discuss the O(n) time and O(1) space solution using counting
- Mention edge case: all songs exactly 60 minutes long
- Consider how to extend to divisibility by arbitrary number