Count Pairs of Dishes Whose Scores Sum to a Power of Two

Quick Overview

Count the pairs of dishes in an array whose scores add up to a power of two, returning the result modulo 1000000007. The problem tests hash map counting, bounding the set of target sums from the value range, and handling equal scores at different indices without double counting.

Count Pairs of Dishes Whose Scores Sum to a Power of Two

Company: Zepto

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

A kitchen has a menu of dishes, where `deliciousness[i]` is the tastiness score of the `i`-th dish. A **good meal** is formed by picking two different dishes, at indices `i < j`, such that `deliciousness[i] + deliciousness[j]` is a power of two, meaning the sum equals ${2^k}$ for some integer $k \ge 0$ (that is, 1, 2, 4, 8, ...). Note that 0 is not a power of two. Dishes at different indices are treated as different items, even when their scores are equal. Each unordered pair of indices is counted once. Return the number of good meals that can be formed. Since the count can be very large, return it modulo `1000000007`. ### Function Signature ```python def count_good_meals(deliciousness: list[int]) -> int: ``` ### Rules - A good meal is a pair of indices `i < j`. Two dishes with the same score at different indices form a valid pair, and no dish pairs with itself. - The powers of two are 1, 2, 4, 8 and so on. A sum of 0 never counts. - Return the count modulo `1000000007`. ### Constraints - `1 <= len(deliciousness) <= 10^5` - `0 <= deliciousness[i] <= 2^20` (that is, `1048576`) ### Examples ```text Input: deliciousness = [2, 2, 6, 1, 3, 5] Output: 5 ``` The good meals are (2, 2) with sum 4, (2, 6) twice with sum 8 (once for each 2), (1, 3) with sum 4, and (3, 5) with sum 8, for 5 in total. ```text Input: deliciousness = [0, 0, 1, 1, 3] Output: 7 ``` Each 0 pairs with each 1 for a sum of 1 (4 pairs), the two 1s sum to 2 (1 pair), and each 1 pairs with the 3 for a sum of 4 (2 pairs). The two 0s sum to 0, which does not count, so the total is 7. ```text Input: deliciousness = [1048576, 1048576] Output: 1 ``` The two largest allowed scores add up to 2097152, which is ${2^{21}}$, so this single pair is a good meal.

Overview: Count the pairs of dishes in an array whose scores add up to a power of two, returning the result modulo 1000000007. The problem tests hash map counting, bounding the set of target sums from the value range, and handling equal scores at different indices without double counting.

Zepto logo
Zepto
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

A kitchen has a menu of dishes, where deliciousness[i] is the tastiness score of the i-th dish. A good meal is formed by picking two different dishes, at indices i < j, such that deliciousness[i] + deliciousness[j] is a power of two, meaning the sum equals 2k{2^k} for some integer k≥0k \ge 0 (that is, 1, 2, 4, 8, ...). Note that 0 is not a power of two.

Dishes at different indices are treated as different items, even when their scores are equal. Each unordered pair of indices is counted once.

Return the number of good meals that can be formed. Since the count can be very large, return it modulo 1000000007.

Function Signature

def count_good_meals(deliciousness: list[int]) -> int:

Rules

  • A good meal is a pair of indices i < j . Two dishes with the same score at different indices form a valid pair, and no dish pairs with itself.
  • The powers of two are 1, 2, 4, 8 and so on. A sum of 0 never counts.
  • Return the count modulo 1000000007 .

Constraints

  • 1 <= len(deliciousness) <= 10^5
  • 0 <= deliciousness[i] <= 2^20 (that is, 1048576 )

Examples

Input: deliciousness = [2, 2, 6, 1, 3, 5]
Output: 5

The good meals are (2, 2) with sum 4, (2, 6) twice with sum 8 (once for each 2), (1, 3) with sum 4, and (3, 5) with sum 8, for 5 in total.

Input: deliciousness = [0, 0, 1, 1, 3]
Output: 7

Each 0 pairs with each 1 for a sum of 1 (4 pairs), the two 1s sum to 2 (1 pair), and each 1 pairs with the 3 for a sum of 4 (2 pairs). The two 0s sum to 0, which does not count, so the total is 7.

Input: deliciousness = [1048576, 1048576]
Output: 1

The two largest allowed scores add up to 2097152, which is 221{2^{21}}, so this single pair is a good meal.