diff --git a/Arrays-algos/mountainSubarrayProblem.cpp b/Arrays-algos/mountainSubarrayProblem.cpp new file mode 100644 index 0000000..131f86b --- /dev/null +++ b/Arrays-algos/mountainSubarrayProblem.cpp @@ -0,0 +1,71 @@ +// https://practice.geeksforgeeks.org/problems/mountain-subarray-problem/1 + +#include +using namespace std; + +class Solution{ + public: + vector processQueries(int a[], int n, vector> &queries, + int q) { + + vectorans; + 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=0; i--) + { + if(a[i+1]> tc; + while (tc--) { + int n, i, q; + cin >> n; + int a[n]; + for (i = 0; i < n; i++) { + cin >> a[i]; + } + cin >> q; + vector> 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; +} \ No newline at end of file diff --git a/Stack/minimumElementOfStack.cpp b/Stack/minimumElementOfStack.cpp new file mode 100644 index 0000000..c79b85c --- /dev/null +++ b/Stack/minimumElementOfStack.cpp @@ -0,0 +1,93 @@ +// https://practice.geeksforgeeks.org/problems/get-minimum-element-from-stack/1/ +// Get minimum element from stack + +#include +using namespace std; +class _stack{ +stack 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<pop()<<" "; + } + else if(qt==3) + { + //getMin + cout<getMin()<<" "; + } + } + cout< 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); +}