category-wise-problems

contains category wise problems(data structures, competitive) of popular platforms.

View the Project on GitHub mayankdutta/category-wise-problems

1010. Pairs of Songs With Total Durations Divisible by 60, Medium

code ```cpp int numPairsDivisibleBy60(vector& time) { map<int, int> mp; for (const auto& i: time) mp[i % 60] ++; int ans = (mp[0] * (mp[0] - 1))/2; for (auto &[key, value]: mp) { auto it = mp.find(60 - key); if (it != mp.end()) { (it -> first == key) ? ans += ((value * (value - 1)) / 2) : ans += (it -> second * value); int mn = min(it -> second, value); value -= mn; it -> second -= mn; } } return ans; } ``` </details> - since we are taking mod by 60. - we know that resulting values won't exceed from 30. - therefore can carry out some space optimizations.
optimal code ```cpp class Solution { vector freq = vector(60, 0); public: int numPairsDivisibleBy60(vector& time) { for(int i=0; i<time.size(); i++) { freq[time[i]%60]++; } int count=0; for(int i=0; i<=30; i++) { if((i== 0 || i==30 )) { if(freq[i]>1) count += freq[i] * (freq[i]-1) * 1/2; continue; } int rem = 60 - i; count+= freq[rem] * freq[i]; } return count; } }; ``` </details>