Count Array Pairs Divisible by K - Complete Solution Guide
Count Array Pairs Divisible by K is LeetCode problem 2183, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given a 0-indexed integer array nums of length n and an integer k , return the number of pairs (i, j) such that: 0 <= i < j <= n - 1 and nums[i] * nums[j] is divisible by k . Example 1: Input: nums = [1,2,3,4,5], k = 2 Output: 7 Explanation: The 7 pairs of indices whose corresponding products are divisible by 2 are (0, 1), (0, 3), (1, 2), (1, 3), (1, 4), (2, 3), and (3, 4). Their products are 2, 4, 6, 8, 10, 12, and 20 respectively. Other pairs such as (0, 2) and (2, 4) have products 3 and 15 re
Detailed Explanation
The problem asks us to count the number of pairs (i, j) in a given array `nums` such that 0 <= i < j <= n - 1 and the product of `nums[i]` and `nums[j]` is divisible by `k`. Essentially, we need to find all pairs of numbers in the array whose product is a multiple of `k`.
Solution Approach
The solution first calculates the GCD of each number in the input array with `k`. It then counts the occurrences of each unique GCD using a hash map. After that, it iterates through all possible pairs of unique GCDs and checks if their product is divisible by `k`. If it is, it updates the count of valid pairs by considering the frequencies of each GCD. The important optimization here is computing and working with GCDs, which are typically smaller than the original numbers, and reduces the number of divisibility checks.
Step-by-Step Algorithm
- Step 1: Iterate through the input array `nums`.
- Step 2: For each number `num` in `nums`, calculate the GCD of `num` and `k` using the `gcd(num, k)` function.
- Step 3: Store the GCD and its frequency in a hash map `gcd_counts`. If the GCD already exists in the map, increment its frequency; otherwise, add it to the map with a frequency of 1.
- Step 4: Extract the unique GCDs from the `gcd_counts` hash map.
- Step 5: Initialize a variable `count` to 0, which will store the number of valid pairs.
- Step 6: Iterate through the unique GCDs. Let `g1` and `g2` be the current pair of GCDs being considered. Ensure to consider pairs (i,j) where i <= j.
- Step 7: Check if the product `g1 * g2` is divisible by `k` (i.e., `(g1 * g2) % k == 0`).
- Step 8: If the product is divisible by `k`, consider the frequency counts in gcd_counts. If `g1` and `g2` are the same (i.e., i == j), add `freq * (freq - 1) / 2` to `count`, where `freq` is the frequency of `g1`. Otherwise, add `freq1 * freq2` to `count`, where `freq1` is the frequency of `g1` and `freq2` is the frequency of `g2`.
- Step 9: Return the final `count`.
Key Insights
- Insight 1: The key is to realize that instead of directly checking the divisibility of `nums[i] * nums[j]` by `k`, we can optimize by first finding the greatest common divisor (GCD) of each `nums[i]` with `k`.
- Insight 2: We can efficiently store and count the frequencies of these GCDs using a hash map (or dictionary).
- Insight 3: After having the frequencies of GCDs, we can iterate through the unique GCDs and check if the product of any two GCDs is divisible by `k`. Careful consideration is required to handle duplicate GCDs (when i==j).
Complexity Analysis
Time Complexity: O(N + D^2)
Space Complexity: O(D)
Topics
This problem involves: Array, Math, Number Theory.
Companies
Asked at: PayPal.