Length and Breadth of rectangle such that ratio of Area to diagonal ^ 2 is maximum | CPP 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findLandB ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; vector < double > arr_pairs ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { arr_pairs . push_back ( arr [ i ] ) ; i ++ ; } } double length = arr_pairs [ 0 ] ; double breadth = arr_pairs [ 1 ] ; double size = arr_pairs . size ( ) ; for ( int 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 ] ; } } cout << length << " , ▁ " << breadth << endl ; } int main ( ) { int arr [ ] = { 4 , 2 , 2 , 2 , 5 , 6 , 5 , 6 , 7 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findLandB ( arr , n ) ; return 0 ; }