C++ auto
Jan 3, 2021
Auto used for map
1711. Count Good Meals
const int MOD = 1e9 + 7;
class Solution {
public:
int countPairs(vector<int>& a) {
map<int, int> cnt;
for (auto &x : a) cnt[x]++;
int k2;
int ret = 0;
for (auto &[k1, v] : cnt) {
for (int sum = 1; sum <= (1 << 21); sum <<= 1) {
k2 = sum - k1;
if (k2 < k1) continue;
if (cnt.find(k2) == cnt.end()) continue;
if (k1 == k2) {
ret += (1ll * v * (v - 1) / 2) % MOD;
} else {
ret += v * cnt[k2] % MOD;
}
if (ret >= MOD) ret %= MOD;
}
}
return ret;
}
};