Ctrl K

Pairs With a Divisible Product

Hard
You are given an array of positive integers values and an integer divisor. Count the index pairs i < j for which the product values[i] · values[j] is divisible by divisor. Checking every pair is too slow here; think about what actually matters in each number.
Example 1
values=[3, 4, 6, 9], divisor=6 → 5

The pair products are 3·4 = 12, 3·6 = 18, 3·9 = 27, 4·6 = 24, 4·9 = 36 and 6·9 = 54. Only 27 is not divisible by 6, so the answer is 5.

Example 2
values=[5, 7, 11], divisor=3 → 0

None of the numbers is divisible by 3, so no product is either.

Example 3
values=[2, 2], divisor=4 → 1

Neither number is divisible by 4 on its own, but their product 4 is.

Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • 1 ≤ values[i] ≤ 10⁵
  • 1 ≤ divisor ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBiggest Shared-Factor Group HardMake One From Multiples HardTowns Joined by Big Divisors HardNearest Coprime Ancestor HardDistinct GCDs of Selections Hard
loading Python…
Input
values=[3, 4, 6, 9], divisor=6
Expected
5

Run checks the first 3 tests in your browser. Submit checks all 104 tests on the server, hidden ones included.