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 * 104
1 <= time[i] <= 500
When 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:
The brute force strategy for this problem involves checking every possible pair of songs to see if their combined duration is divisible by 60. It's like trying every handshake at a party and noting the ones that happen at a multiple of 60 seconds. We will go through each possible pairing to test this divisibility.
Here's how the algorithm would work step-by-step:
def get_number_of_pairs_brute_force(song_durations):
number_of_pairs = 0
# Iterate through each song duration
for first_song_index in range(len(song_durations)):
# Pair the current song with every other song after it
for second_song_index in range(first_song_index + 1, len(song_durations)):
sum_of_durations = song_durations[first_song_index] + song_durations[second_song_index]
# Check if the sum is divisible by 60
if sum_of_durations % 60 == 0:
# Increment the number of pairs if divisible by 60
number_of_pairs += 1
return number_of_pairs
The key is to avoid checking every possible pair of songs. Instead, we focus on how much each song's duration is short of being a multiple of 60, and count how many songs can complement it to reach that multiple.
Here's how the algorithm would work step-by-step:
def get_number_of_pairs(song_durations):
number_of_songs = len(song_durations)
count_of_pairs = 0
needed_times = [0] * 60
for song_duration in song_durations:
needed_time = song_duration % 60
needed_times[needed_time] += 1
for song_duration in song_durations:
needed_time = song_duration % 60
complement_time = (60 - needed_time) % 60
# Count songs that can complete the current song.
count_of_pairs += needed_times[complement_time]
# Adjust for self-pairing or double counting.
if needed_time == complement_time:
count_of_pairs -= 1
# Divide the total count by 2 to avoid double-counting pairs
return count_of_pairs // 2
Case | How to Handle |
---|---|
Empty or null input array | Return 0 since no pairs are possible with an empty array. |
Array with a single element | Return 0 since a pair requires at least two elements. |
Array with all elements being multiples of 60 (e.g., all 60, 120, 180) | The algorithm should correctly count all possible pairs in this scenario using combinations. |
Array with no pairs that sum to a multiple of 60 | The algorithm should return 0 when no valid pairs exist. |
Large input array with durations close to the maximum integer value | Ensure the intermediate sum of durations does not cause integer overflow. |
Input array contains zero values | Zero values should be handled correctly when paired with multiples of 60. |
Very large array (e.g., 10^5 elements) to test for time complexity | Solution needs to be optimized for O(n) time complexity, avoiding brute-force approaches. |
Array with a mix of durations, some near 0 and others near MAX_INT | Algorithm should correctly handle pairs involving both extremely small and large values. |