Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions basic dsa/MonotonicStackC++.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <bits/stdc++.h>
using namespace std;
//Function to demonstrate monotonic decreasing stack
void decreasingMonotonicStack(vector<int> inputs){

vector<int> stack;
for(int p:inputs){

while(!stack.empty() && stack.back()<=p){
stack.pop_back();
}
stack.push(p);

}
for (int val : stack) {
cout << val << " ";
}
cout<<endl;
}
// Function to demonstarte monotonic increasing stack
void increasingMonotonicStack(vector<int> inputs){

vector<int> stack;
for(int p:inputs){

while(!stack.empty() && stack.back()>=p){
stack.pop_back();
}
stack.push(p);

}

for (int val : stack) {
cout << val << " ";
}
cout<<endl;
}
int main(){
vector<int> inputs={3,4,5,6,7,4,5,3};
decreasingMonotonicStack(inputs);
increasingMonotonicStack(inputs);



}