Banana-Leopard/TASTY huggingface dataset:
// C++ program to find N'th element in a set formed
// by sum of two arrays
#include<bits/stdc++.h>
using namespace std;
 
//Function to calculate the set of sums
int calculateSetOfSum(int arr1[], int size1, int arr2[],
                      int size2, int N)
{
    // Insert each pair sum into set. Note that a set
    // stores elements in sorted order and unique elements
    set<int> s;
    for (int i=0 ; i < size1; i++)
        for (int j=0; j < size2; j++)
            s.insert(arr1[i]+arr2[j]);
 
    // If set has less than N elements
    if (s.size() < N)
       return -1;
 
    // Find N'tb item in set and return it
    set<int>::iterator it = s.begin();
    for (int count=1; count<N; count++)
        it++;
    return *it;
}
 
// Driver code
int main()
{
    int arr1[] = {1, 2};
    int size1 = sizeof(arr1) / sizeof(arr1[0]);
    int arr2[] = {3, 4};
    int size2 = sizeof(arr2) / sizeof(arr2[0]);
 
    int N = 3;
    int res = calculateSetOfSum(arr1, size1, arr2, size2, N);
    if (res == -1)
        cout << "N'th term doesn't exists in set";
    else
        cout << "N'th element in the set of sums is "
             << res;
    return 0;
}

Time complexity: quadratic
Space complexity: linear
#include <iostream>
using namespace std;
 
void constructLowerArray(int arr[], int* countSmaller,
                         int n)
{
    int i, j;
 
    // Initialize all the counts in
    // countSmaller array as 0
    for (i = 0; i < n; i++)
        countSmaller[i] = 0;
 
    for (i = 0; i < n; i++) {
        for (j = i + 1; j < n; j++) {
            if (arr[j] < arr[i])
                countSmaller[i]++;
        }
    }
}
 
// Utility function that prints
// out an array on a line
void printArray(int arr[], int size)
{
    int i;
    for (i = 0; i < size; i++)
        cout << arr[i] << " ";
 
    cout << "\n";
}
 
// Driver code
int main()
{
    int arr[] = { 12, 1, 2, 3, 0, 11, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int* low = (int*)malloc(sizeof(int) * n);
 
    constructLowerArray(arr, low, n);
    printArray(low, n);
 
    return 0;
}
 
// This code is contributed by Hemant Jain

Time complexity: quadratic
Space complexity: constant
#include <bits/stdc++.h>
using namespace std;
 
void merge(vector<pair<int, int> >& v, vector<int>& ans,
           int l, int mid, int h)
{
 
    vector<pair<int, int> >
        t; // temporary array for merging both halves
    int i = l;
    int j = mid + 1;
 
    while (i < mid + 1 && j <= h) {
 
        // v[i].first is greater than all
        // the elements from j till h.
        if (v[i].first > v[j].first) {
            ans[v[i].second] += (h - j + 1);
            t.push_back(v[i]);
            i++;
        }
        else {
            t.push_back(v[j]);
            j++;
        }
    }
 
    // if any elements left in left array
    while (i <= mid)
        t.push_back(v[i++]);
    // if any elements left in right array
    while (j <= h)
        t.push_back(v[j++]);
    // putting elements back in main array in
    // descending order
    for (int k = 0, i = l; i <= h; i++, k++)
        v[i] = t[k];
}
 
void mergesort(vector<pair<int, int> >& v, vector<int>& ans,
               int i, int j)
{
    if (i < j) {
        int mid = (i + j) / 2;
 
        // calling mergesort for left half
        mergesort(v, ans, i, mid);
 
        // calling mergesort for right half
        mergesort(v, ans, mid + 1, j);
 
        // merging both halves and generating answer
        merge(v, ans, i, mid, j);
    }
}
 
vector<int> constructLowerArray(int* arr, int n)
{
 
    vector<pair<int, int> > v;
    // inserting elements and corresponding index
    // as pair
    for (int i = 0; i < n; i++)
        v.push_back({ arr[i], i });
 
    // answer array for keeping count
    // initialized by 0,
    vector<int> ans(n, 0);
 
    // calling mergesort
    mergesort(v, ans, 0, n - 1);
 
    return ans;
}
 
// Driver Code Starts.
 
int main()
{
    int arr[] = { 12, 1, 2, 3, 0, 11, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    auto ans = constructLowerArray(arr, n);
    for (auto x : ans) {
        cout << x << " ";
    }
    cout << "\n";
    return 0;
}
// Driver code ends.
 
// This code is contributed by Manjeet Singh

Time complexity: nlogn
Space complexity: linear
#include <bits/stdc++.h>
using namespace std;
 
// BST node structure
class Node {
 
public:
    int val;
    int count;
    Node* left;
    Node* right;
 
    // Constructor
    Node(int num1, int num2)
    {
        this->val = num1;
        this->count = num2;
        this->left = this->right = NULL;
    }
};
 
// Function to addNode and find the smaller
// elements on the right side
int addNode(Node*& root, int value, int countSmaller)
{
 
    // Base case
    if (root == NULL) {
        root = new Node(value, 0);
        return countSmaller;
    }
    if (root->val < value) {
        return root->count
               + addNode(root->right, value,
                         countSmaller + 1);
    }
    else {
        root->count++;
        return addNode(root->left, value, countSmaller);
    }
}
 
// Driver code
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int data[] = { 12, 1, 2, 3, 0, 11, 4 };
    int size = sizeof(data) / sizeof(data[0]);
    int ans[size] = { 0 };
 
    Node* root = NULL;
 
    for (int i = size - 1; i >= 0; i--) {
        ans[i] = addNode(root, data[i], 0);
    }
 
    for (int i = 0; i < size; i++)
        cout << ans[i] << " ";
 
    return 0;
}
 
// This code is contributed by divyanshu gupta

Time complexity: quadratic
Space complexity: linear
// C++ program to sort an array according absolute
// difference with x.
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to sort an array according absolute
// difference with x.
void rearrange(int arr[], int n, int x)
{
    multimap<int, int> m;
    multimap<int, int>::iterator it;
    // Store values in a map with the difference
    // with X as key
    for (int i = 0; i < n; i++)
        m.insert(make_pair(abs(x - arr[i]), arr[i]));
 
    // Update the values of array
    int i = 0;
    for (it = m.begin(); it != m.end(); it++)
        arr[i++] = (*it).second;
}
 
// Function to print the array
void printArray(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// Driver code
int main()
{
    int arr[] = { 10, 5, 3, 9, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int x = 7;
 
    // Function call
    rearrange(arr, n, x);
    printArray(arr, n);
    return 0;
}

Time complexity: nlogn
Space complexity: linear
// CPP program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
void rearrange(int arr[], int n, int x)
{
    /*
        We can send the value x into
        lambda expression as
        follows: [capture]()
        {
            //statements
            //capture value can be used inside
        }
    */
    stable_sort(arr, arr + n, [x](int a, int b) {
        if (abs(a - x) < abs(b - x))
            return true;
        else
            return false;
    });
}
 
// Driver code
int main()
{
    int arr[] = { 10, 5, 3, 9, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int x = 7;
   
      // Function call
    rearrange(arr, n, x);
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    return 0;
}

Time complexity: nlogn
Space complexity: constant
// C++ program to find maximum product of an increasing
// subsequence of size 3
#include <bits/stdc++.h>
using namespace std;
 
// Returns maximum product of an increasing subsequence of
// size 3 in arr[0..n-1].  If no such subsequence exists,
// then it returns INT_MIN
long long int maxProduct(int arr[], int n)
{
    int result = INT_MIN;
    // T.C : O(n^3)
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            for (int k = j + 1; k < n; k++) {
                result
                    = max(result, arr[i] * arr[j] * arr[k]);
            }
        }
    }
 
    return result;
}
 
// Driver Program
int main()
{
    int arr[] = { 10, 11, 9, 5, 6, 1, 20 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << maxProduct(arr, n) << endl;
    return 0;
}

Time complexity: cubic
Space complexity: constant
// C++ program to find maximum product of an increasing
// subsequence of size 3
#include <bits/stdc++.h>
using namespace std;
 
// Returns maximum product of an increasing subsequence of
// size 3 in arr[0..n-1].  If no such subsequence exists,
// then it returns INT_MIN
long long int maxProduct(int arr[], int n)
{
    // An array ti store  closest smaller element on left
    // side of every element. If there is no such element
    // on left side, then smaller[i] be -1.
    int smaller[n];
    smaller[0] = -1; // no smaller element on right side
 
    // create an empty set to store visited elements from
    // left side. Set can also quickly find largest smaller
    // of an element.
    set<int> S;
    for (int i = 0; i < n; i++) {
        // insert arr[i] into the set S
        auto j = S.insert(arr[i]);
        auto itc
            = j.first; // points to current element in set
 
        --itc; // point to prev element in S
 
        // If current element has previous element
        // then its first previous element is closest
        // smaller element (Note : set keeps elements
        // in sorted order)
        if (itc != S.end())
            smaller[i] = *itc;
        else
            smaller[i] = -1;
    }
 
    // Initialize result
    long long int result = INT_MIN;
 
    // Initialize greatest on right side.
    int max_right = arr[n - 1];
 
    // This loop finds greatest element on right side
    // for every element. It also updates result when
    // required.
    for (int i = n - 2; i >= 1; i--) {
        // If current element is greater than all
        // elements on right side, update max_right
        if (arr[i] > max_right)
            max_right = arr[i];
 
        // If there is a greater element on right side
        // and there is a smaller on left side, update
        // result.
        else if (smaller[i] != -1)
            result = max((long long int)(smaller[i] * arr[i]
                                         * max_right),
                         result);
    }
 
    return result;
}
 
// Driver Program
int main()
{
    int arr[] = { 10, 11, 9, 5, 6, 1, 20 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << maxProduct(arr, n) << endl;
    return 0;
}

Time complexity: nlogn
Space complexity: linear
/* CPP program to check if
a tree is height-balanced or not */
 
#include <bits/stdc++.h>
using namespace std;
 
/* A binary tree node has data,
pointer to left child and
a pointer to right child */
class Node {
public:
    int data;
    Node* left;
    Node* right;
    Node(int d)
    {
        int data = d;
        left = right = NULL;
    }
};
 
// Function to calculate the height of a tree
int height(Node* node)
{
    // base case tree is empty
    if (node == NULL)
        return 0;
 
    // If tree is not empty then
    // height = 1 + max of left height
    // and right heights
    return 1 + max(height(node->left), height(node->right));
}
 
// Returns true if binary tree
// with root as root is height-balanced
bool isBalanced(Node* root)
{
    // for height of left subtree
    int lh;
 
    // for height of right subtree
    int rh;
 
    // If tree is empty then return true
    if (root == NULL)
        return 1;
 
    // Get the height of left and right sub trees
    lh = height(root->left);
    rh = height(root->right);
 
    if (abs(lh - rh) <= 1 && isBalanced(root->left)
        && isBalanced(root->right))
        return 1;
 
    // If we reach here then tree is not height-balanced
    return 0;
}
 
// Driver code
int main()
{
    Node* root = new Node(1);
    root->left = new Node(2);
    root->right = new Node(3);
    root->left->left = new Node(4);
    root->left->right = new Node(5);
    root->left->left->left = new Node(8);
 
    if (isBalanced(root))
        cout << "Tree is balanced";
    else
        cout << "Tree is not balanced";
    return 0;
}
 
// This code is contributed by rathbhupendra

Time complexity: quadratic
Space complexity: linear
// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Structure of a tree node
struct Node {
    int key;
    struct Node* left;
    struct Node* right;
    Node(int k)
    {
        key = k;
        left = right = NULL;
    }
};
 
// Function to check if tree is height balanced
int isBalanced(Node* root)
{
    if (root == NULL)
        return 0;
    int lh = isBalanced(root->left);
    if (lh == -1)
        return -1;
    int rh = isBalanced(root->right);
    if (rh == -1)
        return -1;
 
    if (abs(lh - rh) > 1)
        return -1;
    else
        return max(lh, rh) + 1;
}
 
// Driver code
int main()
{
    Node* root = new Node(10);
    root->left = new Node(5);
    root->right = new Node(30);
    root->right->left = new Node(15);
    root->right->right = new Node(20);
 
    if (isBalanced(root) > 0)
        cout << "Balanced";
    else
        cout << "Not Balanced";
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find the height of the generic
// tree(n-ary tree) if parent array is given
#include <bits/stdc++.h>
using namespace std;
 
// function to fill the height vector
int rec(int i, int parent[], vector<int> height)
{
    // if we have reached root node the
    // return 1 as height of root node
    if (parent[i] == -1) {
        return 1;
    }
  
    // if we have calculated height of a
    // node then return if
    if (height[i] != -1) {
        return height[i];
    }
 
    // height from root to a node = height
    // from root to nodes parent + 1
    height[i] = rec(parent[i], parent, height) + 1;
    
    // return nodes height
    return height[i];
}
 
// function to find the height of tree
int findHeight(int* parent, int n)
{
    int res = 0;
 
    // vector to store heights of all nodes
    vector<int> height(n, -1);
 
    for (int i = 0; i < n; i++) {
        res = max(res, rec(i, parent, height));
    }
 
    return res;
}
 
// Driver program
int main()
{
    int parent[] = { -1, 0, 1, 6, 6, 0, 0, 2, 7 };
    int n = sizeof(parent) / sizeof(parent[0]);
    int height = findHeight(parent, n);
    cout << "Height of the given tree is: "
         << height << endl;
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find number
// of children of given node
#include <bits/stdc++.h>
using namespace std;
 
// Represents a node of an n-ary tree
class Node {
 
public:
    int key;
    vector<Node*> child;
 
    Node(int data)
    {
        key = data;
    }
};
 
// Function to calculate number
// of children of given node
int numberOfChildren(Node* root, int x)
{
    // initialize the numChildren as 0
    int numChildren = 0;
 
    if (root == NULL)
        return 0;
 
    // Creating a queue and pushing the root
    queue<Node*> q;
    q.push(root);
 
    while (!q.empty()) {
        int n = q.size();
 
        // If this node has children
        while (n > 0) {
 
            // Dequeue an item from queue and
            // check if it is equal to x
            // If YES, then return number of children
            Node* p = q.front();
            q.pop();
            if (p->key == x) {
                numChildren = numChildren + p->child.size();
                return numChildren;
            }
 
            // Enqueue all children of the dequeued item
            for (int i = 0; i < p->child.size(); i++)
                q.push(p->child[i]);
            n--;
        }
    }
    return numChildren;
}
 
// Driver program
int main()
{
    // Creating a generic tree
    Node* root = new Node(20);
    (root->child).push_back(new Node(2));
    (root->child).push_back(new Node(34));
    (root->child).push_back(new Node(50));
    (root->child).push_back(new Node(60));
    (root->child).push_back(new Node(70));
    (root->child[0]->child).push_back(new Node(15));
    (root->child[0]->child).push_back(new Node(20));
    (root->child[1]->child).push_back(new Node(30));
    (root->child[2]->child).push_back(new Node(40));
    (root->child[2]->child).push_back(new Node(100));
    (root->child[2]->child).push_back(new Node(20));
    (root->child[0]->child[1]->child).push_back(new Node(25));
    (root->child[0]->child[1]->child).push_back(new Node(50));
 
    // Node whose number of
    // children is to be calculated
    int x = 50;
 
    // Function calling
    cout << numberOfChildren(root, x) << endl;
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find sum of all
// elements in generic tree
#include <bits/stdc++.h>
using namespace std;
 
// Represents a node of an n-ary tree
struct Node {
    int key;
    vector<Node*> child;
};
 
// Utility function to create a new tree node
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->key = key;
    return temp;
}
 
// Function to compute the sum
// of all elements in generic tree
int sumNodes(Node* root)
{
    // initialize the sum variable
    int sum = 0;
 
    if (root == NULL)
        return 0;
 
    // Creating a queue and pushing the root
    queue<Node*> q;
    q.push(root);
 
    while (!q.empty()) {
        int n = q.size();
 
        // If this node has children
        while (n > 0) {
 
            // Dequeue an item from queue and
            // add it to variable "sum"
            Node* p = q.front();
            q.pop();
            sum += p->key;
 
            // Enqueue all children of the dequeued item
            for (int i = 0; i < p->child.size(); i++)
                q.push(p->child[i]);
            n--;
        }
    }
    return sum;
}
 
// Driver program
int main()
{
    // Creating a generic tree
    Node* root = newNode(20);
    (root->child).push_back(newNode(2));
    (root->child).push_back(newNode(34));
    (root->child).push_back(newNode(50));
    (root->child).push_back(newNode(60));
    (root->child).push_back(newNode(70));
    (root->child[0]->child).push_back(newNode(15));
    (root->child[0]->child).push_back(newNode(20));
    (root->child[1]->child).push_back(newNode(30));
    (root->child[2]->child).push_back(newNode(40));
    (root->child[2]->child).push_back(newNode(100));
    (root->child[2]->child).push_back(newNode(20));
    (root->child[0]->child[1]->child).push_back(newNode(25));
    (root->child[0]->child[1]->child).push_back(newNode(50));
 
    cout << sumNodes(root) << endl;
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to create a tree with left child
// right sibling representation.
#include <bits/stdc++.h>
using namespace std;
 
struct Node {
    int data;
    struct Node* next;
    struct Node* child;
};
 
// Creating new Node
Node* newNode(int data)
{
    Node* newNode = new Node;
    newNode->next = newNode->child = NULL;
    newNode->data = data;
    return newNode;
}
 
// Adds a sibling to a list with starting with n
Node* addSibling(Node* n, int data)
{
    if (n == NULL)
        return NULL;
 
    while (n->next)
        n = n->next;
 
    return (n->next = newNode(data));
}
 
// Add child Node to a Node
Node* addChild(Node* n, int data)
{
    if (n == NULL)
        return NULL;
 
    // Check if child list is not empty.
    if (n->child)
        return addSibling(n->child, data);
    else
        return (n->child = newNode(data));
}
 
// Traverses tree in level order
void traverseTree(Node* root)
{
    // Corner cases
    if (root == NULL)
        return;
 
    cout << root->data << " ";
 
    if (root->child == NULL)
        return;
 
    // Create a queue and enqueue root
    queue<Node*> q;
    Node* curr = root->child;
    q.push(curr);
 
    while (!q.empty()) {
 
        // Take out an item from the queue
        curr = q.front();
        q.pop();
 
        // Print next level of taken out item and enqueue
        // next level's children
        while (curr != NULL) {
            cout << curr->data << " ";
            if (curr->child != NULL) {
                q.push(curr->child);
            }
            curr = curr->next;
        }
    }
}
 
// Driver code
int main()
{
    Node* root = newNode(10);
    Node* n1 = addChild(root, 2);
    Node* n2 = addChild(root, 3);
    Node* n3 = addChild(root, 4);
    Node* n4 = addChild(n3, 6);
    Node* n5 = addChild(root, 5);
    Node* n6 = addChild(n5, 7);
    Node* n7 = addChild(n5, 8);
    Node* n8 = addChild(n5, 9);
    traverseTree(root);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to count number of nodes
// which has more children than its parent
 
#include<bits/stdc++.h>
using namespace std;
 
// function to count number of nodes
// which has more children than its parent
int countNodes(vector<int> adj[], int root)
{   
    int count = 0;
 
    // queue for applying BFS
    queue<int> q;
 
    // BFS algorithm
    q.push(root);
     
    while (!q.empty())
    {
        int node = q.front();
        q.pop();
 
        // traverse children of node
        for( int i=0;i<adj[node].size();i++)
        {   
            // children of node
            int children = adj[node][i];
 
            // if number of childs of children
            // is greater than number of childs
            // of node, then increment count
            if (adj[children].size() > adj[node].size())
                count++;
            q.push(children);
        }
    }
 
    return count;
}
 
// Driver program to test above functions
int main()
{   
    // adjacency list for n-ary tree
    vector<int> adj[10];
 
    // construct n ary tree as shown
    // in above diagram
    adj[1].push_back(2);
    adj[1].push_back(3);
    adj[2].push_back(4);
    adj[2].push_back(5);
    adj[2].push_back(6);
    adj[3].push_back(9);
    adj[5].push_back(7);
    adj[5].push_back(8);
 
    int root = 1;
 
    cout << countNodes(adj, root);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to generate short url from integer id and
// integer id back from short url.
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
 
// Function to generate a short url from integer ID
string idToShortURL(long int n)
{
    // Map to store 62 possible characters
    char map[] = "abcdefghijklmnopqrstuvwxyzABCDEF"
                 "GHIJKLMNOPQRSTUVWXYZ0123456789";
 
    string shorturl;
 
    // Convert given integer id to a base 62 number
    while (n)
    {
        // use above map to store actual character
        // in short url
        shorturl.push_back(map[n%62]);
        n = n/62;
    }
 
    // Reverse shortURL to complete base conversion
    reverse(shorturl.begin(), shorturl.end());
 
    return shorturl;
}
 
// Function to get integer ID back from a short url
long int shortURLtoID(string shortURL)
{
    long int id = 0; // initialize result
 
    // A simple base conversion logic
    for (int i=0; i < shortURL.length(); i++)
    {
        if ('a' <= shortURL[i] && shortURL[i] <= 'z')
          id = id*62 + shortURL[i] - 'a';
        if ('A' <= shortURL[i] && shortURL[i] <= 'Z')
          id = id*62 + shortURL[i] - 'A' + 26;
        if ('0' <= shortURL[i] && shortURL[i] <= '9')
          id = id*62 + shortURL[i] - '0' + 52;
    }
    return id;
}
 
// Driver program to test above function
int main()
{
    int n = 12345;
    string shorturl = idToShortURL(n);
    cout << "Generated short url is " << shorturl << endl;
    cout << "Id from url is " << shortURLtoID(shorturl);
    return 0;
}

Time complexity: linear
Space complexity: constant