C++ auto

--

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

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response