3162. Find the Number of Good Pairs I LeetCode Solution
In this guide, you will get 3162. Find the Number of Good Pairs I LeetCode Solution with the best time and space complexity. The solution to Find the Number of Good Pairs I problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find the Number of Good Pairs I solution in C++
- Find the Number of Good Pairs I solution in Java
- Find the Number of Good Pairs I solution in Python
- Additional Resources

Problem Statement of Find the Number of Good Pairs I
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 <= 50
1 <= nums1[i], nums2[j] <= 50
1 <= k <= 50
Complexity Analysis
- Time Complexity: O(nm)
- Space Complexity: O(1)
3162. Find the Number of Good Pairs I LeetCode Solution in C++
class Solution {
public:
int numberOfPairs(vector<int>& nums1, vector<int>& nums2, int k) {
int ans = 0;
for (const int num1 : nums1)
for (const int num2 : nums2)
if (num1 % (num2 * k) == 0)
++ans;
return ans;
}
};
/* code provided by PROGIEZ */
3162. Find the Number of Good Pairs I LeetCode Solution in Java
class Solution {
public int numberOfPairs(int[] nums1, int[] nums2, int k) {
int ans = 0;
for (final int num1 : nums1)
for (final int num2 : nums2)
if (num1 % (num2 * k) == 0)
++ans;
return ans;
}
}
// code provided by PROGIEZ
3162. Find the Number of Good Pairs I LeetCode Solution in Python
class Solution:
def numberOfPairs(self, nums1: list[int], nums2: list[int], k: int) -> int:
return sum(num1 % (num2 * k) == 0
for num1 in nums1
for num2 in nums2)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.