forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1711.java
29 lines (25 loc) · 1003 Bytes
/
_1711.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.Map;
public class _1711 {
public static class Solution1 {
private static final long MODUALR = 1000000007;
public int countPairs(int[] deliciousness) {
Map<Integer, Integer> map = new HashMap<>();
long pairs = 0;
for (int i = 0; i < deliciousness.length; i++) {
int power = 1;
//we only need to go up to 21 since one of the constraints is: 0 <= deliciousness[i] <= 2 to the power of 20
for (int j = 0; j < 22; j++) {
if (map.containsKey(power - deliciousness[i])) {
pairs += map.get(power - deliciousness[i]);
pairs %= MODUALR;
}
power *= 2;
}
map.put(deliciousness[i], map.getOrDefault(deliciousness[i], 0) + 1);
}
return (int) pairs;
}
}
}