CHEFRECP - Editorial

can someone help me find what is wrong with my code

void solve(){
	int n ;
	std::cin >> n;
	int arr[n];
	for(auto &i:arr) std::cin >> i;
	int ok = 1;
	int c = 1;
	int freq[1001] = {0};
	int last = arr[0];
	for(int i = 1 ; i< n;i++){
		if(last == arr[i]){
			c++;
		}
		int a = 0;
		if(last != arr[i]){
			for(int j = 0 ; j < 1001; j++){
				if(c == freq[j])
					a = 1;
			}
			freq[arr[i-1]] = c;
			if(a == 1 || (freq[arr[i]]!= 0)){
				ok =0;
				break;
			}
			c = 1;
			last = arr[i];
		}

	}
	if(ok)	yes;
	else 	no;

}

Here is my Code, Can u tell me where it is failing, Looks like I cant find where it is failing.
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while(t–){
int n;
cin >> n;

    int input[1001];
    bool check = false;
    for(int i = 1; i <= 1000; i++){
        input[i] = 0;
    }
    int b = 0;
    for(int i = 0; i < n; i++){
        int a;
        cin >> a;
        if(input[a] != 0){
            if(b == a){
                input[a]++;
            }else{
                cout << "NO" << endl;
                check = true;
                break;
            }
        }else{
            input[a]++;
        }
        b = a;
    }
    if(check){
        continue;
    }
    unordered_set<int> s;
    for(int i = 1; i <= 1000; i++){
        int c = input[i];
        if(c > 0 && s.count(c) == 1){
            cout << "NO" << endl;
            check = true;
            break;
        }
        
        if(c > 0){
            s.insert(c);
        }
    }
    if(check) continue;
    cout << "YES" << endl;
}

}

Thanks for this man!

@kitydexter @mayank94605 @maskedcarrot @maazk8114 Can you guys share your submission links?

This is my code during contest https://www.codechef.com/viewsolution/33287341 Which gave me TLE.
The same code submitted in practice https://www.codechef.com/viewsolution/33343952, this is giving AC.
Something wrong with the Testing Systems? My code is correct I am confirm, everything is alright. It was codechef servers which cost me a impractical penalty

Here is mine submission link:
https://www.codechef.com/viewsolution/33308377

Here is the code which has error.

There are multiple test-cases are there. You should read all the input accordingly.

Here is a test on which your code gives the wrong answer.

2
1
1
2
1 2

I’ll inform the admin about the issue…maybe issue related to new cloud based servers.

CHEFRECP Using Freq Array

Hi there Could you please tell me why I am getting wrong answer on submission even though my test cases and the editorial test case {1, 5, 2, 5, 2, 2} yields right output. Thanks

#include<iostream>
using namespace std;
int main(){
    int t;
    cin>>t;
    int freq[1001] = {0};
    while(t--){
        int n,i; cin>>n;
        int a[n];
        for(i=0;i<n;i++){
            cin>>a[i];
            freq[a[i]]++;
        }
        // 1 1 4 3 4 7 7 7
        string ans;
        for(i=0;i<n;i++){
            if((freq[i]>1) && (a[i]!=a[i+1])){
                ans = "NO";
                break;
            }
            else
            {
                ans = "YES";
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

Hi @rishup_nitdgp my code is yielding an o/p “YES”. But on submission my code is producing wrong o/p. Could you please check the following code -

#include<iostream>
using namespace std;
int main(){
    int t;
    cin>>t;
    int freq[1001] = {0};
    while(t--){
        int n,i; cin>>n;
        int a[n];
        for(i=0;i<n;i++){
            cin>>a[i];
            freq[a[i]]++;
        }
        // 1 1 4 3 4 7 7 7
        string ans;
        for(i=0;i<n;i++){
            if((freq[i]>1) && (a[i]!=a[i+1])){
                ans = "NO";
                break;
            }
            else
            {
                ans = "YES";
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

Here is my C++ code for this problem:

Can’t understand why it shows Wrong Answer. Please help!

Can anyone please tell me where i went wrong.
My code satisfies the preliminary tests but gives wrong answer on submission.
This is the link to my code
https://www.codechef.com/viewsolution/33350348

@dhairyaostwal There is an error in this line. It should be:

`if((freq[a[i]]>1) && (a[i]!=a[i+1])){`

Here is a test case for which your code giving the wrong answer. Same for @kitydexter

2
4
1 2 1 1
2
1 2

P.S. Whenever there are test cases, be careful about reading the input.

https://www.codechef.com/viewsolution/33352678
I don’t know why i am getting wrong answer??
Please Help

Thanks for replying @rishup_nitdgp but still I’m not getting the right answer.
Could you please check.

https://www.codechef.com/viewsolution/33353024

Thanks

@dhairyaostwal The code is wrong due to freq array. It is globle and you are not clearing it in each test case. Here a sample for which your code gives wrong answer.

2
1
1
2
1 1

Here a test case for which your code is giving wrong answer.

1
4
1 2 2 1

Thank u so much for pointing out the mistake.My code is now successfully submitted.

Help me out…where i am wrong…am getting SIGABRT error in this…though sample test case passed…#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main() {
int t;
cin>>t;
while(t–){
int n;
cin>>n;
string y=“YES”;
vector c;
vector v(n);
vector b(n,0);
for(int i=0;i<n;i++){
int a;
cin>>a;
v[i]=a;
}

    for(int i=0;i<n;){
        int x=v[i];
        if(b[x]==0){
        int count=0;
        while(v[i]==x && i<n){
            count++;
            i++;
        }
        b[x]=1;
        c.push_back(count);
        }
        else{
            y="NO";
            i++;
            break;
        }
    }
    for(int i=0;i<c.size();i++){
        int b=c[i];
        int cnt=0;
        for(int j=0;j<c.size();j++){
            if(c[j]==b) cnt++;
        }
        if(cnt!=1){
            y="NO";
            break;
        }
    }
    cout<<y<<endl;
}

return 0;

}