#include <bits/stdc++.h>
using namespace std;
int main()
{
//write your code here
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
set s;
for(int i=n-1;i>=0;i–)
{
for(int j=i-1;j>=0;j–)
{
if(a[i]<a[j])
{
s.insert(a[i]);
s.insert(a[j]);
}
}
}
if(s.empty())
{
cout<<a[n-1]<<" “;
}
for(auto x:s)
{
cout<<x<<” ";
}
cout<<endl;
}
return 0;
}
can anyone help me in finding the test case …i am unable to find it is giving me wrong answer.
Do you mean you want more test cases or a help regarding solution ?
Your Code has O(n^2) complexity. So it will give TLE.
You can think it like for every element of a_i you need to find that if the array is reverse sorted till last index.
Instead of using two loops try to take help from sorting in O(nlogn).
#include<stdio.h>
#include<stdlib.h>
void main()
{ int t;
scanf("%d",&t);
while(t–)
{ int n;
scanf("%d",&n);
int a[n],i,j;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=n-1;i>=0;i–)
{ for(j=n-1;j>=i;j–)
{
if(a[i]<a[j])
exit(0);
}
printf("%d\t",a[i]);
}
}
}
it is giving me wrong answer not tle error
why are you using exit(0)
??
exit(0)
terminates the program.
Use break
instead;
actually i used set so that they gonna print once but i think i am missing edge testcase