Count Array Pairs Divisible by K - Problem
Given a 0-indexed integer array nums of length n and an integer k, return the number of pairs (i, j) such that:
0 <= i < j <= n - 1andnums[i] * nums[j]is divisible byk.
A product is divisible by k if it contains all prime factors of k with at least the same frequency.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [2,6,4,3], k = 12
›
Output:
3
💡 Note:
Valid pairs: (0,1) since 2×6=12 divisible by 12, (1,2) since 6×4=24 divisible by 12, and (2,3) since 4×3=12 divisible by 12
Example 2 — Smaller k
$
Input:
nums = [1,2,3,4,5], k = 2
›
Output:
7
💡 Note:
Products divisible by 2: 1×2=2, 1×4=4, 2×3=6, 2×5=10, 3×4=12, 4×5=20, plus any pair with an even number
Example 3 — All Same Numbers
$
Input:
nums = [3,3,3], k = 9
›
Output:
3
💡 Note:
All pairs (0,1), (0,2), (1,2) give product 3×3=9 which is divisible by 9
Constraints
- 1 ≤ nums.length ≤ 105
- 1 ≤ nums[i], k ≤ 105
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Array nums and divisor k
2
Find Pairs
Check all pairs (i,j) where i < j
3
Count Valid
Count pairs where nums[i] × nums[j] % k == 0
Key Takeaway
🎯 Key Insight: Use GCD to group numbers by their useful factors, avoiding expensive product calculations
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code