forked from Harshita-Kanal/Data-Structures-and-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgetMinAtPop.cpp
More file actions
86 lines (77 loc) · 1.4 KB
/
getMinAtPop.cpp
File metadata and controls
86 lines (77 loc) · 1.4 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<bits/stdc++.h>
using namespace std;
stack<int>_push(int arr[],int n);
void _getMinAtPop(stack<int>s);
stack<int> _push(int arr[],int n)
{
// your code here
stack<int>s;
for(int i=0;i<n;i++){
s.push(arr[i]);
}
return s;
}
void fillArray(stack<int>s,int arr[],int n){
int i=n-1;
while(!s.empty()){
arr[i--]=s.top();
s.pop();
}
// sort(arr,arr+n,greater<int>());
return;
}
void print(int arr[],int n){
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
void printStack(stack<int>s){
while(!s.empty()){
cout<<s.top()<<" ";
s.pop();
}
cout<<endl;
}
void _getMinAtPop(stack<int>s)
{
// your code here
int n=s.size();
int arr[n];
fillArray(s,arr,n);
// print(arr,n);
// cout<<s.size()<<endl;
stack<int>s1;
for(int i=0;i<n;i++){
if(s1.empty()){
s1.push(arr[i]);
}
else if(arr[i]<=s1.top()){
s1.push(arr[i]);
}
}
// printStack(s1);
// int size=s.size();
while(!s.empty()){
cout<<s1.top()<<" ";
if(!s1.empty() && (s.top()==s1.top()) ){
s1.pop();
}
s.pop();
}
}
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
stack<int>mys=_push(arr,n);
_getMinAtPop(mys);
cout<<endl;
}
return 0;
}