Home
DSA Patterns
Binary Search
Successful Pairs of Spells and Potions
Successful Pairs of Spells and Potions (LeetCode 2300)
Successful Pairs of Spells and Potions (LeetCode 2300): for each spell, count the potions whose product with it reaches success. Sort the potions once, then binary search each spell’s first successful potion; everything after it succeeds too.
Problem Understanding
Successful Pairs of Spells and Potions (LeetCode 2300): spells[i] and potions[j] are strengths, and a pair is successful if spells[i] × potions[j] >= success. Return an array whose i-th value is the number of potions that form a successful pair with spell i.
Examples: spells = [5,1,3], potions = [1,2,3,4,5], success = 7 → [4,0,3]; spells = [3,1,2], potions = [8,5,8], success = 16 → [2,0,2].
The Intuition: Successful Potions Form a Suffix
Checking every pair is O(n · m). But sorted potions turn each spell’s question into "where does success start?".
- Sort the potions ascending.
- For each spell, binary search the first index where
spell × potions[index] >= success. - Every potion from that index on succeeds, so the count is
m − index.
It is the lower-bound search from Search Insert Position, run once per spell with a product in place of a plain comparison. Guess Number Higher or Lower and the other binary search lessons use the same lo/hi loop.
Successful Pairs of Spells and Potions Visualization
SUCCESS = 7 · GREY = TOO WEAK · GREEN = SUCCEEDS
SORT THE POTIONS
potions, sorted
0
1
2
3
4
spell × potion
spell
:
lo
:
hi
:
pairs per spell
Sort the potions: [1, 2, 3, 4, 5]. A spell's successful potions are then a suffix
Next
Spell 5: find the first potion with 5 × potion >= 7. Search [0, 5)
spells
potions
success
The top row is the sorted potions; under it, each product for the spell being searched. lo, hi and mid ride over the products: cells left of lo turn grey (too weak) and cells from hi on turn green (they succeed), until nothing is undecided. The count is then added on the right. Try Duplicates, where several equal potions sit together.
Successful Pairs of Spells and Potions Dry Run
Input: spells = [5,1,3], potions = [1,2,3,4,5] (already sorted), success = 7.
| Spell | Probes (mid: product) | First successful index | Pairs |
|---|---|---|---|
| 5 | 2: 15 ✓, 1: 10 ✓, 0: 5 ✗ | 1 | 5 − 1 = 4 |
| 1 | 2: 3 ✗, 4: 5 ✗ | 5 (none) | 0 |
| 3 | 2: 9 ✓, 1: 6 ✗ | 2 | 5 − 2 = 3 |
Answer: [4, 0, 3].
Successful Pairs of Spells and Potions LeetCode Solution
One sort, then a lower-bound binary search per spell. Products are computed in 64 bits where the language needs it.
Successful Pairs of Spells and Potions Code
1var successfulPairs = function(spells, potions, success) {2 potions.sort((a, b) => a - b);3 const m = potions.length;4 return spells.map((spell) => {5 let lo = 0, hi = m;6 while (lo < hi) {7 const mid = (lo + hi) >> 1;8 if (spell * potions[mid] >= success) {9 hi = mid;10 } else {11 lo = mid + 1;12 }13 }14 return m - lo;15 });16};
Successful Pairs of Spells and Potions in Python, Java, C++, TypeScript and C
Successful Pairs of Spells and Potions in Python
class Solution:
def successfulPairs(self, spells: List[int], potions: List[int], success: int) -> List[int]:
potions.sort()
m = len(potions)
pairs = []
for spell in spells:
lo, hi = 0, m
while lo < hi:
mid = (lo + hi) // 2
if spell * potions[mid] >= success:
hi = mid
else:
lo = mid + 1
pairs.append(m - lo)
return pairsSuccessful Pairs of Spells and Potions in Java
class Solution {
public int[] successfulPairs(int[] spells, int[] potions, long success) {
Arrays.sort(potions);
int m = potions.length;
int[] pairs = new int[spells.length];
for (int s = 0; s < spells.length; s++) {
int lo = 0, hi = m;
while (lo < hi) {
int mid = (lo + hi) >>> 1;
if ((long) spells[s] * potions[mid] >= success) {
hi = mid;
} else {
lo = mid + 1;
}
}
pairs[s] = m - lo;
}
return pairs;
}
}Successful Pairs of Spells and Potions in C++
class Solution {
public:
vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success) {
sort(potions.begin(), potions.end());
int m = potions.size();
vector<int> pairs;
for (int spell : spells) {
int lo = 0, hi = m;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if ((long long)spell * potions[mid] >= success) {
hi = mid;
} else {
lo = mid + 1;
}
}
pairs.push_back(m - lo);
}
return pairs;
}
};Successful Pairs of Spells and Potions in TypeScript
function successfulPairs(spells: number[], potions: number[], success: number): number[] {
potions.sort((a, b) => a - b);
const m = potions.length;
return spells.map((spell) => {
let lo = 0, hi = m;
while (lo < hi) {
const mid = (lo + hi) >> 1;
if (spell * potions[mid] >= success) {
hi = mid;
} else {
lo = mid + 1;
}
}
return m - lo;
});
}Successful Pairs of Spells and Potions in C
static int ascending(const void* a, const void* b) {
return *(const int*)a - *(const int*)b;
}
int* successfulPairs(int* spells, int spellsSize, int* potions, int potionsSize, long long success, int* returnSize) {
qsort(potions, potionsSize, sizeof(int), ascending);
int* pairs = malloc(spellsSize * sizeof(int));
*returnSize = spellsSize;
for (int s = 0; s < spellsSize; s++) {
int lo = 0, hi = potionsSize;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if ((long long)spells[s] * potions[mid] >= success) {
hi = mid;
} else {
lo = mid + 1;
}
}
pairs[s] = potionsSize - lo;
}
return pairs;
}Edge Cases & Common Mistakes
- hi = m − 1 instead of m: with no successful potion the search must be able to end at m, giving 0.
- Overflow: spell × potion reaches 10¹⁰; multiply in 64 bits.
- Equal potions: the lower bound lands on the first of them, so all are counted.
- Sorting spells too: not needed, and it would scramble the order the answer must keep.
Summary
Sort the potions; for each spell, binary search the first potion with spell × potion >= success and count everything from there to the end. O((n + m) log m) time.
More practice: DSA interview questions with answers · patterns cheat sheet
