Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<vector>
- #include<stack>
- using namespace std;
- void stackSpan(int *a, int n){
- stack <int> s;
- int b[n];
- for(int i = 0; i < n; i++){
- // cout<<"Hello"<<endl;
- //Why on using s.empty() my program gets segmentation error every time
- while(s.top() < a[i] && !s.empty()){
- s.pop();
- }
- int better = s.empty() ? i + 1: i - s.top();
- s.push(i);
- b[i] = better;
- }
- for(int i = 0; i < n; i++){
- cout<<b[i]<<" ";
- }
- cout<<"END";
- }
- int main(){
- int n;
- cin>>n;
- int a[n];
- stackSpan(a, n);
- return 0;
- }
Add Comment
Please, Sign In to add comment