# python – From a few integers, find triplets where the product of two numbers equals the third number

I'm trying to solve a coding challenge problem and the question is described as follows

N integers A1, A2, …, AN, count the number of triplets (x, y,
z) (with 1 ≤ x <y <z ≤ N) such that at least one of the following elements is
true:

Ax = Ay × Az and / or

Ay = Ax × Az, and / or

Az = Ax × Ay

5 2 4 6 3 1

In Case # 1, the only triplet satisfying the condition given in the problem statement is (2, 4, 5). The triplet is valid since the second, fourth and fifth integers are 2, 6 and 3, and 2 × 3 = 6. The answer is: 1

2 4 8 16 32 64

the six triplets satisfying the condition given in the statement of the problem are: (1, 2, 3), (1, 3, 4), (1, 4, 5), (1, 5, 6), ( 2, 3, 5), (2, 4, 6). so the answer is 6

My python code:

``````import itertools
account = 0
for itertools.combinations (1, 3):
if t[0]* t[1]== t[2] or t[1]* t[2]== t[0] or t[0]* t[2]== t[1]:
account + = 1
print (count)
``````

It is therefore a naive way to generate all combinations of 3 possible lengths and to check the condition. This works great for a smaller input but as the size of the inout increases, the time complexity increases. I suppose for an example that has `1,2,3,6,8` the generated combinations are `(2,3,6), (2,3,8)` 2,3,6 satisfy the condition, so the verification of 2,3,8 is unnecessary and can be avoided. How to modify my code to take advantage of this observation?