Advertisement

Find the Number of Good Pairs II - LeetCode 3164 Solution

Find the Number of Good Pairs II - Complete Solution Guide

Find the Number of Good Pairs II is LeetCode problem 3164, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 Explanatio

Detailed Explanation

The problem asks us to find the number of 'good' pairs between two integer arrays, `nums1` and `nums2`. A pair (i, j) is considered 'good' if `nums1[i]` is divisible by `nums2[j] * k`, where `k` is a given positive integer. The indices `i` and `j` must be within the bounds of their respective arrays (0 <= i < len(nums1) and 0 <= j < len(nums2)). We need to return the total count of such good pairs.

Solution Approach

The provided solution pre-processes `nums1` to determine the count of multiples of each number present in `nums1`, after dividing them by `k` if divisible. Then, for each number in `nums2`, it adds to the answer the pre-computed number of its multiples in the processed `nums1`. This approach relies on pre-computation to avoid the direct pairwise divisibility checks.

Step-by-Step Algorithm

  1. Step 1: Initialize an array `counts` to store the count of each number after dividing by `k` (if divisible) from `nums1`. The size of the array is `MAX_VAL + 1`.
  2. Step 2: Iterate through `nums1`. For each number, check if it's divisible by `k`. If it is, divide it by `k` and increment the corresponding count in the `counts` array.
  3. Step 3: Initialize an array `multiples` to store, for each number `i`, the sum of counts of multiples of `i` present in the `counts` array. The size of the array is `MAX_VAL + 1`.
  4. Step 4: Iterate from 1 to `MAX_VAL`. For each number `i`, iterate from `i` to `MAX_VAL` in increments of `i`. This inner loop iterates through multiples of `i`. For each multiple `j`, add `counts[j]` to `multiples[i]`.
  5. Step 5: Initialize a variable `ans` to 0. Iterate through `nums2`. For each number, add the corresponding value from the `multiples` array to `ans`.
  6. Step 6: Return `ans`.

Key Insights

  • Insight 1: The core of the problem lies in efficiently checking divisibility conditions between elements of the two arrays, considering the constant `k`.
  • Insight 2: Directly iterating through all possible pairs (i, j) would result in a time complexity of O(n*m), which could be inefficient given the constraints. The given solutions reduce complexity by pre-calculating some values.
  • Insight 3: Since `nums1[i]` must be divisible by `nums2[j] * k`, we can rephrase the condition as `nums1[i] / k` being divisible by `nums2[j]`. This suggests pre-processing nums1 by dividing its elements by k if they are divisible. We can then precompute how many multiples of each number exist among nums1/k.

Complexity Analysis

Time Complexity: O(n*k)

Space Complexity: O(k)

Topics

This problem involves: Array, Hash Table.

Companies

Asked at: Airbus SE.