Banana-Leopard/TASTY huggingface dataset:
// C++ Program to Find the top three repeated numbers
#include <bits/stdc++.h>
using namespace std;
 
/* Function to print top three repeated numbers */
void top3Repeated(int arr[], int n)
{
    // There should be atleast two elements
    if (n < 3) {
        cout << "Invalid Input";
        return;
    }
 
    // Count Frequency of each element
    unordered_map<int, int> fre;
    for (int i = 0; i < n; i++)
        fre[arr[i]]++;
 
    // Initialize first value of each variable
    // of Pair type is INT_MIN
    pair<int, int> x, y, z;
    x.first = y.first = z.first = INT_MIN;
 
    for (auto curr : fre) {
 
        // If frequency of current element
        // is not zero and greater than
        // frequency of first largest element
        if (curr.second > x.first) {
 
            // Update second and third largest
            z = y;
            y = x;
 
            // Modify values of x Number
            x.first = curr.second;
            x.second = curr.first;
        }
 
        // If frequency of current element is
        // not zero and frequency of current
        // element is less than frequency of
        // first largest element, but greater
        // than y element
        else if (curr.second > y.first) {
 
            // Modify values of third largest
            z = y;
 
            // Modify values of second largest
            y.first = curr.second;
            y.second = curr.first;
        }
 
        // If frequency of current element
        // is not zero and frequency of
        // current element is less than
        // frequency of first element and
        // second largest, but greater than
        // third largest.
        else if (curr.second > z.first) {
 
            // Modify values of z Number
            z.first = curr.second;
            z.second = curr.first;
        }
    }
 
    cout << "Three largest elements are " << x.second << " "
         << y.second << " " << z.second;
}
 
// Driver's Code
int main()
{
    int arr[]
        = { 3, 4, 2, 3, 16, 3, 15, 16, 15, 15, 16, 2, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
    top3Repeated(arr, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// A simple C++ program to group multiple occurrences of individual
// array elements
#include<bits/stdc++.h>
using namespace std;
 
// A simple method to group all occurrences of individual elements
void groupElements(int arr[], int n)
{
    // Initialize all elements as not visited
    bool *visited = new bool[n];
    for (int i=0; i<n; i++)
        visited[i] = false;
 
    // Traverse all elements
    for (int i=0; i<n; i++)
    {
        // Check if this is first occurrence
        if (!visited[i])
        {
            // If yes, print it and all subsequent occurrences
            cout << arr[i] << " ";
            for (int j=i+1; j<n; j++)
            {
                if (arr[i] == arr[j])
                {
                    cout << arr[i] << " ";
                    visited[j] = true;
                }
            }
        }
    }
 
    delete [] visited; 
}
 
/* Driver program to test above function */
int main()
{
    int arr[] = {4, 6, 9, 2, 3, 4, 9, 6, 10, 4};
    int n = sizeof(arr)/sizeof(arr[0]);
    groupElements(arr, n);
    return 0;
}

Time complexity: quadratic
Space complexity: linear
#include<bits/stdc++.h>
using namespace std;
 
// C++ program to group multiple
// occurrences of individual array elements
 
// A hashing based method to group
// all occurrences of individual elements
void orderedGroup(vector<int>&arr){
     
    // Creates an empty hashmap
    map<int,int>hM;
 
    // Traverse the array elements, and store
    // count for every element in HashMap
    for(int i=0;i<arr.size();i++){
         
        // Increment count of elements
        // in HashMap
        hM[arr[i]]++;
    }
         
    // Traverse array again
    for(int i=0;i<arr.size();i++){
         
        // Check if this is first occurrence
        int count = (hM.find(arr[i]) == hM.end())? 0 : hM[arr[i]];   
        if(hM.find(arr[i]) != hM.end()){
             
            // If yes, then print
            // the element 'count' times
            for(int j=0;j<count;j++){
                cout<<arr[i]<<" ";
            }
                 
            // And remove the element from HashMap.
            hM.erase(arr[i]);
        }
    }
}
     
// Driver Code
int main(){
  
vector<int>arr = {10, 5, 3, 10, 10, 4, 1, 3};
orderedGroup(arr);
   
}
 
// This code is contributed by shinjanpatra

Time complexity: linear
Space complexity: linear
// A Simple C++ program to check if two sets are disjoint
#include<bits/stdc++.h>
using namespace std;
 
// Returns true if set1[] and set2[] are disjoint, else false
bool areDisjoint(int set1[], int set2[], int m, int n)
{
    // Take every element of set1[] and search it in set2
    for (int i=0; i<m; i++)
      for (int j=0; j<n; j++)
         if (set1[i] == set2[j])
            return false;
 
    // If no element of set1 is present in set2
    return true;
}
 
// Driver program to test above function
int main()
{
    int set1[] = {12, 34, 11, 9, 3};
    int set2[] = {7, 2, 1, 5};
    int m = sizeof(set1)/sizeof(set1[0]);
    int n = sizeof(set2)/sizeof(set2[0]);
    areDisjoint(set1, set2, m, n)? cout << "Yes" : cout << " No";
    return 0;
}

Time complexity: quadratic
Space complexity: constant
/* C++ program to check if two sets are distinct or not */
#include<bits/stdc++.h>
using namespace std;
 
// This function prints all distinct elements
bool areDisjoint(int set1[], int set2[], int n1, int n2)
{
    // Creates an empty hashset
    set<int> myset;
 
    // Traverse the first set and store its elements in hash
    for (int i = 0; i < n1; i++)
        myset.insert(set1[i]);
 
    // Traverse the second set and check if any element of it
    // is already in hash or not.
    for (int i = 0; i < n2; i++)
        if (myset.find(set2[i]) != myset.end())
            return false;
 
    return true;
}
 
// Driver method to test above method
int main()
{
    int set1[] = {10, 5, 3, 4, 6};
    int set2[] = {8, 7, 9, 3};
 
    int n1 = sizeof(set1) / sizeof(set1[0]);
    int n2 = sizeof(set2) / sizeof(set2[0]);
    if (areDisjoint(set1, set2, n1, n2))
        cout << "Yes";
    else
        cout << "No";
}
//This article is contributed by Chhavi

Time complexity: linear
Space complexity: linear
// CPP program to find Non-overlapping sum
#include <bits/stdc++.h>
using namespace std;
 
 
// function for calculating
// Non-overlapping sum of two array
int findSum(int A[], int B[], int n)
{
    // Insert elements of both arrays
    unordered_map<int, int> hash;   
    for (int i = 0; i < n; i++) {
        hash[A[i]]++;
        hash[B[i]]++;
    }
 
    // calculate non-overlapped sum
    int sum = 0;
    for (auto x: hash)
        if (x.second == 1)
            sum += x.first;
     
    return sum;
}
 
// driver code
int main()
{
    int A[] = { 5, 4, 9, 2, 3 };
    int B[] = { 2, 8, 7, 6, 3 };
     
    // size of array
    int n = sizeof(A) / sizeof(A[0]);
 
    // function call
    cout << findSum(A, B, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ simple program to
// find elements which are
// not present in second array
#include<bits/stdc++.h>
using namespace std;
 
// Function for finding
// elements which are there
// in a[]  but not in b[].
void findMissing(int a[], int b[],
                 int n, int m)
{
    for (int i = 0; i < n; i++)
    {
        int j;
        for (j = 0; j < m; j++)
            if (a[i] == b[j])
                break;
 
        if (j == m)
            cout << a[i] << " ";
    }
}
 
// Driver code
int main()
{
    int a[] = { 1, 2, 6, 3, 4, 5 };
    int b[] = { 2, 4, 3, 1, 0 };
    int n = sizeof(a) / sizeof(a[0]);
    int m = sizeof(b) / sizeof(b[1]);
    findMissing(a, b, n, m);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ efficient program to
// find elements which are not
// present in second array
#include<bits/stdc++.h>
using namespace std;
 
// Function for finding
// elements which are there
// in a[] but not in b[].
void findMissing(int a[], int b[],
                int n, int m)
{
    // Store all elements of
    // second array in a hash table
    unordered_set <int> s;
    for (int i = 0; i < m; i++)
        s.insert(b[i]);
 
    // Print all elements of
    // first array that are not
    // present in hash table
    for (int i = 0; i < n; i++)
        if (s.find(a[i]) == s.end())
            cout << a[i] << " ";
}
 
// Driver code
int main()
{
    int a[] = { 1, 2, 6, 3, 4, 5 };
    int b[] = { 2, 4, 3, 1, 0 };
    int n = sizeof(a) / sizeof(a[0]);
    int m = sizeof(b) / sizeof(b[1]);
    findMissing(a, b, n, m);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find given two array
// are equal or not
#include <bits/stdc++.h>
using namespace std;
 
// Returns true if arr1[0..n-1] and arr2[0..m-1]
// contain same elements.
bool areEqual(int arr1[], int arr2[], int N, int M)
{
    // If lengths of array are not equal means
    // array are not equal
    if (N != M)
        return false;
 
    // Sort both arrays
    sort(arr1, arr1 + N);
    sort(arr2, arr2 + M);
 
    // Linearly compare elements
    for (int i = 0; i < N; i++)
        if (arr1[i] != arr2[i])
            return false;
 
    // If all elements were same.
    return true;
}
 
// Driver's Code
int main()
{
    int arr1[] = { 3, 5, 2, 5, 2 };
    int arr2[] = { 2, 3, 5, 5, 2 };
    int N = sizeof(arr1) / sizeof(int);
    int M = sizeof(arr2) / sizeof(int);
 
    // Function call
    if (areEqual(arr1, arr2, N, M))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

Time complexity: nlogn
Space complexity: constant
// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Returns true if arr1[0..N-1] and arr2[0..M-1]
// contain same elements.
bool areEqual(int arr1[], int arr2[], int N, int M)
{
    // If lengths of arrays are not equal
    if (N != M)
        return false;
 
    // Store arr1[] elements and their counts in
    // hash map
    unordered_map<int, int> mp;
    for (int i = 0; i < N; i++)
        mp[arr1[i]]++;
 
    // Traverse arr2[] elements and check if all
    // elements of arr2[] are present same number
    // of times or not.
    for (int i = 0; i < N; i++) {
        // If there is an element in arr2[], but
        // not in arr1[]
        if (mp.find(arr2[i]) == mp.end())
            return false;
 
        // If an element of arr2[] appears more
        // times than it appears in arr1[]
        if (mp[arr2[i]] == 0)
            return false;
        // decrease the count of arr2 elements in the
        // unordered map
        mp[arr2[i]]--;
    }
 
    return true;
}
 
// Driver's Code
int main()
{
    int arr1[] = { 3, 5, 2, 5, 2 };
    int arr2[] = { 2, 3, 5, 5, 2 };
    int N = sizeof(arr1) / sizeof(int);
    int M = sizeof(arr2) / sizeof(int);
 
    // Function call
    if (areEqual(arr1, arr2, N, M))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ code to find maximum shortest distance
// from endpoints
#include <bits/stdc++.h>
using namespace std;
 
// function to find maximum shortest distance
int find_maximum(int a[], int n, int k)
{  
    // stores the shortest distance of every
    // element in original array.
    unordered_map<int, int> b;
     
    for (int i = 0; i < n; i++) {
        int x = a[i];
         
        // shortest distance from ends
        int d = min(1 + i, n - i);
        if (b.find(x) == b.end())
            b[x] = d; 
 
        else
 
            /* if duplicates are found, b[x]
            is replaced with minimum of the
            previous and current position's
            shortest distance*/
            b[x] = min(d, b[x]);
    }
     
    int ans = INT_MAX;
    for (int i = 0; i < n; i++) {
        int x = a[i];
         
        // similar elements ignore them
        // cause we need distinct elements   
        if (x != k - x && b.find(k - x) != b.end())        
            ans = min(max(b[x], b[k - x]), ans);       
    }
    return ans;
}
 
// driver code
int main()
{
    int a[] = { 3, 5, 8, 6, 7 };
    int K = 11;
    int n = sizeof(a) / sizeof(a[0]);
    cout << find_maximum(a, n, K) << endl;
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find the k-th missing element
// in a given sequence
#include <bits/stdc++.h>
using namespace std;
 
// Returns k-th missing element. It returns -1 if
// no k is more than number of missing elements.
int find(int a[], int b[], int k, int n1, int n2)
{
    // Insert all elements of givens sequence b[].
    unordered_set<int> s;
    for (int i = 0; i < n2; i++)
        s.insert(b[i]);
 
    // Traverse through increasing sequence and
    // keep track of count of missing numbers.
    int missing = 0;
    for (int i = 0; i < n1; i++) {
        if (s.find(a[i]) == s.end())
            missing++;
        if (missing == k)
            return a[i];
    }
 
    return -1;
}
 
// driver program to test the above function
int main()
{
    int a[] = { 0, 2, 4, 6, 8, 10, 12, 14, 15 };
    int b[] = { 4, 10, 6, 8, 12 };
    int n1 = sizeof(a) / sizeof(a[0]);
    int n2 = sizeof(b) / sizeof(b[0]);
 
    int k = 3;
    cout << find(a, b, k, n1, n2);
    return 0;
}

Time complexity: linear
Space complexity: quadratic
// C++ program to find a pair with product
// in given array.
#include<bits/stdc++.h>
using namespace std;
 
// Function to find greatest number that us
int findGreatest( int arr[] , int n)
{
    int result = -1;
    for (int i = 0; i < n ; i++)
        for (int j = 0; j < n-1; j++)
            for (int k = j+1 ; k < n  ; k++)
                if (arr[j] * arr[k] == arr[i])
                    result = max(result, arr[i]);
    return result;
}
 
// Driver code
int main()
{
    // Your C++ Code
    int arr[] = {10, 3, 5, 30, 35};
    int n = sizeof(arr)/sizeof(arr[0]);
 
    cout << findGreatest(arr, n);
 
    return 0;
}

Time complexity: cubic
Space complexity: constant
// C++ program to find the largest product number
#include <bits/stdc++.h>
using namespace std;
 
// Function to find greatest number
int findGreatest(int arr[], int n)
{
    // Store occurrences of all elements in hash
    // array
    unordered_map<int, int> m;
    for (int i = 0; i < n; i++)
        m[arr[i]]++;
 
    // Sort the array and traverse all elements from
    // end.
    sort(arr, arr + n);
 
    for (int i = n - 1; i > 1; i--) {
        // For every element, check if there is another
        // element which divides it.
        for (int j = 0; j < i && arr[j] <= sqrt(arr[i]);
             j++) {
            if (arr[i] % arr[j] == 0) {
                int result = arr[i] / arr[j];
 
                // Check if the result value exists in array
                // or not if yes the return arr[i]
                if (result != arr[j] && result!=arr[i] && m[result] > 0)
                    return arr[i];
 
                // To handle the case like arr[i] = 4 and
                // arr[j] = 2
                else if (result == arr[j] && m[result] > 1)
                    return arr[i];
            }
        }
    }
    return -1;
}
 
// Drivers code
int main()
{
    int arr[] = {10, 3, 5, 30, 35};
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << findGreatest(arr, n);
    return 0;
}

Time complexity: nlogn
Space complexity: linear
// A sorting based solution to find the
// minimum number of subsets of a set
// such that every subset contains distinct
// elements.
#include <bits/stdc++.h>
using namespace std;
 
// Function to count subsets such that all
// subsets have distinct elements.
int subset(int ar[], int n)
{
    // Take input and initialize res = 0
    int res = 0;
 
    // Sort the array
    sort(ar, ar + n);
 
    // Traverse the input array and
    // find maximum frequency
    for (int i = 0; i < n; i++) {
        int count = 1;
 
        // For each number find its repetition / frequency
        for (; i < n - 1; i++) {
            if (ar[i] == ar[i + 1])
                count++;
            else
                break;
        }
 
        // Update res
        res = max(res, count);
    }
 
    return res;
}
 
// Driver code
int main()
{
    int arr[] = { 5, 6, 9, 3, 4, 3, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << subset(arr, n);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// A hashing based solution to find the
// minimum number of subsets of a set
// such that every subset contains distinct
// elements.
#include <bits/stdc++.h>
using namespace std;
 
// Function to count subsets such that all
// subsets have distinct elements.
int subset(int arr[], int n)
{  
    // Traverse the input array and
    // store frequencies of elements
    unordered_map<int, int> mp;   
    for (int i = 0; i < n; i++)
        mp[arr[i]]++;
     
    // Find the maximum value in map.
    int res = 0;
    for (auto x : mp)
       res = max(res, x.second);
 
    return res;
}
 
// Driver code
int main()
{
    int arr[] = { 5, 6, 9, 3, 4, 3, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << subset(arr, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// CPP program to find minimum element
// to remove so no common element
// exist in both array
#include <bits/stdc++.h>
using namespace std;
 
// To find no elements to remove
// so no common element exist
int minRemove(int a[], int b[], int n, int m)
{
    // To store count of array element
    unordered_map<int, int> countA, countB;
 
    // Count elements of a
    for (int i = 0; i < n; i++)
        countA[a[i]]++;
 
    // Count elements of b
    for (int i = 0; i < m; i++)
        countB[b[i]]++;
 
    // Traverse through all common element, and
    // pick minimum occurrence from two arrays
    int res = 0;
    for (auto x : countA)
        if (countB.find(x.first) != countB.end())
            res += min(x.second, countB[x.first]);
 
    // To return count of minimum elements
    return res;
}
 
// Driver program to test minRemove()
int main()
{
    int a[] = { 1, 2, 3, 4 };
    int b[] = { 2, 3, 4, 5, 8 };
    int n = sizeof(a) / sizeof(a[0]);
    int m = sizeof(b) / sizeof(b[0]);
 
    cout << minRemove(a, b, n, m);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ implementation to count items common to both
// the lists but with different prices
#include <bits/stdc++.h>
 
using namespace std;
 
// details of an item
struct item
{
    string name;
    int price;
};
 
// function to count items common to both
// the lists but with different prices
int countItems(item list1[], int m,
               item list2[], int n)
{
    int count = 0;
     
    // for each item of 'list1' check if it is in 'list2'
    // but with a different price
    for (int i = 0; i < m; i++)   
        for (int j = 0; j < n; j++)
 
            if ((list1[i].name.compare(list2[j].name) == 0) &&
                 (list1[i].price != list2[j].price))
                count++;       
     
    // required count of items
    return count;
}
 
// Driver program to test above
int main()
{
    item list1[] = {{"apple", 60}, {"bread", 20},
                    {"wheat", 50}, {"oil", 30}};
    item list2[] = {{"milk", 20}, {"bread", 15},
                   {"wheat", 40}, {"apple", 60}};
     
    int m = sizeof(list1) / sizeof(list1[0]);
    int n = sizeof(list2) / sizeof(list2[0]);   
     
    cout << "Count = "   
         << countItems(list1, m, list2, n);
          
    return 0;    
}

Time complexity: quadratic
Space complexity: constant
// C++ implementation to count
// items common to both the lists
// but with different prices
#include <bits/stdc++.h>
using namespace std;
 
// Details of an item
struct item
{
  string name;
  int price;
};
 
// comparator function
// used for sorting
bool compare(struct item a,
             struct item b)
{
  return (a.name.compare
         (b.name) <= 0);       
}
 
// Function to search 'str'
// in 'list2[]'. If it exists then
// price associated with 'str'
// in 'list2[]' is being returned
// else -1 is returned. Here binary
// search technique is being applied
// for searching
int binary_search(item list2[], int low,
                  int high, string str)
{
  while (low <= high)
  {
    int mid = (low + high) / 2;
 
    // if true the item 'str'
    // is in 'list2'        
    if (list2[mid].name.compare(str) == 0)
      return list2[mid].price;
    else if (list2[mid].name.compare(str) < 0)   
      low = mid + 1;
    else
      high = mid - 1;   
  }
 
  // item 'str' is not in 'list2'        
  return -1;
}
 
// Function to count items common to both
// the lists but with different prices
int countItems(item list1[], int m,
               item list2[], int n)
{
  // sort 'list2' in alphabetical
  // order of items name
  sort(list2, list2 + n,
       compare);
 
  // initial count
  int count = 0;
 
  for (int i = 0; i < m; i++)
  {
    // get the price of item 'list1[i]'
    // from 'list2' if item in not
    // present in second list then
    // -1 is being obtained
    int r = binary_search(list2, 0, n - 1,
                          list1[i].name);
 
    // if item is present in list2
    // with a different price
    if ((r != -1) &&
        (r != list1[i].price))
      count++;
  }
 
  // Required count of items
  return count;
}
 
// Driver code
int main()
{
  item list1[] = {{"apple", 60},
                  {"bread", 20},
                  {"wheat", 50},
                  {"oil", 30}};
  item list2[] = {{"milk", 20},
                  {"bread", 15},
                  {"wheat", 40},
                  {"apple", 60}};
 
  int m = sizeof(list1) /
          sizeof(list1[0]);
  int n = sizeof(list2) /
          sizeof(list2[0]);   
 
  cout << "Count = " <<
           countItems(list1, m,
                      list2, n);
  return 0;    
}

Time complexity: nlogn
Space complexity: constant
// C++ implementation to count items common to both
// the lists but with different prices
#include <bits/stdc++.h>
 
using namespace std;
 
// details of an item
struct item
{
    string name;
    int price;
};
 
// function to count items common to both
// the lists but with different prices
int countItems(item list1[], int m,
               item list2[], int n)
{
    // 'um' implemented as hash table that contains
    // item name as the key and price as the value
    // associated with the key
    unordered_map<string, int> um;
    int count = 0;
     
    // insert elements of 'list1' in 'um'
    for (int i = 0; i < m; i++)
        um[list1[i].name] = list1[i].price;
     
    // for each element of 'list2' check if it is
    // present in 'um' with a different price
    // value
    for (int i = 0; i < n; i++)   
        if ((um.find(list2[i].name) != um.end()) &&
            (um[list2[i].name] != list2[i].price))
            count++;
     
    // required count of items       
    return count;       
}
 
// Driver program to test above
int main()
{
    item list1[] = {{"apple", 60}, {"bread", 20},
                    {"wheat", 50}, {"oil", 30}};
    item list2[] = {{"milk", 20}, {"bread", 15},
                    {"wheat", 40}, {"apple", 60}};
     
    int m = sizeof(list1) / sizeof(list1[0]);
    int n = sizeof(list2) / sizeof(list2[0]);   
     
    cout << "Count = "   
         << countItems(list1, m, list2, n);
          
    return 0;    
}

Time complexity: linear
Space complexity: linear