🎉 One-stop destination for all your technical interview Preparation 🎉
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
.
60
t % 60
gets the remainder from 0 to 59
.mp
.t
in time
, how many x
satisfy (t + x) % 60 = 0
.x % 60 = 60 - (t % 60)
, which is valid for the most cases.t % 60 = 0
then x % 60
should be 0
instead of 60
.x % 60 = (60 - t % 60) % 60
,x % 60 = (600 - t) % 60
.class Solution {
public:
int numPairsDivisibleBy60(vector<int>& time)
{
vector<int> mp(60);
int n = time.size(), ans = 0;
for (int i = 0; i < n; i++) {
int rem = time[i] % 60;
ans += mp[(60 - rem) % 60];
mp[rem]++;
}
return ans;
}
};