Banana-Leopard/TASTY huggingface dataset:
// C++ code to reverse a
// stack using recursion
#include <bits/stdc++.h>
using namespace std;
 
// Below is a recursive function
// that inserts an element
// at the bottom of a stack.
void insert_at_bottom(stack<int>& st, int x)
{
 
    if (st.size() == 0) {
        st.push(x);
    }
    else {
 
        // All items are held in Function Call
        // Stack until we reach end of the stack
        // When the stack becomes empty, the
        // st.size() becomes 0, the above if
        // part is executed and the item is
        // inserted at the bottom
 
        int a = st.top();
        st.pop();
        insert_at_bottom(st, x);
 
        // push allthe items held in
        // Function Call Stack
        // once the item is inserted
        // at the bottom
        st.push(a);
    }
}
 
// Below is the function that
// reverses the given stack using
// insert_at_bottom()
void reverse(stack<int>& st)
{
    if (st.size() > 0) {
 
        // Hold all items in Function
        // Call Stack until we
        // reach end of the stack
        int x = st.top();
        st.pop();
        reverse(st);
 
        // Insert all the items held
        // in Function Call Stack
        // one by one from the bottom
        // to top. Every item is
        // inserted at the bottom
        insert_at_bottom(st, x);
    }
    return;
}
 
// Driver Code
int main()
{
    stack<int> st, st2;
    // push elements into
    // the stack
    for (int i = 1; i <= 4; i++) {
        st.push(i);
    }
 
    st2 = st;
 
    cout << "Original Stack" << endl;
    // printing the stack after reversal
    while (!st2.empty()) {
        cout << st2.top() << " ";
        st2.pop();
    }
    cout<<endl;
   
    // function to reverse
    // the stack
    reverse(st);
    cout << "Reversed Stack" << endl;
    // printing the stack after reversal
    while (!st.empty()) {
        cout << st.top() << " ";
        st.pop();
    }
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// C++ program to sort a stack using recursion
#include <iostream>
using namespace std;
 
// Stack is represented using linked list
struct stack {
    int data;
    struct stack* next;
};
 
// Utility function to initialize stack
void initStack(struct stack** s) { *s = NULL; }
 
// Utility function to check if stack is empty
int isEmpty(struct stack* s)
{
    if (s == NULL)
        return 1;
    return 0;
}
 
// Utility function to push an item to stack
void push(struct stack** s, int x)
{
    struct stack* p = (struct stack*)malloc(sizeof(*p));
 
    if (p == NULL) {
        fprintf(stderr, "Memory allocation failed.\n");
        return;
    }
 
    p->data = x;
    p->next = *s;
    *s = p;
}
 
// Utility function to remove an item from stack
int pop(struct stack** s)
{
    int x;
    struct stack* temp;
 
    x = (*s)->data;
    temp = *s;
    (*s) = (*s)->next;
    free(temp);
 
    return x;
}
 
// Function to find top item
int top(struct stack* s) { return (s->data); }
 
// Recursive function to insert an item x in sorted way
void sortedInsert(struct stack** s, int x)
{
    // Base case: Either stack is empty or newly inserted
    // item is greater than top (more than all existing)
    if (isEmpty(*s) or x > top(*s)) {
        push(s, x);
        return;
    }
 
    // If top is greater, remove the top item and recur
    int temp = pop(s);
    sortedInsert(s, x);
 
    // Put back the top item removed earlier
    push(s, temp);
}
 
// Function to sort stack
void sortStack(struct stack** s)
{
    // If stack is not empty
    if (!isEmpty(*s)) {
        // Remove the top item
        int x = pop(s);
 
        // Sort remaining stack
        sortStack(s);
 
        // Push the top item back in sorted stack
        sortedInsert(s, x);
    }
}
 
// Utility function to print contents of stack
void printStack(struct stack* s)
{
    while (s) {
        cout << s->data << " ";
        s = s->next;
    }
    cout << "\n";
}
 
// Driver code
int main(void)
{
    struct stack* top;
 
    initStack(⊤);
    push(⊤, 30);
    push(⊤, -5);
    push(⊤, 18);
    push(⊤, 14);
    push(⊤, -3);
 
    cout << "Stack elements before sorting:\n";
    printStack(top);
 
    sortStack(⊤);
    cout << "\n";
 
    cout << "Stack elements after sorting:\n";
    printStack(top);
 
    return 0;
}
 
// This code is contributed by SHUBHAMSINGH10

Time complexity: quadratic
Space complexity: linear
// C++ program to sort a stack using an
// auxiliary stack.
#include <bits/stdc++.h>
using namespace std;
 
// This function return the sorted stack
stack<int> sortStack(stack<int> &input)
{
    stack<int> tmpStack;
 
    while (!input.empty())
    {
        // pop out the first element
        int tmp = input.top();
        input.pop();
 
        // while temporary stack is not empty and top
        // of stack is greater than temp
        while (!tmpStack.empty() && tmpStack.top() > tmp)
        {
            // pop from temporary stack and push
            // it to the input stack
            input.push(tmpStack.top());
            tmpStack.pop();
        }
 
        // push temp in temporary of stack
        tmpStack.push(tmp);
    }
 
    return tmpStack;
}
 
// main function
int main()
{
    stack<int> input;
    input.push(34);
    input.push(3);
    input.push(31);
    input.push(98);
    input.push(92);
    input.push(23);
 
    // This is the temporary stack
    stack<int> tmpStack = sortStack(input);
    cout << "Sorted numbers are:\n";
 
    while (!tmpStack.empty())
    {
        cout << tmpStack.top()<< " ";
        tmpStack.pop();
    }
}

Time complexity: quadratic
Space complexity: linear
// C++ code to delete middle of a stack
// without using additional data structure.
#include<bits/stdc++.h>
using namespace std;
 
// Deletes middle of stack of size
// n. Curr is current item number
    void deleteMid_util(stack<char>&s, int sizeOfStack, int current)
    {
        //if current pointer is half of the size of stack then we
        //are accessing the middle element of stack.
        if(current==sizeOfStack/2)
        {
            s.pop();
            return;
        }
         
        //storing the top element in a variable and popping it.
        int x = s.top();
        s.pop();
        current+=1;
 
        //calling the function recursively.
        deleteMid_util(s,sizeOfStack,current);
         
        //pushing the elements (except middle element) back
        //into stack after recursion calls.
        s.push(x);
    }
    void deleteMid(stack<char>&s, int sizeOfStack)
    {
        deleteMid_util(s,sizeOfStack,0);
    }
 
//Driver function to test above functions
int main()
{
    stack<char> st;
 
    //push elements into the stack
    st.push('1');
    st.push('2');
    st.push('3');
    st.push('4');
    st.push('5');
    st.push('6');
    st.push('7');
 
    deleteMid(st, st.size());
 
    // Printing stack after deletion
    // of middle.
    while (!st.empty())
    {
        char p=st.top();
        st.pop();
        cout << p << " ";
    }
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ code to delete middle of a stack with iterative method
#include <bits/stdc++.h>
using namespace std;
 
// Deletes middle of stack of size n. Curr is current item number
void deleteMid(stack<char>& st)
{
    int n = st.size();
    stack<char> tempSt;
    int count = 0;
     
    // Put first n/2 element of st in tempSt
    while (count < n / 2) {
        char c = st.top();
        st.pop();
        tempSt.push(c);
        count++;
    }
       
    // Delete middle element
    st.pop();
       
    // Put all (n/2) element of tempSt in st
    while (!tempSt.empty()) {
        st.push(tempSt.top());
        tempSt.pop();
    }
}
 
// Driver Code
int main()
{
    stack<char> st;
 
    // Push elements into the stack
    st.push('1');
    st.push('2');
    st.push('3');
    st.push('4');
    st.push('5');
    st.push('6');
    st.push('7');
 
    deleteMid(st);
 
    // Printing stack after deletion of middle.
    while (!st.empty()) {
        char p = st.top();
        st.pop();
        cout << p << " ";
    }
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to sort an array using stack
#include <bits/stdc++.h>
using namespace std;
 
// This function return the sorted stack
stack<int> sortStack(stack<int> input)
{
    stack<int> tmpStack;
 
    while (!input.empty())
    {
        // pop out the first element
        int tmp = input.top();
        input.pop();
 
        // while temporary stack is not empty
        // and top of stack is smaller than temp
        while (!tmpStack.empty() &&
                tmpStack.top() < tmp)
        {
            // pop from temporary stack and
            // push it to the input stack
            input.push(tmpStack.top());
            tmpStack.pop();
        }
 
        // push temp in temporary of stack
        tmpStack.push(tmp);
    }
 
    return tmpStack;
}
 
void sortArrayUsingStacks(int arr[], int n)
{
    // Push array elements to stack
    stack<int> input;
    for (int i=0; i<n; i++)
       input.push(arr[i]);
 
    // Sort the temporary stack
    stack<int> tmpStack = sortStack(input);
 
    // Put stack elements in arrp[]
    for (int i=0; i<n; i++)
    {
        arr[i] = tmpStack.top();
        tmpStack.pop();
    }
}
 
// main function
int main()
{
    int arr[] = {10, 5, 15, 45};
    int n = sizeof(arr)/sizeof(arr[0]);
 
    sortArrayUsingStacks(arr, n);
 
    for (int i=0; i<n; i++)
       cout << arr[i] << " ";
 
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// C++ program to delete elements from array.
#include <bits/stdc++.h>
using namespace std;
 
// Function for deleting k elements
void deleteElements(int arr[], int n, int k)
{
    // Create a stack and push arr[0]
    stack<int> s;
    s.push(arr[0]);
 
    int count = 0;
     
    // traversing a loop from i = 1 to n
    for (int i=1; i<n; i++) {
         
        // condition for deleting an element
        while (!s.empty() && s.top() < arr[i]
                            && count < k) {                                    
            s.pop();
            count++;
        }
         
        s.push(arr[i]);
    }
     
    // Putting elements of stack in a vector
    // from end to begin.
    int m = s.size();
    vector<int> v(m); // Size of vector is m
    while (!s.empty()) {
         
        // push element from stack to vector v
        v[--m] = s.top();
        s.pop();
    }
     
    // printing result
    for (auto x : v)
        cout << x << " ";
         
    cout << endl;
}
 
// Driver code
int main()
{
    int n = 5, k = 2;
    int arr[] = {20, 10, 25, 30, 40};
    deleteElements(arr, n, k);
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// C++ program to count number of distinct instance
// where second highest number lie
// before highest number in all subarrays.
#include <bits/stdc++.h>
#define MAXN 100005
using namespace std;
 
// Finding the next greater element of the array.
void makeNext(int arr[], int n, int nextBig[])
{
    stack<pair<int, int> > s;
 
    for (int i = n - 1; i >= 0; i--) {
 
        nextBig[i] = i;
        while (!s.empty() && s.top().first < arr[i])
            s.pop();
 
        if (!s.empty())
            nextBig[i] = s.top().second;
 
        s.push(pair<int, int>(arr[i], i));
    }
}
 
// Finding the previous greater element of the array.
void makePrev(int arr[], int n, int prevBig[])
{
    stack<pair<int, int> > s;
    for (int i = 0; i < n; i++) {
 
        prevBig[i] = -1;
        while (!s.empty() && s.top().first < arr[i])
            s.pop();
 
        if (!s.empty())
            prevBig[i] = s.top().second;
 
        s.push(pair<int, int>(arr[i], i));
    }
}
 
// Wrapper Function
int wrapper(int arr[], int n)
{
    int nextBig[MAXN];
    int prevBig[MAXN];
    int maxi[MAXN];
    int ans = 0;
 
    // Finding previous largest element
    makePrev(arr, n, prevBig);
 
    // Finding next largest element
    makeNext(arr, n, nextBig);
 
    for (int i = 0; i < n; i++)
        if (nextBig[i] != i)
            maxi[nextBig[i] - i] = max(maxi[nextBig[i] - i],
                                       i - prevBig[i]);
 
    for (int i = 0; i < n; i++)
        ans += maxi[i];
 
    return ans;
}
 
// Driven Program
int main()
{
    int arr[] = { 1, 3, 2, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << wrapper(arr, n) << endl;
    return 0;
}

Time complexity: linear
Space complexity: linear
// A C++ program for merging overlapping intervals
#include <bits/stdc++.h>
using namespace std;
 
// An interval has start time and end time
struct Interval {
    int start, end;
};
 
// Compares two intervals according to their starting time.
// This is needed for sorting the intervals using library
// function std::sort(). See http://goo.gl/iGspV
bool compareInterval(Interval i1, Interval i2)
{
    return (i1.start < i2.start);
}
 
// The main function that takes a set of intervals, merges
// overlapping intervals and prints the result
void mergeIntervals(Interval arr[], int n)
{
    // Test if the given set has at least one interval
    if (n <= 0)
        return;
 
    // Create an empty stack of intervals
    stack<Interval> s;
 
    // sort the intervals in increasing order of start time
    sort(arr, arr + n, compareInterval);
 
    // push the first interval to stack
    s.push(arr[0]);
 
    // Start from the next interval and merge if necessary
    for (int i = 1; i < n; i++) {
        // get interval from stack top
        Interval top = s.top();
 
        // if current interval is not overlapping with stack
        // top, push it to the stack
        if (top.end < arr[i].start)
            s.push(arr[i]);
 
        // Otherwise update the ending time of top if ending
        // of current interval is more
        else if (top.end < arr[i].end) {
            top.end = arr[i].end;
            s.pop();
            s.push(top);
        }
    }
 
    // Print contents of stack
    cout << "\n The Merged Intervals are: ";
    while (!s.empty()) {
        Interval t = s.top();
        cout << "[" << t.start << "," << t.end << "] ";
        s.pop();
    }
    return;
}
 
// Driver program
int main()
{
    Interval arr[]
        = { { 6, 8 }, { 1, 9 }, { 2, 4 }, { 4, 7 } };
    int n = sizeof(arr) / sizeof(arr[0]);
    mergeIntervals(arr, n);
    return 0;
}

Time complexity: nlogn
Space complexity: linear
// C++ program to merge overlapping Intervals in
// O(n Log n) time and O(1) extra space.
#include <bits/stdc++.h>
using namespace std;
 
// An Interval
struct Interval {
    int s, e;
};
 
// Function used in sort
bool mycomp(Interval a, Interval b) { return a.s < b.s; }
 
void mergeIntervals(Interval arr[], int n)
{
    // Sort Intervals in increasing order of
    // start time
    sort(arr, arr + n, mycomp);
 
    int index = 0; // Stores index of last element
    // in output array (modified arr[])
 
    // Traverse all input Intervals
    for (int i = 1; i < n; i++) {
        // If this is not first Interval and overlaps
        // with the previous one
        if (arr[index].e >= arr[i].s) {
            // Merge previous and current Intervals
            arr[index].e = max(arr[index].e, arr[i].e);
        }
        else {
            index++;
            arr[index] = arr[i];
        }
    }
 
    // Now arr[0..index-1] stores the merged Intervals
    cout << "\n The Merged Intervals are: ";
    for (int i = 0; i <= index; i++)
        cout << "[" << arr[i].s << ", " << arr[i].e << "] ";
}
 
// Driver program
int main()
{
    Interval arr[]
        = { { 6, 8 }, { 1, 9 }, { 2, 4 }, { 4, 7 } };
    int n = sizeof(arr) / sizeof(arr[0]);
    mergeIntervals(arr, n);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Time complexity: nlogn
Space complexity: constant
// C++ program to find maximum rectangular area in
// linear time
#include <bits/stdc++.h>
using namespace std;
  
// The main function to find the maximum rectangular
// area under given histogram with n bars
int getMaxArea(int hist[], int n)
{
    // Create an empty stack. The stack holds indexes
    // of hist[] array. The bars stored in stack are
    // always in increasing order of their heights.
    stack<int> s;
  
    int max_area = 0; // Initialize max area
    int tp; // To store top of stack
    int area_with_top; // To store area with top bar
                       // as the smallest bar
  
    // Run through all bars of given histogram
    int i = 0;
    while (i < n) {
        // If this bar is higher than the bar on top
        // stack, push it to stack
        if (s.empty() || hist[s.top()] <= hist[i])
            s.push(i++);
  
        // If this bar is lower than top of stack,
        // then calculate area of rectangle with stack
        // top as the smallest (or minimum height) bar.
        // 'i' is 'right index' for the top and element
        // before top in stack is 'left index'
        else {
            tp = s.top(); // store the top index
            s.pop(); // pop the top
  
            // Calculate the area with hist[tp] stack
            // as smallest bar
            area_with_top
                = hist[tp]
                  * (s.empty() ? i : i - s.top() - 1);
  
            // update max area, if needed
            if (max_area < area_with_top)
                max_area = area_with_top;
        }
    }
  
    // Now pop the remaining bars from stack and calculate
    // area with every popped bar as the smallest bar
    while (s.empty() == false) {
        tp = s.top();
        s.pop();
        area_with_top
            = hist[tp] * (s.empty() ? i : i - s.top() - 1);
  
        if (max_area < area_with_top)
            max_area = area_with_top;
    }
  
    return max_area;
}
  
// Driver code
int main()
{
    int hist[] = { 6, 2, 5, 4, 5, 1, 6 };
    int n = sizeof(hist) / sizeof(hist[0]);
  
    // Function call
    cout << "Maximum area is " << getMaxArea(hist, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ code for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find largest rectangular area possible in a
// given histogram.
int getMaxArea(int arr[], int n)
{
    // Your code here
    // we create an empty stack here.
    stack<int> s;
    // we push -1 to the stack because for some elements
    // there will be no previous smaller element in the
    // array and we can store -1 as the index for previous
    // smaller.
    s.push(-1);
    int area = arr[0];
    int i = 0;
    // We declare left_smaller and right_smaller array of
    // size n and initialize them with -1 and n as their
    // default value. left_smaller[i] will store the index
    // of previous smaller element for ith element of the
    // array. right_smaller[i] will store the index of next
    // smaller element for ith element of the array.
    vector<int> left_smaller(n, -1), right_smaller(n, n);
    while (i < n) {
        while (!s.empty() && s.top() != -1
               && arr[s.top()] > arr[i]) {
            // if the current element is smaller than
            // element with index stored on the top of stack
            // then, we pop the top element and store the
            // current element index as the right_smaller
            // for the popped element.
            right_smaller[s.top()] = i;
            s.pop();
        }
        if (i > 0 && arr[i] == arr[i - 1]) {
            // we use this condition to avoid the
            // unnecessary loop to find the left_smaller.
            // since the previous element is same as current
            // element, the left_smaller will always be the
            // same for both.
            left_smaller[i] = left_smaller[i - 1];
        }
        else {
            // Element with the index stored on the top of
            // the stack is always smaller than the current
            // element. Therefore the left_smaller[i] will
            // always be s.top().
            left_smaller[i] = s.top();
        }
        s.push(i);
        i++;
    }
    for (int j = 0; j < n; j++) {
        // here we find area with every element as the
        // smallest element in their range and compare it
        // with the previous area.
        // in this way we get our max Area form this.
        area = max(area, arr[j]
                             * (right_smaller[j]
                                - left_smaller[j] - 1));
    }
    return area;
}
  
// Driver code
int main()
{
    int hist[] = { 6, 2, 5, 4, 5, 1, 6 };
    int n = sizeof(hist) / sizeof(hist[0]);
  
    // Function call
    cout << "maxArea = " << getMaxArea(hist, n) << endl;
    return 0;
}
  
// This code is Contributed by Arunit Kumar.

Time complexity: linear
Space complexity: linear
// C++ program to reverse a string using stack
#include <bits/stdc++.h>
using namespace std;
 
// A structure to represent a stack
class Stack {
public:
    int top;
    unsigned capacity;
    char* array;
};
 
// function to create a stack of given
// capacity. It initializes size of stack as 0
Stack* createStack(unsigned capacity)
{
    Stack* stack = new Stack();
    stack->capacity = capacity;
    stack->top = -1;
    stack->array
        = new char[(stack->capacity * sizeof(char))];
    return stack;
}
 
// Stack is full when top is equal to the last index
int isFull(Stack* stack)
{
    return stack->top == stack->capacity - 1;
}
 
// Stack is empty when top is equal to -1
int isEmpty(Stack* stack) { return stack->top == -1; }
 
// Function to add an item to stack.
// It increases top by 1
void push(Stack* stack, char item)
{
    if (isFull(stack))
        return;
    stack->array[++stack->top] = item;
}
 
// Function to remove an item from stack.
// It decreases top by 1
char pop(Stack* stack)
{
    if (isEmpty(stack))
        return -1;
    return stack->array[stack->top--];
}
 
// A stack based function to reverse a string
void reverse(char str[])
{
    // Create a stack of capacity
    // equal to length of string
    int n = strlen(str);
    Stack* stack = createStack(n);
 
    // Push all characters of string to stack
    int i;
    for (i = 0; i < n; i++)
        push(stack, str[i]);
 
    // Pop all characters of string and
    // put them back to str
    for (i = 0; i < n; i++)
        str[i] = pop(stack);
}
 
// Driver code
int main()
{
    char str[] = "GeeksQuiz";
 
    reverse(str);
    cout << "Reversed string is " << str;
 
    return 0;
}
 
// This code is contributed by rathbhupendra

Time complexity: linear
Space complexity: linear
// C++ recursive function to
// solve tower of hanoi puzzle
#include <bits/stdc++.h>
using namespace std;
 
void towerOfHanoi(int n, char from_rod, char to_rod,
                  char aux_rod)
{
    if (n == 0) {
        return;
    }
    towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
    cout << "Move disk " << n << " from rod " << from_rod
         << " to rod " << to_rod << endl;
    towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
}
 
// Driver code
int main()
{
    int N = 3;
 
    // A, B and C are names of rods
    towerOfHanoi(N, 'A', 'C', 'B');
    return 0;
}
 
// This is code is contributed by rathbhupendra

Time complexity: linear
Space complexity: linear
// A C++ program to find the maximum depth of nested
// parenthesis in a given expression
#include <bits/stdc++.h>
using namespace std;
 
int maxDepth(string& s)
{
    int count = 0;
    stack<int> st;
 
    for (int i = 0; i < s.size(); i++) {
        if (s[i] == '(')
            st.push(i); // pushing the bracket in the stack
        else if (s[i] == ')') {
            if (count < st.size())
                count = st.size();
            /*keeping track of the parenthesis and storing
            it before removing it when it gets balanced*/
            st.pop();
        }
    }
    return count;
}
 
// Driver program
int main()
{
    string s = "( ((X)) (((Y))) )";
    cout << maxDepth(s);
 
    // This code is contributed by rakeshsahni
 
    return 0;
}

Time complexity: linear
Space complexity: constant