Skip to content
Work Practice Products Coding interviews Machine learning coding Quant developer HFT course Blog Method Why me About Enquire Book a call

Coding · Binary search

Count the partners that clear a threshold

Medium · Target 20 minutes · Binary search · Type asked atAmazonCitadel

You have two lists of positive integers, spells (length n) and potions (length m), and a positive integer success. A spell of strength s and a potion of strength p form a successful pair if s × p ≥ success.

Return a list of length n whose i-th entry is the number of potions that form a successful pair with spells[i].

Examples

InputOutputWhy
spells = [5, 1, 3], potions = [1, 2, 3, 4, 5], success = 7[4, 0, 3]5 pairs with 2, 3, 4 and 5; 1 pairs with none; 3 pairs with 3, 4 and 5.
spells = [3, 1, 2], potions = [8, 5, 8], success = 16[2, 0, 2]

Constraints

  • 1 ≤ n, m ≤ 10⁵
  • 1 ≤ spells[i], potions[j] ≤ 10⁵
  • 1 ≤ success ≤ 10⁹ (a product can exceed 2³¹)

Hints

Hint 1

Checking every pair is n × m = 10¹⁰ multiplications. What would make one spell's count quick?

Hint 2

If potions are sorted, the potions that work with a given spell form a suffix: once one is strong enough, every larger one is too.

Hint 3

For each spell, find the first potion p with s × p ≥ success by binary search; the count is m minus that index.

Where people lose marks

  • Multiplying in 32 bits: 10⁵ × 10⁵ overflows and turns a success into a negative number.
  • Sorting the spells as well, which scrambles the order of the answer.
  • Dividing instead of multiplying with integer division: the smallest p is ⌈success / s⌉, not success / s.
Console⌘/Ctrl + Enter runs

Write a solution and run it against the real test table.


Keep going

Preparing for a real process? Quant interview preparation, or book a free 20-minute call.