We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Poisonous Plants
You are viewing a single comment's thread. Return to all 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(){
}