Count of elements which is product of a pair or an element square | Python3 program to implement the above approach ; Stores all factors a number ; Function to calculate and store in a vector ; Function to return the count of array elements which are a product of two array elements ; Copy elements into a a duplicate array ; Sort the duplicate array ; Store the count of elements ; If the factors are not calculated already ; Traverse its factors ; If a pair of factors is found ; Driver Code
import math NEW_LINE v = [ [ ] for i in range ( 100000 ) ] NEW_LINE def div ( n ) : NEW_LINE INDENT global v NEW_LINE for i in range ( 2 , int ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT v [ n ] . append ( i ) NEW_LINE DEDENT DEDENT DEDENT def prodof2elements ( arr , n ) : NEW_LINE INDENT arr2 = arr . copy ( ) NEW_LINE arr2 . sort ( ) NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( len ( v [ arr [ i ] ] ) == 0 ) : NEW_LINE INDENT div ( arr [ i ] ) NEW_LINE DEDENT for j in v [ arr [ i ] ] : NEW_LINE INDENT if j in arr2 : NEW_LINE INDENT if int ( arr [ i ] / j ) in arr2 : NEW_LINE INDENT ans += 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 2 , 1 , 8 , 4 , 32 , 18 ] NEW_LINE N = len ( arr ) NEW_LINE print ( prodof2elements ( arr , N ) ) NEW_LINE