For each a in nums1, we want to count how many b in nums2 make a % (b * k) == 0. Instead of brute force, we can precompute the frequency of each b in nums2, and for each a, check all divisors of a that are multiples of k and see if b = divisor / k exists in nums2.
classSolution {
public:int numberOfPairs(vector<int>& nums1, vector<int>& nums2, int k) {
unordered_map<int, int> freq;
for (int b : nums2) freq[b]++;
int ans =0;
for (int a : nums1) {
for (int d =1; d * d <= a; ++d) {
if (a % d ==0) {
if (d % k ==0&& freq.count(d / k)) ans += freq[d / k];
int d2 = a / d;
if (d2 != d && d2 % k ==0&& freq.count(d2 / k)) ans += freq[d2 / k];
}
}
}
return ans;
}
};
classSolution {
publicintnumberOfPairs(int[] nums1, int[] nums2, int k) {
Map<Integer, Integer> freq =new HashMap<>();
for (int b : nums2) freq.put(b, freq.getOrDefault(b, 0) + 1);
int ans = 0;
for (int a : nums1) {
for (int d = 1; d * d <= a; ++d) {
if (a % d == 0) {
if (d % k == 0 && freq.containsKey(d / k)) ans += freq.get(d / k);
int d2 = a / d;
if (d2 != d && d2 % k == 0 && freq.containsKey(d2 / k)) ans += freq.get(d2 / k);
}
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
funnumberOfPairs(nums1: IntArray, nums2: IntArray, k: Int): Int {
val freq = mutableMapOf<Int, Int>()
for (b in nums2) freq[b] = freq.getOrDefault(b, 0) + 1var ans = 0for (a in nums1) {
var d = 1while (d * d <= a) {
if (a % d ==0) {
if (d % k ==0) ans += freq.getOrDefault(d / k, 0)
val d2 = a / d
if (d2 != d && d2 % k ==0) ans += freq.getOrDefault(d2 / k, 0)
}
d++ }
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution:
defnumberOfPairs(self, nums1: list[int], nums2: list[int], k: int) -> int:
from collections import Counter
freq = Counter(nums2)
ans =0for a in nums1:
d =1while d * d <= a:
if a % d ==0:
if d % k ==0:
b = d // k
ans += freq.get(b, 0)
d2 = a // d
if d2 != d and d2 % k ==0:
b2 = d2 // k
ans += freq.get(b2, 0)
d +=1return ans
use std::collections::HashMap;
impl Solution {
pubfnnumber_of_pairs(nums1: Vec<i32>, nums2: Vec<i32>, k: i32) -> i32 {
letmut freq = HashMap::new();
for&b in&nums2 {
*freq.entry(b).or_insert(0) +=1;
}
letmut ans =0;
for&a in&nums1 {
letmut d =1;
while d * d <= a {
if a % d ==0 {
if d % k ==0 {
let b = d / k;
ans +=*freq.get(&b).unwrap_or(&0);
}
let d2 = a / d;
if d2 != d && d2 % k ==0 {
let b2 = d2 / k;
ans +=*freq.get(&b2).unwrap_or(&0);
}
}
d +=1;
}
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
numberOfPairs(nums1: number[], nums2: number[], k: number):number {
constfreq=newMap<number, number>();
for (constbofnums2) freq.set(b, (freq.get(b) ||0) +1);
letans=0;
for (constaofnums1) {
for (letd=1; d*d<=a; ++d) {
if (a%d===0) {
if (d%k===0) ans+=freq.get(d/k) ||0;
constd2=a/d;
if (d2!==d&&d2%k===0) ans+=freq.get(d2/k) ||0;
}
}
}
returnans;
}
}
⏰ Time complexity: O(n * sqrt(A) + m), where n is the length of nums1, m is the length of nums2, and A is the maximum value in nums1. For each a in nums1, we check all its divisors.
🧺 Space complexity: O(M), where M is the number of unique elements in nums2 (for the frequency map).