-1

https://www.codechef.com/problems/CHEFRECPhttps://www.codechef.com/problems/CHEFRECP This is the link of the problem. I've been covering up all possible test cases in my opinion but still I am getting a wrong answer.

#include<bits/stdc++.h>
using namespace std;

int main(){
    int t;cin>>t;
    while(t--){
        int N;
        cin>>N;
        map<int,int> recipe;//Map to keep track of the ingredients with its frequency
        vector<int> v;//The input array stored here
        int lastRecipe;
        int temp;
        for(int i = 0;i < N;i++){
            cin>>temp;
            v.push_back(temp);
            recipe[temp]++;
        }
        string ans = "YES\n";
        lastRecipe = v[0];
        set<int> s;
        //check whether the ingredients are contiguous or not
        for(int i = 1;i < (int)v.size();++i){
            if(v[i]==lastRecipe)s.insert(v[i]);
            else{
                if(s.find(v[i])!=s.end()){
                    ans = "NO\n";
                    break;
                }
                lastRecipe = v[i];
            }

        }
        if(ans=="NO\n"){
            cout<<ans;continue;
        }
        else{
            set<int> tt;
//Check for uniqueness of the frequency
            for(auto itr:recipe){
                 //   cout<<itr.first<<" "<<itr.second<<endl;
                if(tt.find(itr.second)==tt.end())
                    tt.insert(itr.second);
                else{
                    ans = "NO\n";break;
                }
            }
            cout<<ans;
        }
    }
    return 0;
}

RISHAB
  • 47
  • 7
  • 1
    What do you hope to get from these contest/challenge/competitive coding/hacking sites. If it's to learn C++, you won't learn anything there. Like in this case, the correct solution is based on a mathematical or a programming trick. If you don't know what the trick is and attempt to code a brute-force approach, the program either runs slow, or fails to handle an obscure edge case. If you're trying to learn C++, you won't learn anything from meaningless online contest sites [but only from a good C++ textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Sam Varshavchik May 25 '20 at 02:57
  • My expectations of learning here is not just to learn c++ but to learn problem-solving and yes I have solved this problem in a different approach but I am just stuck why is this approach not working, thank you – RISHAB May 25 '20 at 03:59
  • One other question must be asked first before asking why this approach doesn't work: whether this approach is the correct and the best approach. Because if it's not, then whether it works, or not, is no longer relevant. I see that the codechef puzzle specified upper limit of only 1000, for both `N` and `A`. This is eminently doable with a simple solution involving just two vectors or arrays. As such, none of this complexity involving sets and maps serves anything but to overthink a very simple problem with a very simple solution. So why this doesn't work? Doesn't matter, it's wrong anyway. – Sam Varshavchik May 25 '20 at 04:15
  • You should copy the problem statement in the post, a link can be broken. Besides, I think your approach with `map`and `set` is the simplest one, a detail must be missing. The issue is to find a simple test case that your code will fail – Damien May 25 '20 at 06:19
  • Yes I am stuck into that detail only. And I'll make sure I'll copy the problem statement next time. – RISHAB May 25 '20 at 06:28

1 Answers1

1

I finally solved it, the problem was I didn't insert the first element in the set while checking for contiguous elements. Thank you everyone.

RISHAB
  • 47
  • 7