-
Notifications
You must be signed in to change notification settings - Fork 254
Count Winning Pairings
LeWiz24 edited this page Aug 13, 2024
·
2 revisions
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Q
- What is the desired outcome?
- To count the number of different winning pairings whose sum equals a power of two.
- What input is provided?
- An array of integers
star_power
.
- An array of integers
- What is the desired outcome?
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use a dictionary to count occurrences of each star power, then iterate through possible pairs and count those that sum to a power of two.
1) Create a list `powers_of_two` containing all powers of two up to `2^21`.
2) Use a dictionary `count` to store the frequency of each star power.
3) Iterate through `star_power`, and for each value, find the complement that would sum to a power of two.
- Update the total pairs count.
4) Return the total pairs modulo `10^9 + 7`.
- Not handling pairs with the same star power correctly.
from collections import defaultdict
def count_winning_pairings(star_power):
MOD = 10**9 + 7
powers_of_two = [1 << i for i in range(22)] # [2^0, 2^1, ..., 2^21]
count = defaultdict(int)
total_pairs = 0
for value in star_power:
for power in powers_of_two:
complement = power - value
if complement in count:
total_pairs = (total_pairs + count[complement]) % MOD
count[value] += 1
return total_pairs