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.
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 :
- 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[i-1] + arr_pairs[i-1]/arr_pairs[i])
- update length = arr_pairs[i], breadth = arr_pairs[i-1]
- if (length/breadth + breadth/length > arr_pairs[i]/arr_pairs[i-1] + arr_pairs[i-1]/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 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[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; } |
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<Double> arr_pairs = new Vector<Double>(); 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<Double> arr_pairs = new List<Double>(); 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[0]; double breadth = arr_pairs[1]; 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 |
2, 2
Time Complexity: O(N * log N)
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.
Recommended Posts:
- Maximum area of a Rectangle that can be circumscribed about a given Rectangle of size LxW
- Rectangle with minimum possible difference between the length and the width
- Cut all the rods with some length such that the sum of cut-off length is maximized
- Length of longest subarray of length at least 2 with maximum GCD
- Deriving the expression of Fibonacci Numbers in terms of golden ratio
- Divide an isosceles triangle in two parts with ratio of areas as n:m
- Program to find Length of Bridge using Speed and Length of Train
- Length of diagonals of a Rhombus using length of Side and vertex Angle
- Find a number M < N such that difference between their XOR and AND is maximum
- Length of the longest subsequence such that xor of adjacent elements is non-decreasing
- Minimize count of flips required such that no substring of 0s have length exceeding K
- Partition into two subarrays of lengths k and (N - k) such that the difference of sums is maximum
- Minimum integer with at most K bits set such that their bitwise AND with N is maximum
- Program to find the Area and Volume of Icosahedron
- Maximum Length Chain of Pairs | DP-20
- Maximum number of parallelograms that can be made using the given length of line segments
- Maximum count of sub-strings of length K consisting of same characters
- Find the minimum of maximum length of a jump required to reach the last island in exactly k jumps
- Length of longest sub-array with maximum arithmetic mean.
- Maximum Length Chain of Pairs | Set-2
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.

