Count of elements which is product of a pair or an element square | C # 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
using System ; using System . Collections . Generic ; class GFG { static List < int > [ ] v = new List < int > [ 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 ] ; } Array . Sort ( arr2 ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( v [ arr [ i ] ] . Count == 0 ) div ( arr [ i ] ) ; foreach ( int j in v [ arr [ i ] ] ) { if ( Array . BinarySearch ( arr2 , j ) >= 0 && Array . 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 List < int > ( ) ; Console . Write ( prodof2elements ( arr , N ) ) ; } }