# Help me in understanding output in CONCUSSIVE problem

### My issue

Input:
3
3
1 1 1
5
1 2 3 4 5
5
1 1 2 2 1

Output
-1
1 4 2 5 3
1 2 1 2 1

Can someone explain why this output is incorrect?

### My code

``````#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
vector<int>v(n, 0);
for(int i=0; i<n; i++) cin>>v[i];
sort(v.begin(), v.end());
vector<int>a(n, 0);
int j=0;
for(int i=0; i<n ;i+=2){
a[i] = v[j++];
}

for(int i=1; i<n; i+=2){
a[i] = v[j++];
}
bool flag = 1;
for(int i=1; i<n-1; i++){
if( (a[i] > a[i+1] && a[i] > a[i-1]) || (a[i] < a[i+1] && a[i] < a[i-1]) ){
flag = 1;
}
else {
flag = 0;
break ;
}
}
if(!flag) {
cout<<-1<<endl;
}
else{
for(int i=0; i<n; i++){
cout<<a[i];
if(i != n-1) cout<<" ";
}
cout<<endl;
}
}
return 0;
}

``````

Problem Link: CONCUSSIVE Problem - CodeChef

@gargmanik6080
your logic is slightly wrong .
plzz refer the following solution for better understanding of the logic.

``````#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<algorithm>
#include<numeric>

using namespace std;
typedef long long int lli;
using namespace __gnu_pbds;
template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//typedef long long int lli;
//typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> multi_pbds;
//typedef tree<pair<int , int>, null_type, less<pair<int ,int>>, rb_tree_tag, tree_order_statistics_node_update> pbdsp;
//typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
#define nl "\n";
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define db long double
#define prq priority_queue<lli>
#define psq priority_queue<lli,vector<lli>,greater<lli>>
#define mod 1000000007
#define lb lower_bound
#define ub upper_bound
#define vlli vector<lli>
#define mslli multiset<lli>
#define inf 1e17
#define sp " "
#define pb push_back
#define pie 3.14159265358979323846
#define test lli t; cin>>t; while(t--)

int32_t main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
test{
lli n,ch=0;
cin>>n;
lli a[n];
for(lli i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n);
lli b[n];
lli j=0;
for(int i=0;i<n;i+=2)
{
b[i]=a[j];
j++;
}
for(int i=1;i<n;i+=2)
{
b[i]=a[j];
j++;
}
for(int i=1;i<n-1;i++)
{
if(b[i]>b[i-1]&&b[i]>b[i+1])
{
continue;
}
else if(b[i]<b[i-1]&&b[i]<b[i+1])
continue;
else
{
ch=1;
}
}
if(ch)
{
j=0;
for(int i=1;i<n;i+=2)
{
b[i]=a[j];
j++;
}
for(int i=0;i<n;i+=2)
{
b[i]=a[j];
j++;
}
for(int i=1;i<n-1;i++)
{
if(b[i]>b[i-1]&&b[i]>b[i+1])
{
continue;
}
else if(b[i]<b[i-1]&&b[i]<b[i+1])
continue;
else
{
ch=2;
}
}
if(ch==1)
{
for(int i=0;i<n;i++)
{
cout<<b[i]<<" ";
}
}
else
cout<<-1;
}
else
{
for(int i=0;i<n;i++)
{
cout<<b[i]<<" ";
}
}
cout<<endl;
}

}
``````