My issue
I am writing the same code but it provide the different output but according to given constraints its output is true.
My code
import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner in=new Scanner(System.in);
int t=in.nextInt();
while(t-->0){
int n=in.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++) a[i]=in.nextInt();
Arrays.sort(a);
if(a[0]==a[n-1]) System.out.println(-1);
else{
int i=2,j=n-2;
System.out.print(a[0]+" "+a[n-1]+" "+a[1]+" ");
while(i<=j){
if(i==j) System.out.print(" "+a[j--]);
else System.out.print(a[j--]+" "+a[i++]);
}
System.out.println();
}
}
}
}
Problem Link: CONCUSSIVE Problem - CodeChef
@siatharva
your logic is not right .
This is my c++ code .
Plzz refer it 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;
}
}