• + 6 comments

    My code is giving wrong answer on test cases 12, 20, 21, 23. I don't know what exception I am missing in my logic building, but since the test case is very large, I cannot possibly trace my code on that test case. Can someone please find a bug in my code?

    int main(){

    unsigned long int totalplants;
    cin>>totalplants;
    unsigned long long int* plants;
    plants = new unsigned long long int [totalplants]; 
    for (int i = 0; i < totalplants; i += 1) {
        cin>>plants [i];
    }
    stack <unsigned long long int> plantsdata; 
    unsigned long int maxdays = 0;
    for (int i = totalplants - 1; i >= 0; i -= 1) {
        if (plantsdata.empty() or plants[i] >= plantsdata.top())
            plantsdata.push(plants[i]);
        else {
            unsigned long int currentdays = 0;
            while (not plantsdata.empty() and plants[i] < plantsdata.top()) {
                currentdays += 1;
                plantsdata.pop();
            }
            if (currentdays > maxdays) maxdays = currentdays;
            plantsdata.push(plants[i]);
        }
    }
    cout<<maxdays;
    return 0;
    

    }