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