Find pair with greatest product in array | C # program to find the largest product number ; Function to find greatest number ; Store occurrences of all elements in hash array ; m [ arr [ i ] ] ++ ; Sort the array and traverse all elements from end . ; For every element , check if there is another element which divides it . ; Check if the result value exists in array or not if yes the return arr [ i ] ; To handle the case like arr [ i ] = 4 and arr [ j ] = 2 ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int findGreatest ( int [ ] arr , int n ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . ContainsKey ( arr [ i ] ) ) { var a = m [ arr [ i ] ] + 1 ; m . Add ( arr [ i ] , a ) ; } else { m . Add ( arr [ i ] , arr [ i ] ) ; } } Array . Sort ( arr ) ; for ( int i = n - 1 ; i > 1 ; i -- ) { for ( int j = 0 ; j < i && arr [ j ] <= Math . Sqrt ( arr [ i ] ) ; j ++ ) { if ( arr [ i ] % arr [ j ] == 0 ) { int result = arr [ i ] / arr [ j ] ; if ( result != arr [ j ] && m [ result ] == null m [ result ] > 0 ) { return arr [ i ] ; } else if ( result == arr [ j ] && m [ result ] > 1 ) { return arr [ i ] ; } } } } return - 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 17 , 2 , 1 , 15 , 30 } ; int n = arr . Length ; Console . WriteLine ( findGreatest ( arr , n ) ) ; } }