Find pair with greatest product in array | 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
function findGreatest ( arr , n ) { let m = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( m . has ( arr [ i ] ) ) { m . set ( arr [ i ] , m [ arr [ i ] ] + 1 ) ; } else { m . set ( arr [ i ] , m . get ( arr [ i ] ) ) ; } } arr . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = n - 1 ; i > 1 ; i -- ) { for ( let j = 0 ; j < i && arr [ j ] <= Math . sqrt ( arr [ i ] ) ; j ++ ) { if ( arr [ i ] % arr [ j ] == 0 ) { let result = Math . floor ( 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 ; } let arr = [ 17 , 2 , 1 , 15 , 30 ] ; let n = arr . length ; document . write ( findGreatest ( arr , n ) ) ;