
Problem Statement
The challenge is to find all pairs of songs from a given list such that their combined duration in seconds is divisible by 60. Specifically, for a list of song durations presented as integers in an array, we need to identify all unique pairs (i, j)
where the index i
is less than index j
, and the sum of durations at indices i
and j
(time[i] + time[j]
) is a multiple of 60.
This type of computation may find utility in determining which pairs of songs align harmoniously with a certain rhythmic cycle, such as a minute-long loop or sequence.
Examples
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
Approach and Intuition
To tackle the problem efficiently, we can leverage the properties of modular arithmetic to simplify the search. Here's a step-by-step breakdown of the intuition and approach:
Understanding that for any two numbers
a
andb
,(a + b) % 60 == 0
is true if and only if(a % 60 + b % 60) % 60 == 0
. This means we only need to consider the remainder when each song duration is divided by 60.Use an array to keep track of how many songs have each possible remainder when divided by 60. This array,
count
, will have a size of 60 (as the remainders can range from 0 to 59).For each song duration
t
in the list:- First, determine the remainder when
t
is divided by 60:remainder = t % 60
. - The complement that, when added to
remainder
, results in a number divisible by 60 is(60 - remainder) % 60
. - Check the
count
array for how many previous song durations have left the required complement remainder. This gives the number of new pairs formed with the current song durationt
. - Update the
count[remainder]
to include the current song duration.
- First, determine the remainder when
Accumulate the count of valid pairs as you iterate through the list. This approach ensures each pair is counted once and is efficient in managing the pairs' counts.
Examples Interpreted:
Example 1:
- With times of
30, 20, 150, 100, 40
, observe that:30 + 150 = 180
(remainder0
after dividing by60
)20 + 100 = 120
(remainder0
after dividing by60
)20 + 40 = 60
(remainder0
after dividing by60
)
- With times of
Example 2:
- With repeated durations of
60
, all pairs formed will always sum to multiples of 60 (since60 % 60 = 0
).
- With repeated durations of
By following the above method, you can efficiently calculate the desired number of song pairs whose durations sum to multiples of 60, using limited space and in a significantly reduced time span compared to checking all possible pairs directly.
Solutions
- Java
class Solution {
public int countMusicPairs(int[] durations) {
int mods[] = new int[60];
int pairs = 0;
for (int duration: durations) {
int mod = duration % 60;
if (mod == 0) {
pairs += mods[0];
} else {
pairs += mods[60 - mod];
}
mods[mod]++;
}
return pairs;
}
}
Solve the problem of finding pairs of songs where the total duration of each pair is divisible by 60 using an efficient Java approach. By analyzing each song's duration and leveraging modulo arithmetic, this method efficiently identifies and counts these pairs with minimized computational complexity:
- First, declare an array
mods
to hold the count of song durations modulo 60. - Initialize a counter
pairs
to accumulate the number of qualifying song pairs. - Loop through each song duration in the array:
- Calculate the remainder of the duration when divided by 60.
- If the remainder is 0 (meaning the duration is already divisible by 60), increase the
pairs
count by the number of songs previously encountered with a remainder of 0. - Otherwise, add to
pairs
the count of previously encountered songs that, when added to the current song, result in a sum divisible by 60. - Update the
mods
array by incrementing the counter at the index corresponding to the current song's remainder.
- The function then returns the total count of qualifying pairs found.
This method provides a focused and streamlined way to calculate pairs without the need for nested loops, thereby improving performance for large datasets. By using the properties of modulo, it efficiently associates songs that can form a pair with others, making the algorithm both intuitive and optimal.
- Python
class Solution:
def countPairsDivisibleBy60(self, time: List[int]) -> int:
remainder_count = collections.defaultdict(int)
count_pairs = 0
for t in time:
if t % 60 == 0:
count_pairs += remainder_count[0]
else:
count_pairs += remainder_count[60 - t % 60]
remainder_count[t % 60] += 1
return count_pairs
You will solve the problem of counting pairs of songs where the total duration of each pair is divisible by 60, effectively managing issues related to handling large lists and modular arithmetic. The Python code defines a function countPairsDivisibleBy60
, where an array time
representing song durations in seconds serves as the input.
- Initialize a dictionary
remainder_count
usingcollections.defaultdict
to store the frequency of each remainder when song durations are divided by 60. - Set
count_pairs
to 0 to keep track of the number of valid pairs.
Iterate over each duration t
in the time
list:
- If
t % 60 == 0
: This checks if the durationt
itself is divisible by 60. If true, addremainder_count[0]
tocount_pairs
because adding a duration that leaves no remainder with another that also leaves no remainder results in a total duration divisible by 60. - Otherwise, add the count of durations that would complement
t
to form a divisible by 60 sum tocount_pairs
. This is achieved byremainder_count[60 - t % 60]
. - Update the remainder count by incrementing
remainder_count[t % 60]
by one.
Finally, return count_pairs
as the result. This approach ensures efficient matching by leveraging the properties of mod operation and taking advantage of hash table operations, which are average O(1) in time complexity. Thus, the overall code runs in O(n) time, where n is the number of items in the time
list.
No comments yet.