Monday, 22 August 2016

Begginer Problem - Sticks

Hello all, after a long vacation, I come back with some new problems and new algorithms. Here is one for begginers and not only:

 "Mr. Algorithm and his little brother are playing with sticks. They have total N sticks. Length of i-th stick is Ai. Mr. Algorithm asks his brother to choose any four sticks and to make a rectangle with those sticks its sides. Mr. Algorithm warns his brother to not to break any of the sticks, he has to use sticks as a whole. Also, he wants that the rectangle formed should have the maximum possible area among all the rectangles that Mr. Algorithm's brother can make. Mr. Algorithm's little brother takes this challenge up and overcomes it. Can you also do so? That is, you have to tell whether it is even possible to create a rectangle? If yes, then you have to tell the maximum possible area of rectangle."  

Input: The first line contains a single integer T denoting the number of test-cases. T test cases follow. The first line of each test case contains a single integer N denoting the number of sticks. The second line of each test case contains N space-separated integers A1, A2, ..., AN denoting the lengths of sticks.  

Output: For each test case, output a single line containing an integer representing the maximum possible area for rectangle or -1 if it's impossible to form any rectangle using the available sticks.

1 ≤ T ≤ 100
1 ≤ N ≤ 103
1 ≤ sum of N's over all test-cases in a single test file ≤ 103
1 ≤ Ai ≤ 103

1 2 3 1 2
1 2 2 3

2 -1

If you want, you can check my solution below for just 1 euro:

No comments:
Write comments