I tried this solution but idk why it’s failing test cases. Can anyone look it up?
import math
import os
import collections
from itertools import permutations, combinations
for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
c = combinations(arr, 2)
count = 0
for a,b in tuple(c):
prod = a*b
den = (arr.index(a)+1) * (arr.index(b)+1)
if prod%den == 0:
count += 1
print(count)
Even I considered the possibility of duplicate elements and modified the above code as follows:
import math
import os
import collections
from itertools import permutations, combinations
for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
c = combinations(arr, 2)
count = 0
index = list((i+1,j+1) for ((i,_),(j,_)) in combinations(enumerate(arr), 2))
values = list(k for k in combinations(arr,2))
for x,y in zip(values, index):
if (x[0]*x[1]) % (y[0]*y[1]) == 0:
count += 1
print(count)
This code is passing a few test cases but showing Runtime error for rest of them.
Can anyone suggest modifications or is my code completely wrong?