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 <= 50When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The most straightforward way to find the number of "good pairs" is to check every single possible pair of numbers. We compare each number with every other number in the group to see if they form a "good pair".
Here's how the algorithm would work step-by-step:
def find_number_of_good_pairs_i(numbers):
number_of_good_pairs = 0
for first_index in range(len(numbers)):
# Need to iterate through each number
# to compare it to the others.
for second_index in range(len(numbers)):
# Compare each number with every other number.
if numbers[first_index] == numbers[second_index]:
# If a good pair is found, increment.
number_of_good_pairs += 1
# Divide by 2 to account for double counting.
number_of_good_pairs = number_of_good_pairs // 2
return number_of_good_pairsThe problem asks us to count pairs of numbers in a list that are equal. The best way to solve this is to count how many times each number appears, then use those counts to figure out how many good pairs each number creates.
Here's how the algorithm would work step-by-step:
def find_number_of_good_pairs(numbers):
number_counts = {}
for number in numbers:
if number in number_counts:
number_counts[number] += 1
else:
number_counts[number] = 1
total_good_pairs = 0
# Iterate through the counts of each number.
for number in number_counts:
count = number_counts[number]
# Calculate pairs for a number.
if count > 1:
number_of_pairs = (count * (count - 1)) // 2
# Add the number of pairs to the total.
total_good_pairs += number_of_pairs
return total_good_pairs| Case | How to Handle |
|---|---|
| Null or empty input array | Return 0 immediately as no pairs can be formed. |
| Array with a single element | Return 0 as a pair requires at least two elements. |
| Array with all identical values | The algorithm should correctly count all pairs with identical indices. |
| Large input array size (close to maximum allowed) | The solution should have a time complexity that scales well, preferably O(n) or O(n log n) using appropriate data structures. |
| Input array containing negative numbers | The solution should handle negative numbers correctly without any modification. |
| Input array containing zero | The solution should not have any issues with zero values. |
| Array with a large number of duplicate elements | Using a hashmap can efficiently count duplicates and avoid quadratic time complexity. |
| Integer overflow in count of good pairs | Use a data type (e.g., long) that can accommodate a large number of pairs to avoid integer overflow. |