-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ3.py
39 lines (26 loc) · 772 Bytes
/
Q3.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
def countTriplets(arr, r):
potential = {}
count = {}
total = 0
for val in arr:
if val in count:
total += count[val]
if val in potential:
if val * r in count:
count[val * r] += potential[val]
else:
count[val * r] = potential[val]
if val * r in potential:
potential[val * r] += 1
else:
potential[val * r] = 1
return total
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
nr = input().rstrip().split()
n = int(nr[0])
r = int(nr[1])
arr = list(map(int, input().rstrip().split()))
ans = countTriplets(arr, r)
fptr.write(str(ans) + '\n')
fptr.close()