Given a 0-indexed integer array nums of length n and an integer k, your task is to find the number of valid pairs (i, j) that satisfy both conditions:
0 ≤ i < j ≤ n - 1(pairs with distinct indices where i comes before j)nums[i] * nums[j]is divisible by k
This problem combines number theory with efficient counting techniques. The key insight is that for two numbers to have a product divisible by k, their combined prime factors must contain all prime factors of k.
Example: If nums = [1,2,3,4,5] and k = 6, we need pairs whose product is divisible by 6. Since 6 = 2×3, we need the product to contain both factors 2 and 3.
Input & Output
Visualization
Time & Space Complexity
Computing GCD for n elements takes O(n * sqrt(k)), then checking all pairs of divisors takes O(d²) where d is number of divisors of k
Hash map stores at most d different GCD values, where d is the number of divisors of k
Constraints
- 1 ≤ nums.length ≤ 105
- 1 ≤ nums[i], k ≤ 105
- Array indices must satisfy 0 ≤ i < j < n
- Products may exceed integer limits, requiring careful overflow handling