Count of elements which is product of a pair or an element square | Java 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 java . util . * ; class GFG { static Vector < Integer > [ ] v = new Vector [ 100000 ] ; static void div ( int n ) { for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { v [ n ] . add ( i ) ; } } } static int prodof2elements ( int arr [ ] , int n ) { int [ ] arr2 = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr2 [ i ] = arr [ i ] ; } Arrays . sort ( arr2 ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( v [ arr [ i ] ] . size ( ) == 0 ) div ( arr [ i ] ) ; for ( int j : v [ arr [ i ] ] ) { if ( Arrays . binarySearch ( arr2 , j ) >= 0 && Arrays . binarySearch ( arr2 , ( int ) arr [ i ] / j ) >= 0 ) { ans ++ ; break ; } } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 8 , 4 , 32 , 18 } ; int N = arr . length ; for ( int i = 0 ; i < v . length ; i ++ ) v [ i ] = new Vector < Integer > ( ) ; System . out . print ( prodof2elements ( arr , N ) ) ; } }