Write a Java program to Find Maximum Element in a Stack


The code you provided demonstrates the implementation of a stack with the ability to retrieve the maximum element efficiently. Let's go through it:

  • The code begins with the import statement import java.util.Stack; , which imports the Stack class from the java.util package.
  • The MaxElementInStack class is defined, serving as the entry point of the program.
  • In the main method, an instance of the MaxStack class is created.
  • Several elements are pushed onto the stack using the push method of the MaxStack class.
  • The getMax method of the MaxStack class is called to retrieve the maximum element in the stack.
  • The maximum element is printed using the System.out.println statement.
  • The MaxStack class is defined, implementing the stack with the ability to retrieve the maximum element.
  • It has two private member variables: stack and maxStack, both of type Stack<Integer>. The stack variable stores the elements of the stack, while the maxStack variable stores the maximum elements encountered so far.
  • The constructor MaxStack initializes both stacks.
  • The push method takes an integer value as a parameter and pushes it onto the stack. It also checks if the maxStack is empty or if the value being pushed is greater than or equal to the top element of the maxStack. If so, it pushes the value onto the maxStack as well.
  • The pop method removes and returns the top element from the stack. It also checks if the popped element is equal to the top element of the maxStack. If so, it removes the top element from the maxStack as well.
  • The getMax method returns the top element of the maxStack, which represents the maximum element in the stack.

By using the MaxStack implementation, you can efficiently retrieve the maximum element in a stack at any given time. The maximum element is updated dynamically as elements are pushed and popped from the stack, ensuring constant-time retrieval.

Source Code

import java.util.Stack;
public class MaxElementInStack
{
	public static void main(String[] args)
	{
		MaxStack stack = new MaxStack();
		stack.push(54);
		stack.push(72);
		stack.push(89);
		stack.push(23);
		stack.push(60);
		stack.push(68);
 
		int maxElement = stack.getMax();
		System.out.println("Maximum Element in Stack : " + maxElement);
	}
}
 
class MaxStack
{
	private Stack<Integer> stack;
	private Stack<Integer> maxStack;
 
	public MaxStack()
	{
		stack = new Stack<>();
		maxStack = new Stack<>();
	}
 
	public void push(int value)
	{
		stack.push(value);
		if (maxStack.isEmpty() || value >= maxStack.peek())
		{
			maxStack.push(value);
		}
	}
 
	public int pop()
	{
		if (stack.isEmpty())
		{
			throw new IllegalStateException("Stack is Empty");
		}
 
		int popped = stack.pop();
		if (popped == maxStack.peek())
		{
			maxStack.pop();
		}
 
		return popped;
	}
 
	public int getMax()
	{
		if (maxStack.isEmpty())
		{
			throw new IllegalStateException("Stack is Empty");
		}
 
		return maxStack.peek();
	}
}
 

Output

Maximum Element in Stack : 89