Problem Link: CONCUSSIVE Problem - CodeChef
Feedback
I don’t know how my code works fine and also gives correct output. But I found a test case {1,2,2,2,5} where my code doesn’t gives correct output. It’s as usual as codechef.
I have below attached my code:
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int arr[n];
// vector<int>brr(n,0);
int c=0;
map<int,int>mp;
for(int i=0;i<n;i++){
cin>>arr[i];
mp[arr[i]]++;
c=max(c,mp[arr[i]]);
}
// sort(arr,arr+n);
int max_occur=(n+1)/2;
if(c>max_occur){
cout<<-1<<endl;
continue;
}
sort(arr,arr+n);
int i=(n+1)/2;
int k=i-1;
int j=n-1;
if(n&1)
cout<<arr[0]<<" ";
if(n&1){
while(j>=i){
cout<<arr[j]<<" "<<arr[k]<<" ";
j--;
k--;
}
}
else{
while(k>=0){
cout<<arr[k]<<" "<<arr[j]<<" ";
j--;
k--;
}
}
cout<<endl;
}
return 0;
}