The Pairs of Songs With Total Durations Divisible by 60 problem asks you to count pairs (i, j) where i < j and (time[i] + time[j]) % 60 == 0. This is the modular complement pair counting problem. The array, hash table, and counting interview pattern is demonstrated.
Apple, Goldman Sachs, Microsoft, Amazon, Google, and many others ask this because it's a direct application of the "two sum with modular arithmetic" pattern. Count durations modulo 60 and look for complementary remainders. It tests whether candidates can adapt two-sum to modular arithmetic.
Modular complement counting. For each song time t: compute r = t % 60. The complement needed = (60 - r) % 60. Count how many previous songs had remainder (60-r)%60. Add to result. Update the frequency array at index r.
times=[30,20,150,100,40]. Mod 60: [30,20,30,40,40].
(60-r)%60 instead of just 60-r (forgetting to handle r=0 case where complement=60, not 0).r+complement=60 instead of (r+complement)%60==0.Modular complement pair problems are extensions of two-sum to modular arithmetic. The pattern: for each element with remainder r, its complement has remainder (m-r)%m. A frequency array of size m gives O(1) lookup. Practice this for different moduli: "pairs summing to divisible by 7," "pairs of strings with length divisible by k." The (m-r)%m formula handles the r=0 case cleanly.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Tuple with Same Product | Medium | Solve | |
| Check If Array Pairs Are Divisible by k | Medium | Solve | |
| Find All Lonely Numbers in the Array | Medium | Solve | |
| Count Pairs That Form a Complete Day II | Medium | Solve | |
| Longest Common Subsequence Between Sorted Arrays | Medium | Solve |