Banana-Leopard/TASTY huggingface dataset:
// C++ program to find circular tour for a truck
#include <bits/stdc++.h>
using namespace std;
 
// A petrol pump has petrol and distance to next petrol pump
class petrolPump {
public:
    int petrol;
    int distance;
};
 
// The function returns starting point if there is a
// possible solution, otherwise returns -1
int printTour(petrolPump p[], int n)
{
    // deficit is used to store the value of the capacity as
    // soon as the value of capacity becomes negative so as
    // not to traverse the array twice in order to get the
    // solution
    int start = 0, deficit = 0;
    int capacity = 0;
    for (int i = 0; i < n; i++) {
        capacity += p[i].petrol - p[i].distance;
        if (capacity < 0) {
            // If this particular step is not done then the
            // between steps would be redundant
            start = i + 1;
            deficit += capacity;
            capacity = 0;
        }
    }
    return (capacity + deficit >= 0) ? start : -1;
}
// Driver code
int main()
{
    petrolPump arr[] = { { 6, 4 }, { 3, 6 }, { 7, 3 } };
 
    int n = sizeof(arr) / sizeof(arr[0]);
    int start = printTour(arr, n);
 
    (start == -1) ? cout << "No solution"
                  : cout << "Start = " << start;
 
    return 0;
}
 
// This code is contributed by aditya kumar

Time complexity: linear
Space complexity: constant
// CPP program to find smallest multiple of a
// given number made of digits 0 and 9 only
#include <bits/stdc++.h>
using namespace std;
 
// Maximum number of numbers made of 0 and 9
#define MAX_COUNT 10000
 
// vector to store all numbers that can be formed
// using digits 0 and 9 and are less than 10^5
vector<string> vec;
 
/* Preprocessing function to generate all possible
   numbers formed by 0 and 9 */
void generateNumbersUtil()
{  
    // Create an empty queue of strings
    queue<string> q;
         
    // enqueue the first number
    q.push("9");
     
    // This loops is like BFS of a tree with 9 as root
    // 0 as left child and 9 as right child and so on
    for (int count = MAX_COUNT; count > 0; count--)
    {
        string s1 = q.front();
        q.pop();
         
        // storing the front of queue in the vector
        vec.push_back(s1);
         
        string s2 = s1;
         
        // Append "0" to s1 and enqueue it
        q.push(s1.append("0"));
         
        // Append "9" to s2 and enqueue it. Note that
        // s2 contains the previous front
        q.push(s2.append("9"));
    }
}
 
// function to find smallest number made up of only
// digits 9’s and 0’s, which is a multiple of n.
string findSmallestMultiple(int n)
{  
    // traverse the vector to find the smallest
    // multiple of n
    for (int i = 0; i < vec.size(); i++)
 
        // stoi() is used for string to int conversion
        if (stoi(vec[i])%n == 0)
            return vec[i];       
}
 
// Driver Code
int main()
{
    generateNumbersUtil();   
    int n = 7;   
    cout << findSmallestMultiple(n);   
    return 0;
}

Time complexity: linear
Space complexity: np
// C++ program to generate binary numbers from 1 to n
#include <bits/stdc++.h>
using namespace std;
 
// This function uses queue data structure to print binary
// numbers
void generatePrintBinary(int n)
{
    // Create an empty queue of strings
    queue<string> q;
 
    // Enqueue the first binary number
    q.push("1");
 
    // This loops is like BFS of a tree with 1 as root
    // 0 as left child and 1 as right child and so on
    while (n--) {
        // print the front of queue
        string s1 = q.front();
        q.pop();
        cout << s1 << "\n";
 
        string s2 = s1; // Store s1 before changing it
 
        // Append "0" to s1 and enqueue it
        q.push(s1.append("0"));
 
        // Append "1" to s2 and enqueue it. Note that s2
        // contains the previous front
        q.push(s2.append("1"));
    }
}
 
// Driver code
int main()
{
    int n = 10;
 
    // Function call
    generatePrintBinary(n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find sum of all minimum and maximum
// elements Of Sub-array Size k.
#include<bits/stdc++.h>
using namespace std;
 
// Returns sum of min and max element of all subarrays
// of size k
int SumOfKsubArray(int arr[] , int n , int k)
{
    int sum = 0;  // Initialize result
 
    // The queue will store indexes of useful elements
    // in every window
    // In deque 'G' we maintain decreasing order of
    // values from front to rear
    // In deque 'S' we  maintain increasing order of
    // values from front to rear
    deque< int > S(k), G(k);
 
    // Process first window of size K
    int i = 0;
    for (i = 0; i < k; i++)
    {
        // Remove all previous greater elements
        // that are useless.
        while ( (!S.empty()) && arr[S.back()] >= arr[i])
            S.pop_back(); // Remove from rear
 
        // Remove all previous smaller that are elements
        // are useless.
        while ( (!G.empty()) && arr[G.back()] <= arr[i])
            G.pop_back(); // Remove from rear
 
        // Add current element at rear of both deque
        G.push_back(i);
        S.push_back(i);
    }
 
    // Process rest of the Array elements
    for (  ; i < n; i++ )
    {
        // Element at the front of the deque 'G' & 'S'
        // is the largest and smallest
        // element of previous window respectively
        sum += arr[S.front()] + arr[G.front()];
 
        // Remove all elements which are out of this
        // window
        while ( !S.empty() && S.front() <= i - k)
            S.pop_front();
        while ( !G.empty() && G.front() <= i - k)
            G.pop_front();
 
        // remove all previous greater element that are
        // useless
        while ( (!S.empty()) && arr[S.back()] >= arr[i])
            S.pop_back(); // Remove from rear
 
        // remove all previous smaller that are elements
        // are useless
        while ( (!G.empty()) && arr[G.back()] <= arr[i])
            G.pop_back(); // Remove from rear
 
        // Add current element at rear of both deque
        G.push_back(i);
        S.push_back(i);
    }
 
    // Sum of minimum and maximum element of last window
    sum += arr[S.front()] + arr[G.front()];
 
    return sum;
}
 
// Driver program to test above functions
int main()
{
    int arr[] = {2, 5, -1, 7, -3, -1, -2} ;
    int n = sizeof(arr)/sizeof(arr[0]);
    int k = 3;
    cout << SumOfKsubArray(arr, n, k) ;
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to find distance of nearest
// cell having 1 in a binary matrix.
#include <bits/stdc++.h>
#define N 3
#define M 4
using namespace std;
 
// Print the distance of nearest cell
// having 1 for each cell.
void printDistance(int mat[N][M])
{
    int ans[N][M];
 
    // Initialize the answer matrix with INT_MAX.
    for (int i = 0; i < N; i++)
        for (int j = 0; j < M; j++)
            ans[i][j] = INT_MAX;
 
    // For each cell
    for (int i = 0; i < N; i++)
        for (int j = 0; j < M; j++) {
            // Traversing the whole matrix
            // to find the minimum distance.
            for (int k = 0; k < N; k++)
                for (int l = 0; l < M; l++) {
                    // If cell contain 1, check
                    // for minimum distance.
                    if (mat[k][l] == 1)
                        ans[i][j]
                            = min(ans[i][j],
                                  abs(i - k) + abs(j - l));
                }
        }
 
    // Printing the answer.
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++)
            cout << ans[i][j] << " ";
 
        cout << endl;
    }
}
 
// Driver code
int main()
{
    int mat[N][M] = { 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0 };
 
      // Function call
    printDistance(mat);
 
    return 0;
}

Time complexity: quadratic
Space complexity: quadratic
// C++ program to do level order traversal line by
// line
#include <bits/stdc++.h>
using namespace std;
 
struct Node
{
    int data;
    Node *left, *right;
};
 
// Prints level order traversal line by line
// using two queues.
void levelOrder(Node *root)
{
    queue<Node *> q1, q2;
 
    if (root == NULL)
        return;
 
    // Pushing first level node into first queue
    q1.push(root);
 
    // Executing loop till both the queues
    // become empty
    while (!q1.empty() || !q2.empty())
    {
        while (!q1.empty())
        {
            // Pushing left child of current node in
            // first queue into second queue
            if (q1.front()->left != NULL)
                q2.push(q1.front()->left);
 
            // pushing right child of current node
            // in first queue into second queue
            if (q1.front()->right != NULL)
                q2.push(q1.front()->right);
 
            cout << q1.front()->data << " ";
            q1.pop();
        }
 
        cout << "\n";
 
        while (!q2.empty())
        {
            // pushing left child of current node
            // in second queue into first queue
            if (q2.front()->left != NULL)
                q1.push(q2.front()->left);
 
            // pushing right child of current
            // node in second queue into first queue
            if (q2.front()->right != NULL)
                q1.push(q2.front()->right);
 
            cout << q2.front()->data << " ";
            q2.pop();
        }
 
        cout << "\n";
    }
}
 
// Utility function to create a new tree node
Node* newNode(int data)
{
    Node *temp = new Node;
    temp->data = data;
    temp->left = NULL;
    temp->right = NULL;
    return temp;
}
 
// Driver program to test above functions
int main()
{
    Node *root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
    root->right->right = newNode(6);
 
    levelOrder(root);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find min sum of squares
// of characters after k removals
#include <bits/stdc++.h>
using namespace std;
 
const int MAX_CHAR = 26;
 
// Main Function to calculate min sum of
// squares of characters after k removals
int minStringValue(string str, int k)
{
    int l = str.length(); // find length of string
 
    // if K is greater than length of string
    // so reduced string will become 0
    if (k >= l)
        return 0;
 
    // Else find Frequency of each character and
    // store in an array
    int frequency[MAX_CHAR] = { 0 };
    for (int i = 0; i < l; i++)
        frequency[str[i] - 'a']++;
 
    // Push each char frequency into a priority_queue
    priority_queue<int> q;
    for (int i = 0; i < MAX_CHAR; i++)
        q.push(frequency[i]);
 
    // Removal of K characters
    while (k--) {
        // Get top element in priority_queue,
        // remove it. Decrement by 1 and again
        // push into priority_queue
        int temp = q.top();
        q.pop();
        temp = temp - 1;
        q.push(temp);
    }
 
    // After removal of K characters find sum
    // of squares of string Value
    int result = 0; // Initialize result
    while (!q.empty()) {
        int temp = q.top();
        result += temp * temp;
        q.pop();
    }
 
    return result;
}
 
// Driver Code
int main()
{
    string str = "abbccc"; // Input 1
    int k = 2;
    cout << minStringValue(str, k) << endl;
 
    str = "aaab"; // Input 2
    k = 2;
    cout << minStringValue(str, k);
 
    return 0;
}

Time complexity: nlogn
Space complexity: constant
// C++ program to find min sum of squares
// of characters after k removals
#include <bits/stdc++.h>
using namespace std;
 
const int MAX_CHAR = 26;
 
// Main Function to calculate min sum of
// squares of characters after k removals
int minStringValue(string str, int k)
{
    int alphabetCount[MAX_CHAR]= {0};
 
    // Here the array stored frequency the number of
    // occurrences in string m[frequency]=number of alphabets
    // with frequency i.e, in our example abbccc m[1]=1(1
    // a's occur),m[2]=1(2 b's occur),m[3]=1(3 c'soccur)
    int m[str.length()] = { 0 };
   
    for (int i = 0; i < str.length(); i++) {
        alphabetCount[str[i] - 'a']++;
    }
    // Store the maximum
    int maximum = 0;
   
    for (int i = 0; i < MAX_CHAR; i++) {
        m[alphabetCount[i]]++;
        maximum = max(maximum, alphabetCount[i]);
    }
 
    while (k > 0) {
        int z = m[maximum];
        if (z <= k) {
            // Remove one occurrence of alphabet from each
            // with frequency as maximum.
            // So we will have k-z more remove operations to
            // perform as z is number of characters and we
            // perform one removal from each of the alphabet
            // with that frequency.
            k = k - z;
            // As we removed one occurrence from each the
            // alphabets will no longer have the frequency
            // of maximum their frequency will be decreased
            // by one so add these number of alphabets to
            // group with frequency one less than maximum.
            // Remove them from maximum count.
            m[maximum] = 0;
            // Add those to frequency one less.
            m[maximum - 1] += z;
            // new maximum will be one less.
            maximum--;
        }
        else {
            // if all the elements of that frequency cannot
            // be removed we should partially remove them.
            m[maximum] -= k;
            maximum--;
            m[maximum] += k;
            k = 0;
        }
    }
 
    int ans = 0;
    for (int i = 0; i < str.length(); i++) {
        //(square of frequency)*(number of
        // characters corresponding to that frequency)
        ans += (i * i) * m[i];
    }
 
    return ans;
}
 
// Driver Code
int main()
{
    string str = "abbccc"; // Input 1
    int k = 2;
    cout << minStringValue(str, k) << endl;
 
    str = "aaab"; // Input 2
    k = 2;
    cout << minStringValue(str, k);
 
    return 0;
}
 
// This code is contributed by Kasina Dheeraj.

Time complexity: linear
Space complexity: linear
// C++ program for a Queue based approach
// to find first non-repeating character
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
 
// function to find first non repeating
// character of sa stream
void firstnonrepeating(char str[])
{
    queue<char> q;
    int charCount[MAX_CHAR] = { 0 };
 
    // traverse whole stream
    for (int i = 0; str[i]; i++) {
 
        // push each character in queue
        q.push(str[i]);
 
        // increment the frequency count
        charCount[str[i] - 'a']++;
 
        // check for the non repeating character
        while (!q.empty()) {
            if (charCount[q.front() - 'a'] > 1)
                q.pop();
            else {
                cout << q.front() << " ";
                break;
            }
        }
 
        if (q.empty())
            cout << -1 << " ";
    }
    cout << endl;
}
 
// Driver function
int main()
{
    char str[] = "aabc";
    firstnonrepeating(str);
    return 0;
}

Time complexity: linear
Space complexity: linear
// Given two arrays, check if one array is
// stack permutation of other.
#include<bits/stdc++.h>
using namespace std;
 
// function to check if input queue is
// permutable to output queue
bool checkStackPermutation(int ip[], int op[], int n)
{
    // Input queue
    queue<int> input;
    for (int i=0;i<n;i++)
        input.push(ip[i]);
 
    // output queue
    queue<int> output;
    for (int i=0;i<n;i++)
        output.push(op[i]);
 
    // stack to be used for permutation
    stack <int> tempStack;
    while (!input.empty())
    {
        int ele = input.front();
        input.pop();
        if (ele == output.front())
        {
            output.pop();
            while (!tempStack.empty())
            {
                if (tempStack.top() == output.front())
                {
                    tempStack.pop();
                    output.pop();
                }
                else
                    break;
            }
        }
        else
            tempStack.push(ele);
    }
 
    // If after processing, both input queue and
    // stack are empty then the input queue is
    // permutable otherwise not.
    return (input.empty()&&tempStack.empty());
}
 
// Driver program to test above function
int main()
{
    // Input Queue
    int input[] = {1, 2, 3};
 
    // Output Queue
    int output[] = {2, 1, 3};
 
    int n = 3;
 
    if (checkStackPermutation(input, output, n))
        cout << "Yes";
    else
        cout << "Not Possible";
    return 0;
}

Time complexity: linear
Space complexity: linear
// Given two arrays, check if one array is
// stack permutation of other.
#include<bits/stdc++.h>
using namespace std;
 
// function to check if input array is
// permutable to output array
bool checkStackPermutation(int ip[], int op[], int n)
{
     // we will be pushing elements from input array to stack uptill top of our stack
     //  matches with first element of output array
      stack<int>s;
      
    // will maintain a variable j to iterate on output array
      int j=0;
   
    // will iterate one by one in input array
      for(int i=0;i<n;i++)
      {
        // pushed an element from input array to stack
        s.push(ip[i]);
        // if our stack isn't empty and top matches with output array
        // then we will keep popping out from stack uptill top matches with
        // output array
        while(!s.empty() and s.top()==op[j])
        {
          s.pop();
          // increasing j so next time we can compare next element in output array
          j++;
        }
      }
       
      // if output array was a correct permutation of input array then
      // by now our stack should be empty
      if(s.empty())
      {
        return true;
      }
      return false; 
       
}
 
// Driver program to test above function
int main()
{
    // Input Array
    int input[] = {4,5,6,7,8};
 
    // Output Array
    int output[] = {8,7,6,5,4};
 
    int n = 5;
 
    if (checkStackPermutation(input, output, n))
        cout << "Yes";
    else
        cout << "Not Possible";
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to implement a stack using
// Priority queue(min heap)
#include<bits/stdc++.h>
using namespace std;
 
typedef pair<int, int> pi;
 
// User defined stack class
class Stack{
     
    // cnt is used to keep track of the number of
    //elements in the stack and also serves as key
    //for the priority queue.
    int cnt;
    priority_queue<pair<int, int> > pq;
public:
    Stack():cnt(0){}
    void push(int n);
    void pop();
    int top();
    bool isEmpty();
};
 
// push function increases cnt by 1 and
// inserts this cnt with the original value.
void Stack::push(int n){
    cnt++;
    pq.push(pi(cnt, n));
}
 
// pops element and reduces count.
void Stack::pop(){
    if(pq.empty()){ cout<<"Nothing to pop!!!";}
    cnt--;
    pq.pop();
}
 
// returns the top element in the stack using
// cnt as key to determine top(highest priority),
// default comparator for pairs works fine in this case
int Stack::top(){
    pi temp=pq.top();
    return temp.second;
}
 
// return true if stack is empty
bool Stack::isEmpty(){
    return pq.empty();
}
 
// Driver code
int main()
{
    Stack* s=new Stack();
    s->push(1);
    s->push(2);
    s->push(3);
    while(!s->isEmpty()){
        cout<<s->top()<<endl;
        s->pop();
    }
}

Time complexity: logn
Space complexity: linear
// program to demonstrate customized data structure
// which supports functions in O(1)
#include <iostream>
#include <vector>
using namespace std;
const int MAXX = 1000;
 
// class stack
class stack {
    int minn;
    int size;
 
public:
    stack()
    {
        minn = 99999;
        size = -1;
    }
    vector<pair<int, int> > arr;
    int GetLastElement();
    int RemoveLastElement();
    int AddElement(int element);
    int GetMin();
};
 
// utility function for adding a new element
int stack::AddElement(int element)
{
    if (size > MAXX) {
        cout << "stack overflow, max size reached!\n";
        return 0;
    }
    if (element < minn)
        minn = element;
    arr.push_back(make_pair(element, minn));
    size++;
    return 1;
}
 
// utility function for returning last element of stack
int stack::GetLastElement()
{
    if (size == -1) {
        cout << "No elements in stack\n";
        return 0;
    }
    return arr[size].first;
}
 
// utility function for removing last element successfully;
int stack::RemoveLastElement()
{
    if (size == -1) {
        cout << "stack empty!!!\n";
        return 0;
    }
 
    // updating minimum element
    if (size > 0 && arr[size - 1].second > arr[size].second) {
        minn = arr[size - 1].second;
    }
    arr.pop_back();
    size -= 1;
    return 1;
}
 
// utility function for returning min element till now;
int stack::GetMin()
{
    if (size == -1) {
        cout << "stack empty!!\n";
        return 0;
    }
    return arr[size].second;
}
 
// Driver code
int main()
{
    stack s;
    int success = s.AddElement(5);
    if (success == 1)
        cout << "5 inserted successfully\n";
 
    success = s.AddElement(7);
    if (success == 1)
        cout << "7 inserted successfully\n";
 
    success = s.AddElement(3);
    if (success == 1)
        cout << "3 inserted successfully\n";
    int min1 = s.GetMin();
    cout << "min element  :: " << min1 << endl;
 
    success = s.RemoveLastElement();
    if (success == 1)
        cout << "removed successfully\n";
 
    success = s.AddElement(2);
    if (success == 1)
        cout << "2 inserted successfully\n";
 
    success = s.AddElement(9);
    if (success == 1)
        cout << "9 inserted successfully\n";
    int last = s.GetLastElement();
    cout << "Last element :: " << last << endl;
 
    success = s.AddElement(0);
    if (success == 1)
        cout << "0 inserted successfully\n";
    min1 = s.GetMin();
    cout << "min element  :: " << min1 << endl;
 
    success = s.RemoveLastElement();
    if (success == 1)
        cout << "removed successfully\n";
 
    success = s.AddElement(11);
    if (success == 1)
        cout << "11 inserted successfully\n";
    min1 = s.GetMin();
    cout << "min element  :: " << min1 << endl;
 
    return 0;
}

Time complexity: constant
Space complexity: linear
// C++ Program to convert prefix to Infix
#include <iostream>
#include <stack>
using namespace std;
 
// function to check if character is operator or not
bool isOperator(char x) {
  switch (x) {
  case '+':
  case '-':
  case '/':
  case '*':
  case '^':
  case '%':
    return true;
  }
  return false;
}
 
// Convert prefix to Infix expression
string preToInfix(string pre_exp) {
  stack<string> s;
 
  // length of expression
  int length = pre_exp.size();
 
  // reading from right to left
  for (int i = length - 1; i >= 0; i--) {
 
    // check if symbol is operator
    if (isOperator(pre_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 = "(" + op1 + pre_exp[i] + op2 + ")";
 
      // 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, pre_exp[i]));
    }
  }
 
  // Stack now contains the Infix expression
  return s.top();
}
 
// Driver Code
int main() {
  string pre_exp = "*-A/BC-/AKL";
  cout << "Infix : " << preToInfix(pre_exp);
  return 0;
}

Time complexity: linear
Space complexity: linear