Banana-Leopard/TASTY huggingface dataset:
// CPP program to find all those
// elements of arr1[] that are not
// present in arr2[]
#include <iostream>
using namespace std;
 
void relativeComplement(int arr1[], int arr2[],
                               int n, int m) {
 
  int i = 0, j = 0;
  while (i < n && j < m) {
 
    // If current element in arr2[] is
    // greater, then arr1[i] can't be
    // present in arr2[j..m-1]
    if (arr1[i] < arr2[j]) {
      cout << arr1[i] << " ";
      i++;
 
    // Skipping smaller elements of
    // arr2[]
    } else if (arr1[i] > arr2[j]) {
      j++;
 
    // Equal elements found (skipping
    // in both arrays)
    } else if (arr1[i] == arr2[j]) {
      i++;
      j++;
    }
  }
 
  // Printing remaining elements of
  // arr1[]
  while (i < n)
    cout << arr1[i] << " "; 
}
 
// Driver code
int main() {
  int arr1[] = {3, 6, 10, 12, 15};
  int arr2[] = {1, 3, 5, 10, 16};
  int n = sizeof(arr1) / sizeof(arr1[0]);
  int m = sizeof(arr2) / sizeof(arr2[0]);
  relativeComplement(arr1, arr2, n, m);
  return 0;
}

Time complexity: linear
Space complexity: constant
// Program to make all array equal
#include <bits/stdc++.h>
using namespace std;
 
// function for calculating min operations
int minOps(int arr[], int n, int k)
{
    // max elements of array
    int max = *max_element(arr, arr + n);
    int res = 0;
 
    // iterate for all elements
    for (int i = 0; i < n; i++) {
 
        // check if element can make equal to
        // max or not if not then return -1
        if ((max - arr[i]) % k != 0)
            return -1;
 
        // else update res for required operations
        else
            res += (max - arr[i]) / k;
    }
 
    // return result
    return res;
}
 
// driver program
int main()
{
    int arr[] = { 21, 33, 9, 45, 63 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 6;
    cout << minOps(arr, n, k);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ code for above approach
#include<bits/stdc++.h>
using namespace std;
 
int solve(int A[], int B[], int C[], int i, int j, int k)
{
        int min_diff, current_diff, max_term;
 
        // calculating min difference from last
        // index of lists
           min_diff = abs(max(A[i], max(B[j], C[k]))
                            - min(A[i], min(B[j], C[k])));;
 
        while (i != -1 && j != -1 && k != -1)
        {
            current_diff = abs(max(A[i], max(B[j], C[k]))
                            - min(A[i], min(B[j], C[k])));
 
            // checking condition
            if (current_diff < min_diff)
                min_diff = current_diff;
 
            // calculating max term from list
            max_term = max(A[i], max(B[j], C[k]));
 
            // Moving to smaller value in the
            // array with maximum out of three.
            if (A[i] == max_term)
                i -= 1;
            else if (B[j] == max_term)
                j -= 1;
            else
                k -= 1;
        }
         
        return min_diff;
    }
 
    // Driver code
    int main()
    {
        int D[] = { 5, 8, 10, 15 };
        int E[] = { 6, 9, 15, 78, 89 };
        int F[] = { 2, 3, 6, 6, 8, 8, 10 };
        int nD = sizeof(D) / sizeof(D[0]);
        int nE = sizeof(E) / sizeof(E[0]);
        int nF = sizeof(F) / sizeof(F[0]);
        cout << solve(D, E, F, nD-1, nE-1, nF-1);
         
        return 0;
    }
 
// This code is contributed by Ravi Maurya.

Time complexity: linear
Space complexity: constant
// C++ program to print BST in given range
#include<bits/stdc++.h>
using namespace std;
 
/* A Binary Tree node */
class TNode
{
    public:
    int data;
    TNode* left;
    TNode* right;
};
 
TNode* newNode(int data);
 
/* A function that constructs Balanced
Binary Search Tree from a sorted array */
TNode* sortedArrayToBST(int arr[],
                        int start, int end)
{
    /* Base Case */
    if (start > end)
    return NULL;
 
    /* Get the middle element and make it root */
    int mid = (start + end)/2;
    TNode *root = newNode(arr[mid]);
 
    /* Recursively construct the left subtree
    and make it left child of root */
    root->left = sortedArrayToBST(arr, start,
                                    mid - 1);
 
    /* Recursively construct the right subtree
    and make it right child of root */
    root->right = sortedArrayToBST(arr, mid + 1, end);
 
    return root;
}
 
/* Helper function that allocates a new node
with the given data and NULL left and right
pointers. */
TNode* newNode(int data)
{
    TNode* node = new TNode();
    node->data = data;
    node->left = NULL;
    node->right = NULL;
 
    return node;
}
 
/* A utility function to print
preorder traversal of BST */
void preOrder(TNode* node)
{
    if (node == NULL)
        return;
    cout << node->data << " ";
    preOrder(node->left);
    preOrder(node->right);
}
 
// Driver Code
int main()
{
    int arr[] = {1, 2, 3, 4, 5, 6, 7};
    int n = sizeof(arr) / sizeof(arr[0]);
 
    /* Convert List to BST */
    TNode *root = sortedArrayToBST(arr, 0, n-1);
    cout << "PreOrder Traversal of constructed BST \n";
    preOrder(root);
 
    return 0;
}
 
// This code is contributed by rathbhupendra

Time complexity: linear
Space complexity: logn
// C++ program to transform a BST to sum tree
#include<iostream>
using namespace std;
 
// A BST node
struct Node
{
    int data;
    struct Node *left, *right;
};
 
// A utility function to create a new Binary Tree Node
struct Node *newNode(int item)
{
    struct Node *temp =  new Node;
    temp->data = item;
    temp->left = temp->right = NULL;
    return temp;
}
 
// Recursive function to transform a BST to sum tree.
// This function traverses the tree in reverse inorder so
// that we have visited all greater key nodes of the currently
// visited node
void transformTreeUtil(struct Node *root, int *sum)
{
   // Base case
   if (root == NULL)  return;
 
   // Recur for right subtree
   transformTreeUtil(root->right, sum);
 
   // Update sum
   *sum = *sum + root->data;
 
   // Store old sum in current node
   root->data = *sum - root->data;
 
   // Recur for left subtree
   transformTreeUtil(root->left, sum);
}
 
// A wrapper over transformTreeUtil()
void transformTree(struct Node *root)
{
    int sum = 0; // Initialize sum
    transformTreeUtil(root, ∑);
}
 
// A utility function to print indorder traversal of a
// binary tree
void printInorder(struct Node *root)
{
    if (root == NULL) return;
 
    printInorder(root->left);
    cout << root->data << " ";
    printInorder(root->right);
}
 
// Driver Program to test above functions
int main()
{
    struct Node *root = newNode(11);
    root->left = newNode(2);
    root->right = newNode(29);
    root->left->left = newNode(1);
    root->left->right = newNode(7);
    root->right->left = newNode(15);
    root->right->right = newNode(40);
    root->right->right->left = newNode(35);
 
    cout << "Inorder Traversal of given tree\n";
    printInorder(root);
 
    transformTree(root);
 
    cout << "\n\nInorder Traversal of transformed tree\n";
    printInorder(root);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ implementation to convert the given
// BST to Min Heap
 
#include <bits/stdc++.h>
using namespace std;
 
// Structure of a node of BST
struct Node {
 
    int data;
    Node *left, *right;
};
 
/* Helper function that allocates a new node
   with the given data and NULL left and right
   pointers. */
struct Node* getNode(int data)
{
    struct Node* newNode = new Node;
    newNode->data = data;
    newNode->left = newNode->right = NULL;
    return newNode;
}
 
// function prototype for preorder traversal
// of the given tree
void preorderTraversal(Node*);
 
// function for the inorder traversal of the tree
// so as to store the node values in 'arr' in
// sorted order
void inorderTraversal(Node* root, vector<int>& arr)
{
    if (root == NULL)
        return;
 
    // first recur on left subtree
    inorderTraversal(root->left, arr);
 
    // then copy the data of the node
    arr.push_back(root->data);
 
    // now recur for right subtree
    inorderTraversal(root->right, arr);
}
 
// function to convert the given BST to MIN HEAP
// performs preorder traversal of the tree
void BSTToMinHeap(Node* root, vector<int> arr, int* i)
{
    if (root == NULL)
        return;
 
    // first copy data at index 'i' of 'arr' to
    // the node
    root->data = arr[++*i];
 
    // then recur on left subtree
    BSTToMinHeap(root->left, arr, i);
 
    // now recur on right subtree
    BSTToMinHeap(root->right, arr, i);
}
 
// utility function to convert the given BST to
// MIN HEAP
void convertToMinHeapUtil(Node* root)
{
    // vector to store the data of all the
    // nodes of the BST
    vector<int> arr;
    int i = -1;
 
    // inorder traversal to populate 'arr'
    inorderTraversal(root, arr);
 
    // BST to MIN HEAP conversion
    BSTToMinHeap(root, arr, &i);
}
 
// function for the preorder traversal of the tree
void preorderTraversal(Node* root)
{
    if (!root)
        return;
 
    // first print the root's data
    cout << root->data << " ";
 
    // then recur on left subtree
    preorderTraversal(root->left);
 
    // now recur on right subtree
    preorderTraversal(root->right);
}
 
// Driver program to test above
int main()
{
    // BST formation
    struct Node* root = getNode(4);
    root->left = getNode(2);
    root->right = getNode(6);
    root->left->left = getNode(1);
    root->left->right = getNode(3);
    root->right->left = getNode(5);
    root->right->right = getNode(7);
 
    // Function call
    convertToMinHeapUtil(root);
    cout << "Preorder Traversal:" << endl;
    preorderTraversal(root);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ implementation to construct a BST
// from its level order traversal
#include <bits/stdc++.h>
 
using namespace std;
 
// node of a BST
struct Node {
    int data;
    Node *left, *right;
};
 
// function to get a new node
Node* getNode(int data)
{
    // Allocate memory
    Node* newNode = (Node*)malloc(sizeof(Node));
 
    // put in the data
    newNode->data = data;
    newNode->left = newNode->right = NULL;
    return newNode;
}
 
// function to construct a BST from
// its level order traversal
Node* LevelOrder(Node* root, int data)
{
    if (root == NULL) {
        root = getNode(data);
        return root;
    }
    if (data <= root->data)
        root->left = LevelOrder(root->left, data);
    else
        root->right = LevelOrder(root->right, data);
    return root;
}
 
Node* constructBst(int arr[], int n)
{
    if (n == 0)
        return NULL;
    Node* root = NULL;
 
    for (int i = 0; i < n; i++)
        root = LevelOrder(root, arr[i]);
 
    return root;
}
 
// function to print the inorder traversal
void inorderTraversal(Node* root)
{
    if (!root)
        return;
 
    inorderTraversal(root->left);
    cout << root->data << " ";
    inorderTraversal(root->right);
}
 
// Driver program to test above
int main()
{
    int arr[] = { 7, 4, 12, 3, 6, 8, 1, 5, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    Node* root = constructBst(arr, n);
 
    cout << "Inorder Traversal: ";
    inorderTraversal(root);
    return 0;
}

Time complexity: nlogn
Space complexity: linear
// C++ implementation to construct a BST
// from its level order traversal
 
#include <bits/stdc++.h>
using namespace std;
 
// node of a BST
struct Node {
    int data;
    Node *left, *right;
 
    Node(int x)
    {
        data = x;
        right = NULL;
        left = NULL;
    }
};
 
// Function to construct a BST from
// its level order traversal
Node* constructBst(int arr[], int n)
{
    // Create queue to store the tree nodes
    queue<pair<Node*, pair<int, int> > > q;
 
    // If array is empty we return NULL
    if (n == 0)
        return NULL;
 
    // Create root node and store a copy of it in head
    Node *root = new Node(arr[0]), *head = root;
 
    // Push the root node and the initial range
    q.push({ root, { INT_MIN, INT_MAX } });
 
    // Loop over the contents of arr to process all the
    // elements
    for (int i = 1; i < n; i++) {
 
        // Get the node and the range at the front of the
        // queue
        Node* temp = q.front().first;
        pair<int, int> range = q.front().second;
 
        // Check if arr[i] can be a child of the temp node
        if (arr[i] > range.first && arr[i] < range.second) {
 
            // Check if arr[i] can be left child
            if (arr[i] < temp->data) {
 
                // Set the left child and range
                temp->left = new Node(arr[i]);
                q.push({ temp->left,
                         { range.first, temp->data } });
            }
            // Check if arr[i] can be left child
            else {
 
                // Pop the temp node from queue, set the
                // right child and new range
                q.pop();
                temp->right = new Node(arr[i]);
                q.push({ temp->right,
                         { temp->data, range.second } });
            }
        }
        else {
 
            q.pop();
            i--;
        }
    }
    return head;
}
 
// Function to print the inorder traversal
void inorderTraversal(Node* root)
{
    if (!root)
        return;
 
    inorderTraversal(root->left);
    cout << root->data << " ";
    inorderTraversal(root->right);
}
 
// Driver program to test above
int main()
{
    int arr[] = { 7, 4, 12, 3, 6, 8, 1, 5, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    Node* root = constructBst(arr, n);
 
    cout << "Inorder Traversal: ";
    inorderTraversal(root);
    return 0;
}
 
// This code is contributed by Rohit Iyer (rohit_iyer)

Time complexity: linear
Space complexity: linear
/* CPP program to convert a Binary tree to BST
   using sets as containers. */
#include <bits/stdc++.h>
using namespace std;
 
struct Node {
    int data;
    struct Node *left, *right;
};
 
// function to store the nodes in set while
// doing inorder traversal.
void storeinorderInSet(Node* root, set<int>& s)
{
    if (!root)
        return;
 
    // visit the left subtree first
    storeinorderInSet(root->left, s);
 
    // insertion takes order of O(logn) for sets
    s.insert(root->data);
 
    // visit the right subtree
    storeinorderInSet(root->right, s);
 
} // Time complexity  = O(nlogn)
 
// function to copy items of set one by one
// to the tree while doing inorder traversal
void setToBST(set<int>& s, Node* root)
{
    // base condition
    if (!root)
        return;
 
    // first move to the left subtree and
    // update items
    setToBST(s, root->left);
 
    // iterator initially pointing to the
    // beginning of set
    auto it = s.begin();
 
    // copying the item at beginning of
    // set(sorted) to the tree.
    root->data = *it;
 
    // now erasing the beginning item from set.
    s.erase(it);
 
    // now move to right subtree and update items
    setToBST(s, root->right);
 
} // T(n) = O(nlogn) time
 
// Converts Binary tree to BST.
void binaryTreeToBST(Node* root)
{
    set<int> s;
 
    // populating the set with the tree's
    // inorder traversal data
    storeinorderInSet(root, s);
 
    // now sets are by default sorted as
    // they are implemented using self-
    // balancing BST
 
    // copying items from set to the tree
    // while inorder traversal which makes a BST
    setToBST(s, root);
 
} // Time complexity  =  O(nlogn),
  // Auxiliary Space = O(n) for set.
 
// helper function to create a node
Node* newNode(int data)
{
    // dynamically allocating memory
    Node* temp = new Node();
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
 
// function to do inorder traversal
void inorder(Node* root)
{
    if (!root)
        return;
    inorder(root->left);
    cout << root->data << " ";
    inorder(root->right);
}
 
int main()
{
    Node* root = newNode(5);
    root->left = newNode(7);
    root->right = newNode(9);
    root->right->left = newNode(10);
    root->left->left = newNode(1);
    root->left->right = newNode(6);
    root->right->right = newNode(11);
 
    /* Constructing tree given in the above figure
           5
         /   \
        7     9
       /\    / \
      1  6   10 11   */
 
    // converting the above Binary tree to BST
    binaryTreeToBST(root);
    cout << "Inorder traversal of BST is: " << endl;
    inorder(root);
    return 0;
}

Time complexity: nlogn
Space complexity: linear
#include <bits/stdc++.h>
using namespace std;
 
// Structure of a BST Node
class Node {
public:
    int val;
    Node* left;
    Node* right;
};
 
/* Utility function to create a new Binary Tree Node */
Node* newNode(int data)
{
    Node* temp = new Node;
    temp->val = data;
    temp->left = nullptr;
    temp->right = nullptr;
    return temp;
}
 
vector<int> mergeTwoBST(Node* root1, Node* root2)
{
    vector<int> res;
    stack<Node*> s1, s2;
    while (root1 || root2 || !s1.empty() || !s2.empty()) {
        while (root1) {
            s1.push(root1);
            root1 = root1->left;
        }
        while (root2) {
            s2.push(root2);
            root2 = root2->left;
        }
        // Step 3 Case 1:-
        if (s2.empty()
            || (!s1.empty()
                && s1.top()->val <= s2.top()->val)) {
            root1 = s1.top();
            s1.pop();
            res.push_back(root1->val);
            root1 = root1->right;
        }
        // Step 3 case 2 :-
        else {
            root2 = s2.top();
            s2.pop();
            res.push_back(root2->val);
            root2 = root2->right;
        }
    }
    return res;
}
 
/* Driver program to test above functions */
int main()
{
    Node *root1 = nullptr, *root2 = nullptr;
 
    /* Let us create the following tree as first tree
       3
      / \
      1 5
    */
    root1 = newNode(3);
    root1->left = newNode(1);
    root1->right = newNode(5);
 
    /* Let us create the following tree as second tree
       4
      / \
      2 6
    */
    root2 = newNode(4);
    root2->left = newNode(2);
    root2->right = newNode(6);
 
    // Print sorted Nodes of both trees
    vector<int> ans = mergeTwoBST(root1, root2);
    for (auto it : ans)
        cout << it << " ";
    return 0;
}
 
// This code is contributed by Aditya kumar (adityakumar129)

Time complexity: linear
Space complexity: logn
// C++ program to find minimum value node in binary search
// Tree.
#include <bits/stdc++.h>
using namespace std;
 
/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node {
    int data;
    struct node* left;
    struct node* right;
};
 
/* Helper function that allocates a new node
with the given data and NULL left and right
pointers. */
struct node* newNode(int data)
{
    struct node* node
        = (struct node*)malloc(sizeof(struct node));
    node->data = data;
    node->left = NULL;
    node->right = NULL;
 
    return (node);
}
 
/* Give a binary search tree and a number,
inserts a new node with the given number in
the correct place in the tree. Returns the new
root pointer which the caller should then use
(the standard trick to avoid using reference
parameters). */
struct node* insert(struct node* node, int data)
{
    /* 1. If the tree is empty, return a new,
        single node */
    if (node == NULL)
        return (newNode(data));
    else {
        /* 2. Otherwise, recur down the tree */
        if (data <= node->data)
            node->left = insert(node->left, data);
        else
            node->right = insert(node->right, data);
 
        /* return the (unchanged) node pointer */
        return node;
    }
}
 
/* Given a non-empty binary search tree,
return the minimum data value found in that
tree. Note that the entire tree does not need
to be searched. */
int minValue(struct node* node)
{
    struct node* current = node;
 
    /* loop down to find the leftmost leaf */
    while (current->left != NULL) {
        current = current->left;
    }
    return (current->data);
}
 
/* Driver Code*/
int main()
{
    struct node* root = NULL;
    root = insert(root, 4);
    insert(root, 2);
    insert(root, 1);
    insert(root, 3);
    insert(root, 6);
    insert(root, 5);
 
      // Function call
    cout << "\n Minimum value in BST is " << minValue(root);
    getchar();
    return 0;
}
 
// This code is contributed by Mukul Singh.

Time complexity: linear
Space complexity: linear
// C++ program for an efficient solution to check if
// a given array can represent Preorder traversal of
// a Binary Search Tree
#include<bits/stdc++.h>
using namespace std;
 
bool canRepresentBST(int pre[], int n)
{
    // Create an empty stack
    stack<int> s;
 
    // Initialize current root as minimum possible
    // value
    int root = INT_MIN;
 
    // Traverse given array
    for (int i=0; i<n; i++)
    {
        // If we find a node who is on right side
        // and smaller than root, return false
        if (pre[i] < root)
            return false;
 
        // If pre[i] is in right subtree of stack top,
        // Keep removing items smaller than pre[i]
        // and make the last removed item as new
        // root.
        while (!s.empty() && s.top()<pre[i])
        {
            root = s.top();
            s.pop();
        }
 
        // At this point either stack is empty or
        // pre[i] is smaller than root, push pre[i]
        s.push(pre[i]);
    }
    return true;
}
 
// Driver program
int main()
{
    int pre1[] = {40, 30, 35, 80, 100};
    int n = sizeof(pre1)/sizeof(pre1[0]);
    canRepresentBST(pre1, n)? cout << "true\n":
                              cout << "false\n";
 
    int pre2[] = {40, 30, 35, 20, 80, 100};
    n = sizeof(pre2)/sizeof(pre2[0]);
    canRepresentBST(pre2, n)? cout << "true\n":
                              cout << "false\n";
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to illustrate if a given array can represent
// a preorder traversal of a BST or not
 
#include <bits/stdc++.h>
using namespace std;
 
// We are actually not building the tree
void buildBST_helper(int& preIndex, int n, int pre[],
                     int min, int max)
{
    if (preIndex >= n)
        return;
 
    if (min <= pre[preIndex] && pre[preIndex] <= max) {
        // build node
        int rootData = pre[preIndex];
        preIndex++;
 
        // build left subtree
        buildBST_helper(preIndex, n, pre, min, rootData);
 
        // build right subtree
        buildBST_helper(preIndex, n, pre, rootData, max);
    }
    // else
    // return NULL;
}
 
bool canRepresentBST(int arr[], int N)
{
    // code here
    int min = INT_MIN, max = INT_MAX;
    int preIndex = 0;
 
    buildBST_helper(preIndex, N, arr, min, max);
 
    return preIndex == N;
}
 
// Driver Code
int main()
{
 
    int preorder1[] = { 2, 4, 3 };
    /*
            2
            \
             4
            /
           3
 
*/
    int n1 = sizeof(preorder1) / sizeof(preorder1[0]);
 
    if (canRepresentBST(preorder1, n1))
        cout << "\npreorder1 can represent BST";
    else
        cout << "\npreorder1 can not represent BST  :(";
 
    int preorder2[] = { 5, 3, 4, 1, 6, 10 };
    int n2 = sizeof(preorder2) / sizeof(preorder2[0]);
    /*
                    5
         /     \
       3         1
         \     /  \
         4    6    10
 
*/
    if (canRepresentBST(preorder2, n2))
        cout << "\npreorder2 can represent BST";
    else
        cout << "\npreorder2 can not represent BST  :(";
 
    int preorder3[] = { 5, 3, 4, 8, 6, 10 };
    int n3 = sizeof(preorder3) / sizeof(preorder3[0]);
    /*
                    5
         /     \
       3         8
         \     /  \
         4    6    10
 
*/
    if (canRepresentBST(preorder3, n3))
        cout << "\npreorder3 can represent BST";
    else
        cout << "\npreorder3 can not represent BST  :(";
 
    return 0;
}
 
// This code is contributed by Sourashis Mondal

Time complexity: linear
Space complexity: logn