Banana-Leopard/TASTY huggingface dataset:
// C++ program to count the number less than N,
// whose all permutation is greater
// than or equal to the number.
#include <bits/stdc++.h>
using namespace std;
 
// Return the count of the number having all
// permutation greater than or equal to the number.
int countNumber(int n)
{
    int result = 0;
 
    // Pushing 1 to 9 because all number from 1
    // to 9 have this property.
    stack<int> s;
    for (int i = 1; i <= 9; i++)
    {
         
        if (i <= n)
        {
            s.push(i);
            result++;
        }
 
        // take a number from stack and add
        // a digit smaller than or equal to last digit
        // of it.
        while (!s.empty())
        {
            int tp = s.top();
            s.pop();
            for (int j = tp % 10; j <= 9; j++)
            {
                int x = tp * 10 + j;
                if (x <= n)
                {
                    s.push(x);
                    result++;
                }
            }
        }
    }
 
    return result;
}
 
// Driven Code
int main()
{
    int n = 15;
    cout << countNumber(n) << endl;
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program for bubble sort
// using stack
#include <bits/stdc++.h>
using namespace std;
 
// Function for bubble sort using Stack
void bubbleSortStack(int a[], int n)
{
    stack<int> s1;
     
    // Push all elements of array in 1st stack
    for(int i = 0; i < n; i++)
    {
        s1.push(a[i]);
    }
 
    stack<int> s2;
 
    for(int i = 0; i < n; i++)
    {
        if (i % 2 == 0)
        {
            while (!s1.empty())
            {
                int t = s1.top();
                s1.pop();
 
                if (s2.empty())
                {
                    s2.push(t);
                }
 
                else
                {
                    // Swapping
                    if (s2.top() > t)
                    {
                        int temp = s2.top();
                        s2.pop();
                        s2.push(t);
                        s2.push(temp);
                    }
                    else
                    {
                        s2.push(t);
                    }
                }
            }
             
            // Tricky step
            a[n - 1 - i] = s2.top();
            s2.pop();
        }
 
        else
        {
            while (!s2.empty())
            {
                int t = s2.top();
                s2.pop();
 
                if (s1.empty())
                {
                    s1.push(t);
                }
 
                else
                {
                    if (s1.top() > t)
                    {
                        int temp = s1.top();
                        s1.pop();
 
                        s1.push(t);
                        s1.push(temp);
                    }
 
                    else
                    {
                        s1.push(t);
                    }
                }
            }
             
            // Tricky step
            a[n - 1 - i] = s1.top();
            s1.pop();
        }
    }
     
    cout << "[";
    for(int i = 0; i < n; i++)
    {
        cout << a[i] << ", ";
    }
    cout << "]";
}
 
// Driver code
int main()
{
    int a[] = { 15, 12, 44, 2, 5, 10 };
    int n = sizeof(a) / sizeof(a[0]);
 
    bubbleSortStack(a, n);
 
    return 0;
}
 
// This code is contributed by pawki

Time complexity: quadratic
Space complexity: linear
// C++ program to print all ancestors of a given key
#include <bits/stdc++.h>
using namespace std;
 
// Structure for a tree node
struct Node {
    int data;
    struct Node* left, *right;
};
 
// A utility function to create a new tree node
struct Node* newNode(int data)
{
    struct Node* node = (struct Node*)malloc(sizeof(struct Node));
    node->data = data;
    node->left = node->right = NULL;
    return node;
}
 
// Iterative Function to print all ancestors of a
// given key
void printAncestors(struct Node* root, int key)
{
    if (root == NULL)
        return;
 
    // Create a stack to hold ancestors
    stack<struct Node*> st;
 
    // Traverse the complete tree in postorder way till
    // we find the key
    while (1) {
 
        // Traverse the left side. While traversing, push
        // the nodes into  the stack so that their right
        // subtrees can be traversed later
        while (root && root->data != key) {
            st.push(root); // push current node
            root = root->left; // move to next node
        }
 
        // If the node whose ancestors are to be printed
        // is found, then break the while loop.
        if (root && root->data == key)
            break;
 
        // Check if right sub-tree exists for the node at top
        // If not then pop that node because we don't need
        // this node any more.
        if (st.top()->right == NULL) {
            root = st.top();
            st.pop();
 
            // If the popped node is right child of top,
            // then remove the top as well. Left child of
            // the top must have processed before.
            while (!st.empty() && st.top()->right == root) {
                root = st.top();
                st.pop();
            }
        }
 
        // if stack is not empty then simply set the root
        // as right child of top and start traversing right
        // sub-tree.
        root = st.empty() ? NULL : st.top()->right;
    }
 
    // If stack is not empty, print contents of stack
    // Here assumption is that the key is there in tree
    while (!st.empty()) {
        cout << st.top()->data << " ";
        st.pop();
    }
}
 
// Driver program to test above functions
int main()
{
    // Let us construct a binary tree
    struct Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(7);
    root->left->left = newNode(3);
    root->left->right = newNode(5);
    root->right->left = newNode(8);
    root->right->right = newNode(9);
    root->left->left->left = newNode(4);
    root->left->right->right = newNode(6);
    root->right->right->left = newNode(10);
 
    int key = 6;
    printAncestors(root, key);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to keep track of maximum
// element in a stack
#include <bits/stdc++.h>
using namespace std;
 
class StackWithMax
{
    // main stack
    stack<int> mainStack;
 
    // stack to keep track of max element
    stack<int> trackStack;
 
public:
    void push(int x)
    {
        mainStack.push(x);
        if (mainStack.size() == 1)
        {
            trackStack.push(x);
            return;
        }
 
        // If current element is greater than
        // the top element of track stack, push
        // the current element to track stack
        // otherwise push the element at top of
        // track stack again into it.
        if (x > trackStack.top())
            trackStack.push(x);
        else
            trackStack.push(trackStack.top());
    }
 
    int getMax()
    {
        return trackStack.top();
    }
 
    int pop()
    {
        mainStack.pop();
        trackStack.pop();
    }
};
 
// Driver program to test above functions
int main()
{
    StackWithMax s;
    s.push(20);
    cout << s.getMax() << endl;
    s.push(10);
    cout << s.getMax() << endl;
    s.push(50);
    cout << s.getMax() << endl;
    return 0;
}

Time complexity: constant
Space complexity: linear
// CPP program to reverse the number
// using a stack
 
#include <bits/stdc++.h>
using namespace std;
 
// Stack to maintain order of digits
stack <int> st;
 
// Function to push digits into stack
void push_digits(int number)
{
    while (number != 0)
    {
        st.push(number % 10);
        number = number / 10;
    }
}
 
// Function to reverse the number
int reverse_number(int number)
{
    // Function call to push number's
    // digits to stack
    push_digits(number);
     
    int reverse = 0;
    int i = 1;
     
    // Popping the digits and forming
    // the reversed number
    while (!st.empty())
    {
        reverse = reverse + (st.top() * i);
        st.pop();
        i = i * 10;
    }
     
    // Return the reversed number formed
    return reverse;
}
 
// Driver program to test above function
int main()
{
    int number = 39997;
     
    // Function call to reverse number
    cout << reverse_number(number);
     
    return 0;
}

Time complexity: logn
Space complexity: logn
// C++ program to check if successive
// pair of numbers in the stack are
// consecutive or not
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if elements are
// pairwise consecutive in stack
bool pairWiseConsecutive(stack<int> s)
{
    // Transfer elements of s to aux.
    stack<int> aux;
    while (!s.empty()) {
        aux.push(s.top());
        s.pop();
    }
 
    // Traverse aux and see if
    // elements are pairwise
    // consecutive or not. We also
    // need to make sure that original
    // content is retained.
    bool result = true;
    while (aux.size() > 1) {
 
        // Fetch current top two
        // elements of aux and check
        // if they are consecutive.
        int x = aux.top();
        aux.pop();
        int y = aux.top();
        aux.pop();
        if (abs(x - y) != 1)
          result = false;
 
        // Push the elements to original
        // stack.
        s.push(x);
        s.push(y);
    }
 
    if (aux.size() == 1)
        s.push(aux.top());
 
    return result;
}
 
// Driver program
int main()
{
    stack<int> s;
    s.push(4);
    s.push(5);
    s.push(-2);
    s.push(-3);
    s.push(11);
    s.push(10);
    s.push(5);
    s.push(6);
    s.push(20);
 
    if (pairWiseConsecutive(s))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
 
    cout << "Stack content (from top)"
          " after function call\n";
    while (s.empty() == false)
    {
       cout << s.top() << " ";
       s.pop();
    }
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// CPP Program to implement growable array based stack
// using tight strategy
#include <iostream>
using namespace std;
 
// constant amount at which stack is increased
#define BOUND 4
 
// top of the stack
int top = -1;
 
// length of stack
int length = 0;
 
// function to create new stack
int* create_new(int* a)
{
    // allocate memory for new stack
    int* new_a = new int[length + BOUND];
 
    // copying the content of old stack
    for (int i = 0; i < length; i++)
        new_a[i] = a[i];
 
    // re-sizing the length
    length += BOUND;
    return new_a;
}
 
// function to push new element
int* push(int* a, int element)
{
    // if stack is full, create new one
    if (top == length - 1)
        a = create_new(a);
 
    // insert element at top of the stack
    a[++top] = element;
    return a;
}
 
// function to pop an element
void pop(int* a)
{
    if (top < 0) {
        cout << "Stack is empty" << endl;
        return;
    }
    top--;
}
 
// function to display
void display(int* a)
{
    // if top is less than 0, that means stack is empty
    if (top < 0)
        cout << "Stack is Empty" << endl;
    else {
        cout << "Stack: ";
        for (int i = 0; i <= top; i++)
            cout << a[i] << " ";
        cout << endl;
    }
}
 
// Driver Code
int main()
{
    // creating initial stack
    int* a = create_new(a);
 
    // pushing element to top of stack
    a = push(a, 1);
    a = push(a, 2);
    a = push(a, 3);
    a = push(a, 4);
    display(a);
 
    // pushing more element when stack is full
    a = push(a, 5);
    a = push(a, 6);
    display(a);
 
    a = push(a, 7);
    a = push(a, 8);
    display(a);
 
    // pushing more element so that stack can grow
    a = push(a, 9);
    display(a);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// CPP code to answer the query in constant time
#include <bits/stdc++.h>
using namespace std;
 
/*
BOP[] stands for "Balanced open parentheses"
BCP[] stands for "Balanced close parentheses"
 
*/
 
// function for precomputation
void constructBalanceArray(int BOP[], int BCP[],
                          char* str, int n)
{
 
    // Create a stack and push -1 as initial index to it.
    stack<int> stk;
 
    // Initialize result
    int result = 0;
 
    // Traverse all characters of given string
    for (int i = 0; i < n; i++) {
        // If opening bracket, push index of it
        if (str[i] == '(')
            stk.push(i);
 
        else // If closing bracket, i.e., str[i] = ')'
        {
            // If closing bracket, i.e., str[i] = ')'
            // && stack is not empty then mark both
            // "open & close" bracket indexes as 1 .
            // Pop the previous opening bracket's index
            if (!stk.empty()) {
                BCP[i] = 1;
                BOP[stk.top()] = 1;
                stk.pop();
            }
 
            // If stack is empty.
            else
                BCP[i] = 0;
        }
    }
 
    for (int i = 1; i < n; i++) {
        BCP[i] += BCP[i - 1];
        BOP[i] += BOP[i - 1];
    }
}
 
// Function return output of each query in O(1)
int query(int BOP[], int BCP[],
          int s, int e)
{
    if (BOP[s - 1] == BOP[s]) {
        return (BCP[e] - BOP[s]) * 2;
    }
 
    else {
        return (BCP[e] - BOP[s] + 1) * 2;
    }
}
 
// Driver program to test above function
int main()
{
 
    char str[] = "())(())(())(";
    int n = strlen(str);
 
    int BCP[n + 1] = { 0 };
    int BOP[n + 1] = { 0 };
 
    constructBalanceArray(BOP, BCP, str, n);
 
    int startIndex = 5, endIndex = 11;
 
    cout << "Maximum Length Correct Bracket"
            " Subsequence between "
         << startIndex << " and " << endIndex << " = "
         << query(BOP, BCP, startIndex, endIndex) << endl;
 
    startIndex = 4, endIndex = 5;
    cout << "Maximum Length Correct Bracket"
            " Subsequence between "
         << startIndex << " and " << endIndex << " = "
         << query(BOP, BCP, startIndex, endIndex) << endl;
 
    startIndex = 1, endIndex = 5;
    cout << "Maximum Length Correct Bracket"
            " Subsequence between "
         << startIndex << " and " << endIndex << " = "
         << query(BOP, BCP, startIndex, endIndex) << endl;
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// Recursive Program to remove consecutive
// duplicates from string S.
#include <bits/stdc++.h>
using namespace std;
  
// A recursive function that removes
// consecutive duplicates from string S
void removeDuplicates(char* S)
{
    // When string is empty, return
    if (S[0] == '\0')
        return;
  
    // if the adjacent characters are same
    if (S[0] == S[1]) {
  
        // Shift character by one to left
        int i = 0;
        while (S[i] != '\0') {
            S[i] = S[i + 1];
            i++;
        }
  
        // Check on Updated String S
        removeDuplicates(S);
    }
  
    // If the adjacent characters are not same
    // Check from S+1 string address
    removeDuplicates(S + 1);
}
  
// Driver Program
int main()
{
    char S1[] = "geeksforgeeks";
    removeDuplicates(S1);
    cout << S1 << endl;
  
    char S2[] = "aabcca";
    removeDuplicates(S2);
    cout << S2 << endl;
  
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program to remove consecutive
// duplicates from a given string.
#include <bits/stdc++.h>
using namespace std;
  
// A iterative function that removes
// consecutive duplicates from string S
string removeDuplicates(string s)
{
  
    int n = s.length();
    string str = "";
    // We don't need to do anything for
    // empty string.
    if (n == 0)
        return str;
  
    // Traversing string
    for (int i = 0; i < n - 1; i++) {
        // checking if s[i] is not same as s[i+1] then add
        // it into str
        if (s[i] != s[i + 1]) {
            str += s[i];
        }
    }
    // Since the last character will not be inserted in the
    // loop we add it at the end
    str.push_back(s[n - 1]);
    return str;
}
  
// Driver Program
int main()
{
  
    string s1 = "geeksforgeeks";
    cout << removeDuplicates(s1) << endl;
  
    string s2 = "aabcca";
    cout << removeDuplicates(s2) << endl;
  
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP program to find smallest
// number to find smallest number
// with N as sum of digits and
// divisible by 10^N.
#include <bits/stdc++.h>
using namespace std;
 
void digitsNum(int N)
{
    // If N = 0 the string will be 0
    if (N == 0)
        cout << "0\n";
     
    // If n is not perfectly divisible
    // by 9 output the remainder
    if (N % 9 != 0)
        cout << (N % 9);
     
    // Print 9 N/9 times
    for (int i = 1; i <= (N / 9); ++i)
        cout << "9";
     
    // Append N zero's to the number so
    // as to make it divisible by 10^N
    for (int i = 1; i <= N; ++i)
        cout << "0";
     
    cout << "\n";
}
 
// Driver Code
int main()
{
    int N = 5;
    cout << "The number is : ";
    digitsNum(N);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to find maximum and minimum
// possible sums of two numbers that we can
// get if replacing digit from 5 to 6 and vice
// versa are allowed.
#include<bits/stdc++.h>
using namespace std;
 
// Find new value of x after replacing digit
// "from" to "to"
int replaceDig(int x, int from, int to)
{
    int result = 0;
    int multiply = 1;
 
    while (x > 0)
    {
        int reminder = x % 10;
 
        // Required digit found, replace it
        if (reminder == from)
            result = result + to * multiply;
 
        else
            result = result + reminder * multiply;
 
        multiply *= 10;
        x = x / 10;
    }
    return result;
}
 
// Returns maximum and minimum possible sums of
// x1 and x2 if digit replacements are allowed.
void calculateMinMaxSum(int x1, int x2)
{
    // We always get minimum sum if we replace
    // 6 with 5.
    int minSum = replaceDig(x1, 6, 5) +
                 replaceDig(x2, 6, 5);
 
    // We always get maximum sum if we replace
    // 5 with 6.
    int maxSum = replaceDig(x1, 5, 6) +
                 replaceDig(x2, 5, 6);
    cout << "Minimum sum = " << minSum;
    cout << "nMaximum sum = " << maxSum;
}
 
// Driver code
int main()
{
    int x1 = 5466, x2 = 4555;
    calculateMinMaxSum(x1, x2);
    return 0;
}

Time complexity: logn
Space complexity: constant
// C++ program to check if a given string
// is sum-string or not
#include <bits/stdc++.h>
using namespace std;
 
// this is function for finding sum of two
// numbers as string
string string_sum(string str1, string str2)
{
    if (str1.size() < str2.size())
        swap(str1, str2);
 
    int m = str1.size();
    int n = str2.size();
    string ans = "";
 
    // sum the str2 with str1
    int carry = 0;
    for (int i = 0; i < n; i++) {
 
        // Sum of current digits
        int ds = ((str1[m - 1 - i] - '0')
                  + (str2[n - 1 - i] - '0') + carry)
                 % 10;
 
        carry = ((str1[m - 1 - i] - '0')
                 + (str2[n - 1 - i] - '0') + carry)
                / 10;
 
        ans = char(ds + '0') + ans;
    }
 
    for (int i = n; i < m; i++) {
        int ds = (str1[m - 1 - i] - '0' + carry) % 10;
        carry = (str1[m - 1 - i] - '0' + carry) / 10;
        ans = char(ds + '0') + ans;
    }
 
    if (carry)
        ans = char(carry + '0') + ans;
    return ans;
}
 
// Returns true if two substrings of given
// lengths of str[beg..] can cause a positive
// result.
bool checkSumStrUtil(string str, int beg, int len1,
                     int len2)
{
 
    // Finding two substrings of given lengths
    // and their sum
    string s1 = str.substr(beg, len1);
    string s2 = str.substr(beg + len1, len2);
    string s3 = string_sum(s1, s2);
 
    int s3_len = s3.size();
 
    // if number of digits s3 is greater than
    // the available string size
    if (s3_len > str.size() - len1 - len2 - beg)
        return false;
 
    // we got s3 as next number in main string
    if (s3 == str.substr(beg + len1 + len2, s3_len)) {
 
        // if we reach at the end of the string
        if (beg + len1 + len2 + s3_len == str.size())
            return true;
 
        // otherwise call recursively for n2, s3
        return checkSumStrUtil(str, beg + len1, len2,
                               s3_len);
    }
 
    // we do not get s3 in main string
    return false;
}
 
// Returns true if str is sum string, else false.
bool isSumStr(string str)
{
    int n = str.size();
 
    // choosing first two numbers and checking
    // whether it is sum-string or not.
    for (int i = 1; i < n; i++)
        for (int j = 1; i + j < n; j++)
            if (checkSumStrUtil(str, 0, i, j))
                return true;
 
    return false;
}
 
// Driver code
int main()
{
    bool result;
 
    result = isSumStr("1212243660");
    cout << (result == 1 ? "True\n" : "False\n");
       
    result = isSumStr("123456787");
    cout << (result == 1 ? "True\n" : "False\n");
    return 0;
}

Time complexity: cubic
Space complexity: linear
// C++ program to find maximum value
#include <bits/stdc++.h>
 
using namespace std;
 
// Function to calculate the value
int calcMaxValue(string str)
{
    // Store first character as integer
    // in result
    int res = str[0] -'0';
 
    // Start traversing the string
    for (int i = 1; i < str.length(); i++)
    {
        // Check if any of the two numbers
        // is 0 or 1, If yes then add current
        // element
        if (str[i] == '0' || str[i] == '1' ||
            res < 2 )
            res += (str[i]-'0');
 
        // Else multiply
        else
            res *= (str[i]-'0');
    }
 
    // Return maximum value
    return res;
}
 
// Drivers code
int main()
{
    string str = "01891";
    cout << calcMaxValue(str);
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP program to find the maximum segment
// value after putting k breaks.
#include <bits/stdc++.h>
using namespace std;
 
// Function to Find Maximum Number
int findMaxSegment(string &s, int k) {
 
  // Maximum segment length
  int seg_len = s.length() - k;
 
  // Find value of first segment of seg_len
  int res = 0;
  for (int i=0; i<seg_len; i++)
     res = res * 10 + (s[i] - '0');
 
  // Find value of remaining segments using sliding
  // window
  int seg_len_pow = pow(10, seg_len-1);
  int curr_val = res;
  for (int i = 1; i <= (s.length() - seg_len); i++) {
 
    // To find value of current segment, first remove
    // leading digit from previous value   
    curr_val = curr_val - (s[i-1]- '0')*seg_len_pow;
 
    // Then add trailing digit
    curr_val = curr_val*10 + (s[i+seg_len-1]- '0');
 
    res = max(res, curr_val);
  }
  return res;
}
 
// Driver's Function
int main() {
  string s = "8754";
  int k = 2;
  cout << "Maximum number = " << findMaxSegment(s, k);
  return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to find if a number is divisible by
// 4 or not
#include <bits/stdc++.h>
using namespace std;
 
// Function to find that number divisible by
// 4 or not
bool check(string str)
{
    // Get the length of the string
    int n = str.length();
 
    // Empty string
    if (n == 0)
        return false;
    // stoi(string_variable) is used in C++
    // to convert string to integer
 
    // If there is single digit
    if (n == 1)
        return ((stoi(str)) % 4 == 0);
 
    // getting last two characters using substring
    str = str.substr(n - 2, 2);
    // If number formed by last two digits is
    // divisible by 4.
    return ((stoi(str)) % 4 == 0);
}
 
// Driver code
int main()
{
    string str = "76952";
 
    // Function call
    check(str) ? cout << "Yes" : cout << "No ";
    return 0;
}
 
// This code is contributed by Abhijeet Kumar(abhijeet19403)

Time complexity: constant
Space complexity: constant