A sequence
of length is called good if any one of the following hold:
- The sequence is strictly increasing, or
- The sequence is strictly decreasing, or
- There exists an index such that:
- , for each
- , for each
For example, the sequences , , are good, while , are not.
You are given an array consisting of integers. Find a good sequence by rearranging the elements of the array or report that this is impossible. If there are multiple good sequences, print the lexicographically largest one.
Output Format
For each test case, if it is not possible to rearrange the given array into a good sequence, print . Otherwise, print a single line containing space-separated integers - the lexicographically largest good sequence that can be made.
Constraints
- Sum of over all test cases does not exceed
Code:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ll t;
cin>>t;
while(t--)
{
ll n,x,f=0;
cin>>n;
map<ll,ll> m;
vector<ll>res,v;
for(ll i=0;i<n;i++)
{
cin>>x;
m[x]++;
}
for(auto x: m)
{
if(x.second>2)
{
f=1;
break;
}
v.push_back(x.first);
}
sort(v.begin(),v.end());
if(f==1 || m[v[v.size()-1]]==2)
cout<<"-1";
else
{
for(ll i=0;i<v.size();i++)
{
if(m[v[i]]==2)
cout<<v[i]<<" ";
}
for(ll i=v.size()-1;i>=0;i--)
cout<<v[i]<<" ";
}
cout<<endl;
}
}
ConversionConversion EmoticonEmoticon