implementing min stack in java solution
\nclass MinStack {\n    Stack<Long> st = new Stack<>();\n    long min;\n    public MinStack() {\n        //if stack has any element before just remove the eleme
```\nclass MinStack {\n    Stack<Long> st = new Stack<>();\n    long min;\n    public MinStack() {\n        //if stack has any element before just remove the elements till it gets empty just for               safety purpose.\n        while(st.isEmpty() == false) st.pop();\n        min = Integer.MAX_VALUE;\n    }\n    \n    public void push(int val) {\n        long value = val;\n        //if stack is empty just push the element and update the min val.\n        if(st.isEmpty()){  \n            st.push(value);\n            min = value;\n        }else{\n            //if the val is lower the curr min insert a modified value in stack by using the                     formula "2*val-min". this is the trick to maintain the stack.\n            if(value < min){ \n                st.push(2*value-min);  \n                min = value;   \n            //if the element is bigger just push   \n            }else{          \n                st.push(value);                \n            }\n        }\n    }\n    \n    public void pop() {\n        if(st.isEmpty()) return;\n        long value = st.pop();\n        //if the val is less the min that means it was a modified value so to get the previous min we use the formula "2*min-val" so that we restore the min to its previous min value.\n        if(value < min){\n            min = 2*min-value;\n        }\n    }\n    \n    public int top() {\n        if(st.isEmpty()) return -1;\n        long value = st.peek();\n        //if the top ele is less than min that means min is our curr top.\n        if(value < min) return (int)min; \n        return (int)value;\n    }\n    \n    public int getMin() {\n        return (int)min;\n    }\n}\n```
['Java']