
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find maximum in stack in O(1) without using additional stack in C++
Suppose we want to make a stack that can store the maximum element in the stack. And we can get it in O(1) time. The constraint is that, it should not use any additional space, so O(1) extra space.
We can make one user-defined stack, that will store the max value, when one operation is performed, like pop or peek, then the max will be returned. For peek operation, return the maximum of stack top and the max element, for pop operation, when the top element is larger, then print it and update max as 2*max – top_element. otherwise return top_element. For push operation update the max element as x(data to be inserted), 2*x – max.
Example
#include <iostream> #include <stack> using namespace std; class CustomStack { stack<int> stk; int stack_max; public: void getMax() { if (stk.empty()) cout << "Stack is empty"<<endl; else cout << "Maximum Element in the stack is: "<< stack_max <<endl; } void peek() { if (stk.empty()) { cout << "Stack is empty "; return; } int top = stk.top(); // Top element. cout << "Top Most Element is: "<<endl; (top > stack_max) ? cout << stack_max : cout << top; } void pop() { if (stk.empty()) { cout << "Stack is empty"<<endl; return; } cout << "Top Most Element Removed: "; int top = stk.top(); stk.pop(); if (top > stack_max) { cout << stack_max <<endl; stack_max = 2 * stack_max - top; } else cout << top <<endl; } void push(int element) { if (stk.empty()) { stack_max = element; stk.push(element); cout << "Element Inserted: " << element <<endl; return; } if (element > stack_max) { stk.push(2 * element - stack_max); stack_max = element; } else stk.push(element); cout << "Element Inserted: " << element <<endl; } }; int main() { CustomStack stk; stk.push(4); stk.push(6); stk.getMax(); stk.push(8); stk.push(20); stk.getMax(); stk.pop(); stk.getMax(); stk.pop(); stk.peek(); }
Output
Element Inserted: 4 Element Inserted: 6 Maximum Element in the stack is: 6 Element Inserted: 8 Element Inserted: 20 Maximum Element in the stack is: 20 Top Most Element Removed: 20 Maximum Element in the stack is: 8 Top Most Element Removed: 8 Top Most Element is: 6
- Related Articles
- Find maximum in a stack in O(1) time and O(1) extra space in C++
- Maximum Frequency Stack in C++
- Implement Stack using Queues in C++
- Prefix calculator using stack in JavaScript
- Postorder traversal of Binary Tree without recursion and without stack in C++
- Stack and the stack pointer in 8085 Microprocessor
- stack empty() and stack size() in C++ STL
- How to rethrow InnerException without losing stack trace in C#?
- Stack in Java
- Reverse a number using stack in C++
- Program to construct Maximum Stack with given operations in C++
- Get an element from a Stack in Java without removing it
- Stack in Java Programming
- Stack Unwinding in C++
- Stack Class in C#

Advertisements