Problem
Chef wants to inherit land from his father's will. But his father chose Chef's elder brother to give all his land to, poor Chef :(.
However, Chef's elder brother is not selfish. He decides to give some land to Chef but on certain conditions. He has fences, the fence has length . He asked Chef to pick exactly four fences and cover a rectangular region of the land with them. Assume Chef's father had infinite area of land. Whatever area of the land Chef is able to cover, will become his own. Note that Chef cannot break a fence and has to use a fence as a whole (refer sample explanation for clarity).
Chef wants to acquire the maximum area of the land using the available fences or determine if it is impossible to form any rectangular area. Help him do so!
Input Format
- The first line contains a single integer , denoting the number of test cases. The description of each test case follows.
- The first line of each test case contains a single integer , denoting the number of fences.
- The second line contains space-separated integers, of them denoting the length of the fence.
Output Format
For each test case print the maximum area of land Chef can obtain or if it's impossible to form a rectangular area, in a separate line.
Constraints
- The sum of over all test cases does not exceed .
Sample 1:
2 6 3 1 2 3 4 2 4 6 4 5 4
6 -1
Explanation:
- In the first test case, Chef can pick fences with lengths and to construct a rectangle of area .
- In the second test case, Chef has to pick all four fences but he cannot form a rectangle using them.
Program :
#include<stdio.h>
void asc(int a[],int n)
{
int i, j, count;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(a[i]<a[j])
{
count = a[i];
a[i] = a[j];
a[j] = count;
}
}
int findArea(int arr[], int n)
{
asc(arr,n);
int dim[2] = { 0, 0 };
for (int i = 0, j = 0; i < n - 1 && j < 2; i++)
if (arr[i] == arr[i + 1])
dim[j++] = arr[i++];
return (dim[0] * dim[1]);
}
int main()
{
int t,i,n,a[1000],j,k,l,b;
scanf("%d",&t);
for(i=1;i<=t;i++)
{
scanf("%d",&n);
for(j=0;j<n;j++)
scanf("%d",&a[j]);
k=findArea(a,n);
if(k>0)
printf("%d\n",k);
else
printf("-1\n");
}
}
0 Comments