Advertisement

Count Good Meals - LeetCode 1711 Solution

Count Good Meals - Complete Solution Guide

Count Good Meals is LeetCode problem 1711, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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

Detailed Explanation

The problem asks us to find the number of 'good meals' we can create from a given list of food item deliciousness values. A 'good meal' consists of exactly two *different* food items, and their combined deliciousness must be a power of 2 (e.g., 2, 4, 8, 16, ...). The input is an array `deliciousness` of integers representing the deliciousness of each food item. We need to return the total number of good meals modulo 10^9 + 7. The constraints specify that the array size is up to 10^5 and individual deliciousness values can be up to 2^20.

Solution Approach

The solution iterates through the `deliciousness` array. For each deliciousness value, it checks all possible powers of 2 (from 2^0 to 2^21) to determine the 'complement' needed to reach that power of 2. Using a hash table, it efficiently looks up the number of occurrences of that complement. The number of complements is added to a running count. Finally, the result is taken modulo 10^9 + 7 before being returned.

Step-by-Step Algorithm

  1. Step 1: Initialize a hash table (or frequency map) to store the count of each deliciousness value. Initialize a counter variable `count` to 0 and the modulo constant `MOD` to 10^9 + 7.
  2. Step 2: Iterate through the `deliciousness` array.
  3. Step 3: For each deliciousness value `d`, iterate through the possible powers of 2 (from 2^0 to 2^21).
  4. Step 4: Calculate the 'complement' needed to reach the current power of 2: `complement = power_of_two - d`.
  5. Step 5: Look up the number of occurrences of the `complement` in the hash table. Add this count to the running `count`.
  6. Step 6: Increment the count of the current deliciousness value `d` in the hash table.
  7. Step 7: After iterating through all deliciousness values, return the `count` modulo `MOD`.

Key Insights

  • Insight 1: We need to efficiently find pairs whose sum is a power of 2. Since individual deliciousness values are at most 2^20, the maximum possible sum is 2 * 2^20 = 2^21. Therefore, we only need to check powers of 2 up to 2^21.
  • Insight 2: A hash table (or frequency map) is an efficient data structure to count the occurrences of each deliciousness value. This allows us to quickly determine the number of complements needed to reach a power of 2.
  • Insight 3: We must ensure to return the result modulo 10^9 + 7 to prevent integer overflow as the number of good meals can be very large.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table.

Companies

Asked at: Robinhood, Swiggy.