# Length and Breadth of rectangle such that ratio of Area to diagonal^2 is maximum

• Difficulty Level : Medium
• Last Updated : 30 Apr, 2021

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 diagonal2 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 Diameter2.
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 Diameter2.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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/(l2 + b2) 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 :

1. Sort the given array.
2. Create an array (arr_pairs[]) of integers which occur twice in array.
3. Set length = arr_pairs, breadth = arr_pairs.
4. Iterate from i=2 to sizeof (arr_pairs)
• update length = arr_pairs[i], breadth = arr_pairs[i-1]

Below is the implementation of the above approach.

## C++

 `// CPP for finding maximum``// p^2/A ratio of rectangle``#include ``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 occurring 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;``    ``double` `breadth = arr_pairs;``    ``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);``    ``findLandB(arr, n);``    ``return` `0;``}`

## Java

 `// JAVA for finding maximum``// p^2/A ratio of rectangle``import` `java.util.*;` `class` `GFG``{` `// function to print length and breadth``static` `void` `findLandB(``int` `arr[], ``int` `n)``{``    ``// sort the input array``    ``Arrays.sort(arr);` `    ``// create array vector of integers occurring in pairs``    ``Vector arr_pairs = ``new` `Vector();``    ``for` `(``int` `i = ``0``; i < n - ``1``; i++)``    ``{` `        ``// push the same pairs``        ``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();` `    ``// 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.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``);``        ``}``    ``}` `    ``// print the required answer``    ``System.out.print((``int``)length + ``", "` `+ (``int``)breadth +``"\n"``);``}` `// Driver Code``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);``}``}` `// This code is contributed by PrinciRaj1992`

## 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``    ``# occurring 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`

## C#

 `// C# for finding maximum``// p^2/A ratio of rectangle``using` `System;``using` `System.Collections.Generic;` `class` `GFG``{` `// function to print length and breadth``static` `void` `findLandB(``int` `[]arr, ``int` `n)``{``    ``// sort the input array``    ``Array.Sort(arr);` `    ``// create array vector of integers occurring in pairs``    ``List arr_pairs = ``new` `List();``    ``for` `(``int` `i = 0; i < n - 1; i++)``    ``{` `        ``// push the same pairs``        ``if` `(arr[i] == arr[i + 1])``        ``{``            ``arr_pairs.Add((``double``) arr[i]);``            ``i++;``        ``}``    ``}` `    ``double` `length = arr_pairs;``    ``double` `breadth = arr_pairs;``    ``double` `size = arr_pairs.Count;` `    ``// 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``    ``Console.Write((``int``)length + ``", "` `+ (``int``)breadth +``"\n"``);``}` `// Driver Code``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);``}``}` `// This code is contributed by Rajput-Ji`

## Javascript

 ``
Output:
`2, 2`

Time Complexity: O(N * log N)

My Personal Notes arrow_drop_up