0

I was trying to solve the record breaker problem from google kickstart Round D 2020 I submitted the following code in C++:

#include <bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int testcase;
    cin>>testcase;
    int t=1;
    while(t<=testcase){
        int n;
        cin>>n;
        int arr[n+1];
        for(int i=0;i<n;i++)
            cin>>arr[i];
        arr[n]=0;
        int prev=0,ans=0;
        for(int i=0;i<n;i++){
            if(arr[i]>prev && arr[i]>arr[i+1]){
                ans++;
            }
            prev=max(prev,arr[i]);
        }
        cout<<"Case #"<<t<<": "<<ans<<endl;
        t++;
    }
    return 0;
}

It passed the sample test set and I also tried to check this on certain edge cases from different articles and websites but still unable to find the error as it is not passing the test set 1. Please help.

  • 2
    `int arr[n+1];`: Variable length arrays are not C++ standard. Try using a vector and see if that solves the problem – Harshit Jindal Jul 11 '21 at 02:14
  • I mean it passed the sample test case but not the test set 1 – Suraj Kumar Sahani Jul 11 '21 at 07:09
  • See [Why should I not #include ?](https://stackoverflow.com/q/31816095) and [Why using namespace std is bad practice](https://stackoverflow.com/questions/1452721). – prapin Jul 11 '21 at 08:07
  • 1
    Also, you should edit your question to make it self-contained. It is missing the input necessary to reproduce the error, the expected output for that input, and the actual output. It would be even better if the problematic case was hardcoded so that no input is necessary. – JaMiT Jul 12 '21 at 00:44

1 Answers1

1

Finally I found the answer to my own question. Well I don't know whether posting answer on my own question is right or not but still I think it might help someone who may face similar problem. There is just a little problem with the logic, I missed just this one edge case which I tried hard to find. Kickstart considers that if the number of days is 1, then irrespective of number of visitors on that day, that day is a record breaking day. Which means the following input should output 1:

1
1
0

Hence I made necessary changes(indicated inside the code) as follows:

#include <bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int testcase;
    cin>>testcase;
    int t=1;
    while(t<=testcase){
        int n;
        cin>>n;
        int arr[n+1];
        arr[n]=-1;                  //last element is assigned -1 instead of 0
        for(int i=0;i<n;i++)
            cin>>arr[i];
        int prev=-1,ans=0;          //prev is assined -1 instead of 0
        for(int i=0;i<n;i++){
            if(arr[i]>prev && arr[i]>arr[i+1]){
                ans++;
            }
            prev=max(prev,arr[i]);
        }
        cout<<"Case #"<<t<<": "<<ans<<endl;
        t++;
    }
    return 0;
}

It passed all test cases. I felt very happy when I found the solution, you won't believe, it took me 43 attempts(here is the proof) to find this edge case. Well I am a beginner in competitive coding so, I just want to leave a message for all the beginners out there, NEVER GIVE UP!!! I found my own mistake by just trying hard. So, just keep trying.

  • *"Well I don't know whether posting answer on my own question is right or not"* -- [Answering your own question is allowed.](https://stackoverflow.com/help/self-answer) – JaMiT Jul 18 '21 at 22:58
  • 1
    *"because no one could help me"* -- A better phrasing is "would" instead of "could". There was at least one person who could help you, but you ignored the request to make your question self-contained. Readers should not have to follow a link to find out what your code is supposed to do. Without that information in the question itself, I believe the appropriate response is to close the question, not answer it. (I know you did not know the specific case that was failing, but some sort of expected input/output should be in the question as well.) In any event, congrats on finding the edge case. – JaMiT Jul 18 '21 at 23:04