Skip to content

Get minimum element from stack #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
71 changes: 71 additions & 0 deletions Arrays-algos/mountainSubarrayProblem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// https://practice.geeksforgeeks.org/problems/mountain-subarray-problem/1

#include <bits/stdc++.h>
using namespace std;

class Solution{
public:
vector<bool> processQueries(int a[], int n, vector<pair<int, int>> &queries,
int q) {

vector<bool>ans;
int left[n], right[n];
left[0]=0;
right[n-1]=n-1;
int k=0;

// left[i] stores the last index on left side which is increasing
// i.e. greater than its previous element
for(int i=1; i<n; i++)
{
if(a[i-1]<a[i])
k=i;
left[i]=k;
}
// right[i] will store the first index on the right side which is decreasing
// i.e. greater than its next element.
for(int i=n-2; i>=0; i--)
{
if(a[i+1]<a[i])
k=i;
right[i]=k;

}

for(auto x: queries)
{
int l= x.first;
int r= x.second;

if(left[r]<=right[l])
ans.push_back(true);
else
ans.push_back(false);
}
return ans;
}
};

int main() {
int tc;
cin >> tc;
while (tc--) {
int n, i, q;
cin >> n;
int a[n];
for (i = 0; i < n; i++) {
cin >> a[i];
}
cin >> q;
vector<pair<int, int>> queries(q);
for (i = 0; i < q; i++) {
cin >> queries[i].first >> queries[i].second;
}
Solution obj;
auto v = obj.processQueries(a, n, queries, q);
for (bool u : v) {
cout << (u ? "Yes\n" : "No\n");
}
}
return 0;
}
93 changes: 93 additions & 0 deletions Stack/minimumElementOfStack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// https://practice.geeksforgeeks.org/problems/get-minimum-element-from-stack/1/
// Get minimum element from stack

#include<bits/stdc++.h>
using namespace std;
class _stack{
stack<int> s;
int minEle;
public :
int getMin();
int pop();
void push(int);
};

int main()
{
int t;
cin>>t;
while(t--)
{
int q;
cin>>q;
_stack *a = new _stack();
while(q--){

int qt;
cin>>qt;

if(qt==1)
{
//push
int att;
cin>>att;
a->push(att);
}
else if(qt==2)
{
//pop
cout<<a->pop()<<" ";
}
else if(qt==3)
{
//getMin
cout<<a->getMin()<<" ";
}
}
cout<<endl;
}

}

stack<int> min_stack;
/*returns min element from stack*/
int _stack :: getMin()
{
if(!s.empty())
{
return min_stack.top();
}
return -1;
}

/*returns poped element from stack*/
int _stack ::pop()
{
if( !s.empty())
{
int e= s.top();
s.pop();
min_stack.pop();
return e;
}
return -1;

}

/*push element x into the stack*/
void _stack::push(int x)
{
if(s.empty())
{
s.push(x);
min_stack.push(x);
return;
}
if(x>= min_stack.top()) {
min_stack.push(min_stack.top());
}
else{
min_stack.push(x);
}
s.push(x);
}