-
Notifications
You must be signed in to change notification settings - Fork 0
/
KthMinElementfromStream.cpp
55 lines (47 loc) · 1.02 KB
/
KthMinElementfromStream.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
//
// Created by Amit on 11/16/2016.
//
#include <iostream>
#include <stack>
using namespace std;
void getMin(){
stack<int> s1;
stack<int> s2;
while(1){
int x;
cout<<"enter the ext number:";
cin>>x;
int k =3;
if(s1.size()<k){
while(s1.top()>x){
int t = s1.top();
s1.pop();
s2.push(t);
}
s1.push(x);
while(!s2.empty()){
int t = s2.top();
s2.pop();
s1.push(t);
}
}else if(s1.size()==k){
while(s1.top()>x){
int t = s1.top();
s1.pop();
s2.push(t);
}s1.push(x);
while(!s2.empty()){
int t = s2.top();
s2.pop();
s1.push(t);
}
s1.pop();
}
int n = s1.top();
cout<<"number is:::"<<n<<endl;
cout<<"size"<<s1.size()<<endl;
}
}
int main(){
getMin();
}