# Solving Leetcode Interviews in Seconds with AI: Count Good Meals


	# Introduction
	In this blog post, we will explore how to solve the LeetCode problem "1711" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like [Chatmagic](https://www.chatmagic.app), we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

	# Problem Statement
	> 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 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  

	# Explanation
	Here's a solution to the problem, along with explanations:

*   **Key Idea:** The core idea is to iterate through the `deliciousness` array and, for each number, check how many other numbers exist in the array that, when added to the current number, result in a power of 2. To efficiently count these other numbers, we use a hash map (dictionary) to store the frequency of each deliciousness value.

*   **Optimization:** We precompute all powers of 2 within the relevant range (up to 2\*2\*\*20 because each `deliciousness[i]` is <= 2\*\*20). This avoids repeated calculation of powers of 2 within the main loop.

*   **Counting Pairs:** When iterating through the `deliciousness` array, we look for the 'complement' needed to form a power of 2. We handle the case where the two numbers in the pair are the same carefully to avoid overcounting.

*   **Runtime Complexity:** O(N), where N is the length of the `deliciousness` array.
*   **Storage Complexity:** O(N), primarily due to the `deliciousness_counts` dictionary which in the worst case can store each unique value in `deliciousness`.

	
	# Code
	```python
	def count_good_meals(deliciousness):
    """
    Counts the number of good meals (pairs summing to a power of 2).

    Args:
        deliciousness: A list of integers representing the deliciousness of each item.

    Returns:
        The number of good meals modulo 10^9 + 7.
    """

    MOD = 10**9 + 7
    deliciousness_counts = {}
    for d in deliciousness:
        deliciousness_counts[d] = deliciousness_counts.get(d, 0) + 1

    powers_of_two = set()
    for i in range(22):  # Up to 2**21 because max sum is 2 * 2**20
        powers_of_two.add(2**i)

    count = 0
    seen = set() # Avoid double counting

    for num in deliciousness:
        for power in powers_of_two:
            complement = power - num
            if complement in deliciousness_counts:
                count = (count + deliciousness_counts[complement]) % MOD

        deliciousness_counts[num] -= 1
        
    return count
	```
			
