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.
// 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 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 |
# 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# 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 |
<script> // Javascript for finding maximum // p^2/A ratio of rectangle // function to print length and breadth function findLandB(arr, n)
{ // sort the input array
arr.sort();
// create array vector of integers occurring in pairs
var arr_pairs =[];
for ( var i = 0; i < n; i++) {
// push the same pairs
if (arr[i] == arr[i + 1]) {
arr_pairs.push(arr[i]);
i++;
}
}
var length = arr_pairs[0];
var breadth = arr_pairs[1];
var size = arr_pairs.length;
// calculate length and breadth as per requirement
for ( var 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
document.write( length + ", " + breadth );
} // Driver Code var arr = [ 4, 2, 2, 2, 5, 6, 5, 6, 7, 2 ];
var n = arr.length;
findLandB(arr, n); </script> |
2, 2
Time Complexity: O(N * log N)
Auxiliary Space: O(N)