Watch 8 video solutions for Find the Number of Good Pairs I, a easy level problem involving Array, Hash Table. This walkthrough by Aryan Mittal has 5,970 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 (0 <= i <= n - 1, 0 <= j <= m - 1).
Return the total number of good pairs.
Example 1:
Input: nums1 = [1,3,4], nums2 = [1,3,4], k = 1
Output: 5
Explanation:
The 5 good pairs are(0, 0), (1, 0), (1, 1), (2, 0), and (2, 2).Example 2:
Input: nums1 = [1,2,4,12], nums2 = [2,4], k = 3
Output: 2
Explanation:
The 2 good pairs are (3, 0) and (3, 1).
Constraints:
1 <= n, m <= 501 <= nums1[i], nums2[j] <= 501 <= k <= 50Problem Overview: You are given two integer arrays nums1 and nums2, along with an integer k. A pair (i, j) is considered good if nums1[i] is divisible by nums2[j] * k. The task is to count how many such pairs exist across both arrays.
Approach 1: Brute Force Pair Check (Time: O(n * m), Space: O(1))
The most direct strategy is to check every possible pair. Iterate through each element in nums1 and for each element iterate through nums2. For every pair, compute nums2[j] * k and check whether nums1[i] % (nums2[j] * k) == 0. If the condition holds, increment the count. This approach uses simple nested iteration over the two arrays and requires no additional data structures. It works well when the arrays are small, but performance degrades as their sizes grow because every combination must be evaluated.
Approach 2: Hash Table + Divisor Enumeration (Time: O(n * sqrt(V) + m), Space: O(m))
A more efficient strategy reduces repeated checks using a hash table. First build a frequency map of values in nums2. Then iterate through nums1. Only elements divisible by k can form valid pairs, so compute x = nums1[i] / k. The condition becomes finding values in nums2 that divide x. Instead of checking every element in nums2, enumerate all divisors of x up to sqrt(x). If a divisor exists in the frequency map, add its count. Also check the paired divisor x / d when it differs from d. This preprocessing step avoids scanning the entire second array for each element and replaces it with constant-time hash lookups.
The key insight is rewriting the divisibility condition. If nums1[i] % (nums2[j] * k) == 0, then nums2[j] must divide nums1[i] / k. Turning the problem into divisor matching allows efficient lookups using a hash map rather than repeated multiplication checks. This technique appears frequently in problems combining array traversal with factor relationships.
Recommended for interviews: Start with the brute force explanation because it demonstrates clear understanding of the pair condition. Then move to the hash-table optimization that transforms the divisibility rule and uses divisor enumeration. Interviewers typically expect this transition from O(n * m) scanning to a structure-assisted lookup that significantly reduces redundant comparisons.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Pair Check | O(n * m) | O(1) | Best for small arrays or when implementing the simplest correct solution first |
| Hash Table + Divisor Enumeration | O(n * sqrt(V) + m) | O(m) | General case optimization when nums1 values are large and repeated pair checks must be avoided |