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