forked from Imran9898/Beginners-Data-Structure
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcurmin.cpp
62 lines (45 loc) · 845 Bytes
/
curmin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <bits/stdc++.h>
#include "MYSTACK.h"
using namespace std;
/*
5
1 2 3 4 5
7
1 6 43 1 2 0 5
10
6 5 5 5 4 7 1 2 1 2
*/
int main()
{
int n;
cin>>n;
int array[n];
for(int i=0; i<n;i++){
cin>>array[i];
}
Stack <int> st;
Stack <int> minStack;
int min =INT_MAX;
for(int i=0; i<n;i++){
if(array[i]<=min){
minStack.push(array[i]);
min = array[i];
}
st.push(array[i]);
}
while(!st.empty()){
//If TOP of minStack and st is equal
if(minStack.Top()!= st.Top()){
st.pop();
cout<<minStack.Top() << " ";
}
// ELSE
else{
cout<< minStack.Top() <<" ";
minStack.pop();
st.pop();
}
}
cout << endl << endl;
return 0;
}