forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count-array-pairs-divisible-by-k.py
56 lines (47 loc) · 1.35 KB
/
count-array-pairs-divisible-by-k.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Time: O(nlogk + sqrt(k)^2) = O(nlogk + k)
# Space: O(sqrt(k)), number of factors of k is at most sqrt(k)
import collections
# math, number theory
class Solution(object):
def countPairs(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
def gcd(x, y):
while y:
x, y = y, x%y
return x
cnt = collections.Counter()
for x in nums:
cnt[gcd(x, k)] += 1
result = 0
for x in cnt.iterkeys():
for y in cnt.iterkeys():
if x > y or x*y%k:
continue
result += cnt[x]*cnt[y] if x != y else cnt[x]*(cnt[x]-1)//2
return result
# Time: O(nlogk + n * sqrt(k))
# Space: O(sqrt(k)), number of factors of k is at most sqrt(k)
import collections
# math, number theory
class Solution2(object):
def countPairs(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
def gcd(x, y):
while y:
x, y = y, x%y
return x
result = 0
gcds = collections.Counter()
for x in nums:
gcd_i = gcd(x, k)
result += sum(cnt for gcd_j, cnt in gcds.iteritems() if gcd_i*gcd_j%k == 0)
gcds[gcd_i] += 1
return result