Search code examples
pythonlistinputanalyticsdata-handling

Extracting the numbers of two elements which their sum is equal to their multiplication


we have array A that has N len(N = the len of the array) I have to found the numbers of pairs (J, I) that work on the following statement:

A[J]+A[I} == A[J] * A[I]

1<=i, j<=N

(1 ≤ N ≤ 40000)

(0 ≤ A(i) ≤ 10^9)

example:

input:

3
2 4 2

output:

1

well i counldn't know how to limit the input size to only have 2 spaces or to split it if there was any more than the N

edit*

A = []
N = int(input(""))
B = (input(""))

B = B.split()
z = 0
myList = []
mylist2= []
pairs = 0
for q in B:
    if z < N:
        myList.append(q)
        z += 1
    elif z >= N:
        break

for w in myList:
    w = int(w)
    mylist2.append(w)

for i in mylist2:
    for k in mylist2:`enter code here`
        if i + k == i * k:
            pairs+1

that what i have done so far


Solution

  • So, as already mentioned in comments only pairs (2, 2) and (0, 0) satisfy the condition. The number of (0, 0) pairs is count(0) * (count(0) - 1) / 2. The same for (2, 2) pairs. Expressing this in python (assuming that array a is given).

    def countsumprod(a):
        c0 = a.count(0)
        c2 = a.count(2)
        return (c0 * (c0 - 1) + c2 * (c2 - 1)) // 2