Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to count good meals with exactly two items in Python
Suppose we have an array deli where deli[i] is the deliciousness of the ith food. We need to find the number of different good meals we can make from this list. A good meal contains exactly two different food items with a sum of deliciousness that is a power of two. If the answer is too large, return the result modulo 10^9 + 7.
So, if the input is like deli = [1,7,3,6,5], then the output will be 3 because we can make pairs (1,3), (1,7) and (3,5) whose sums are powers of 2 (4, 8, and 8 respectively).
Algorithm
To solve this, we will follow these steps:
- Create a frequency map of all deliciousness values
- For each deliciousness value, check all possible powers of 2 (up to 2^21)
- Find the complement that makes the sum a power of 2
- Count valid pairs, handling duplicates carefully
- Return the result divided by 2 (since each pair is counted twice) modulo 10^9 + 7
Example
Let us see the following implementation to get better understanding ?
from collections import Counter
def solve(deli):
m = 10**9 + 7
count = Counter(deli)
ans = 0
for i in count:
for n in range(22):
j = (1 << n) - i # 2^n - i
if j in count:
if i == j:
# Same values: choose 2 from count[i]
ans += count[i] * (count[i] - 1)
else:
# Different values
ans += count[i] * count[j]
return (ans // 2) % m
# Test the function
deli = [1, 7, 3, 6, 5]
result = solve(deli)
print(f"Input: {deli}")
print(f"Output: {result}")
Input: [1, 7, 3, 6, 5] Output: 3
How It Works
The algorithm works by:
-
Frequency counting: Using
Counterto count occurrences of each deliciousness value -
Power of 2 checking: For each value
i, we check all powers of 2 from 2^0 to 2^21 -
Complement finding: For each power of 2, we find the complement
j = 2^n - i - Pair counting: If the complement exists, we count the valid pairs
- Duplicate handling: We divide by 2 at the end since each pair is counted twice
Edge Cases
Let's test with another example ?
from collections import Counter
def solve(deli):
m = 10**9 + 7
count = Counter(deli)
ans = 0
for i in count:
for n in range(22):
j = (1 << n) - i
if j in count:
if i == j:
ans += count[i] * (count[i] - 1)
else:
ans += count[i] * count[j]
return (ans // 2) % m
# Test with duplicate values
deli2 = [1, 1, 1, 3, 3]
result2 = solve(deli2)
print(f"Input: {deli2}")
print(f"Output: {result2}")
print("Valid pairs: (1,1), (1,1), (1,1), (1,3), (1,3), (1,3), (1,3), (1,3), (1,3)")
Input: [1, 1, 1, 3, 3] Output: 12 Valid pairs: (1,1), (1,1), (1,1), (1,3), (1,3), (1,3), (1,3), (1,3), (1,3)
Conclusion
This solution efficiently counts good meals by using frequency mapping and checking all possible powers of 2. The time complexity is O(n + k × 22) where n is the array length and k is the number of unique values.
