Its a very common question but I haven't found a clear answer anywhere. What I am doing is implementing Queue using 2 stacks , and in the node along with data , I maintain min and max values as well. Implementation is done in Java.
Now , the problem is that if the first element is Max / Min
and I deQueue it , then the rest of the nodes contain min/max values as the one which was dequeued.
Example :10 7 8 9 2
Node - [data,max,min]
[10,10,10] , [7,10,7] ,[8,10,7] , [9,10,7] , [2,10,2]
Now , if I dequeue it , then the queue is : [7,10,7] ,[8,10,7] , [9,10,7] , [2,10,2]
And the min and max values are wrong (10,7) , where it should have been (9,2).
My algorithm basically works for a stack and i am using a queue. So how can i modify my algorithm so that it gives correct result?