Banana-Leopard/TASTY huggingface dataset:
// LCM of given range queries using Segment Tree
#include <bits/stdc++.h>
using namespace std;
 
#define MAX 1000
 
// allocate space for tree
int tree[4 * MAX];
 
// declaring the array globally
int arr[MAX];
 
// Function to return gcd of a and b
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
 
// utility function to find lcm
int lcm(int a, int b) { return a * b / gcd(a, b); }
 
// Function to build the segment tree
// Node starts beginning index of current subtree.
// start and end are indexes in arr[] which is global
void build(int node, int start, int end)
{
    // If there is only one element in current subarray
    if (start == end) {
        tree[node] = arr[start];
        return;
    }
 
    int mid = (start + end) / 2;
 
    // build left and right segments
    build(2 * node, start, mid);
    build(2 * node + 1, mid + 1, end);
 
    // build the parent
    int left_lcm = tree[2 * node];
    int right_lcm = tree[2 * node + 1];
 
    tree[node] = lcm(left_lcm, right_lcm);
}
 
// Function to make queries for array range )l, r).
// Node is index of root of current segment in segment
// tree (Note that indexes in segment tree begin with 1
// for simplicity).
// start and end are indexes of subarray covered by root
// of current segment.
int query(int node, int start, int end, int l, int r)
{
    // Completely outside the segment, returning
    // 1 will not affect the lcm;
    if (end < l || start > r)
        return 1;
 
    // completely inside the segment
    if (l <= start && r >= end)
        return tree[node];
 
    // partially inside
    int mid = (start + end) / 2;
    int left_lcm = query(2 * node, start, mid, l, r);
    int right_lcm = query(2 * node + 1, mid + 1, end, l, r);
    return lcm(left_lcm, right_lcm);
}
 
// driver function to check the above program
int main()
{
    // initialize the array
    arr[0] = 5;
    arr[1] = 7;
    arr[2] = 5;
    arr[3] = 2;
    arr[4] = 10;
    arr[5] = 12;
    arr[6] = 11;
    arr[7] = 17;
    arr[8] = 14;
    arr[9] = 1;
    arr[10] = 44;
 
    // build the segment tree
    build(1, 0, 10);
 
    // Now we can answer each query efficiently
 
    // Print LCM of (2, 5)
    cout << query(1, 0, 10, 2, 5) << endl;
 
    // Print LCM of (5, 10)
    cout << query(1, 0, 10, 5, 10) << endl;
 
    // Print LCM of (0, 10)
    cout << query(1, 0, 10, 0, 10) << endl;
 
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to construct an n x n
// matrix such that every row and every
// column has distinct values.
#include <iostream>
#include <bits/stdc++.h>
 
using namespace std;
 
const int MAX = 100;
int mat[MAX][MAX];
 
// Fills non-one entries in column j
// Given that there is a "1" at
// position mat[i][j], this function
// fills other entries of column j.
void fillRemaining(int i, int j, int n)
{
    // Initialize value to be filled
    int x = 2;
 
    // Fill all values below i as 2, 3, ...p
    for (int k = i + 1; k < n; k++)
        mat[k][j] = x++;
 
    // Fill all values above i
    // as p + 1, p + 2, .. n
    for (int k = 0; k < i; k++)
        mat[k][j] = x++;
}
 
// Fills entries in mat[][]
// with the given set of rules
void constructMatrix(int n)
{
    // Alternatively fill 1s starting from
    // rightmost and leftmost columns. For
    // example for n = 3, we get { {_ _ 1},
    // {1 _ _} {_ 1 _}}
    int right = n - 1, left = 0;
    for (int i = 0; i < n; i++)
    {
        // If i is even, then fill 
        // next column from right
        if (i % 2 == 0)
        {
            mat[i][right] = 1;
 
            // After filling 1, fill remaining
            // entries of column "right"
            fillRemaining(i, right, n);
 
            // Move right one column back
            right--;
        }
         
        // Fill next column from left
        else
        {
            mat[i][left] = 1;
 
            // After filling 1, fill remaining
            // entries of column "left"
            fillRemaining(i, left, n);
 
            // Move left one column forward
            left++;
        }
    }
}
 
// Driver code
int main()
{
    int n = 5;
 
    // Passing n to constructMatrix function
    constructMatrix(n);
 
    // Printing the desired unique matrix
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
            printf("%d ",mat[i][j]);
        printf ("\n");
    }
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// Given a binary matrix of M X N of integers,
// you need to return only unique rows of binary array
#include <bits/stdc++.h>
using namespace std;
#define ROW 4
#define COL 5
 
// The main function that prints
// all unique rows in a given matrix.
void findUniqueRows(int M[ROW][COL])
{
    //Traverse through the matrix
    for(int i=0; i<ROW; i++)
    {
        int flag=0;
         
        //check if there is similar column
        //is already printed, i.e if i and
        //jth column match.
        for(int j=0; j<i; j++)
        {
            flag=1;
             
            for(int k=0; k<=COL; k++)
            if(M[i][k]!=M[j][k])
                flag=0;
             
            if(flag==1)
            break;
        }
         
        //if no row is similar
        if(flag==0)
        {
            //print the row
            for(int j=0; j<COL; j++)
                cout<<M[i][j]<<" ";
            cout<<endl;
        }
    }
}
 
// Driver Code
int main()
{
    int M[ROW][COL] = {{0, 1, 0, 0, 1},
                       {1, 0, 1, 1, 0},
                       {0, 1, 0, 0, 1},
                       {1, 0, 1, 0, 0}};
 
    findUniqueRows(M);
 
    return 0;
}

Time complexity: cubic
Space complexity: constant
// Given a binary matrix of M X N of integers,
// you need to return only unique rows of binary array
#include <bits/stdc++.h>
using namespace std;
#define ROW 4
#define COL 5
 
class BST
{
    int data;
    BST *left, *right;
 
    public:
     
    // Default constructor.
    BST();
     
    // Parameterized constructor.
    BST(int);
     
    // Insert function.
    BST* Insert(BST *, int);
     
    // Inorder traversal.
    void Inorder(BST *);
};
 
//convert array to decimal
int convert(int arr[])
{
    int sum=0;
     
    for(int i=0; i<COL; i++)
    {
        sum+=pow(2,i)*arr[i];
    }
    return sum;
}
 
//print the column represented as integers
void print(int p)
{
    for(int i=0; i<COL; i++)
    {
        cout<<p%2<<" ";
        p/=2;
    }
    cout<<endl;
}
 
 
// Default Constructor definition.
BST :: BST() : data(0), left(NULL), right(NULL){}
 
// Parameterized Constructor definition.
BST :: BST(int value)
{
    data = value;
    left = right = NULL;
}
 
// Insert function definition.
BST* BST :: Insert(BST *root, int value)
{
    if(!root)
    {
        // Insert the first node, if root is NULL.
        return new BST(value);
    }
     
    //if the value is present
    if(value == root->data)
     return root;
 
    // Insert data.
    if(value > root->data)
    {
        // Insert right node data, if the 'value'
        // to be inserted is greater than 'root' node data.
         
        // Process right nodes.
        root->right = Insert(root->right, value);
    }
    else
    {
        // Insert left node data, if the 'value'
        // to be inserted is greater than 'root' node data.
         
        // Process left nodes.
        root->left = Insert(root->left, value);
    }
     
    // Return 'root' node, after insertion.
    return root;
}
 
// Inorder traversal function.
// This gives data in sorted order.
void BST :: Inorder(BST *root)
{
    if(!root)
    {
        return;
    }
    Inorder(root->left);
    print( root->data );
    Inorder(root->right);
}
 
 
// The main function that prints
// all unique rows in a given matrix.
void findUniqueRows(int M[ROW][COL])
{
     
    BST b, *root = NULL;
     
    //Traverse through the matrix
    for(int i=0; i<ROW; i++)
    {
        //insert the row into BST
        root=b.Insert(root,convert(M[i]));
    }
     
     
    //print
    b.Inorder(root);
     
}
 
// Driver Code
int main()
{
    int M[ROW][COL] = {{0, 1, 0, 0, 1},
                       {1, 0, 1, 1, 0},
                       {0, 1, 0, 0, 1},
                       {1, 0, 1, 0, 0}};
 
    findUniqueRows(M);
 
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// Given a binary matrix of M X N of integers,
// you need to return only unique rows of binary array
#include <bits/stdc++.h>
using namespace std;
#define ROW 4
#define COL 5
 
// A Trie node
class Node
{
    public:
    bool isEndOfCol;
    Node *child[2]; // Only two children needed for 0 and 1
} ;
 
 
// A utility function to allocate memory
// for a new Trie node
Node* newNode()
{
    Node* temp = new Node();
    temp->isEndOfCol = 0;
    temp->child[0] = temp->child[1] = NULL;
    return temp;
}
 
// Inserts a new matrix row to Trie.
// If row is already present,
// then returns 0, otherwise insets the row and
// return 1
bool insert(Node** root, int (*M)[COL],
                int row, int col )
{
    // base case
    if (*root == NULL)
        *root = newNode();
 
    // Recur if there are more entries in this row
    if (col < COL)
        return insert (&((*root)->child[M[row][col]]),
                                        M, row, col + 1);
 
    else // If all entries of this row are processed
    {
        // unique row found, return 1
        if (!((*root)->isEndOfCol))
            return (*root)->isEndOfCol = 1;
 
        // duplicate row found, return 0
        return 0;
    }
}
 
// A utility function to print a row
void printRow(int(*M)[COL], int row)
{
    int i;
    for(i = 0; i < COL; ++i)
        cout << M[row][i] << " ";
    cout << endl;
}
 
// The main function that prints
// all unique rows in a given matrix.
void findUniqueRows(int (*M)[COL])
{
    Node* root = NULL; // create an empty Trie
    int i;
 
    // Iterate through all rows
    for (i = 0; i < ROW; ++i)
     
        // insert row to TRIE
        if (insert(&root, M, i, 0))
         
            // unique row found, print it
            printRow(M, i);
}
 
// Driver Code
int main()
{
    int M[ROW][COL] = {{0, 1, 0, 0, 1},
                       {1, 0, 1, 1, 0},
                       {0, 1, 0, 0, 1},
                       {1, 0, 1, 0, 0}};
 
    findUniqueRows(M);
 
    return 0;
}
 
// This code is contributed by rathbhupendra

Time complexity: quadratic
Space complexity: quadratic
// C++ code to print unique row in a
// given binary matrix
#include<bits/stdc++.h>
using namespace std;
 
void printArray(int arr[][5], int row,
                              int col)
{
    unordered_set<string> uset;
     
    for(int i = 0; i < row; i++)
    {
        string s = "";
         
        for(int j = 0; j < col; j++)
            s += to_string(arr[i][j]);
         
        if(uset.count(s) == 0)
        {
            uset.insert(s);
            cout << s << endl;
             
        }
    }
}
 
// Driver code
int main()
{
    int arr[][5] = {{0, 1, 0, 0, 1},
                    {1, 0, 1, 1, 0},
                    {0, 1, 0, 0, 1},
                    {1, 1, 1, 0, 0}};
     
    printArray(arr, 4, 5);
}
 
// This code is contributed by
// rathbhupendra

Time complexity: quadratic
Space complexity: linear
// A C++ program to find the count of distinct substring
// of a string using trie data structure
#include <bits/stdc++.h>
#define MAX_CHAR 26
using namespace std;
 
// A Suffix Trie (A Trie of all suffixes) Node
class SuffixTrieNode
{
public:
    SuffixTrieNode *children[MAX_CHAR];
    SuffixTrieNode() // Constructor
    {
        // Initialize all child pointers as NULL
        for (int i = 0; i < MAX_CHAR; i++)
          children[i] = NULL;
    }
 
    // A recursive function to insert a suffix of the s
    // in subtree rooted with this node
    void insertSuffix(string suffix);
};
 
// A Trie of all suffixes
class SuffixTrie
{
    SuffixTrieNode *root;
    int _countNodesInTrie(SuffixTrieNode *);
public:
    // Constructor (Builds a trie of suffies of the given text)
    SuffixTrie(string s)
    {
        root = new SuffixTrieNode();
 
        // Consider all suffixes of given string and insert
        // them into the Suffix Trie using recursive function
        // insertSuffix() in SuffixTrieNode class
        for (int i = 0; i < s.length(); i++)
            root->insertSuffix(s.substr(i));
    }
 
    //  method to count total nodes in suffix trie
    int countNodesInTrie() { return _countNodesInTrie(root); }
};
 
// A recursive function to insert a suffix of the s in
// subtree rooted with this node
void SuffixTrieNode::insertSuffix(string s)
{
    // If string has more characters
    if (s.length() > 0)
    {
        // Find the first character and convert it
        // into 0-25 range.
        char cIndex = s.at(0) - 'a';
 
        // If there is no edge for this character,
        // add a new edge
        if (children[cIndex] == NULL)
            children[cIndex] = new SuffixTrieNode();
 
        // Recur for next suffix
        children[cIndex]->insertSuffix(s.substr(1));
    }
}
 
// A recursive function to count nodes in trie
int SuffixTrie::_countNodesInTrie(SuffixTrieNode* node)
{
    // If all characters of pattern have been processed,
    if (node == NULL)
        return 0;
 
    int count = 0;
    for (int i = 0; i < MAX_CHAR; i++)
    {
        // if children is not NULL then find count
        // of all nodes in this subtrie
        if (node->children[i] != NULL)
            count += _countNodesInTrie(node->children[i]);
    }
 
    // return count of nodes of subtrie and plus
    // 1 because of node's own count
    return (1 + count);
}
 
// Returns count of distinct substrings of str
int countDistinctSubstring(string str)
{
    // Construct a Trie of all suffixes
    SuffixTrie sTrie(str);
 
    // Return count of nodes in Trie of Suffixes
    return sTrie.countNodesInTrie();
}
 
// Driver program to test above function
int main()
{
    string str = "ababa";
    cout << "Count of distinct substrings is "
         << countDistinctSubstring(str);
    return 0;
}

Time complexity: linear
Space complexity: constant
#include <bits/stdc++.h>
using namespace std;
 
// Function to find minimum XOR pair
int minXOR(int arr[], int n)
{
    // Sort given array
    sort(arr, arr + n);
 
    int minXor = INT_MAX;
    int val = 0;
 
    // calculate min xor of consecutive pairs
    for (int i = 0; i < n - 1; i++) {
        val = arr[i] ^ arr[i + 1];
        minXor = min(minXor, val);
    }
 
    return minXor;
}
 
// Driver program
int main()
{
    int arr[] = { 9, 5, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << minXOR(arr, n) << endl;
 
    return 0;
}

Time complexity: nlogn
Space complexity: constant
// A simple C++ program to find max subarray XOR
#include<bits/stdc++.h>
using namespace std;
 
int maxSubarrayXOR(int arr[], int n)
{
    int ans = INT_MIN;     // Initialize result
 
    // Pick starting points of subarrays
    for (int i=0; i<n; i++)
    {
        int curr_xor = 0; // to store xor of current subarray
 
        // Pick ending points of subarrays starting with i
        for (int j=i; j<n; j++)
        {
            curr_xor = curr_xor ^ arr[j];
            ans = max(ans, curr_xor);
        }
    }
    return ans;
}
 
// Driver program to test above functions
int main()
{
    int arr[] = {8, 1, 2, 12};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << "Max subarray XOR is " << maxSubarrayXOR(arr, n);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program for a Trie based O(n) solution to find max
// subarray XOR
#include<bits/stdc++.h>
using namespace std;
 
// Assumed int size
#define INT_SIZE 32
 
// A Trie Node
struct TrieNode
{
    int value;  // Only used in leaf nodes
    TrieNode *arr[2];
};
 
// Utility function to create a Trie node
TrieNode *newNode()
{
    TrieNode *temp = new TrieNode;
    temp->value = 0;
    temp->arr[0] = temp->arr[1] = NULL;
    return temp;
}
 
// Inserts pre_xor to trie with given root
void insert(TrieNode *root, int pre_xor)
{
    TrieNode *temp = root;
 
    // Start from the msb, insert all bits of
    // pre_xor into Trie
    for (int i=INT_SIZE-1; i>=0; i--)
    {
        // Find current bit in given prefix
        bool val = pre_xor & (1<<i);
 
        // Create a new node if needed
        if (temp->arr[val] == NULL)
            temp->arr[val] = newNode();
 
        temp = temp->arr[val];
    }
 
    // Store value at leaf node
    temp->value = pre_xor;
}
 
// Finds the maximum XOR ending with last number in
// prefix XOR 'pre_xor' and returns the XOR of this maximum
// with pre_xor which is maximum XOR ending with last element
// of pre_xor.
int query(TrieNode *root, int pre_xor)
{
    TrieNode *temp = root;
    for (int i=INT_SIZE-1; i>=0; i--)
    {
        // Find current bit in given prefix
        bool val = pre_xor & (1<<i);
 
        // Traverse Trie, first look for a
        // prefix that has opposite bit
        if (temp->arr[1-val]!=NULL)
            temp = temp->arr[1-val];
 
        // If there is no prefix with opposite
        // bit, then look for same bit.
        else if (temp->arr[val] != NULL)
            temp = temp->arr[val];
    }
    return pre_xor^(temp->value);
}
 
// Returns maximum XOR value of a subarray in arr[0..n-1]
int maxSubarrayXOR(int arr[], int n)
{
    // Create a Trie and insert 0 into it
    TrieNode *root = newNode();
    insert(root, 0);
 
    // Initialize answer and xor of current prefix
    int result = INT_MIN, pre_xor =0;
 
    // Traverse all input array element
    for (int i=0; i<n; i++)
    {
        // update current prefix xor and insert it into Trie
        pre_xor = pre_xor^arr[i];
        insert(root, pre_xor);
 
        // Query for current prefix xor in Trie and update
        // result if required
        result = max(result, query(root, pre_xor));
    }
    return result;
}
 
// Driver program to test above functions
int main()
{
    int arr[] = {8, 1, 2, 12};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << "Max subarray XOR is " << maxSubarrayXOR(arr, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ code to demonstrate operations of Binary Index Tree
#include <iostream>
 
using namespace std;
 
/*         n --> No. of elements present in input array.
    BITree[0..n] --> Array that represents Binary Indexed Tree.
    arr[0..n-1] --> Input array for which prefix sum is evaluated. */
 
// Returns sum of arr[0..index]. This function assumes
// that the array is preprocessed and partial sums of
// array elements are stored in BITree[].
int getSum(int BITree[], int index)
{
    int sum = 0; // Initialize result
 
    // index in BITree[] is 1 more than the index in arr[]
    index = index + 1;
 
    // Traverse ancestors of BITree[index]
    while (index>0)
    {
        // Add current element of BITree to sum
        sum += BITree[index];
 
        // Move index to parent node in getSum View
        index -= index & (-index);
    }
    return sum;
}
 
// Updates a node in Binary Index Tree (BITree) at given index
// in BITree. The given value 'val' is added to BITree[i] and
// all of its ancestors in tree.
void updateBIT(int BITree[], int n, int index, int val)
{
    // index in BITree[] is 1 more than the index in arr[]
    index = index + 1;
 
    // Traverse all ancestors and add 'val'
    while (index <= n)
    {
    // Add 'val' to current node of BI Tree
    BITree[index] += val;
 
    // Update index to that of parent in update View
    index += index & (-index);
    }
}
 
// Constructs and returns a Binary Indexed Tree for given
// array of size n.
int *constructBITree(int arr[], int n)
{
    // Create and initialize BITree[] as 0
    int *BITree = new int[n+1];
    for (int i=1; i<=n; i++)
        BITree[i] = 0;
 
    // Store the actual values in BITree[] using update()
    for (int i=0; i<n; i++)
        updateBIT(BITree, n, i, arr[i]);
 
    // Uncomment below lines to see contents of BITree[]
    //for (int i=1; i<=n; i++)
    //     cout << BITree[i] << " ";
 
    return BITree;
}
 
 
// Driver program to test above functions
int main()
{
    int freq[] = {2, 1, 1, 3, 2, 3, 4, 5, 6, 7, 8, 9};
    int n = sizeof(freq)/sizeof(freq[0]);
    int *BITree = constructBITree(freq, n);
    cout << "Sum of elements in arr[0..5] is "
        << getSum(BITree, 5);
 
    // Let use test the update operation
    freq[3] += 6;
    updateBIT(BITree, n, 3, 6); //Update BIT for above change in arr[]
 
    cout << "\nSum of elements in arr[0..5] after update is "
        << getSum(BITree, 5);
 
    return 0;
}

Time complexity: nlogn
Space complexity: linear
// A Simple C++ O(n^3)  program to count inversions of size 3
#include<bits/stdc++.h>
using namespace std;
 
// Returns counts of inversions of size three
int getInvCount(int arr[],int n)
{
    int invcount = 0;  // Initialize result
 
    for (int i=0; i<n-2; i++)
    {
        for (int j=i+1; j<n-1; j++)
        {
            if (arr[i]>arr[j])
            {
                for (int k=j+1; k<n; k++)
                {
                    if (arr[j]>arr[k])
                        invcount++;
                }
            }
        }
    }
    return invcount;
}
 
// Driver program to test above function
int main()
{
    int arr[] = {8, 4, 2, 1};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << "Inversion Count : " << getInvCount(arr, n);
    return 0;
}

Time complexity: cubic
Space complexity: constant
// A O(n^2) C++  program to count inversions of size 3
#include<bits/stdc++.h>
using namespace std;
 
// Returns count of inversions of size 3
int getInvCount(int arr[], int n)
{
    int invcount = 0;  // Initialize result
 
    for (int i=1; i<n-1; i++)
    {
        // Count all smaller elements on right of arr[i]
        int small = 0;
        for (int j=i+1; j<n; j++)
            if (arr[i] > arr[j])
                small++;
 
        // Count all greater elements on left of arr[i]
        int great = 0;
        for (int j=i-1; j>=0; j--)
            if (arr[i] < arr[j])
                great++;
 
        // Update inversion count by adding all inversions
        // that have arr[i] as middle of three elements
        invcount += great*small;
    }
 
    return invcount;
}
 
// Driver program to test above function
int main()
{
    int arr[] = {8, 4, 2, 1};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << "Inversion Count : " << getInvCount(arr, n);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// Naive algorithm for building suffix array of a given text
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
  
// Structure to store information of a suffix
struct suffix
{
    int index;
    char *suff;
};
  
// A comparison function used by sort() to compare two suffixes
int cmp(struct suffix a, struct suffix b)
{
    return strcmp(a.suff, b.suff) < 0? 1 : 0;
}
  
// This is the main function that takes a string 'txt' of size n as an
// argument, builds and return the suffix array for the given string
int *buildSuffixArray(char *txt, int n)
{
    // A structure to store suffixes and their indexes
    struct suffix suffixes[n];
  
    // Store suffixes and their indexes in an array of structures.
    // The structure is needed to sort the suffixes alphabetically
    // and maintain their old indexes while sorting
    for (int i = 0; i < n; i++)
    {
        suffixes[i].index = i;
        suffixes[i].suff = (txt+i);
    }
  
    // Sort the suffixes using the comparison function
    // defined above.
    sort(suffixes, suffixes+n, cmp);
  
    // Store indexes of all sorted suffixes in the suffix array
    int *suffixArr = new int[n];
    for (int i = 0; i < n; i++)
        suffixArr[i] = suffixes[i].index;
  
    // Return the suffix array
    return  suffixArr;
}
  
// A utility function to print an array of given size
void printArr(int arr[], int n)
{
    for(int i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << endl;
}
  
// Driver program to test above functions
int main()
{
    char txt[] = "banana";
    int n = strlen(txt);
    int *suffixArr = buildSuffixArray(txt,  n);
    cout << "Following is suffix array for " << txt << endl;
    printArr(suffixArr, n);
    return 0;
}

Time complexity: nlogn
Space complexity: linear