# 24. Insert an Element at the Bottom of a Stack

The problem can be found at the following link: [Question Link](https://www.geeksforgeeks.org/problems/insert-an-element-at-the-bottom-of-a-stack/1)

## My Approach

* Create a temporary stack to store elements.
* Pop all elements from the original stack and push them into the temporary stack. This reverses the order of elements.
* Push the new element 'x' into the original stack.
* Pop all elements from the temporary stack and push them back into the original stack. This restores the original order of elements with the new element inserted at the bottom.

## Time and Auxiliary Space Complexity

* **Time Complexity**: The time complexity of this approach is `O(N)`, where 'N' is the number of elements in the stack.
* **Auxiliary Space Complexity**: The auxiliary space complexity is `O(N)`, where 'N' is the number of elements in the stack.

## Code (C++)

```cpp
class Solution{
public:
    stack<int> insertAtBottom(stack<int> st,int x)
    {
        stack<int>temp;
        while (!st.empty())
        {
            temp.push(st.top());
            st.pop();
        }
        st.push(x);
        while (!temp.empty())
        {
            st.push(temp.top());
            temp.pop();
        }
        return st;
    }
};
```

## Contribution and Support

For discussions, questions, or doubts related to this solution, please visit our [discussion section](https://github.com/getlost01/gfg-potd/discussions). We welcome your input and aim to foster a collaborative learning environment.

If you find this solution helpful, consider supporting us by giving a ⭐ star to the [getlost01/gfg-potd](https://github.com/getlost01/gfg-potd) repository.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gl01.gitbook.io/gfg-editorials/2024/03-2024-march-20/24-insert-an-element-at-the-bottom-of-a-stack.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
