Find a pair in Array with second largest product | Function to find second largest product pair in arr [ 0. . n - 1 ] ; No pair exits ; Initialize max product pair ; Traverse through every possible pair and keep track of largest product ; If pair is largest ; Second largest ; If pair dose not largest but larger then second largest ; Print the pairs ; Given array ; Function Call
function maxProduct ( arr , N ) { if ( N < 3 ) { return ; } let a = arr [ 0 ] , b = arr [ 1 ] ; let c = 0 , d = 0 ; for ( let i = 0 ; i < N ; i ++ ) for ( let j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] * arr [ j ] > a * b ) { c = a , d = b ; a = arr [ i ] , b = arr [ j ] ; } if ( arr [ i ] * arr [ j ] < a * b && arr [ i ] * arr [ j ] > c * d ) c = arr [ i ] , d = arr [ j ] ; } document . write ( c + " " + d ) ; } let arr = [ 5 , 2 , 67 , 45 , 160 , 78 ] ; let N = arr . length ; maxProduct ( arr , N ) ;