-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcedure.txt
35 lines (29 loc) · 1.63 KB
/
Procedure.txt
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
Step 1 : We start with first element and push if Queue is empty.
Step 2 : For every next element check if it is smaller than the front of queue and greater than the rear of queue
Step 3 : If it is smaller,
and min stack is empty, push at front of queue
if min stack is not empty,
and if element is less than top of min stack,
then apply pop operation on min stack and push at front of queue till top of min stack is greater than element or min stack is empty.
push element in min stack
else if element is greater or equal to top of min stack
then push element in min stack
If it is greater,
and max stack is empty, push at rear of queue
if max stack is not empty,
and if element is greater than top of max stack,
then apply pop operation on max stack and push at rear of queue till top of max stack is smaller than element or max stack is empty.
push element in max stack
else if max is greater than equal to element
then push element in max stack
If element is greater than front of queue and less than rear of queue,
then push front of queue in min stack and rear of queue in max stack until element is greater than queue front and less than queue rear.
if queue is empty or element is less than equal to front of queue,
push element in front of queue
else
push element in rear of queue
Repeat step 3 until last element is processed
Step 4 : if min stack is not empty.
push all elements of min stack in front of queue
if max stack is not empty
push all elements of max stack in rear of queue