Successful Pairs of Spells and Potions

You are given two positive integer arrays spells and potions, where spells[i] is the strength of the i-th spell and potions[j] is the strength of the j-th potion, and an integer success. A spell and a potion form a successful pair when the product of their strengths is at least success. Return an array pairs of length n where pairs[i] is the number of potions that form a successful pair with the i-th spell.

30:00

Successful Pairs of Spells and Potions
medium
Free written solution
Topics
Companies

You are given two positive integer arrays spells and potions, where spells[i] is the strength of the i-th spell and potions[j] is the strength of the j-th potion, and an integer success. A spell and a potion form a successful pair when the product of their strengths is at least success. Return an array pairs of length n where pairs[i] is the number of potions that form a successful pair with the i-th spell.

Example 1:
Input: {"spells":[5,1,3],"potions":[1,2,3,4,5],"success":7}
Output: [4,0,3]
Constraints:
  • n=spells.lengthn = \text{spells.length}, m=potions.lengthm = \text{potions.length}

  • 1≤n,m≤1051 \leq n, m \leq 10^5

  • 1≤spells[i],potions[i]≤1051 \leq \text{spells}[i], \text{potions}[i] \leq 10^5

  • 1≤success≤10101 \leq \text{success} \leq 10^{10}

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"spells":[5,1,3],"potions":[1,2,3,4,5],"success":7}

A pair works when spell x potion reaches 7. For each of the 3 spells we must count how many of the 5 potions are strong enough.

Spells and their pair counts

success = 7

5
?
1
?
3
?

Potions, as given

1
2
3
4
5
0
1
2
3
4
5

- - need line

■ forms a pair

■ outside [lo, hi)

Variables
VariableValue
success7
n3
m5
DepthFunction Call
Stack empty
0/22