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 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 :

  1. Sort the given array.
  2. Create an array (arr_pairs[]) of integers which occur twice in array.
  3. Set length = arr_pairs[0], breadth = arr_pairs[0].
  4. 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]
  5. Print length and breadth.

Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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

Output:

2, 2

Time Complexity: O(N * log N)



My Personal Notes arrow_drop_up

Discovering ways to develop a plane for soaring career goals

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



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.