Length and Breadth of rectangle such that ratio of Area to diagonal^2 is maximum
Given an array of positive integers. The task is to choose a pair of elements from the given array such that they represent the length and breadth of a rectangle and the ratio of its area and its diagonal^{2} is maximum.
Note: The array must contains all sides of the rectangle. That is you can choose elements from array which appears atleast twice as a rectangle has two sides of same length and two sides of same breadth.
Examples:
Input: arr[] = {4, 3, 5, 4, 3, 5, 7}
Output: 5, 4
Among all pairs of length and breadth 5, 4 will generate maximum ratio of Area to Diameter^{2}.Input: arr[] = {2, 2, 2, 2, 2, 2}
Output: 2, 2
There is only one possible pair of length and breadth 2, 2 which will generate maximum ratio of Area to Diameter^{2}.
Given below are some properties of the rectangle that are to be satisfied in order to form it.
 A rectangle can only be formed when we have at least pair of equal integers. An integer occurring once can’t be a part of any rectangle. So, in our solution, we will consider only the integers whose occurrence is more than once.
 The ratio of Area and its diameter ^{2} is lb/(l^{2} + b^{2}) is a monotonic decreasing function
in term of one variable. This means if we are fixing the length then as we will decrease the breadth the ratio got decreases and accordingly same for fixed breadth. Taking advantage of this we need not iterate the whole array for finding the length for a fixed breadth.
Algorithm :
 Sort the given array.
 Create an array (arr_pairs[]) of integers which occur twice in array.
 Set length = arr_pairs[0], breadth = arr_pairs[0].

Iterate from i=2 to sizeof (arr_pairs)
 if (length/breadth + breadth/length > arr_pairs[i]/arr_pairs[i1] + arr_pairs[i1]/arr_pairs[i])
 update length = arr_pairs[i], breadth = arr_pairs[i1]
 if (length/breadth + breadth/length > arr_pairs[i]/arr_pairs[i1] + arr_pairs[i1]/arr_pairs[i])
 Print length and breadth.
Below is the implementation of the above approach.
C++
// CPP for finding maximum // p^2/A ratio of rectangle #include <bits/stdc++.h> using namespace std; // function to print length and breadth void findLandB( int arr[], int n) { // sort the input array sort(arr, arr + n); // create array vector of integers occuring in pairs vector< double > arr_pairs; for ( int i = 0; i < n; i++) { // push the same pairs 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(); // calculate length and breadth as per requirement for ( int i = 2; i < size; i++) { // check for given condition 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]; } } // print the required answer cout << length << ", " << breadth << endl; } // Driver Code 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; } 
Python3
# Python 3 for finding maximum p^2/A
# ratio of rectangle
# function to print length and breadth
def findLandB(arr, n):
# sort the input array
arr.sort(reverse = False)
# create array vector of integers
# occuring in pairs
arr_pairs = []
for i in range(n – 1):
# push the same pairs
if (arr[i] == arr[i + 1]):
arr_pairs.append(arr[i])
i += 1
length = arr_pairs[0]
breadth = arr_pairs[1]
size = len(arr_pairs)
# calculate length and breadth as
# per requirement
for i in range(1, size – 1):
# check for given condition
if ((int(length / breadth) +
int(breadth / length)) >
(int(arr_pairs[i] / arr_pairs[i – 1]) +
int(arr_pairs[i – 1] / arr_pairs[i]))):
length = arr_pairs[i]
breadth = arr_pairs[i – 1]
# print the required answer
print(length, “,”, breadth)
# Driver Code
if __name__== ‘__main__’:
arr = [4, 2, 2, 2, 5, 6, 5, 6, 7, 2]
n = len(arr)
findLandB(arr, n)
# This code is contributed by
# Surendra_Gangwar
2, 2
Time Complexity: O(N * log N)
Recommended Posts:
 Rectangle with minimum possible difference between the length and the width
 Maximum Length Chain of Pairs  DP20
 Maximum Length Chain of Pairs  Set2
 Length of longest subarray with maximum arithmetic mean.
 Maximum count of substrings of length K consisting of same characters
 Maximum number of parallelograms that can be made using the given length of line segments
 Find the minimum of maximum length of a jump required to reach the last island in exactly k jumps
 Program to find Length of Bridge using Speed and Length of Train
 Divide an isosceles triangle in two parts with ratio of areas as n:m
 Deriving the expression of Fibonacci Numbers in terms of golden ratio
 Cut all the rods with some length such that the sum of cutoff length is maximized
 Program to find the Area of an Ellipse
 Program to find the Area and Volume of Icosahedron
 Program to calculate area of an Circle inscribed in a Square
 Area of largest Circle inscribe in Nsided Regular polygon
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : SURENDRA_GANGWAR