Length and Breadth of rectangle such that ratio of Area to diagonal ^ 2 is maximum | function to print length and breadth ; sort the input array ; create array vector of integers occurring in pairs ; push the same pairs ; calculate length and breadth as per requirement ; check for given condition ; print the required answer ; Driver Code
function findLandB ( arr , n ) { arr . sort ( ) ; var arr_pairs = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { arr_pairs . push ( arr [ i ] ) ; i ++ ; } } var length = arr_pairs [ 0 ] ; var breadth = arr_pairs [ 1 ] ; var size = arr_pairs . length ; for ( var i = 2 ; i < size ; i ++ ) { if ( ( length / breadth + breadth / length ) > ( arr_pairs [ i ] / arr_pairs [ i - 1 ] + arr_pairs [ i - 1 ] / arr_pairs [ i ] ) ) { length = arr_pairs [ i ] ; breadth = arr_pairs [ i - 1 ] ; } } document . write ( length + " " + breadth ) ; } var arr = [ 4 , 2 , 2 , 2 , 5 , 6 , 5 , 6 , 7 , 2 ] ; var n = arr . length ; findLandB ( arr , n ) ;