Given a stack st containing n integers and an integer x, insert x at the bottom of the stack while maintaining the relative order of the existing elements.
Note: While displaying the stack, the bottommost element is printed first.
Examples:
Input: st = [5, 4, 3, 2, 1], x = 7
Output: [7, 5, 4, 3, 2, 1]
Explanation: After inserting 7 at the bottom, the stack becomes [7, 5, 4, 3, 2, 1].Input: st = [5, 3, 1], x = 4
Output: [4, 5, 3, 1]
Explanation: After inserting 4 at the bottom, the stack becomes [4, 5, 3, 1].
Table of Content
Using Temporary Stack - O(n) time and O(n) space
- First, pop all elements from st and push them into a temporary stack temp. This empties the original stack.
- Push x into the empty st, making it the bottommost element.
- Then, pop all elements from temp and push them back into st to restore their original order.
- Finally, return the modified st.
#include <iostream>
#include <stack>
using namespace std;
stack<int> insertAtBottom(stack<int>& st, int x) {
stack<int> temp;
// Move all elements to the temporary stack
while (!st.empty()) {
temp.push(st.top());
st.pop();
}
// Insert x at the bottom
st.push(x);
// Restore the original order
while (!temp.empty()) {
st.push(temp.top());
temp.pop();
}
return st;
}
int main() {
stack<int> st;
st.push(5);
st.push(4);
st.push(3);
st.push(2);
st.push(1);
int x = 7;
st = insertAtBottom(st, x);
while (!st.empty()) {
cout << st.top() << " ";
st.pop();
}
return 0;
}
import java.util.Stack;
class GFG {
public static Stack<Integer> insertAtBottom(Stack<Integer> st, int x) {
Stack<Integer> temp = new Stack<>();
// Move all elements to the temporary stack
while (!st.empty()) {
temp.push(st.peek());
st.pop();
}
// Insert x at the bottom
st.push(x);
// Restore the original order
while (!temp.empty()) {
st.push(temp.peek());
temp.pop();
}
return st;
}
public static void main(String[] args) {
Stack<Integer> st = new Stack<>();
st.push(5);
st.push(4);
st.push(3);
st.push(2);
st.push(1);
int x = 7;
st = insertAtBottom(st, x);
while (!st.empty()) {
System.out.print(st.peek() + " ");
st.pop();
}
}
}
def insertAtBottom(st, x):
temp = []
# Move all elements to the temporary stack
while st:
temp.append(st[-1])
st.pop()
# Insert x at the bottom
st.append(x)
# Restore the original order
while temp:
st.append(temp[-1])
temp.pop()
return st
if __name__ == "__main__":
st = []
st.append(5)
st.append(4)
st.append(3)
st.append(2)
st.append(1)
x = 7
st = insertAtBottom(st, x)
while st:
print(st[-1], end=" ")
st.pop()
using System;
using System.Collections.Generic;
class GFG {
static Stack<int> insertAtBottom(Stack<int> st, int x) {
Stack<int> temp = new Stack<int>();
// Move all elements to the temporary stack
while (st.Count > 0) {
temp.Push(st.Peek());
st.Pop();
}
// Insert x at the bottom
st.Push(x);
// Restore the original order
while (temp.Count > 0) {
st.Push(temp.Peek());
temp.Pop();
}
return st;
}
static void Main() {
Stack<int> st = new Stack<int>();
st.Push(5);
st.Push(4);
st.Push(3);
st.Push(2);
st.Push(1);
int x = 7;
st = insertAtBottom(st, x);
while (st.Count > 0) {
Console.Write(st.Peek() + " ");
st.Pop();
}
}
}
function insertAtBottom(st, x) {
let temp = [];
// Move all elements to the temporary stack
while (st.length > 0) {
temp.push(st[st.length - 1]);
st.pop();
}
// Insert x at the bottom
st.push(x);
// Restore the original order
while (temp.length > 0) {
st.push(temp[temp.length - 1]);
temp.pop();
}
return st;
}
// Driver code
let st = [];
st.push(5);
st.push(4);
st.push(3);
st.push(2);
st.push(1);
let x = 7;
st = insertAtBottom(st, x);
while (st.length > 0) {
process.stdout.write(st[st.length - 1] + " ");
st.pop();
}
Output
1 2 3 4 5 7
Using Recursion - O(n) time and O(n) space
The idea is to recursively remove elements from the top until the stack becomes empty.
Then, insert x and restore the removed elements while returning from the recursion.
Consider: st = [5, 4, 3, 2, 1] and x = 7
Here, 5 is the bottom element and 1 is the top element.
First, the recursive function removes elements from the top one by one:
- [5, 4, 3, 2, 1] -> remove 1
- [5, 4, 3, 2] -> remove 2
- [5, 4, 3] -> remove 3
- [5, 4] -> remove 4
- [5] -> remove 5
- [] -> stack is empty
Now, the stack is empty, so 7 is inserted: [7]
As the recursive calls return, the removed elements are restored:
- [7] -> push 5
- [7, 5] -> push 4
- [7, 5, 4] -> push 3
- [7, 5, 4, 3] -> push 2
- [7, 5, 4, 3, 2] -> push 1
Therefore, the final stack is: [7, 5, 4, 3, 2, 1]
Thus, 7 is inserted at the bottom while the original order of the elements is maintained.
#include <iostream>
#include <stack>
using namespace std;
void insertBottom(stack<int>& st, int x) {
// If stack is empty, insert x
if (st.empty()) {
st.push(x);
return;
}
// Store top element and remove it
int top = st.top();
st.pop();
// Recursively insert x at bottom
insertBottom(st, x);
// Restore the removed element
st.push(top);
}
// Function to insert an element at the bottom of a stack.
stack<int> insertAtBottom(stack<int>& st, int x) {
insertBottom(st, x);
return st;
}
int main() {
stack<int> st;
st.push(5);
st.push(4);
st.push(3);
st.push(2);
st.push(1);
int x = 7;
st = insertAtBottom(st, x);
while (!st.empty()) {
cout << st.top() << " ";
st.pop();
}
return 0;
}
import java.util.Stack;
class GFG {
public static void insertBottom(Stack<Integer> st, int x) {
// If stack is empty, insert x
if (st.empty()) {
st.push(x);
return;
}
// Store top element and remove it
int top = st.peek();
st.pop();
// Recursively insert x at bottom
insertBottom(st, x);
// Restore the removed element
st.push(top);
}
// Function to insert an element at the bottom of a stack.
public static Stack<Integer> insertAtBottom(Stack<Integer> st, int x) {
insertBottom(st, x);
return st;
}
public static void main(String[] args) {
Stack<Integer> st = new Stack<>();
st.push(5);
st.push(4);
st.push(3);
st.push(2);
st.push(1);
int x = 7;
st = insertAtBottom(st, x);
while (!st.empty()) {
System.out.print(st.peek() + " ");
st.pop();
}
}
}
def insertBottom(st, x):
# If stack is empty, insert x
if not st:
st.append(x)
return
# Store top element and remove it
top = st[-1]
st.pop()
# Recursively insert x at bottom
insertBottom(st, x)
# Restore the removed element
st.append(top)
# Function to insert an element at the bottom of a stack.
def insertAtBottom(st, x):
insertBottom(st, x)
return st
if __name__ == "__main__":
st = []
st.append(5)
st.append(4)
st.append(3)
st.append(2)
st.append(1)
x = 7
st = insertAtBottom(st, x)
while st:
print(st[-1], end=" ")
st.pop()
using System;
using System.Collections.Generic;
class GFG {
static void insertBottom(Stack<int> st, int x) {
// If stack is empty, insert x
if (st.Count == 0) {
st.Push(x);
return;
}
// Store top element and remove it
int top = st.Peek();
st.Pop();
// Recursively insert x at bottom
insertBottom(st, x);
// Restore the removed element
st.Push(top);
}
// Function to insert an element at the bottom of a stack.
static Stack<int> insertAtBottom(Stack<int> st, int x) {
insertBottom(st, x);
return st;
}
static void Main() {
Stack<int> st = new Stack<int>();
st.Push(5);
st.Push(4);
st.Push(3);
st.Push(2);
st.Push(1);
int x = 7;
st = insertAtBottom(st, x);
while (st.Count > 0) {
Console.Write(st.Peek() + " ");
st.Pop();
}
}
}
function insertBottom(st, x)
{
// If stack is empty, insert x
if (st.length === 0) {
st.push(x);
return;
}
// Store top element and remove it
let top = st[st.length - 1];
st.pop();
// Recursively insert x at bottom
insertBottom(st, x);
// Restore the removed element
st.push(top);
}
// Function to insert an element at the bottom of a stack.
function insertAtBottom(st, x)
{
insertBottom(st, x);
return st;
}
// Driver code
let st = [];
st.push(5);
st.push(4);
st.push(3);
st.push(2);
st.push(1);
let x = 7;
st = insertAtBottom(st, x);
while (st.length > 0) {
process.stdout.write(st[st.length - 1] + " ");
st.pop();
}
Output
1 2 3 4 5 7