Count Array Pairs Divisible by K LeetCode solution in C++
1typedef long long ll;23class Solution {4public:5long long countPairs(vector<int> &nums, int k) {6unordered_map<ll, ll> gcdCount;7ll ans = 0;8for (ll i = 0; i < nums.size(); ++i) {9ll currgcd = __gcd(nums[i], k);10for (auto &[gc_d, count] : gcdCount)11if ((currgcd * gc_d) % k == 0) ans += count;12gcdCount[currgcd]++;13}14return ans;15}16};