Count Good Meals - Problem

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 i-th item of food, return the number of different good meals you can make from this list modulo 10^9 + 7.

Note: Items with different indices are considered different even if they have the same deliciousness value.

Input & Output

Example 1 — Basic Case
$ Input: deliciousness = [1,3,5,7,9]
Output: 4
💡 Note: Good meals are: (1,3)→4, (1,7)→8, (3,5)→8, (7,9)→16. All sums are powers of 2.
Example 2 — Duplicates
$ Input: deliciousness = [1,1,1,3,3,3,7]
Output: 15
💡 Note: Multiple pairs can form same sum: 1+3=4 (9 combinations), 1+7=8 (3 combinations), 3+7=10 is not power of 2. Also 3+3=6 is not valid, but we need to check all pairs carefully.
Example 3 — No Valid Pairs
$ Input: deliciousness = [2,5,11]
Output: 0
💡 Note: Check all pairs: 2+5=7 (not power of 2), 2+11=13 (not power of 2), 5+11=16 (power of 2). So result is 1, not 0. Actually (5,11)→16 is valid.

Constraints

  • 1 ≤ deliciousness.length ≤ 2 × 104
  • 0 ≤ deliciousness[i] ≤ 220

Visualization

Tap to expand
Count Good Meals: Find Pairs with Power-of-2 Sums13579Powers of 2: 1, 2, 4, 8, 16, 32, 64...Valid Pairs(1,3) → 4 = 2²(1,7) → 8 = 2³(3,5) → 8 = 2³More Valid(7,9) → 16 = 2⁴Result: 4 good meals found
Understanding the Visualization
1
Input Array
Array of deliciousness values [1,3,5,7,9]
2
Find Pairs
Check all pairs for power-of-2 sums
3
Count Result
Return count of valid good meals
Key Takeaway
🎯 Key Insight: Only ~22 power-of-2 targets exist within constraints, so we can efficiently check complements using a hash map
Asked in
Facebook 35 Google 28 Amazon 22
28.0K Views
Medium Frequency
~25 min Avg. Time
895 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen