Count Good Meals LeetCode solution in Java

1
class Solution {
2
int mod = 1000000007;
3

4
public int countPairs(int[] arr) {
5
Map<Integer, Integer> map = new HashMap<>();
6
int n = arr.length;
7
long res = 0;
8
for (int num : arr) {
9
int power = 1;
10
for (int i = 0; i < 22; i++) {
11
if (map.containsKey(power - num)) {
12
res += map.get(power - num);
13
res %= mod;
14
}
15
power *= 2;
16
}
17
map.put(num, map.getOrDefault(num, 0) + 1);
18
}
19
return (int) res;
20
}
21
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0