Can you help in pointing out any error in the code. I am a beginner on codechef

#include<bits/stdc++.h>
using namespace std;
int main()
{

    int t;
    cin>>t;
    while(t-->0)
    {
        int flag1=0;int flag=0;
        int n;cin>>n;
        int a[n+1];
        a[n]=0;
        vector<int>v;

        for(int i=0;i<n;i++)
        cin>>a[i];
        int c=1;int b[10000000];
        int z=0;
        for(int i=0;i<n;i++)
        {
            b[a[i]]++;
        }
        for(int i=0;i<n;i++)
        {
            
            if(a[i]==a[i+1])
            c++;
            else
            {
                if(c==b[a[i]])
                {
                    v.push_back(c);
                    c=1;
                    continue;
                }
                else
                {
                    flag=1;
                    break;
                }
            }
        }
        
        if(flag==1)
        cout<<("No")<<"\n";
        else
        {
                   set<int>s;
                   for(int x:v)
                   s.insert(x);
                   int t=s.size();
                   int q=v.size();
                   
                   if(q==t)
                   cout<<("Yes")<<"\n";
                   else
                   cout<<("No")<<"\n";

        }
    }
    }

Problem Link ?

1 Like

The problem link is: CHEFRECP Problem - CodeChef
Mistakes which I can see:

  • You have to output YES and NO, not Yes and No
  • if(a[i]==a[i+1])
    The above line will give you a RunTime error. This is because when i = n - 1, you are doing if(a[n - 1] == a[n]), but a[n] doesn’t exist. So it will give you a runtime error.
2 Likes

I have created the array of size n+1 and stored 0 in a[n] to check for the frequency of the last elements. The code is able to pass the given sample cases but somehow it is not getting submitted

Thanks bro, changing the case and initializing the frequency array to 0 worked

1 Like