Banana-Leopard/TASTY huggingface dataset:
// A C++ program to find the maximum depth of nested
// parenthesis in a given expression
#include <iostream>
using namespace std;
 
// function takes a string and returns the
// maximum depth nested parenthesis
int maxDepth(string S)
{
    int current_max = 0; // current count
    int max = 0; // overall maximum count
    int n = S.length();
 
    // Traverse the input string
    for (int i = 0; i < n; i++)
    {
        if (S[i] == '(')
        {
            current_max++;
 
            // update max if required
            if (current_max > max)
                max = current_max;
        }
        else if (S[i] == ')')
        {
            if (current_max > 0)
                current_max--;
            else
                return -1;
        }
    }
 
    // finally check for unbalanced string
    if (current_max != 0)
        return -1;
 
    return max;
}
 
// Driver program
int main()
{
    string s = "( ((X)) (((Y))) )";
    cout << maxDepth(s);
    return 0;
}

Time complexity: linear
Space complexity: constant
// A naive method to find maximum of
// minimum of all windows of different
// sizes
#include <bits/stdc++.h>
using namespace std;
 
void printMaxOfMin(int arr[], int n)
{
    // Consider all windows of different
    // sizes starting from size 1
    for (int k = 1; k <= n; k++) {
        // Initialize max of min for
        // current window size k
        int maxOfMin = INT_MIN;
 
        // Traverse through all windows
        // of current size k
        for (int i = 0; i <= n - k; i++) {
            // Find minimum of current window
            int min = arr[i];
            for (int j = 1; j < k; j++) {
                if (arr[i + j] < min)
                    min = arr[i + j];
            }
 
            // Update maxOfMin if required
            if (min > maxOfMin)
                maxOfMin = min;
        }
 
        // Print max of min for current
        // window size
        cout << maxOfMin << " ";
    }
}
 
// Driver program
int main()
{
    int arr[] = { 10, 20, 30, 50, 10, 70, 30 };
    int n = sizeof(arr) / sizeof(arr[0]);
    printMaxOfMin(arr, n);
    return 0;
}

Time complexity: cubic
Space complexity: constant
// An efficient C++ program to find
// maximum of all minimums of
// windows of different sizes
#include <iostream>
#include <stack>
using namespace std;
 
void printMaxOfMin(int arr[], int n)
{
    // Used to find previous and next smaller
    stack<int> s;
 
    // Arrays to store previous and next smaller
    int left[n + 1];
    int right[n + 1];
 
    // Initialize elements of left[] and right[]
    for (int i = 0; i < n; i++) {
        left[i] = -1;
        right[i] = n;
    }
 
    // Fill elements of left[] using logic discussed on
    // https://www.geeksforgeeks.org/next-greater-element/
    for (int i = 0; i < n; i++) {
        while (!s.empty() && arr[s.top()] >= arr[i])
            s.pop();
 
        if (!s.empty())
            left[i] = s.top();
 
        s.push(i);
    }
 
    // Empty the stack as stack is
    // going to be used for right[]
    while (!s.empty())
        s.pop();
 
    // Fill elements of right[] using same logic
    for (int i = n - 1; i >= 0; i--) {
        while (!s.empty() && arr[s.top()] >= arr[i])
            s.pop();
 
        if (!s.empty())
            right[i] = s.top();
 
        s.push(i);
    }
 
    // Create and initialize answer array
    int ans[n + 1];
    for (int i = 0; i <= n; i++)
        ans[i] = 0;
 
    // Fill answer array by comparing minimums of all
    // lengths computed using left[] and right[]
    for (int i = 0; i < n; i++) {
        // length of the interval
        int len = right[i] - left[i] - 1;
 
        // arr[i] is a possible answer for this length
        // 'len' interval, check if arr[i] is more than
        // max for 'len'
        ans[len] = max(ans[len], arr[i]);
    }
 
    // Some entries in ans[] may not be filled yet. Fill
    // them by taking values from right side of ans[]
    for (int i = n - 1; i >= 1; i--)
        ans[i] = max(ans[i], ans[i + 1]);
 
    // Print the result
    for (int i = 1; i <= n; i++)
        cout << ans[i] << " ";
}
 
// Driver program
int main()
{
    int arr[] = { 10, 20, 30, 50, 10, 70, 30 };
    int n = sizeof(arr) / sizeof(arr[0]);
    printMaxOfMin(arr, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
/* C++ Program to check whether valid
 expression is redundant or not*/
#include <bits/stdc++.h>
using namespace std;
 
// Function to check redundant brackets in a
// balanced expression
bool checkRedundancy(string& str)
{
    // create a stack of characters
    stack<char> st;
 
    // Iterate through the given expression
    for (auto& ch : str) {
 
        // if current character is close parenthesis ')'
        if (ch == ')') {
            char top = st.top();
            st.pop();
 
            // If immediate pop have open parenthesis '('
            // duplicate brackets found
            bool flag = true;
 
            while (!st.empty() and top != '(') {
 
                // Check for operators in expression
                if (top == '+' || top == '-' ||
                    top == '*' || top == '/')
                    flag = false;
 
                // Fetch top element of stack
                top = st.top();
                st.pop();
            }
 
            // If operators not found
            if (flag == true)
                return true;
        }
 
        else
            st.push(ch); // push open parenthesis '(',
                  // operators and operands to stack
    }
    return false;
}
 
// Function to check redundant brackets
void findRedundant(string& str)
{
    bool ans = checkRedundancy(str);
    if (ans == true)
        cout << "Yes\n";
    else
        cout << "No\n";
}
 
// Driver code
int main()
{
    string str = "((a+b))";
    findRedundant(str);
    return 0;
}

Time complexity: linear
Space complexity: linear
// CPP program to mark balanced and unbalanced
// parenthesis.
#include <bits/stdc++.h>
using namespace std;
 
void identifyParenthesis(string a)
{
    stack<int> st;
 
    // run the loop upto end of the string
    for (int i = 0; i < a.length(); i++) {
 
        // if a[i] is opening bracket then push
        // into stack
        if (a[i] == '(')
            st.push(i);
         
        // if a[i] is closing bracket ')'
        else if (a[i] == ')') {
 
            // If this closing bracket is unmatched
            if (st.empty())
                a.replace(i, 1, "-1");
             
            else {
 
                // replace all opening brackets with 0
                // and closing brackets with 1
                a.replace(i, 1, "1");
                a.replace(st.top(), 1, "0");
                st.pop();
            }
        }
    }
 
    // if stack is not empty then pop out all
    // elements from it and replace -1 at that
    // index of the string
    while (!st.empty()) {
        a.replace(st.top(), 1, "-1");
        st.pop();
    }
 
    // print final string
    cout << a << endl;
}
 
// Driver code
int main()
{
    string str = "(a))";
    identifyParenthesis(str);
    return 0;
}

Time complexity: linear
Space complexity: linear
// CPP program to check if two expressions
// evaluate to same.
#include <bits/stdc++.h>
using namespace std;
 
const int MAX_CHAR = 26;
 
// Return local sign of the operand. For example,
// in the expr a-b-(c), local signs of the operands
// are +a, -b, +c
bool adjSign(string s, int i)
{
 if (i == 0)
  return true;
 if (s[i - 1] == '-')
  return false;
 return true;
};
 
// Evaluate expressions into the count vector of
// the 26 alphabets.If add is true, then add count
// to the count vector of the alphabets, else remove
// count from the count vector.
void eval(string s, vector<int>& v, bool add)
{
 // stack stores the global sign
 // for operands.
 stack<bool> stk;
 stk.push(true);
 
 // + means true
 // global sign is positive initially
 
 int i = 0;
 while (s[i] != '\0') {
  if (s[i] == '+' || s[i] == '-') {
   i++;
   continue;
  }
  if (s[i] == '(') {
 
   // global sign for the bracket is
   // pushed to the stack
   if (adjSign(s, i))
    stk.push(stk.top());
   else
    stk.push(!stk.top());
  }
 
  // global sign is popped out which
  // was pushed in for the last bracket
  else if (s[i] == ')')
   stk.pop();
   
  else {
 
   // global sign is positive (we use different
   // values in two calls of functions so that
   // we finally check if all vector elements
   // are 0.
   if (stk.top())    
    v[s[i] - 'a'] += (adjSign(s, i) ? add ? 1 : -1 :
            add ? -1 : 1);
 
   // global sign is negative here
   else   
    v[s[i] - 'a'] += (adjSign(s, i) ? add ? -1 : 1 :
            add ? 1 : -1);
  }
  i++;
 }
};
 
// Returns true if expr1 and expr2 represent
// same expressions
bool areSame(string expr1, string expr2)
{
 // Create a vector for all operands and
 // initialize the vector as 0.
 vector<int> v(MAX_CHAR, 0);
 
 // Put signs of all operands in expr1
 eval(expr1, v, true);
 
 // Subtract signs of operands in expr2
 eval(expr2, v, false);
 
 // If expressions are same, vector must
 // be 0.
 for (int i = 0; i < MAX_CHAR; i++)
  if (v[i] != 0)
   return false;
 
 return true;
}
 
// Driver code
int main()
{
 string expr1 = "-(a+b+c)", expr2 = "-a-b-c";
 if (areSame(expr1, expr2))
  cout << "Yes\n";
 else
  cout << "No\n";
 return 0;
}

Time complexity: linear
Space complexity: linear
// CPP program to find index of closing
// bracket for given opening bracket.
#include <bits/stdc++.h>
using namespace std;
  
// Function to find index of closing
// bracket for given opening bracket.
void test(string expression, int index){
    int i;
      
    // If index given is invalid and is 
    // not an opening bracket.
    if(expression[index]!='['){
        cout << expression << ", " <<
                    index << ": -1\n";
        return;
    }
      
    // Stack to store opening brackets.
    stack <int> st;
      
    // Traverse through string starting from
    // given index.
    for(i = index; i < expression.length(); i++){
          
        // If current character is an 
        // opening bracket push it in stack.
        if(expression[i] == '[')
          st.push(expression[i]);
          
        // If current character is a closing
        // bracket, pop from stack. If stack 
        // is empty, then this closing
        // bracket is required bracket.
        else if(expression[i] == ']'){
            st.pop();
            if(st.empty()){
                cout << expression << ", " << 
                       index << ": " << i << "\n";
                return;
            }
        }
    }
      
    // If no matching closing bracket
    // is found.
    cout << expression << ", " <<
                index << ": -1\n";
}
  
// Driver Code
int main() {
    test("[ABC[23]][89]", 0); // should be 8
    test("[ABC[23]][89]", 4); // should be 7
    test("[ABC[23]][89]", 9); // should be 12
    test("[ABC[23]][89]", 1); // No matching bracket
    return 0;
}
  
// This code is contributed by Nikhil Jindal.

Time complexity: linear
Space complexity: linear
// C++ program to determine whether given
// expression is balanced/ parenthesis
// expression or not.
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if two brackets are matching
// or not.
int isMatching(char a, char b)
{
    if ((a == '{' && b == '}') || (a == '[' && b == ']')
        || (a == '(' && b == ')') || a == 'X')
        return 1;
    return 0;
}
 
// Recursive function to check if given expression
// is balanced or not.
int isBalanced(string s, stack<char> ele, int ind)
{
 
    // Base case.
    // If the string is balanced then all the opening
    // brackets had been popped and stack should be
    // empty after string is traversed completely.
    if (ind == s.length())
        return ele.empty();
 
    // variable to store element at the top of the stack.
    char topEle;
 
    // variable to store result of recursive call.
    int res;
 
    // Case 1: When current element is an opening bracket
    // then push that element in the stack.
    if (s[ind] == '{' || s[ind] == '(' || s[ind] == '[') {
        ele.push(s[ind]);
        return isBalanced(s, ele, ind + 1);
    }
 
    // Case 2: When current element is a closing bracket
    // then check for matching bracket at the top of the
    // stack.
    else if (s[ind] == '}' || s[ind] == ')' || s[ind] == ']') {
 
        // If stack is empty then there is no matching opening
        // bracket for current closing bracket and the
        // expression is not balanced.
        if (ele.empty())
            return 0;
 
        topEle = ele.top();
        ele.pop();
 
        // Check bracket is matching or not.
        if (!isMatching(topEle, s[ind]))
            return 0;
         
        return isBalanced(s, ele, ind + 1);
    }
 
    // Case 3: If current element is 'X' then check
    // for both the cases when 'X' could be opening
    // or closing bracket.
    else if (s[ind] == 'X') {
        stack<char> tmp = ele;
        tmp.push(s[ind]);
        res = isBalanced(s, tmp, ind + 1);
        if (res)
            return 1;
        if (ele.empty())
            return 0;
        ele.pop();
        return isBalanced(s, ele, ind + 1);
    }
}
 
int main()
{
    string s = "{(X}[]";
    stack<char> ele;
   
     //Check if the String is of even length
      if(s.length()%2==0){
        if (isBalanced(s, ele, 0))
            cout << "Balanced";   
        else
            cout << "Not Balanced";
    }
   
      // If the length of the string is not even
      // then it is not a balanced string
      else{
          cout << "Not Balanced";
      }
    return 0;
}

Time complexity: np
Space complexity: linear
// C++ program to print minimum number that can be formed
// from a given sequence of Is and Ds
#include <bits/stdc++.h>
using namespace std;
 
// Function to decode the given sequence to construct
// minimum number without repeated digits
void PrintMinNumberForPattern(string seq)
{
    // result store output string
    string result;
 
    // create an empty stack of integers
    stack<int> stk;
 
    // run n+1 times where n is length of input sequence
    for (int i = 0; i <= seq.length(); i++)
    {
        // push number i+1 into the stack
        stk.push(i + 1);
 
        // if all characters of the input sequence are
        // processed or current character is 'I'
        // (increasing)
        if (i == seq.length() || seq[i] == 'I')
        {
            // run till stack is empty
            while (!stk.empty())
            {
                // remove top element from the stack and
                // add it to solution
                result += to_string(stk.top());
                result += " ";
                stk.pop();
            }
        }
    }
 
    cout << result << endl;
}
 
// main function
int main()
{
    PrintMinNumberForPattern("IDID");
    PrintMinNumberForPattern("I");
    PrintMinNumberForPattern("DD");
    PrintMinNumberForPattern("II");
    PrintMinNumberForPattern("DIDI");
    PrintMinNumberForPattern("IIDDD");
    PrintMinNumberForPattern("DDIDDIID");
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program of above approach
#include <bits/stdc++.h>
using namespace std;
   
// Returns minimum number made from given sequence without repeating digits
string getMinNumberForPattern(string seq)
{
    int n = seq.length();
 
    if (n >= 9)
        return "-1";
 
    string result(n+1, ' ');
 
    int count = 1; 
 
    // The loop runs for each input character as well as
    // one additional time for assigning rank to remaining characters
    for (int i = 0; i <= n; i++)
    {
        if (i == n || seq[i] == 'I')
        {
            for (int j = i - 1 ; j >= -1 ; j--)
            {
                result[j + 1] = '0' + count++;
                if(j >= 0 && seq[j] == 'I')
                    break;
            }
        }
    }
    return result;
}
   
// main function
int main()
{
    string inputs[] = {"IDID", "I", "DD", "II", "DIDI", "IIDDD", "DDIDDIID"};
 
    for (string input : inputs)
    {
        cout << getMinNumberForPattern(input) << "\n";
    }
    return 0;
}

Time complexity: linear
Space complexity: linear
// c++ program to generate required sequence
#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
 
//:param s: a seq consisting only of 'D' and 'I' chars. D is
//for decreasing and I for increasing :return: digits from
//1-9 that fit the str. The number they repr should the min
//such number
vector<string> didi_seq_gen(string s)
{
    if (s.size() == 0)
        return {};
    vector<string> base_list = { "1" };
    for (int i = 2; i < s.size() + 2; i++)
        base_list.push_back(to_string(i));
    int last_D = -1;
    for (int i = 1; i < base_list.size(); i++) {
        if (s[i - 1] == 'D') {
            if (last_D < 0)
                last_D = i - 1;
            string v = base_list[i];
            base_list.erase(base_list.begin() + i);
            base_list.insert(base_list.begin() + last_D, v);
        }
        else
            last_D = -1;
    }
    return base_list;
}
 
int main()
{
    vector<string> inputs
        = { "IDID", "I",     "DD",      "II",
            "DIDI", "IIDDD", "DDIDDIID" };
    for (auto x : inputs) {
        vector<string> ans = didi_seq_gen(x);
        for (auto i : ans) {
            cout << i;
        }
        cout << endl;
    }
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find the difference b/w left and
// right smaller element of every element in array
#include<bits/stdc++.h>
using namespace std;
 
// Function to fill left smaller element for every
// element of arr[0..n-1]. These values are filled
// in SE[0..n-1]
void leftSmaller(int arr[], int n, int SE[])
{
    // Create an empty stack
    stack<int>S;
 
    // Traverse all array elements
    // compute nearest smaller elements of every element
    for (int i=0; i<n; i++)
    {
        // Keep removing top element from S while the top
        // element is greater than or equal to arr[i]
        while (!S.empty() && S.top() >= arr[i])
            S.pop();
 
        // Store the smaller element of current element
        if (!S.empty())
            SE[i] = S.top();
 
        // If all elements in S were greater than arr[i]
        else
            SE[i] = 0;
 
        // Push this element
        S.push(arr[i]);
    }
}
 
// Function returns maximum difference b/w Left &
// right smaller element
int findMaxDiff(int arr[], int n)
{
    int LS[n]; // To store left smaller elements
 
    // find left smaller element of every element
    leftSmaller(arr, n, LS);
 
    // find right smaller element of every element
    // first reverse the array and do the same process
    int RRS[n]; // To store right smaller elements in
                // reverse array
    reverse(arr, arr + n);
    leftSmaller(arr, n, RRS);
 
    // find maximum absolute difference b/w LS & RRS
    // In the reversed array right smaller for arr[i] is
    // stored at RRS[n-i-1]
    int result = -1;
    for (int i=0 ; i< n ; i++)
        result = max(result, abs(LS[i] - RRS[n-1-i]));
 
    // return maximum difference b/w LS & RRS
    return result;
}
 
// Driver program
int main()
{
    int arr[] = {2, 4, 8, 7, 7, 9, 3};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << "Maximum diff : "
        << findMaxDiff(arr, n) << endl;
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ Program to find Right smaller element of next
// greater element
#include<bits/stdc++.h>
using namespace std;
 
// function find Next greater element
void nextGreater(int arr[], int n, int next[], char order)
{
    // create empty stack
    stack<int> S;
 
    // Traverse all array elements in reverse order
    // order == 'G' we compute next greater elements of
    //              every element
    // order == 'S' we compute right smaller element of
    //              every element
    for (int i=n-1; i>=0; i--)
    {
        // Keep removing top element from S while the top
        // element is smaller than or equal to arr[i] (if Key is G)
        // element is greater than or equal to arr[i] (if order is S)
        while (!S.empty() &&
              ((order=='G')? arr[S.top()] <= arr[i]:
                           arr[S.top()] >= arr[i]))
            S.pop();
 
        // store the next greater element of current element
        if (!S.empty())
            next[i] = S.top();
 
        // If all elements in S were smaller than arr[i]
        else
            next[i] = -1;
 
        // Push this element
        S.push(i);
    }
}
 
// Function to find Right smaller element of next greater
// element
void nextSmallerOfNextGreater(int arr[], int n)
{
    int NG[n]; // stores indexes of next greater elements
    int RS[n]; // stores indexes of right smaller elements
 
    // Find next greater element
    // Here G indicate next greater element
    nextGreater(arr, n, NG, 'G');
 
    // Find right smaller element
    // using same function nextGreater()
    // Here S indicate right smaller elements
    nextGreater(arr, n, RS, 'S');
 
    // If NG[i] == -1 then there is no smaller element
    // on right side. We can find Right smaller of next
    // greater by arr[RS[NG[i]]]
    for (int i=0; i< n; i++)
    {
        if (NG[i] != -1 && RS[NG[i]] != -1)
            cout << arr[RS[NG[i]]] << " ";
        else
            cout<<"-1"<<" ";
    }
}
 
// Driver program
int main()
{
    int arr[] = {5, 1, 9, 2, 5, 1, 7};
    int n = sizeof(arr)/sizeof(arr[0]);
    nextSmallerOfNextGreater(arr, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to calculate maximum sum with equal
// stack sum.
#include <bits/stdc++.h>
using namespace std;
 
// Returns maximum possible equal sum of three stacks
// with removal of top elements allowed
int maxSum(int stack1[], int stack2[], int stack3[], int n1,
           int n2, int n3)
{
    int sum1 = 0, sum2 = 0, sum3 = 0;
 
    // Finding the initial sum of stack1.
    for (int i = 0; i < n1; i++)
        sum1 += stack1[i];
 
    // Finding the initial sum of stack2.
    for (int i = 0; i < n2; i++)
        sum2 += stack2[i];
 
    // Finding the initial sum of stack3.
    for (int i = 0; i < n3; i++)
        sum3 += stack3[i];
 
    // As given in question, first element is top
    // of stack..
    int top1 = 0, top2 = 0, top3 = 0;
    while (1) {
        // If any stack is empty
        if (top1 == n1 || top2 == n2 || top3 == n3)
            return 0;
 
        // If sum of all three stack are equal.
        if (sum1 == sum2 && sum2 == sum3)
            return sum1;
 
        // Finding the stack with maximum sum and
        // removing its top element.
        if (sum1 >= sum2 && sum1 >= sum3)
            sum1 -= stack1[top1++];
        else if (sum2 >= sum1 && sum2 >= sum3)
            sum2 -= stack2[top2++];
        else if (sum3 >= sum2 && sum3 >= sum1)
            sum3 -= stack3[top3++];
    }
}
 
// Driven Program
int main()
{
    int stack1[] = { 3, 2, 1, 1, 1 };
    int stack2[] = { 4, 3, 2 };
    int stack3[] = { 1, 1, 4, 1 };
 
    int n1 = sizeof(stack1) / sizeof(stack1[0]);
    int n2 = sizeof(stack2) / sizeof(stack2[0]);
    int n3 = sizeof(stack3) / sizeof(stack3[0]);
 
    cout << maxSum(stack1, stack2, stack3, n1, n2, n3)
         << endl;
    return 0;
}

Time complexity: linear
Space complexity: constant