//Can anyone help me to know where am I going wrong
// I adapted the same ideology as given in this editorial but still got WA
`import java.util.*;
public class q2n{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
for(int z=0;z<t;z++){
int n=sc.nextInt();
Integer a[]=new Integer[n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
Arrays.sort(a);
boolean flag=true;
for(int i=0;i<n-1;i++){
if(a[i]==a[i+1])
flag=false;
}
if(flag==true){
System.out.println(“YES”);
for(int i=0;i<n;i++)
System.out.print(a[i]+" “);
}
else{
boolean f=true;
for(int i=0;i<n-2;i++){
if(a[i]==a[i+1] && a[i]==a[i+2]){
f=false;
break;
}
}
if(a[n-2]==a[n-1])
f=false;
if(f==false)
System.out.print(“NO”);
else{
ArrayList al=new ArrayList<>();
for(int i=0;i<n-1;i++){
al.add(a[i]);
if(a[i]==a[i+1])
i++;
}
Arrays.sort(a,Collections.reverseOrder());
for(int i=0;al.size()<n;i++){
al.add(a[i]);
if(a[i]==a[i+1])
i++;
}
System.out.println(“YES”);
for(int i=0;i<n;i++)
System.out.print(al.get(i)+” ");
}
}
System.out.println();
}
}
}
I am getting runtime error on running my code but it is getting successfully submitted.why this is happening so?
plz help
my code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t --)
{
int flag=0;
int n;
cin>>n;
int a[n];
int maxx=0;
map<int,int>m;
for(int i=0;i<n;i++)
{
cin>>a[i];
m[a[i]]++;
if(m[a[i]]>=3)
{
flag=1;
}
if (a[i]>maxx)
{
maxx=a[i];
}
}
if (flag==1)
{
cout<<"NO"<<endl;
continue;
}
if (m[maxx]>1)
{
cout<<"NO"<<endl;
continue;
}
cout<<"YES"<<endl;
for (auto i=m.begin();i!=m.end();i++)
{
cout<<i->first<<" ";
i->second-=1;
}
for (auto i=m.rbegin();i!=m.rend();i++)
{
if(i->second!=0)
cout<<i->first<<" ";
}
cout<<endl;
}
}
Nice and simple
I did it a similar way here . The difference is I used the same loop for the following:
It worked @pankaj_chopra . Didn’t know it will make such a huge difference. Thank you so much.
Hi,
I don’t know why my code is giving me a TLE. I have only used single for loop which does work in O(n) and done rest of the work via map which does work in O(1).
Plz help why is O(n) not working here.
https://www.codechef.com/viewsolution/34932382
Do not print elements of array one by one. Store it in a String builder object and then print.
CodeChef: Practical coding for everyone .
Also try to use fast I/O methods instead of Scanner.