STOCK SPAN PROBLEM - Doubt

Why am I getting run error on the Coding blocks platform. It is working fine on Sublime Text.

There are multiple reasons for run time error in your code:

  1. How can you say that the array a and b would not have more than 10000 elements. Though this is not causes run time error for given test cases in your program, but you should keep this in mind while writing code.

  2. Suppose if the stack is empty at an instance of execution. So, if you would try to access the top of stack, this will give run time error.

Modified code:
while((!s.empty()) && (a[i]>a[s.top()])){
s.pop();
}

Now, it will first check if stack is empty and is it comes false, then it won’t check the other condition. Thus, preventing run-time error.

Suggestion:

  1. There is no need to make return type of the function as changes made inside the function on the array(formal parameter) would be reflected in the array(actual parameter) in the main function.

Hope, this would help.
Give a like, if you are satisfied.

Mark it as resolved , if your problem is solved.