Browse curriculum

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?".

  1. Sort the potions ascending.
  2. For each spell, binary search the first index where spell × potions[index] >= success.
  3. 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

1

0

2

1

3

2

4

3

5

4

spell × potion

spell

:

lo

:

hi

:

pairs per spell

5: ?
1: ?
3: ?

 

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)

Free preview: the first 5 of 15 steps

spells

potions

success

7

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 pairs
Successful 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

The approach, step by step

  1. Sort the potions

    Sort the potions ascending once.

  2. Binary search per spell

    Find the first index with spell × potion >= success, using lo = 0 and hi = m.

  3. Count

    That spell’s answer is m − index.

Frequently asked questions

Why sort the potions?

For a fixed spell, spell × potion grows with the potion. Once the potions are sorted, the successful ones form a suffix: everything from the first successful potion to the end. Counting them is then finding one index, which binary search does in O(log m).


What exactly does the binary search look for?

The lower bound: the first index where spell × potions[index] >= success, searching [0, m). If the product at mid is big enough, the answer is mid or to its left, so hi = mid; otherwise it is to the right, so lo = mid + 1. When lo meets hi, the count is m − lo, which is 0 if no potion works.


Why use 64-bit multiplication?

Spells and potions go up to 10⁵, so a product reaches 10¹⁰, and success can be up to 10¹⁰. That overflows a 32-bit int, so Java, C++ and C cast to long or long long before multiplying.


Is there an approach without binary search?

Sort both arrays, then walk the spells from weakest to strongest with one pointer moving left through the potions: stronger spells need weaker potions. It is O(n log n + m log m) and also passes. Binary search is the approach LeetCode 75 lists this problem under.

New to the technique behind this problem? Watch it run step by step:

Binary Search Visualization

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description