Tracking current Maximum Element in a Stack | Java program to keep track of maximum element in a stack ; main stack ; tack to keep track of max element ; If current element is greater than the top element of track stack , push the current element to track stack otherwise push the element at top of track stack again into it . ; Driver program to test above functions
import java . util . * ; class GfG { static class StackWithMax { static Stack < Integer > mainStack = new Stack < Integer > ( ) ; static Stack < Integer > trackStack = new Stack < Integer > ( ) ; static void push ( int x ) { mainStack . push ( x ) ; if ( mainStack . size ( ) == 1 ) { trackStack . push ( x ) ; return ; } if ( x > trackStack . peek ( ) ) trackStack . push ( x ) ; else trackStack . push ( trackStack . peek ( ) ) ; } static int getMax ( ) { return trackStack . peek ( ) ; } static void pop ( ) { mainStack . pop ( ) ; trackStack . pop ( ) ; } } ; public static void main ( String [ ] args ) { StackWithMax s = new StackWithMax ( ) ; s . push ( 20 ) ; System . out . println ( s . getMax ( ) ) ; s . push ( 10 ) ; System . out . println ( s . getMax ( ) ) ; s . push ( 50 ) ; System . out . println ( s . getMax ( ) ) ; } }