Guest User

Untitled

a guest
Feb 1st, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <iostream>
  2. #include<stack>
  3. using namespace std;
  4.  
  5. int * find(int *p, int n){
  6.    
  7.     int *a = new int[n];
  8.     stack<int> s;
  9.     s.push(0);
  10.  
  11.     for(int k = 0,i = 1; k != i ; i = (i+1) % n){
  12.        
  13.         if(s.top()< p[i] ){
  14.            
  15.             while(s.top()<p[i] && !s.empty()){
  16.                  
  17.                 a[s.top()] = i;
  18.                
  19.                 s.pop();
  20.                //  cout<<"Hello2"<<endl;
  21.             }
  22.             //After here the their is abnormal termination
  23.              cout<<"Hello3"<<endl;
  24.             s.push(i);
  25.             // cout<<"Hello4"<<endl;
  26.             k = i;
  27.            
  28.         }
  29.         else
  30.             s.push(i);
  31.     }
  32.     a[s.top()] = -1;
  33.     s.pop();
  34.     return a;
  35. }
  36.  
  37.  
  38. int main() {
  39.    int n;
  40.    cin>>n;
  41.  
  42.    int *a = new int[n];
  43.    for(int i = 0; i < n; i++)
  44.    {
  45.        cin>>a[i];
  46.    }
  47.  
  48.   int *k = find(a, n);
  49.    
  50.   for(int i = 0; i < n; i++)
  51.     cout<<a[i]<<" ";
  52.    
  53.    return 0;
  54. }
Add Comment
Please, Sign In to add comment