Banana-Leopard/TASTY huggingface dataset:
// C++ program Count total number of sub-arrays
// having total distinct elements same as that
// original array.
#include<bits/stdc++.h>
using namespace std;
 
// Function to calculate distinct sub-array
int countDistictSubarray(int arr[], int n)
{
    // Count distinct elements in whole array
    unordered_map<int, int>  vis;
    for (int i = 0; i < n; ++i)
        vis[arr[i]] = 1;
    int k = vis.size();
 
    // Reset the container by removing all elements
    vis.clear();
 
    // Use sliding window concept to find
    // count of subarrays having k distinct
    // elements.
    int ans = 0, right = 0, window = 0;
    for (int left = 0; left < n; ++left)
    {
        while (right < n && window < k)
        {
            ++vis[ arr[right] ];
 
            if (vis[ arr[right] ] == 1)
                ++window;
 
            ++right;
        }
 
        // If window size equals to array distinct
        // element size, then update answer
        if (window == k)
            ans += (n - right + 1);
 
        // Decrease the frequency of previous element
        // for next sliding window
        --vis[ arr[left] ];
 
        // If frequency is zero then decrease the
        // window size
        if (vis[ arr[left] ] == 0)
                --window;
    }
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = {2, 1, 3, 2, 3};
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << countDistictSubarray(arr, n) <<"n";
    return 0;
}

Time complexity: linear
Space complexity: linear
// Make a set of maximum elements from two
// arrays A[] and B[]
#include <bits/stdc++.h>
using namespace std;
 
void maximizeTheFirstArray(int A[], int B[],
                                    int n)
{
    // Create copies of A[] and B[] and sort
    // the copies in descending order.
    vector<int> temp1(A, A+n);
    vector<int> temp2(B, B+n);
    sort(temp1.begin(), temp1.end(), greater<int>());
    sort(temp2.begin(), temp2.end(), greater<int>());
 
    // Put maximum n distinct elements of
    // both sorted arrays in a map.
    unordered_map<int, int> m;
    int i = 0, j = 0;
    while (m.size() < n)
    {
         if (temp1[i] >= temp2[j])
         {
            m[temp1[i]]++;
            i++;
         }
         else
         {
            m[temp2[j]]++;
            j++;
         }
    }
 
    // Copy elements of A[] to that
    // are present in hash m.
    vector<int> res;
    for (int i = 0; i < n; i++)
        if (m.find(A[i]) != m.end())
           res.push_back(A[i]);
 
    // Copy elements of B[] to that
    // are present in hash m. This time
    // we also check if the element did
    // not appear twice.
    for (int i = 0; i < n; i++)
        if (m.find(B[i]) != m.end() &&
            m[B[i]] == 1)
           res.push_back(B[i]);
 
    // print result
    for (int i = 0; i < n; i++)
        cout << res[i] << " ";
}
 
// driver program
int main()
{
    int A[] = { 9, 7, 2, 3, 6 };
    int B[] = { 7, 4, 8, 0, 1 };
    int n = sizeof(A) / sizeof(A[0]);
    maximizeTheFirstArray(A, B, n);
    return 0;
}

Time complexity: nlogn
Space complexity: linear
// C++ implementation to find the maximum number
// of chocolates to be distributed equally among
// k students
#include <bits/stdc++.h>
using namespace std;
 
// function to find the maximum number of chocolates
// to be distributed equally among k students
int maxNumOfChocolates(int arr[], int n, int k)
{
    // unordered_map 'um' implemented as
    // hash table
    unordered_map<int, int> um;
 
    // 'sum[]' to store cumulative sum, where
    // sum[i] = sum(arr[0]+..arr[i])
    int sum[n], curr_rem;
 
    // to store sum of sub-array having maximum sum
    int maxSum = 0;
 
    // building up 'sum[]'
    sum[0] = arr[0];
    for (int i = 1; i < n; i++)
        sum[i] = sum[i - 1] + arr[i];
 
    // traversing 'sum[]'
    for (int i = 0; i < n; i++) {
 
        // finding current remainder
        curr_rem = sum[i] % k;
 
        // if true then sum(0..i) is divisible
        // by k
        if (curr_rem == 0) {
            // update 'maxSum'
            if (maxSum < sum[i])
                maxSum = sum[i];
        }
 
        // if value 'curr_rem' not present in 'um'
        // then store it in 'um' with index of its
        // first occurrence
        else if (um.find(curr_rem) == um.end())
            um[curr_rem] = i;
 
        else
            // if true, then update 'max'
            if (maxSum < (sum[i] - sum[um[curr_rem]]))
            maxSum = sum[i] - sum[um[curr_rem]];
    }
 
    // required maximum number of chocolates to be
    // distributed equally among 'k' students
    return (maxSum / k);
}
 
// Driver program to test above
int main()
{
    int arr[] = { 2, 7, 6, 1, 4, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 3;
    cout << "Maximum number of chocolates: "
         << maxNumOfChocolates(arr, n, k);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to print n-th number in Recaman's
// sequence
#include <bits/stdc++.h>
using namespace std;
 
// Prints first n terms of Recaman sequence
int recaman(int n)
{
    // Create an array to store terms
    int arr[n];
 
    // First term of the sequence is always 0
    arr[0] = 0;
    printf("%d, ", arr[0]);
 
    // Fill remaining terms using recursive
    // formula.
    for (int i=1; i< n; i++)
    {
        int curr = arr[i-1] - i;
        int j;
        for (j = 0; j < i; j++)
        {
            // If arr[i-1] - i is negative or
            // already exists.
            if ((arr[j] == curr) || curr < 0)
            {
                curr = arr[i-1] + i;
                break;
            }
        }
 
        arr[i] = curr;
        printf("%d, ", arr[i]);
    }
}
 
// Driver code
int main()
{
    int n = 17;
    recaman(n);
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// C++ program to print n-th number in Recaman's
// sequence
#include <bits/stdc++.h>
using namespace std;
 
// Prints first n terms of Recaman sequence
void recaman(int n)
{
    if (n <= 0)
      return;
 
    // Print first term and store it in a hash
    printf("%d, ", 0);
    unordered_set<int> s;
    s.insert(0);
 
    // Print remaining terms using recursive
    // formula.
    int prev = 0;
    for (int i=1; i< n; i++)
    {
        int curr = prev - i;
 
        // If arr[i-1] - i is negative or
        // already exists.
        if (curr < 0 || s.find(curr) != s.end())
           curr = prev + i;
 
        s.insert(curr);
 
        printf("%d, ", curr);
        prev = curr;
    }
}
 
// Driver code
int main()
{
    int n = 17;
    recaman(n);
    return 0;
}

Time complexity: linear
Space complexity: linear
#include <bits/stdc++.h>
using namespace std;
void findFibSubset(int arr[], int n)
{
    for (int i = 0; i < n; i++) {
        int fact1 = 5 * pow(arr[i], 2) + 4;
        int fact2 = 5 * pow(arr[i], 2) - 4;
        if ((int)pow((int)pow(fact1, 0.5), 2) == fact1
            || (int)pow((int)pow(fact2, 0.5), 2) == fact2)
            cout << arr[i] << " ";
    }
}
 
int main()
{
    int arr[] = { 4, 2, 8, 5, 20, 1, 40, 13, 23 };
    int n = 9;
    findFibSubset(arr, n);
}
 
// This code is contributed by garg28harsh.

Time complexity: linear
Space complexity: constant
// C++ program to find largest Fibonacci subset
#include<bits/stdc++.h>
using namespace std;
 
// Prints largest subset of an array whose
// all elements are fibonacci numbers
void findFibSubset(int arr[], int n)
{
    // Find maximum element in arr[]
    int max = *std::max_element(arr, arr+n);
 
    // Generate all Fibonacci numbers till
    // max and store them in hash.
    int a = 0, b = 1;
    unordered_set<int> hash;
    hash.insert(a);
    hash.insert(b);
    while (b < max)
    {
        int c = a + b;
        a = b;
        b = c;
        hash.insert(b);
    }
 
    // Npw iterate through all numbers and
    // quickly check for Fibonacci using
    // hash.
    for (int i=0; i<n; i++)
        if (hash.find(arr[i]) != hash.end())
            printf("%d ", arr[i]);
}
 
// Driver code
int main()
{
    int arr[] = {4, 2, 8, 5, 20, 1, 40, 13, 23};
    int n = sizeof(arr)/sizeof(arr[0]);
    findFibSubset(arr, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// A brute force approach based CPP program to
// find if there is a rectangle with 1 as corners.
#include <bits/stdc++.h>
using namespace std;
 
// Returns true if there is a rectangle with
// 1 as corners.
bool isRectangle(const vector<vector<int> >& m)
{
    // finding row and column size
    int rows = m.size();
    if (rows == 0)
        return false;
    int columns = m[0].size();
 
    // scanning the matrix
    for (int y1 = 0; y1 < rows; y1++)
      for (int x1 = 0; x1 < columns; x1++)
 
          // if any index found 1 then try
          // for all rectangles
          if (m[y1][x1] == 1)
            for (int y2 = y1 + 1; y2 < rows; y2++)
              for (int x2 = x1 + 1; x2 < columns; x2++)
                if (m[y1][x2] == 1 && m[y2][x1] == 1 &&
                                       m[y2][x2] == 1)
                  return true;
    return false;
}
 
// Driver code
int main()
{
    vector<vector<int> > mat = { { 1, 0, 0, 1, 0 },
                                 { 0, 0, 1, 0, 1 },
                                 { 0, 0, 0, 1, 0 },
                                 { 1, 0, 1, 0, 1 } };
    if (isRectangle(mat))
        cout << "Yes";
    else
        cout << "No";
}

Time complexity: quadratic
Space complexity: constant
// An efficient approach based CPP program to
// find if there is a rectangle with 1 as
// corners.
#include <bits/stdc++.h>
using namespace std;
 
// Returns true if there is a rectangle with
// 1 as corners.
bool isRectangle(const vector<vector<int> >& matrix)
{
    // finding row and column size
    int rows = matrix.size();
    if (rows == 0)
        return false;
 
    int columns = matrix[0].size();
 
    // map for storing the index of combination of 2 1's
    unordered_map<int, unordered_set<int> > table;
 
    // scanning from top to bottom line by line
    for (int i = 0; i < rows; ++i) {
 
        for (int j = 0; j < columns - 1; ++j) {
            for (int k = j + 1; k < columns; ++k) {
 
                // if found two 1's in a column
                if (matrix[i][j] == 1 && matrix[i][k] == 1) {
 
                    // check if there exists 1's in same
                    // row previously then return true
                    // we don't need to check (j, k) pair
                    // and again (k, j) pair because we always
                    // store pair in ascending order and similarly
                    // check in ascending order, i.e. j always less
                    // than k.
                    if (table.find(j) != table.end()
                        && table[j].find(k) != table[j].end())
                        return true;
 
                    // store the indexes in hashset
                    table[j].insert(k);
                }
            }
        }
    }
    return false;
}
 
// Driver code
int main()
{
    vector<vector<int> > mat = { { 1, 0, 0, 1, 0 },
                                 { 0, 1, 1, 1, 1 },
                                 { 0, 0, 0, 1, 0 },
                                 { 1, 1, 1, 1, 0 } };
    if (isRectangle(mat))
        cout << "Yes";
    else
        cout << "No";
}
// This code is improved by Gautam Agrawal

Time complexity: quadratic
Space complexity: quadratic
// C++ implementation comes from:
// https://github.com/MichaelWehar/FourCornersProblem
// Written by Niteesh Kumar and Michael Wehar
// References:
//   [1] F. Mráz, D. Prusa, and M. Wehar.
//   Two-dimensional Pattern Matching against
//    Basic Picture Languages. CIAA 2019.
//   [2] D. Prusa and M. Wehar. Complexity of
//    Searching for 2 by 2 Submatrices in Boolean
//    Matrices. DLT 2020.
 
#include <bits/stdc++.h>
using namespace std;
 
bool searchForRectangle(int rows, int cols,
                        vector<vector<int>> mat)
{
    // Make sure that matrix is non-trivial
    if (rows < 2 || cols < 2)
    {
        return false;
    }
 
    // Create map
    int num_of_keys;
    map<int, vector<int>> adjsList;
    if (rows >= cols)
    {
        // Row-wise
        num_of_keys = rows;
         
        // Convert each row into vector of col indexes
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                if (mat[i][j])
                {
                    adjsList[i].push_back(j);
                }
            }
        }
    }
     
    else
    {
        // Col-wise
        num_of_keys = cols;
         
        // Convert each col into vector of row indexes
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                if (mat[i][j])
                {
                    adjsList[j].push_back(i);
                }
            }
        }
    }
 
    // Search for a rectangle whose four corners are 1's
    map<pair<int, int>, int> pairs;
    for (int i = 0; i < num_of_keys; i++)
    {
        vector<int> values = adjsList[i];
        int size = values.size();
        for (int j = 0; j < size - 1; j++)
        {
            for (int k = j + 1; k < size; k++)
            {
                pair<int, int> temp
                        = make_pair(values[j],
                                     values[k]);
                if (pairs.find(temp)
                    != pairs.end())
                {
                    return true;
                } else {
                    pairs[temp] = i;
                }
            }
        }
    }
    return false;
}
 
// Driver code
int main()
{
    vector<vector<int> > mat = { { 1, 0, 0, 1, 0 },
                                 { 0, 1, 1, 1, 1 },
                                 { 0, 0, 0, 1, 0 },
                                 { 1, 1, 1, 1, 0 } };
    if (searchForRectangle(4, 5, mat))
        cout << "Yes";
    else
        cout << "No";
}

Time complexity: quadratic
Space complexity: quadratic
// CPP program for finding maximum area possible
// of a rectangle
#include <bits/stdc++.h>
using namespace std;
 
// function for finding max area
int findArea(int arr[], int n)
{
    // sort array in non-increasing order
    sort(arr, arr + n, greater<int>());
 
    // Initialize two sides of rectangle
    int dimension[2] = { 0, 0 };
 
    // traverse through array
    for (int i = 0, j = 0; i < n - 1 && j < 2; i++)
 
        // if any element occurs twice
        // store that as dimension
        if (arr[i] == arr[i + 1])
            dimension[j++] = arr[i++];
 
    // return the product of dimensions
    return (dimension[0] * dimension[1]);
}
 
// driver function
int main()
{
    int arr[] = { 4, 2, 1, 4, 6, 6, 2, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << findArea(arr, n);
    return 0;
}

Time complexity: nlogn
Space complexity: constant
// CPP program for finding maximum area possible
// of a rectangle
#include <bits/stdc++.h>
using namespace std;
 
// function for finding max area
int findArea(int arr[], int n)
{
    unordered_set<int> s;
 
    // traverse through array
    int first = 0, second = 0;
    for (int i = 0; i < n; i++) {
 
        // If this is first occurrence of arr[i],
        // simply insert and continue
        if (s.find(arr[i]) == s.end()) {
            s.insert(arr[i]);
            continue;
        }
 
        // If this is second (or more) occurrence,
        // update first and second maximum values.
        if (arr[i] > first) {
            second = first;
            first = arr[i];
        } else if (arr[i] > second)
            second = arr[i];
    }
 
    // return the product of dimensions
    return (first * second);
}
 
// driver function
int main()
{
    int arr[] = { 4, 2, 1, 4, 6, 6, 2, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << findArea(arr, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ implementation to find length of longest
// strict bitonic subsequence
#include <bits/stdc++.h>
using namespace std;
    
// function to find length of longest
// strict bitonic subsequence
int longLenStrictBitonicSub(int arr[], int n)
{
    // hash table to map the array element with the
    // length of the longest subsequence of which
    // it is a part of and is the last/first element of
    // that subsequence
    unordered_map<int, int> inc, dcr;
     
    // arrays to store the length of increasing and
    // decreasing subsequences which end at them
    // or start from them 
    int len_inc[n], len_dcr[n];
     
    // to store the length of longest strict
    // bitonic subsequence
    int longLen = 0;
     
    // traverse the array elements
    // from left to right
    for (int i=0; i<n; i++)
    {
        // initialize current length
        // for element arr[i] as 0
        int len = 0;
           
        // if 'arr[i]-1' is in 'inc'
        if (inc.find(arr[i]-1) != inc.end())
            len = inc[arr[i]-1];
           
        // update arr[i] subsequence length in 'inc'   
        // and in len_inc[]
        inc[arr[i]] = len_inc[i] = len + 1;
    }
     
    // traverse the array elements
    // from right to left
    for (int i=n-1; i>=0; i--)
    {
        // initialize current length
        // for element arr[i] as 0
        int len = 0;
           
        // if 'arr[i]-1' is in 'dcr'
        if (dcr.find(arr[i]-1) != dcr.end())
            len = dcr[arr[i]-1];
           
        // update arr[i] subsequence length in 'dcr'
        // and in len_dcr[]   
        dcr[arr[i]] = len_dcr[i] = len + 1;
    }
     
    // calculating the length of all the strict
    // bitonic subsequence
    for (int i=0; i<n; i++)
        if (longLen < (len_inc[i] + len_dcr[i] - 1))
            longLen = len_inc[i] + len_dcr[i] - 1;
        
    // required longest length strict
    // bitonic subsequence
    return longLen;       
}
    
// Driver program to test above
int main()
{
    int arr[] = {1, 5, 2, 3, 4, 5, 3, 2};
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Longest length strict bitonic subsequence = "
         << longLenStrictBitonicSub(arr, n);
    return 0;
} 

Time complexity: linear
Space complexity: linear
// C++ program to find last seen element in
// an array.
#include <bits/stdc++.h>
using namespace std;
 
// Returns last seen element in arr[]
int lastSeenElement(int a[], int n)
{
    // Store last occurrence index of
    // every element
    unordered_map<int, int> hash;
    for (int i = 0; i < n; i++)
        hash[a[i]] = i;
 
    // Find an element in hash with minimum
    // index value
    int res_ind = INT_MAX, res;
    for (auto x : hash)
    {
       if (x.second < res_ind)
       {
            res_ind = x.second;
            res = x.first;
       }
    }
 
    return res;
}
 
// driver program
int main()
{
    int a[] = { 2, 1, 2, 2, 4, 1 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << lastSeenElement(a, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// A complete working C++ program to 
// demonstrate all insertion methods
// on Linked List
#include <bits/stdc++.h>
using namespace std;
 
// A linked list node
class Node
{
    public:
    int data;
    Node *next;
};
 
// Given a reference (pointer to pointer)
// to the head of a list and an int, inserts
// a new node on the front of the list.
void push(Node** head_ref, int new_data)
{
   
    // 1. allocate node
    Node* new_node = new Node();
 
    // 2. put in the data
    new_node->data = new_data;
 
    // 3. Make next of new node as head
    new_node->next = (*head_ref);
 
    // 4. move the head to point
    // to the new node
    (*head_ref) = new_node;
}
 
// Given a node prev_node, insert a new 
// node after the given prev_node
void insertAfter(Node* prev_node, int new_data)
{
    // 1. check if the given prev_node
    // is NULL
    if (prev_node == NULL)
    {
        cout<<"The given previous node cannot be NULL";
        return;
    }
 
    // 2. allocate new node
    Node* new_node = new Node();
 
    // 3. put in the data
    new_node->data = new_data;
 
    // 4. Make next of new node
    // as next of prev_node
    new_node->next = prev_node->next;
 
    // 5. move the next of prev_node
    // as new_node
    prev_node->next = new_node;
}
 
// Given a reference (pointer to pointer) 
// to the head of a list and an int,
// appends a new node at the end
void append(Node** head_ref, int new_data)
{
   
    // 1. allocate node
    Node* new_node = new Node();
 
    //used in step 5
    Node *last = *head_ref;
 
    // 2. put in the data
    new_node->data = new_data;
 
    /* 3. This new node is going to be
    the last node, so make next of
    it as NULL*/
    new_node->next = NULL;
 
    /* 4. If the Linked List is empty,
    then make the new node as head */
    if (*head_ref == NULL)
    {
        *head_ref = new_node;
        return;
    }
 
    /* 5. Else traverse till the last node */
    while (last->next != NULL)
    {
        last = last->next;
    }
 
    /* 6. Change the next of last node */
    last->next = new_node;
    return;
}
 
// This function prints contents of
// linked list starting from head
void printList(Node *node)
{
    while (node != NULL)
    {
        cout<<" "<<node->data;
        node = node->next;
    }
}
 
// Driver code
int main()
{
   
    // Start with the empty list
    Node* head = NULL;
     
    // Insert 6. So linked list becomes 6->NULL
    append(&head, 6);
     
    // Insert 7 at the beginning.
    // So linked list becomes 7->6->NULL
    push(&head, 7);
     
    // Insert 1 at the beginning.
    // So linked list becomes 1->7->6->NULL
    push(&head, 1);
     
    // Insert 4 at the end. So
    // linked list becomes 1->7->6->4->NULL
    append(&head, 4);
     
    // Insert 8, after 7. So linked
    // list becomes 1->7->8->6->4->NULL
    insertAfter(head->next, 8);
     
    cout<<"Created Linked list is: ";
    printList(head);
     
    return 0;
}
// This code is contributed by rathbhupendra, arkajyotibasak

Time complexity: linear
Space complexity: constant
// Alternate method to declare the class
// in order to minimize the
// memory allocation work
 
#include <bits/stdc++.h>
using namespace std;
 
class node {
public:
    int data;
    node* next;
 
    // A constructor is called here
    node(int value)
    {
 
        // It automatically assigns the
        // value to the data
        data = value;
 
        // Next pointer is pointed to NULL
        next = NULL;
    }
};
 
// Function to insert an element
// at head position
void insertathead(node*& head, int val)
{
    node* n = new node(val);
    n->next = head;
    head = n;
}
 
// Function to insert a element
// at a specified position
void insertafter(node* head, int key, int val)
{
    node* n = new node(val);
    if (key == head->data) {
        n->next = head->next;
        head->next = n;
        return;
    }
 
    node* temp = head;
    while (temp->data != key) {
        temp = temp->next;
        if (temp == NULL) {
            return;
        }
    }
    n->next = temp->next;
    temp->next = n;
}
 
// Function to insert an
// element at the end
void insertattail(node*& head, int val)
{
    node* n = new node(val);
    if (head == NULL) {
        head = n;
        return;
    }
 
    node* temp = head;
    while (temp->next != NULL) {
        temp = temp->next;
    }
    temp->next = n;
}
 
// Function to print the
// singly linked list
void print(node*& head)
{
    node* temp = head;
 
    while (temp != NULL) {
        cout << temp->data << " -> ";
        temp = temp->next;
    }
    cout << "NULL" << endl;
}
 
// Main function
int main()
{
 
    // Declaring an empty linked list
    node* head = NULL;
 
    insertathead(head, 1);
    insertathead(head, 2);
    cout << "After insertion at head: ";
    print(head);
    cout << endl;
 
    insertattail(head, 4);
    insertattail(head, 5);
    cout << "After insertion at tail: ";
    print(head);
    cout << endl;
 
    insertafter(head, 1, 2);
    insertafter(head, 5, 6);
    cout << "After insertion at a given position: ";
    print(head);
    cout << endl;
 
    return 0;
}
// contributed by divyanshmishra101010

Time complexity: linear
Space complexity: constant