给定正整数数组。任务是从给定的数组中选择一对元素,以使其代表矩形的长度和宽度,并且其面积与对角线2的比率最大。
注意:数组必须包含矩形的所有边。也就是说,您可以从出现至少两次的数组中选择元素,因为矩形的两个边的长度相同,而两个边的宽度相同。
例子:
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.
下面给出了矩形的一些属性,这些属性必须要形成。
- 只有当我们至少有一对相等的整数时,才能形成一个矩形。一次出现的整数不能是任何矩形的一部分。因此,在我们的解决方案中,我们将仅考虑出现次数不止一次的整数。
- 面积与直径之比2为lb /(l 2 + b 2 )是单调递减函数
就一个变量而言这意味着,如果我们固定长度,则随着宽度的减小,比率将减小,因此固定宽度相同。利用这一点,我们不需要遍历整个数组来找到固定宽度的长度。
算法 :
- 对给定的数组进行排序。
- 创建一个整数数组(arr_pairs []),该整数在数组中出现两次。
- 设置长度= arr_pairs [0],宽度= arr_pairs [0]。
- 从i = 2迭代到sizeof(arr_pairs)
- if(长度/宽度+宽度/长度> arr_pairs [i] / arr_pairs [i-1] + arr_pairs [i-1] / arr_pairs [i])
- 更新长度= arr_pairs [i],宽度= arr_pairs [i-1]
- if(长度/宽度+宽度/长度> arr_pairs [i] / arr_pairs [i-1] + arr_pairs [i-1] / arr_pairs [i])
- 打印长度和宽度。
下面是上述方法的实现。
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 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 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[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
时间复杂度: O(N * log N)