Banana-Leopard/TASTY huggingface dataset:
// CPP Program to convert postfix to prefix
#include <bits/stdc++.h>
using namespace std;
 
// function to check if character is operator or not
bool isOperator(char x)
{
    switch (x) {
    case '+':
    case '-':
    case '/':
    case '*':
        return true;
    }
    return false;
}
 
// Convert postfix to Prefix expression
string postToPre(string post_exp)
{
    stack<string> s;
 
    // length of expression
    int length = post_exp.size();
 
    // reading from right to left
    for (int i = 0; i < length; i++) {
 
        // check if symbol is operator
        if (isOperator(post_exp[i])) {
 
            // pop two operands from stack
            string op1 = s.top();
            s.pop();
            string op2 = s.top();
            s.pop();
 
            // concat the operands and operator
            string temp = post_exp[i] + op2 + op1;
 
            // Push string temp back to stack
            s.push(temp);
        }
 
        // if symbol is an operand
        else {
 
            // push the operand to the stack
            s.push(string(1, post_exp[i]));
        }
    }
 
    string ans = "";
    while (!s.empty()) {
        ans += s.top();
        s.pop();
    }
    return ans;
}
 
// Driver Code
int main()
{
    string post_exp = "ABC/-AK/L-*";
 
    // Function call
    cout << "Prefix : " << postToPre(post_exp);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ linear time solution for stock span problem
#include <iostream>
#include <stack>
using namespace std;
 
// A stack based efficient method to calculate
// stock span values
void calculateSpan(int price[], int n, int S[])
{
    // Create a stack and push index of first
    // element to it
    stack<int> st;
    st.push(0);
 
    // Span value of first element is always 1
    S[0] = 1;
 
    // Calculate span values for rest of the elements
    for (int i = 1; i < n; i++) {
        // Pop elements from stack while stack is not
        // empty and top of stack is smaller than
        // price[i]
        while (!st.empty() && price[st.top()] <= price[i])
            st.pop();
 
        // If stack becomes empty, then price[i] is
        // greater than all elements on left of it,
        // i.e., price[0], price[1], ..price[i-1].  Else
        // price[i] is greater than elements after
        // top of stack
        S[i] = (st.empty()) ? (i + 1) : (i - st.top());
 
        // Push this element to stack
        st.push(i);
    }
}
 
// A utility function to print elements of array
void printArray(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// Driver program to test above function
int main()
{
    int price[] = { 10, 4, 5, 90, 120, 80 };
    int n = sizeof(price) / sizeof(price[0]);
    int S[n];
 
    // Fill the span values in array S[]
    calculateSpan(price, n, S);
 
    // print the calculated span values
    printArray(S, n);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program for brute force method
// to calculate stock span values
#include <bits/stdc++.h>
using namespace std;
 
 
vector <int> calculateSpan(int arr[], int n)
    {
       // Your code here
        stack<int> s;
        vector<int> ans;
        for(int i=0;i<n;i++)
        {
            while(!s.empty() and arr[s.top()] <= arr[i])
                        s.pop();
                     
            if(s.empty())
                ans.push_back(i+1);
            else
            {
                int top = s.top();
                    ans.push_back(i-top);
            }
            s.push(i);
        }
     
        return ans;
    }
     
// A utility function to print elements of array
void printArray(vector<int> arr)
{
    for (int i = 0; i < arr.size(); i++)
        cout << arr[i] << " ";
}
 
// Driver code
int main()
{
    int price[] = { 10, 4, 5, 90, 120, 80 };
    int n = sizeof(price) / sizeof(price[0]);
    int S[n];
 
     
    vector<int> arr = calculateSpan(price, n);
    printArray(arr);
    
 
    return 0;
}
 
// This is code is contributed by Arpit Jain

Time complexity: linear
Space complexity: linear
// C++ program to check for balanced brackets.
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if brackets are balanced
bool areBracketsBalanced(string expr)
{
    // Declare a stack to hold the previous brackets.
    stack<char> temp;
    for (int i = 0; i < expr.length(); i++) {
        if (temp.empty()) {
             
            // If the stack is empty
            // just push the current bracket
            temp.push(expr[i]);
        }
        else if ((temp.top() == '(' && expr[i] == ')')
                 || (temp.top() == '{' && expr[i] == '}')
                 || (temp.top() == '[' && expr[i] == ']')) {
             
            // If we found any complete pair of bracket
            // then pop
            temp.pop();
        }
        else {
            temp.push(expr[i]);
        }
    }
    if (temp.empty()) {
         
        // If stack is empty return true
        return true;
    }
    return false;
}
 
// Driver code
int main()
{
    string expr = "{()}[]";
 
    // Function call
    if (areBracketsBalanced(expr))
        cout << "Balanced";
    else
        cout << "Not Balanced";
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program of Next Greater Frequency Element
#include <iostream>
#include <stack>
#include <stdio.h>
 
using namespace std;
 
/*NFG function to find the next greater frequency
element for each element in the array*/
void NFG(int a[], int n, int freq[])
{
 
    // stack data structure to store the position
    // of array element
    stack<int> s;
    s.push(0);
 
    // res to store the value of next greater
    // frequency element for each element
    int res[n] = { 0 };
    for (int i = 1; i < n; i++)
    {
        /* If the frequency of the element which is
            pointed by the top of stack is greater
            than frequency of the current element
            then push the current position i in stack*/
 
        if (freq[a[s.top()]] > freq[a[i]])
            s.push(i);
        else {
            /*If the frequency of the element which
            is pointed by the top of stack is less
            than frequency of the current element, then
            pop the stack and continuing popping until
            the above condition is true while the stack
            is not empty*/
 
            while ( !s.empty()
                   && freq[a[s.top()]] < freq[a[i]])
            {
 
                res[s.top()] = a[i];
                s.pop();
            }
            //  now push the current element
            s.push(i);
        }
    }
 
    while (!s.empty()) {
        res[s.top()] = -1;
        s.pop();
    }
    for (int i = 0; i < n; i++)
    {
        // Print the res list containing next
        // greater frequency element
        cout << res[i] << " ";
    }
}
 
// Driver code
int main()
{
 
    int a[] = { 1, 1, 2, 3, 4, 2, 1 };
    int len = 7;
    int max = INT16_MIN;
    for (int i = 0; i < len; i++)
    {
        // Getting the max element of the array
        if (a[i] > max) {
            max = a[i];
        }
    }
    int freq[max + 1] = { 0 };
 
    // Calculating frequency of each element
    for (int i = 0; i < len; i++)
    {
        freq[a[i]]++;
    }
 
    // Function call
    NFG(a, len, freq);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program of Next Greater Frequency Element
#include <bits/stdc++.h>
using namespace std;
 
stack<pair<int,int>> mystack;
map<int, int> mymap;
  
/*NFG function to find the next greater frequency
element for each element and for placing it in the
resultant array */
void NGF(int arr[], int res[], int n) {
       
    // Initially store the frequencies of all elements
    // in a hashmap
    for(int i = 0; i < n; i++) {
        mymap[arr[i]] += 1;
    }
       
    // Get the frequency of the last element
    int curr_freq = mymap[arr[n-1]];
    
    // push it to the stack
    mystack.push({arr[n-1], curr_freq});
    
    // place -1 as next greater freq for the last
    // element as it does not have next greater.
    res[n-1] = -1;
       
    // iterate through array in reverse order
    for(int i = n-2;i>=0;i--) {
        curr_freq = mymap[arr[i]];
           
        /* If the frequency of the element which is
        pointed by the top of stack is greater
        than frequency of the current element
        then push the current position i in stack*/
        while(mystack.size() > 0  &&  curr_freq >= mystack.top().second)
            mystack.pop();
           
        // If the stack is empty, place -1. If it is not empty
        // then we will have next higher freq element at the top of the stack.
        res[i] = (mystack.size() == 0) ? -1 : mystack.top().first;
           
        // push the element at current position
        mystack.push({arr[i], mymap[arr[i]]});
    }
}
     
int main()
{
    int arr[] = {1, 1, 1, 2, 2, 2, 2, 11, 3, 3};
    int n = sizeof(arr) / sizeof(arr[0]);
       
    int res[n];
    NGF(arr, res, n);
    cout << "[";
    for(int i = 0; i < n - 1; i++)
    {
        cout << res[i] << ", ";
    }
    cout << res[n - 1] << "]";
 
    return 0;
}
 
// This code is contributed by divyeshrabadiya07.

Time complexity: linear
Space complexity: linear
// C++ code for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find number of next
// greater elements on the right of
// a given element
int nextGreaterElements(vector<int>& a, int index)
{
    int count = 0, N = a.size();
    for (int i = index + 1; i < N; i++)
        if (a[i] > a[index])
            count++;
 
    return count;
}
 
// Driver's code
int main()
{
 
    vector<int> a = { 3, 4, 2, 7, 5, 8, 10, 6 };
    int Q = 2;
    vector<int> queries = { 0, 5 };
 
    for (int i = 0; i < Q; i++)
        // Function call
        cout << nextGreaterElements(a, queries[i]) << " ";
 
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to find celebrity
#include <bits/stdc++.h>
#include <list>
using namespace std;
 
// Max # of persons in the party
#define N 8
 
// Person with 2 is celebrity
bool MATRIX[N][N] = { { 0, 0, 1, 0 },
                      { 0, 0, 1, 0 },
                      { 0, 0, 0, 0 },
                      { 0, 0, 1, 0 } };
 
bool knows(int a, int b) { return MATRIX[a][b]; }
 
// Returns -1 if celebrity
// is not present. If present,
// returns id (value from 0 to n-1).
int findCelebrity(int n)
{
    // the graph needs not be constructed
    // as the edges can be found by
    // using knows function
 
    // degree array;
    int indegree[n] = { 0 }, outdegree[n] = { 0 };
 
    // query for all edges
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int x = knows(i, j);
 
            // set the degrees
            outdegree[i] += x;
            indegree[j] += x;
        }
    }
 
    // find a person with indegree n-1
    // and out degree 0
    for (int i = 0; i < n; i++)
        if (indegree[i] == n - 1 && outdegree[i] == 0)
            return i;
 
    return -1;
}
 
// Driver code
int main()
{
    int n = 4;
    int id = findCelebrity(n);
    id == -1 ? cout << "No celebrity"
             : cout << "Celebrity ID " << id;
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// C++ program to find celebrity
#include <bits/stdc++.h>
#include <list>
using namespace std;
 
// Max # of persons in the party
#define N 8
 
// Person with 2 is celebrity
bool MATRIX[N][N] = { { 0, 0, 1, 0 },
                      { 0, 0, 1, 0 },
                      { 0, 0, 0, 0 },
                      { 0, 0, 1, 0 } };
 
bool knows(int a, int b) { return MATRIX[a][b]; }
 
// Returns -1 if a 'potential celebrity'
// is not present. If present,
// returns id (value from 0 to n-1).
int findPotentialCelebrity(int n)
{
    // base case - when n reaches 0 , returns -1
    // since n represents the number of people,
    // 0 people implies no celebrity(= -1)
    if (n == 0)
        return -1;
 
    // find the celebrity with n-1
    // persons
    int id = findPotentialCelebrity(n - 1);
 
    // if there are no celebrities
    if (id == -1)
        return n - 1;
 
    // if the id knows the nth person
    // then the id cannot be a celebrity, but nth person
    // could be one
    else if (knows(id, n - 1)) {
        return n - 1;
    }
    // if the nth person knows the id,
    // then the nth person cannot be a celebrity and the id
    // could be one
    else if (knows(n - 1, id)) {
        return id;
    }
 
    // if there is no celebrity
    return -1;
}
 
// Returns -1 if celebrity
// is not present. If present,
// returns id (value from 0 to n-1).
// a wrapper over findCelebrity
int Celebrity(int n)
{
    // find the celebrity
    int id = findPotentialCelebrity(n);
 
    // check if the celebrity found
    // is really the celebrity
    if (id == -1)
        return id;
    else {
        int c1 = 0, c2 = 0;
 
        // check the id is really the
        // celebrity
        for (int i = 0; i < n; i++)
            if (i != id) {
                c1 += knows(id, i);
                c2 += knows(i, id);
            }
 
        // if the person is known to
        // everyone.
        if (c1 == 0 && c2 == n - 1)
            return id;
 
        return -1;
    }
}
 
// Driver code
int main()
{
    int n = 4;
    int id = Celebrity(n);
    id == -1 ? cout << "No celebrity"
             : cout << "Celebrity ID " << id;
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to find celebrity
#include <bits/stdc++.h>
#include <list>
using namespace std;
 
// Max # of persons in the party
#define N 8
 
// Person with 2 is celebrity
bool MATRIX[N][N] = { { 0, 0, 1, 0 },
                      { 0, 0, 1, 0 },
                      { 0, 0, 0, 0 },
                      { 0, 0, 1, 0 } };
 
bool knows(int a, int b) { return MATRIX[a][b]; }
 
// Returns -1 if celebrity
// is not present. If present,
// returns id (value from 0 to n-1).
int findCelebrity(int n)
{
 
    stack<int> s;
 
    // Celebrity
    int C;
 
    // Push everybody to stack
    for (int i = 0; i < n; i++)
        s.push(i);
 
    // Extract top 2
 
    // Find a potential celebrity
    while (s.size() > 1) {
        int A = s.top();
        s.pop();
        int B = s.top();
        s.pop();
        if (knows(A, B)) {
            s.push(B);
        }
        else {
            s.push(A);
        }
    }
 
    // Potential candidate?
    C = s.top();
    s.pop();
 
    // Check if C is actually
    // a celebrity or not
    for (int i = 0; i < n; i++) {
        // If any person doesn't
        // know 'C' or 'C' doesn't
        // know any person, return -1
        if ((i != C) && (knows(C, i) || !knows(i, C)))
            return -1;
    }
 
    return C;
}
 
// Driver code
int main()
{
    int n = 4;
    int id = findCelebrity(n);
    id == -1 ? cout << "No celebrity"
             : cout << "Celebrity ID " << id;
    return 0;
}

Time complexity: linear
Space complexity: linear
#include <bits/stdc++.h>
using namespace std;
 
class Solution {
public:
    // Function to find if there is a celebrity in the party
    // or not.
    int celebrity(int M[4][4], int n)
    {
        // r=row number
        int r = 0;
        for (int i = 1; i < n; i++) {
            // checking if r th person knows i th person
            if (M[r][i] == 1) {
                M[r][r] = 1;
                r = i;
            }
            else {
                M[i][i] = 1;
            }
        }
        for (int i = 0; i < n; i++) {
            // checking if i th person can be a celebrity or
            // not
            if (M[i][i] == 0) {
                int flag = 0;
                // iterating in the i th column to check
                // whether everyone knows i th person or not
                for (int j = 0; j < n; j++) {
                    // checking if M[j][i] is not a diagonal
                    // element and if j th person knows i th
                    // person
                    if (j != i && M[j][i] == 0) {
                        flag = 1;
                        break;
                    }
                }
                if (flag == 0)
                    return i;
            }
        }
        return -1;
    }
};
 
int main()
{
    int M[4][4] = { { 0, 0, 1, 0 },
                    { 0, 0, 1, 0 },
                    { 0, 0, 0, 0 },
                    { 0, 0, 1, 0 } };
    Solution ob;
    int a = ob.celebrity(M, 4);
    if (a == -1) {
        cout << "No Celebrity" << endl;
    }
    else {
        cout << "Celebrity ID " << a << endl;
    }
}
// Contributed by Yash Goyal

Time complexity: linear
Space complexity: constant
// C++ program to find celebrity
// in the given Matrix of people
#include <bits/stdc++.h>
using namespace std;
#define N 4
int celebrity(int M[N][N], int n)
{
    // This function returns the celebrity
    // index 0-based (if any)
 
    int i = 0, j = n - 1;
    while (i < j) {
        if (M[j][i] == 1) // j knows i
            j--;
        else // j doesnt know i so i cant be celebrity
            i++;
    }
    // i points to our celebrity candidate
    int candidate = i;
 
    // Now, all that is left is to check that whether
    // the candidate is actually a celebrity i.e: he is
    // known by everyone but he knows no one
    for (i = 0; i < n; i++) {
        if (i != candidate) {
            if (M[i][candidate] == 0
                || M[candidate][i] == 1)
                return -1;
        }
    }
    // if we reach here this means that the candidate
    // is really a celebrity
    return candidate;
}
 
int main()
{
    int M[N][N] = { { 0, 0, 1, 0 },
                    { 0, 0, 1, 0 },
                    { 0, 0, 0, 0 },
                    { 0, 0, 1, 0 } };
 
    int celebIdx = celebrity(M, 4);
 
    if (celebIdx == -1)
        cout << ("No Celebrity");
    else {
        cout << "Celebrity ID " << celebIdx;
    }
    return 0;
}
 
// This code contributed by gauravrajput1

Time complexity: linear
Space complexity: constant
// C++ program to evaluate value of a postfix expression
#include <iostream>
#include <string.h>
 
using namespace std;
 
// Stack type
struct Stack
{
    int top;
    unsigned capacity;
    int* array;
};
 
// Stack Operations
struct Stack* createStack( unsigned capacity )
{
    struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
 
    if (!stack) return NULL;
 
    stack->top = -1;
    stack->capacity = capacity;
    stack->array = (int*) malloc(stack->capacity * sizeof(int));
 
    if (!stack->array) return NULL;
 
    return stack;
}
 
int isEmpty(struct Stack* stack)
{
    return stack->top == -1 ;
}
 
char peek(struct Stack* stack)
{
    return stack->array[stack->top];
}
 
char pop(struct Stack* stack)
{
    if (!isEmpty(stack))
        return stack->array[stack->top--] ;
    return '$';
}
 
void push(struct Stack* stack, char op)
{
    stack->array[++stack->top] = op;
}
 
 
// The main function that returns value of a given postfix expression
int evaluatePostfix(char* exp)
{
    // Create a stack of capacity equal to expression size
    struct Stack* stack = createStack(strlen(exp));
    int i;
 
    // See if stack was created successfully
    if (!stack) return -1;
 
    // Scan all characters one by one
    for (i = 0; exp[i]; ++i)
    {
        // If the scanned character is an operand (number here),
        // push it to the stack.
        if (isdigit(exp[i]))
            push(stack, exp[i] - '0');
 
        // If the scanned character is an operator, pop two
        // elements from stack apply the operator
        else
        {
            int val1 = pop(stack);
            int val2 = pop(stack);
            switch (exp[i])
            {
            case '+': push(stack, val2 + val1); break;
            case '-': push(stack, val2 - val1); break;
            case '*': push(stack, val2 * val1); break;
            case '/': push(stack, val2/val1); break;
            }
        }
    }
    return pop(stack);
}
 
// Driver program to test above functions
int main()
{
    char exp[] = "231*+9-";
    cout<<"postfix evaluation: "<< evaluatePostfix(exp);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to evaluate value of a postfix
// expression having multiple digit operands
#include <bits/stdc++.h>
using namespace std;
 
// Stack type
class Stack
{
    public:
    int top;
    unsigned capacity;
    int* array;
};
 
// Stack Operations
Stack* createStack( unsigned capacity )
{
    Stack* stack = new Stack();
 
    if (!stack) return NULL;
 
    stack->top = -1;
    stack->capacity = capacity;
    stack->array = new int[(stack->capacity * sizeof(int))];
 
    if (!stack->array) return NULL;
 
    return stack;
}
 
int isEmpty(Stack* stack)
{
    return stack->top == -1 ;
}
 
int peek(Stack* stack)
{
    return stack->array[stack->top];
}
 
int pop(Stack* stack)
{
    if (!isEmpty(stack))
        return stack->array[stack->top--] ;
    return '$';
}
 
void push(Stack* stack,int op)
{
    stack->array[++stack->top] = op;
}
 
 
// The main function that returns value
// of a given postfix expression
int evaluatePostfix(char* exp)
{
    // Create a stack of capacity equal to expression size
    Stack* stack = createStack(strlen(exp));
    int i;
 
    // See if stack was created successfully
    if (!stack) return -1;
 
    // Scan all characters one by one
    for (i = 0; exp[i]; ++i)
    {
        //if the character is blank space then continue
        if(exp[i] == ' ')continue;
         
        // If the scanned character is an
        // operand (number here),extract the full number
        // Push it to the stack.
        else if (isdigit(exp[i]))
        {
            int num=0;
             
            //extract full number
            while(isdigit(exp[i]))
            {
            num = num * 10 + (int)(exp[i] - '0');
                i++;
            }
            i--;
             
            //push the element in the stack
            push(stack,num);
        }
         
        // If the scanned character is an operator, pop two
        // elements from stack apply the operator
        else
        {
            int val1 = pop(stack);
            int val2 = pop(stack);
             
            switch (exp[i])
            {
            case '+': push(stack, val2 + val1); break;
            case '-': push(stack, val2 - val1); break;
            case '*': push(stack, val2 * val1); break;
            case '/': push(stack, val2/val1); break;
             
            }
        }
    }
    return pop(stack);
}
 
// Driver code
int main()
{
    char exp[] = "100 200 + 2 / 5 * 7 +";
    cout << evaluatePostfix(exp);
    return 0;
}
 
// This code is contributed by rathbhupendra

Time complexity: linear
Space complexity: linear