Leetcode 3162: Find the Number of Good Pairs I

grid47
grid47
Exploring patterns and algorithms
Dec 26, 2023 5 min read

You are given two integer arrays nums1 and nums2 of lengths n and m respectively, and 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). Your task is to return the total number of good pairs.
Problem
Approach
Steps
Complexity
Input: The input consists of two integer arrays `nums1` and `nums2`, and a positive integer `k`.
Example: Example 1: Input: nums1 = [1,2,5], nums2 = [1,2,3], k = 2 Output: 4
Constraints:
• 1 <= n, m <= 50
• 1 <= nums1[i], nums2[j] <= 50
• 1 <= k <= 50
Output: Return the total number of good pairs, where each pair satisfies the divisibility condition.
Example: Example 1: Input: nums1 = [1,2,5], nums2 = [1,2,3], k = 2 Output: 4
Constraints:
• The output should be an integer representing the count of good pairs.
Goal: The goal is to identify all pairs (i, j) where `nums1[i]` is divisible by `nums2[j] * k`.
Steps:
• Iterate through each element in `nums1`.
• For each element in `nums1`, iterate through each element in `nums2`.
• Check if `nums1[i] % (nums2[j] * k) == 0`.
• Count each valid pair where the condition holds.
Goal: The input arrays and the integer `k` have the following constraints.
Steps:
• 1 <= n, m <= 50
• 1 <= nums1[i], nums2[j] <= 50
• 1 <= k <= 50
Assumptions:
• The arrays `nums1` and `nums2` will always have values between 1 and 50.
• The integer `k` will also be between 1 and 50.
Input: Example 1:
Explanation: For `nums1 = [1, 2, 5]`, `nums2 = [1, 2, 3]`, and `k = 2`, we have the following good pairs: - (0, 0) because `1 % (1 * 2) == 0` - (0, 1) because `1 % (2 * 2) == 0` - (1, 0) because `2 % (1 * 2) == 0` - (2, 1) because `5 % (2 * 2) == 0`.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus