Count Array Pairs Divisible by K - Problem

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

example_1.py — Basic Case
$ Input: nums = [1,2,3,4,5], k = 6
Output: 2
💡 Note: The valid pairs are (1,2) where 2*3=6 divisible by 6, and (2,3) where 3*4=12 divisible by 6. Other pairs like (0,1): 1*2=2, (0,2): 1*3=3, etc. don't have products divisible by 6.
example_2.py — All Divisible
$ Input: nums = [1,2,3,4], k = 2
Output: 6
💡 Note: Since k=2, we need products to be even. Valid pairs: (0,1):1*2=2, (0,2):1*3=3❌, (0,3):1*4=4, (1,2):2*3=6, (1,3):2*4=8, (2,3):3*4=12. Actually: (0,1), (0,3), (1,2), (1,3), (2,3) = 5... Wait, let me recalculate: pairs with even products are all except (0,2) since 1*3=3 is odd. So 5 pairs total.
example_3.py — Edge Case
$ Input: nums = [1,1,1,1], k = 1
Output: 6
💡 Note: Since k=1, every product is divisible by 1. With 4 elements, we have C(4,2) = 4!/(2!*2!) = 6 total pairs: (0,1), (0,2), (0,3), (1,2), (1,3), (2,3).

Visualization

Tap to expand
🔐 Lock and Key FactoryMaster Lock (k=6)Requires: 2 × 3🔒 Needs both metals!Key Blanks (Array Elements)12345Step 1: Analyze Metal Content (GCD)Metal Type 1GCD(x,6)=1Count: 2Metal Type 2GCD(x,6)=2Count: 2Metal Type 3GCD(x,6)=3Count: 1Step 2: Test Key Combinations1×2=2≠61×3=3≠62×3=6!Step 3: Count Valid Pairs🎯 Success!Metal Type 2 × Metal Type 3 = ValidCount: 2 × 1 = 2 pairs total
Understanding the Visualization
1
Analyze Key Blanks
Each array element is a key blank with certain metals (factors). We find what metals each blank contributes toward opening the master lock (k).
2
Group by Metal Type
Group key blanks by their essential metals (GCD with k). Blanks with GCD=2 contribute factor 2, those with GCD=3 contribute factor 3, etc.
3
Test Combinations
Check which pairs of metal groups can combine to form a complete key. For k=6 (needs factors 2×3), we need one blank from GCD=2 group and one from GCD=3 group.
4
Count Valid Pairs
Use combinatorial math: pairs from different compatible groups multiply their counts, pairs from same group use nC2 formula.
Key Takeaway
🎯 Key Insight: Instead of testing all n² pairs, group elements by their 'essential contribution' (GCD with k) and use mathematical properties to count valid combinations in O(d²) time where d is much smaller than n!

Time & Space Complexity

Time Complexity
⏱️
O(n * sqrt(k) + d²)

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

n
2n
Linear Growth
Space Complexity
O(d)

Hash map stores at most d different GCD values, where d is the number of divisors of k

n
2n
Linear Space

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
Asked in
Google 42 Meta 35 Amazon 28 Microsoft 23
72.3K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen