Mid/SeniorArrayHash Table

Count Good Meals

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 that items with different indices are considered different even if they have the same deliciousness value.

Example 1
Inputdeliciousness = [1,3,5,7,9]
Output4
The good meals are (1,3), (1,7), (3,5), and (7,9), whose sums are 4, 8, 8, and 16, all powers of 2.
Example 2
Inputdeliciousness = [1,1,1,3,3,3,7]
Output15
The good meals are (1,1) with 3 ways, (1,3) with 9 ways, and (1,7) with 3 ways.

Constraints

  • 1 <= deliciousness.length <= 10^5
  • 0 <= deliciousness[i] <= 2^20

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate