Untitled
Anonymous
java
03/20/2026 5:49 AM
1.2 KB
12
Indexable
import java.util.*;
public class Main {
static class Solution {
public int countPairs(int[] deliciousness) {
final int MOD = 1_000_000_007;
Map<Integer, Integer> freq = new HashMap<>();
long ans = 0;
// 2^21 = 2097152, safe enough for common constraints
for (int val : deliciousness) {
int power = 1;
for (int i = 0; i <= 21; i++) {
int needed = power - val;
ans = (ans + freq.getOrDefault(needed, 0)) % MOD;
power <<= 1;
}
freq.put(val, freq.getOrDefault(val, 0) + 1);
}
return (int) ans;
}
}
public static void main(String[] args) {
Solution sol = new Solution();
int[] deliciousness = {1, 3, 5, 7, 9};
System.out.println(sol.countPairs(deliciousness)); // Output: 4
}
}Editor is loading...
Leave a Comment