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 <= 105
0 <= deliciousness[i] <= 220
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force way to count good meals is to try every possible pair of food items. We check if each pair sums up to a power of two. If it does, we count it as a good meal.
Here's how the algorithm would work step-by-step:
def count_good_meals_brute_force(food_items):
number_of_food_items = len(food_items)
good_meal_count = 0
for first_food_index in range(number_of_food_items):
for second_food_index in range(number_of_food_items):
# Calculate the sum of the two food items.
food_sum = food_items[first_food_index] + food_items[second_food_index]
power_of_two = 1
# We must check if food_sum is a power of 2.
while power_of_two < food_sum:
power_of_two *= 2
if power_of_two == food_sum:
#We found a good meal; increment the count
good_meal_count += 1
# Divide by two as each meal is counted twice
return good_meal_count // 2
The goal is to efficiently find pairs of numbers that add up to a power of two. Instead of checking every single pair, we use a technique that dramatically reduces the number of checks needed. We use a way to quickly count how many numbers would complete a power of two when paired with each number in the list.
Here's how the algorithm would work step-by-step:
def count_good_meals(deliciousness):
number_of_meals = 0
deliciousness_counts = {}
modulo_value = 10**9 + 7
for deliciousness_value in deliciousness:
deliciousness_counts[deliciousness_value] = deliciousness_counts.get(deliciousness_value, 0) + 1
for deliciousness_value in deliciousness:
power_of_two = 1
#Powers of two must be up to at least twice the maximum deliciousness value.
for _ in range(22):
complement = power_of_two - deliciousness_value
if complement in deliciousness_counts:
#Avoid double counting pairs with the same value
if complement == deliciousness_value:
number_of_meals = (number_of_meals + deliciousness_counts[complement] - 1) % modulo_value
else:
number_of_meals = (number_of_meals + deliciousness_counts[complement]) % modulo_value
power_of_two *= 2
# Divide result by two because each pair was counted twice
return (number_of_meals // 2) % modulo_value
Case | How to Handle |
---|---|
Null or empty input array | Return 0 if the input array is null or empty because there are no meals to form pairs. |
Input array with only one element | Return 0 since we need at least two meals to form a pair. |
Maximum sized input array (e.g., n = 10^5) | The solution should use a time complexity that allows processing of large arrays, such as O(n) or O(n log n), avoiding O(n^2). |
Input array with all identical deliciousness values | The hash map must correctly count pairs even when all values are same. |
Input array contains very large integers (close to the maximum integer value) | Ensure that the sum of two integers does not cause an integer overflow, potentially using a larger data type (long). |
No good meals exist in the input array | The solution should correctly return 0 when no pair sums to a power of two. |
Multiple pairs can sum to the same power of two | The solution needs to count all such pairs accurately. |
Potential Integer Overflow when summing pairs | Use modulo operator (%) to avoid integer overflow when accumulating the count of good meals. |