Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
A good meal is a meal that contains exactly two different food items with a sum of deliciousness equal to a power of two.
You can pick any two different foods to make a good meal.
Given an array of integers deliciousness where deliciousness[i] is the deliciousness of the ith item of food, return the number of different good meals you can make from this list modulo 109 + 7.
Note that items with different indices are considered different even if they have the same deliciousness value.
Example 1:
Input: deliciousness = [1,3,5,7,9] Output: 4 Explanation: The good meals are (1,3), (1,7), (3,5) and, (7,9). Their respective sums are 4, 8, 8, and 16, all of which are powers of 2.
Example 2:
Input: deliciousness = [1,1,1,3,3,3,7] Output: 15 Explanation: The good meals are (1,1) with 3 ways, (1,3) with 9 ways, and (1,7) with 3 ways.
Constraints:
1 <= deliciousness.length <= 1050 <= deliciousness[i] <= 220Problem summary: A good meal is a meal that contains exactly two different food items with a sum of deliciousness equal to a power of two. You can pick any two different foods to make a good meal. Given an array of integers deliciousness where deliciousness[i] is the deliciousness of the ith item of food, return the number of different good meals you can make from this list modulo 109 + 7. Note that items with different indices are considered different even if they have the same deliciousness value.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,3,5,7,9]
[1,1,1,3,3,3,7]
two-sum)max-number-of-k-sum-pairs)find-all-possible-recipes-from-given-supplies)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1711: Count Good Meals
class Solution {
private static final int MOD = (int) 1e9 + 7;
public int countPairs(int[] deliciousness) {
int mx = Arrays.stream(deliciousness).max().getAsInt() << 1;
int ans = 0;
Map<Integer, Integer> cnt = new HashMap<>();
for (int d : deliciousness) {
for (int s = 1; s <= mx; s <<= 1) {
ans = (ans + cnt.getOrDefault(s - d, 0)) % MOD;
}
cnt.merge(d, 1, Integer::sum);
}
return ans;
}
}
// Accepted solution for LeetCode #1711: Count Good Meals
func countPairs(deliciousness []int) (ans int) {
mx := slices.Max(deliciousness) << 1
const mod int = 1e9 + 7
cnt := map[int]int{}
for _, d := range deliciousness {
for s := 1; s <= mx; s <<= 1 {
ans = (ans + cnt[s-d]) % mod
}
cnt[d]++
}
return
}
# Accepted solution for LeetCode #1711: Count Good Meals
class Solution:
def countPairs(self, deliciousness: List[int]) -> int:
mod = 10**9 + 7
mx = max(deliciousness) << 1
cnt = Counter()
ans = 0
for d in deliciousness:
s = 1
while s <= mx:
ans = (ans + cnt[s - d]) % mod
s <<= 1
cnt[d] += 1
return ans
// Accepted solution for LeetCode #1711: Count Good Meals
struct Solution;
use std::collections::HashMap;
const MOD: i64 = 1_000_000_007;
impl Solution {
fn count_pairs(deliciousness: Vec<i32>) -> i32 {
let mut hm: HashMap<i32, i64> = HashMap::new();
let mut res = 0;
for x in deliciousness {
for i in 0..22 {
let sum = 1 << i;
let y = sum - x;
if let Some(k) = hm.get(&y) {
res += k;
res %= MOD;
}
}
*hm.entry(x).or_default() += 1;
}
res as i32
}
}
#[test]
fn test() {
let deliciousness = vec![1, 3, 5, 7, 9];
let res = 4;
assert_eq!(Solution::count_pairs(deliciousness), res);
let deliciousness = vec![1, 1, 1, 3, 3, 3, 7];
let res = 15;
assert_eq!(Solution::count_pairs(deliciousness), res);
let deliciousness = vec![
149, 107, 1, 63, 0, 1, 6867, 1325, 5611, 2581, 39, 89, 46, 18, 12, 20, 22, 234,
];
let res = 12;
assert_eq!(Solution::count_pairs(deliciousness), res);
}
// Accepted solution for LeetCode #1711: Count Good Meals
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1711: Count Good Meals
// class Solution {
// private static final int MOD = (int) 1e9 + 7;
//
// public int countPairs(int[] deliciousness) {
// int mx = Arrays.stream(deliciousness).max().getAsInt() << 1;
// int ans = 0;
// Map<Integer, Integer> cnt = new HashMap<>();
// for (int d : deliciousness) {
// for (int s = 1; s <= mx; s <<= 1) {
// ans = (ans + cnt.getOrDefault(s - d, 0)) % MOD;
// }
// cnt.merge(d, 1, Integer::sum);
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.