Length and Breadth of rectangle such that ratio of Area to diagonal ^ 2 is maximum | JAVA for finding maximum p ^ 2 / A ratio of rectangle ; 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
import java . util . * ; class GFG { static void findLandB ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; Vector < Double > arr_pairs = new Vector < Double > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { arr_pairs . add ( ( double ) arr [ i ] ) ; i ++ ; } } double length = arr_pairs . get ( 0 ) ; double breadth = arr_pairs . get ( 1 ) ; double size = arr_pairs . size ( ) ; for ( int i = 2 ; i < size ; i ++ ) { if ( ( length / breadth + breadth / length ) > ( arr_pairs . get ( i ) / arr_pairs . get ( i - 1 ) + arr_pairs . get ( i - 1 ) / arr_pairs . get ( i ) ) ) { length = arr_pairs . get ( i ) ; breadth = arr_pairs . get ( i - 1 ) ; } } System . out . print ( ( int ) length + " , ▁ " + ( int ) breadth + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 2 , 2 , 5 , 6 , 5 , 6 , 7 , 2 } ; int n = arr . length ; findLandB ( arr , n ) ; } }