Complete-Preparation

🎉 One-stop destination for all your technical interview Preparation 🎉

View the Project on GitHub

1010. Pairs of Songs With Total Durations Divisible by 60 🌟🌟

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.

Brute force (TLE)

Hashing Solution

Code

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;
    }
};