
- 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 a stack in O(1) time and O(1) extra space 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 use 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 the maximum repeating number in O(n) time and O(1) extra space in Python
- Find duplicates in O(n) time and O(1) extra space - Set 1 in C++
- Rearrange positive and negative numbers in O(n) time and O(1) extra space in C++
- Count frequencies of all elements in array in O(1) extra space and O(n) time in C++
- Find duplicate in an array in O(n) and by using O(1) extra space in C++
- Find median of BST in O(n) time and O(1) space in Python
- Find median of BST in O(n) time and O(1) space in C++
- Find maximum in stack in O(1) without using additional stack in C++
- Reverse Individual Words With O(1) Extra Space
- Print left rotation of array in O(n) time and O(1) space in C Program.
- Count Fibonacci numbers in given range in O(Log n) time and O(1) space in C++
- Rearrange array in alternating positive & negative items with O(1) extra space in C++
- Check if the characters in a string form a Palindrome in O(1) extra space in Python
- Print n x n spiral matrix using O(1) extra space in C Program.
- Check for balanced parentheses in an expression O(1) space O(N^2) time complexity in Python

Advertisements