Find the Number of Good Pairs II - Problem
You are given 2 integer arrays nums1 and nums2 of lengths n and m respectively. You are also given a positive integer k.
A pair (i, j) is called good if nums1[i] is divisible by nums2[j] * k (where 0 <= i <= n - 1 and 0 <= j <= m - 1).
Return the total number of good pairs.
Input & Output
Example 1 — Basic Case
$
Input:
nums1 = [1,3,4], nums2 = [1,3,4], k = 1
›
Output:
5
💡 Note:
Good pairs: (0,0) since 1%(1*1)=0, (1,0) since 3%(1*1)=0, (1,1) since 3%(3*1)=0, (2,0) since 4%(1*1)=0, (2,2) since 4%(4*1)=0. Total = 5 pairs.
Example 2 — With Multiplier k=2
$
Input:
nums1 = [1,2,4,12], nums2 = [2,4], k = 3
›
Output:
2
💡 Note:
Check divisibility by nums2[j]*k: nums2*3 = [6,12]. Only 12%6=0 and 12%12=0 are valid. Good pairs: (3,0) and (3,1). Total = 2 pairs.
Example 3 — No Valid Pairs
$
Input:
nums1 = [1,3], nums2 = [5], k = 2
›
Output:
0
💡 Note:
nums2*k = [10]. Neither 1%10=1 nor 3%10=3 equals 0. No good pairs exist.
Constraints
- 1 ≤ nums1.length, nums2.length ≤ 105
- 1 ≤ nums1[i], nums2[i] ≤ 106
- 1 ≤ k ≤ 103
Visualization
Tap to expand
Understanding the Visualization
1
Input Arrays
nums1=[1,3,4], nums2=[1,3,4], k=1
2
Check Divisibility
For each nums1[i], check if divisible by nums2[j] * k
3
Count Valid Pairs
Sum all pairs where nums1[i] % (nums2[j] * k) == 0
Key Takeaway
🎯 Key Insight: Use frequency counting to optimize repeated divisibility checks for duplicate values
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code