December Lunchtime 2021 - Maximum Trio - MXMTRIO
Maximum Trio Problem Code: MXMTRIO
You are given an array of elements. For any ordered triplet such that , , and are pairwise distinct and , the value of this triplet is . You need to find the maximum value among all possible ordered triplets.
Note: Two ordered triplets and are only equal when and and . As an example, and are two different ordered triplets.
Input Format
- The first line of the input contains a single integer - the number of test cases. The test cases then follow.
- The first line of each test case contains an integer .
- The second line of each test case contains space-separated integers .
Output Format
For each test case, output the maximum value among all different ordered triplets.
Constraints
- Sum of over all testcases won't exceed .
Subtasks
- Subtask (30 points): Sum of over all cases won't exceed .
- Subtask (70 points): Original constraint
Sample Input 1
3
3
1 1 3
5
3 4 4 1 2
5
23 17 21 18 19
Sample Output 1
2
12
126
Explanation
- Test case : The desired triplet is , which yields the value of .
- Test case : The desired triplet is , which yields the value of .
- Test case : The desired triplet is , which yields the value of .
Solution
#include <bits/stdc++.h>
using namespace std;
int main()
{
int tc;
cin >> tc;
while (tc--)
{
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
sort(arr, arr + n);
long ans = (long)(arr[n - 1] - arr[0]) * arr[n - 2];
cout << ans << endl;
}
return 0;
}
Comments
Post a Comment