VISUALGO//PROBLEMS
successful pairs of spells and potions · binary searchmedium
spells (success: 7)
5
1
3
sorted potions
1
2
3
4
5
pairs per spell
ELEMENTS
spells
potions
success
Python
def successfulPairs(spells: list[int], potions: list[int], success: int) -> list[int]:
potions.sort()
n = len(potions)
result = []
for spell in spells:
# Binary search for first potion where spell * potion >= success
low, high = 0, n - 1
idx = n
while low <= high:
mid = (low + high) // 2
if spell * potions[mid] >= success:
idx = mid
high = mid - 1
else:
low = mid + 1
result.append(n - idx)
return result