# Chef and Distinct Numbers i have just started coding please tell me whats wrong in my code though my output is right

#include
using namespace std;

int main() {
int T,N,i,j,c=0;
int a[100000];
cin>>T;
while(T>=1 && T<=100)
{
cin>>N;
if(N>=2 && N<=100000)
{

``````          for(i=0;i<N;i++)
{
cin>>a[i];
}
for(j=0;j<N;j++)
{
for(i=j+1;i<N;i++)
{
if(a[j]==a[i])
{
c++;
}
}
}
}
if(c>0)
{
cout<<"yes";
cout<<"\n";
}
else
{
cout<<"no";
cout<<"\n";
}
c=0;
T--;
}
return 0;
``````

}

1)Print â€śYesâ€ť, and â€śNoâ€ť, instead of â€śyesâ€ť and â€śnoâ€ť.
2) Try an O(N) or O(NlogN) approach per test case.

### O(NlogN) per test case:

Sort the array in O(NlogN). Check every consecutive element. If any pair of consecutive elements contains equal numbers, print â€śYesâ€ť, else print â€śNoâ€ť.

### O(N) per test case:

Use a set, which gives O(1) complexity in set.contains(number) operation.
Iterate through the elements, each time checking first, whether the set already contains it.
If the set contains it, the answer is â€śYesâ€ť, else add the number to the set.
Eventually, if you havenâ€™t got a â€śYesâ€ť, print â€śNoâ€ť.

1 Like

thanks bro it worked @akashbhalotia

1 Like