You are given a list of songs where the ith 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.
Example 1:
Input: time = [30,20,150,100,40] Output: 3 Explanation: Three pairs have a total duration divisible by 60: (time[0] = 30, time[2] = 150): total duration 180 (time[1] = 20, time[3] = 100): total duration 120 (time[1] = 20, time[4] = 40): total duration 60
Example 2:
Input: time = [60,60,60] Output: 3 Explanation: All three pairs have a total duration of 120, which is divisible by 60.
Constraints:
1 <= time.length <= 6 * 1041 <= time[i] <= 500When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a playlist of songs and you want to find every unique pair of songs whose combined length adds up to an exact multiple of sixty seconds. The brute force approach relies on checking every single possible pair of songs to see if their combined duration fits the rule.
Here's how the algorithm would work step-by-step:
def num_pairs_divisible_by_60(song_durations):
total_valid_pairs = 0
# Iterate through each song to establish it as the first element of our pairing
for outer_index in range(len(song_durations)):
# Compare against all subsequent songs to avoid duplicate pair evaluations
for inner_index in range(outer_index + 1, len(song_durations)):
combined_duration = song_durations[outer_index] + song_durations[inner_index]
# Verify if the combined duration is an exact multiple of sixty seconds
if combined_duration % 60 == 0:
total_valid_pairs += 1
return total_valid_pairsInstead of checking every possible pairing of songs, the optimal approach uses a clever counting strategy based on remainders. By figuring out what remainder is needed to make a multiple of sixty for each song, we can instantly find matching partners we have already seen.
Here's how the algorithm would work step-by-step:
def num_pairs_divisible_by_sixty(song_durations): valid_pair_count = 0 remainder_tally_sheet = [0] * 60 for current_duration in song_durations: current_remainder = current_duration % 60 # We need the complementary remainder that sums with the current remainder to reach a multiple of sixty. complementary_remainder = (60 - current_remainder) % 60 valid_pair_count += remainder_tally_sheet[complementary_remainder] # Record the current song's remainder onto the tally sheet so future songs can match with it. remainder_tally_sheet[current_remainder] += 1 return valid_pair_count| Case | How to Handle |
|---|---|
| Empty array or array with only one element | Return zero immediately since at least two elements are required to form a pair. |
| Durations that are exact multiples of 60 | Map lookups must use the remainder modulo 60, meaning a song of length 60 pairs with another song of length 60 because 0 plus 0 is divisible by 60. |
| Complement remainder is 0 | When the current song has a remainder of 0, we must look for previously seen songs with a remainder of 0 to form a valid sum divisible by 60. |
| Array containing all identical song durations | The frequency array or hash map correctly accumulates counts so that every prior occurrence pairs with the current element. |
| Very large dataset with maximum constraints | A single-pass frequency array approach runs in O(N) time complexity, efficiently scaling without exceeding time limits. |
| No valid pairs exist in the array | The running count remains zero and the function correctly returns zero at the end of execution. |
| Integer overflow during pair counting | Using a 64-bit integer for the total pair count prevents arithmetic overflow when the input size is maximal and all pairs are valid. |
| Durations greater than 60 | Taking the modulo 60 of each duration normalizes the values into the correct remainder bucket from 0 to 59. |