
Tracking current Maximum Element in a Stack - GeeksforGeeks
Feb 16, 2023 · Given a Stack, keep track of the maximum value in it. The maximum value may be the top element of the stack, but once a new element is pushed or an element is popped from the stack, the maximum element will be now from the rest of the elements. Examples: Input : 4 19 7 14 20 Output : Max Values in stack are . 4 19 19 19 20
java - How to Find the Max Integer Value in a Stack without using max …
Jul 25, 2013 · This code: public static Integer max(Stack stack) { if (stack.isEmpty()) { return Integer.MIN_VALUE; } else { Integer last = (Integer)stack.pop(); Integer next = max(stack); stack.push(last); if (last > next) { return last; } else { return next; } } } public static void main(String[] args){ Stack lifo = new Stack(); lifo.push(new Integer(4 ...
Java: Find the maximum and minimum elements in a stack
Mar 11, 2025 · Write a Java program to use recursion to determine the maximum and minimum elements in a stack without modifying it. Write a Java program to compare the maximum and minimum elements from two stacks and output the difference.
What is the fastest way to search the maximum element in a stack in Java?
Jul 13, 2017 · I've created my own stack class and used the following approach: Node node = top; //created a new node which points to the top of stack. int max = node.data; //max contains the value of the top node. while(node != null) { if(node.data > max) { max = node.data; node = node.next; //Print the value of max.
Find the maximum and a minimum in a stack
Oct 29, 2014 · A better solution would be to have a separate stack for the max and the min. For the max stack you will push a new element onto the max stack only if the new element is greater than the current max, and vice-versa for min.
Find maximum in a stack in O(1) time and O(1) extra space
Feb 25, 2025 · Given a stack of integers. The task is to design a special stack such that the maximum element can be found in O (1) time and O (1) extra space. Examples: The idea behind this approach is to use a transformation to encode the previous maximum when a new maximum is pushed onto the stack.
Find Maximum and Minimum Elements in a Stack using Java
Aug 18, 2024 · Learn how to find the maximum and minimum elements in a stack using Java programming with step-by-step examples.
Java Stack - Exercises, Practice, Solution - w3resource
Mar 11, 2025 · Implement stack operations such as push, pop, and check for the top element and empty stack. Also, sort, reverse, and find the maximum and minimum elements in a stack. Java Code Editor available for live demonstration.
Get Max from Stack | Practice | GeeksforGeeks
Given q queries, You task is to implement the following three functions for a stack: push(x) – Insert an integer x onto the stack. pop() – Remove the top element from the stack. peek() - Return the top eleme
Stack with find-min/find-max more efficient than O (n)?
Aug 21, 2011 · Then, when you push a new element onto the stack, you can easily (in O (1) time) compute the maximum and minimum value anywhere in the stack by comparing the new element you just pushed to the current maximum and minimum.