Banana-Leopard/TASTY huggingface dataset:
// C++ implementation to count quadruples from
// four sorted arrays whose sum is equal to a
// given value x
#include <bits/stdc++.h>
 
using namespace std;
 
// function to count all quadruples from four sorted
// arrays whose sum is equal to a given value x
int countQuadruples(int arr1[], int arr2[], int arr3[],
                    int arr4[], int n, int x)
{
    int count = 0;
 
    // unordered_map 'um' implemented as hash table
    // for <sum, frequency> tuples
    unordered_map<int, int> um;
 
    // count frequency of each sum obtained from the
    // pairs of arr1[] and arr2[] and store them in 'um'
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            um[arr1[i] + arr2[j]]++;
 
    // generate pair from arr3[] and arr4[]
    for (int k = 0; k < n; k++)
        for (int l = 0; l < n; l++) {
 
            // calculate the sum of elements in
            // the pair so generated
            int p_sum = arr3[k] + arr4[l];
 
            // if 'x-p_sum' is present in 'um' then
            // add frequency of 'x-p_sum' to 'count'
            if (um.find(x - p_sum) != um.end())
                count += um[x - p_sum];
        }
 
    // required count of quadruples
    return count;
}
 
// Driver program to test above
int main()
{
    // four sorted arrays each of size 'n'
    int arr1[] = { 1, 4, 5, 6 };
    int arr2[] = { 2, 3, 7, 8 };
    int arr3[] = { 1, 4, 6, 10 };
    int arr4[] = { 2, 4, 7, 8 };
 
    int n = sizeof(arr1) / sizeof(arr1[0]);
    int x = 30;
    cout << "Count = "
         << countQuadruples(arr1, arr2, arr3, arr4, n, x);
    return 0;
}

Time complexity: quadratic
Space complexity: quadratic
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
int main()
{
  int arr[] = {10, 2, -2, -20, 10};
  int k = -10;
  int n = sizeof(arr) / sizeof(arr[0]);
  int res = 0;
 
  // Calculate all subarrays
  for (int i = 0; i < n; i++)
  {
    int sum = 0;
    for (int j = i; j < n; j++)
    {
      // Calculate required sum
      sum += arr[j];
      // Check if sum is equal to required sum
      if (sum == k)
        res++;
    }
  }
  cout << (res) << endl;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Time complexity: quadratic
Space complexity: constant
// C++ program to find number of subarrays with sum exactly
// equal to k.
#include <bits/stdc++.h>
using namespace std;
 
// Function to find number of subarrays with sum exactly
// equal to k.
int findSubarraySum(int arr[], int n, int sum)
{
    // STL map to store number of subarrays starting from
    // index zero having particular value of sum.
    unordered_map<int, int> prevSum;
 
    int res = 0;
 
    // Sum of elements so far.
    int currSum = 0;
 
    for (int i = 0; i < n; i++) {
 
        // Add current element to sum so far.
        currSum += arr[i];
 
        // If currsum is equal to desired sum, then a new
        // subarray is found. So increase count of
        // subarrays.
        if (currSum == sum)
            res++;
 
        // currsum exceeds given sum by currsum  - sum. Find
        // number of subarrays having this sum and exclude
        // those subarrays from currsum by increasing count
        // by same amount.
        if (prevSum.find(currSum - sum) != prevSum.end())
            res += (prevSum[currSum - sum]);
 
        // Add currsum value to count of different values of
        // sum.
        prevSum[currSum]++;
    }
 
    return res;
}
 
int main()
{
    int arr[] = { 10, 2, -2, -20, 10 };
    int sum = -10;
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << findSubarraySum(arr, n, sum);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Time complexity: linear
Space complexity: linear
// C++ program to find all pairs in both arrays
// whose sum is equal to given value x
#include <bits/stdc++.h>
using namespace std;
 
// Function to print all pairs in both arrays
// whose sum is equal to given value x
void findPairs(int arr1[], int arr2[], int n, int m, int x)
{
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (arr1[i] + arr2[j] == x)
                cout << arr1[i] << " " << arr2[j] << endl;
}
 
// Driver code
int main()
{
    int arr1[] = { 1, 2, 3, 7, 5, 4 };
    int arr2[] = { 0, 7, 4, 3, 2, 1 };
    int n = sizeof(arr1) / sizeof(int);
    int m = sizeof(arr2) / sizeof(int);
    int x = 8;
    findPairs(arr1, arr2, n, m, x);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Time complexity: quadratic
Space complexity: constant
// C++ program to find all pair in both arrays
// whose sum is equal to given value x
#include <bits/stdc++.h>
using namespace std;
 
// Function to find all pairs in both arrays
// whose sum is equal to given value x
void findPairs(int arr1[], int arr2[], int n,
               int m, int x)
{
    // Insert all elements of first array in a hash
    unordered_set<int> s;
    for (int i = 0; i < n; i++)
        s.insert(arr1[i]);
 
    // Subtract sum from second array elements one
    // by one and check it's present in array first
    // or not
    for (int j = 0; j < m; j++)
        if (s.find(x - arr2[j]) != s.end())
            cout << x - arr2[j] << " "
                 << arr2[j] << endl;
}
 
// Driver code
int main()
{
    int arr1[] = { 1, 0, -4, 7, 6, 4 };
    int arr2[] = { 0, 2, 4, -3, 2, 1 };
    int x = 8;
    int n = sizeof(arr1) / sizeof(int);
    int m = sizeof(arr2) / sizeof(int);
    findPairs(arr1, arr2, n, m, x);
    return 0;
}

Time complexity: linear
Space complexity: linear
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the cumulative frequency according to
// the order given
void countFreq(int a[], int n)
{
   
    // Declaring a map so values get inserted in a sorted
    // manner
    map<int, int> m;
   
    // Inserting values into the map
    for (int i = 0; i < n; i++) {
        m[a[i]]++;
    }
   
    // Variable to store the count of previous number
    // cumulative frequency
    int cumul = 0;
    for (auto v : m) {
        cout << v.first << " " << v.second + cumul
             << endl;
        cumul += v.second;
    }
}
 
int main()
{
    int arr[] = { 1, 3, 2, 4, 2, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    countFreq(arr, n);
    return 0;
}
 
// This code is contributed by Vinayak Pareek (Kargil)

Time complexity: nlogn
Space complexity: linear
// C++ program to print the cumulative frequency
// according to the order given
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the cumulative frequency
// according to the order given
void countFreq(int a[], int n)
{
    // Insert elements and their
    // frequencies in hash map.
    unordered_map<int, int> hm;
    for (int i=0; i<n; i++)
        hm[a[i]]++;
    int cumul = 0;
     
   // traverse in the array
   for(int i=0;i<n;i++)
   {
       // add the frequencies
       cumul += hm[a[i]];
        
       // if the element has not been
       // visited previously
       if(hm[a[i]])
       {
           cout << a[i] << "->" << cumul << endl;
       }
       // mark the hash 0
       // as the element's cumulative frequency
       // has been printed
       hm[a[i]]=0;
   }
}
 
// Driver Code
int main()
{
    int a[] = {1, 3, 2, 4, 2, 1};
    int n = sizeof(a)/sizeof(a[0]);
    countFreq(a, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
//     CPP program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Used for sorting by frequency. And if frequency is same,
// then by appearance
bool sortByVal(const pair<int, int>& a,
                      const pair<int, int>& b)
{
 
   // If frequency is same then sort by index
   if (a.second == b.second) 
       return a.first < b.first;
 
   return a.second > b.second;
}
 
// function to sort elements by frequency
vector<int>sortByFreq(int a[], int n)
{
 
   vector<int>res;
 
   unordered_map<int, int> m;
 
   vector<pair<int, int> > v;
 
   for (int i = 0; i < n; ++i) {
 
       // Map m is used to keep track of count 
       // of elements in array
       m[a[i]]++;     
   }
 
   // Copy map to vector
   copy(m.begin(), m.end(), back_inserter(v));
 
   // Sort the element of array by frequency
   sort(v.begin(), v.end(), sortByVal);
 
   for (int i = 0; i < v.size(); ++i) 
      while(v[i].second--)
      {
              res.push_back(v[i].first);
      }
 
   return res;
}
 
// Driver program
int main()
{
 
   int a[] = { 2, 5, 2, 6, -1, 9999999, 5, 8, 8, 8 };
   int n = sizeof(a) / sizeof(a[0]);
   vector<int>res;
   res = sortByFreq(a, n);
 
   for(int i = 0;i < res.size(); i++)
         cout<<res[i]<<" ";
 
   return 0;
 
}

Time complexity: linear
Space complexity: linear
// A simple C++ program to find pair whose sum
// already exists in array
#include <bits/stdc++.h>
using namespace std;
 
// Function to find pair whose sum exists in arr[]
void findPair(int arr[], int n)
{
    bool found = false;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            for (int k = 0; k < n; k++) {
                if (arr[i] + arr[j] == arr[k]) {
                    cout << arr[i] << " " << arr[j] << endl;
                    found = true;
                }
            }
        }
    }
 
    if (found == false)
        cout << "Not exist" << endl;
}
 
// Driven code
int main()
{
    int arr[] = { 10, 4, 8, 13, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    findPair(arr, n);
    return 0;
}

Time complexity: cubic
Space complexity: constant
// C++ program to find pair whose sum already
// exists in array
#include <bits/stdc++.h>
using namespace std;
 
// Function to find pair whose sum exists in arr[]
void findPair(int arr[], int n)
{
    // Hash to store all element of array
    unordered_set<int> s;
    for (int i = 0; i < n; i++)
        s.insert(arr[i]);
 
    bool found = false;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            // Check sum already exists or not
            if (s.find(arr[i] + arr[j]) != s.end()) {
                cout << arr[i] << " " << arr[j] << endl;
                found = true;
            }
        }
    }
 
    if (found == false)
        cout << "Not exist" << endl;
}
 
// Driven code
int main()
{
    int arr[] = { 10, 4, 8, 13, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    findPair(arr, n);
    return 0;
}

Time complexity: quadratic
Space complexity: quadratic
// C++ implementation to find such pairs
#include <bits/stdc++.h>
using namespace std;
 
// Function to find pair such that (a % b = k)
bool printPairs(int arr[], int n, int k)
{
    bool isPairFound = true;
 
    // Consider each and every pair
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            // Print if their modulo equals to k
            if (i != j && arr[i] % arr[j] == k) {
                cout << "(" << arr[i] << ", "
                     << arr[j] << ")"
                     << " ";
                isPairFound = true;
            }
        }
    }
 
    return isPairFound;
}
 
// Driver program
int main()
{
    int arr[] = { 2, 3, 5, 4, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 3;
 
    if (printPairs(arr, n, k) == false)
        cout << "No such pair exists";
 
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program to find all pairs such that
// a % b = k.
#include <bits/stdc++.h>
using namespace std;
 
// Utility function to find the divisors of
// n and store in vector v[]
vector<int> findDivisors(int n)
{
    vector<int> v;
 
    // Vector is used to store  the divisors
    for (int i = 1; i <= sqrt(n); i++) {
        if (n % i == 0) {
            // If n is a square number, push
            // only one occurrence
            if (n / i == i)
                v.push_back(i);
            else {
                v.push_back(i);
                v.push_back(n / i);
            }
        }
    }
    return v;
}
 
// Function to find pairs such that (a%b = k)
bool printPairs(int arr[], int n, int k)
{
    // Store all the elements in the map
    // to use map as hash for finding elements
    // in O(1) time.
    unordered_map<int, bool> occ;
    for (int i = 0; i < n; i++)
        occ[arr[i]] = true;
 
    bool isPairFound = false;
    for (int i = 0; i < n; i++) {
        // Print all the pairs with (a, b) as
        // (k, numbers greater than k) as
        // k % (num (> k)) = k i.e. 2%4 = 2
        if (occ[k] && k < arr[i]) {
            cout << "(" << k << ", " << arr[i] << ") ";
            isPairFound = true;
        }
 
        // Now check for the current element as 'a'
        // how many b exists such that a%b = k
        if (arr[i] >= k) {
            // find all the divisors of (arr[i]-k)
            vector<int> v = findDivisors(arr[i] - k);
 
            // Check for each divisor i.e. arr[i] % b = k
            // or not, if yes then print that pair.
            for (int j = 0; j < v.size(); j++) {
                if (arr[i] % v[j] == k && arr[i] != v[j] && occ[v[j]]) {
                    cout << "(" << arr[i] << ", "
                         << v[j] << ") ";
                    isPairFound = true;
                }
            }
 
            // Clear vector
            v.clear();
        }
    }
 
    return isPairFound;
}
 
// Driver program
int main()
{
    int arr[] = { 3, 1, 2, 5, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 2;
 
    if (printPairs(arr, n, k) == false)
        cout << "No such pair exists";
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to convert an array in reduced
// form
#include <bits/stdc++.h>
using namespace std;
 
void convert(int arr[], int n)
{
    // Create a temp array and copy contents
    // of arr[] to temp
    int temp[n];
    memcpy(temp, arr, n*sizeof(int));
 
    // Sort temp array
    sort(temp, temp + n);
 
    // Create a hash table. Refer
    // http://tinyurl.com/zp5wgef
    unordered_map<int, int> umap;
 
    // One by one insert elements of sorted
    // temp[] and assign them values from 0
    // to n-1
    int val = 0;
    for (int i = 0; i < n; i++)
        umap[temp[i]] = val++;
 
    // Convert array by taking positions from
    // umap
    for (int i = 0; i < n; i++)
        arr[i] = umap[arr[i]];
}
 
void printArr(int arr[], int n)
{
    for (int i=0; i<n; i++)
        cout << arr[i] << " ";
}
 
// Driver program to test above method
int main()
{
    int arr[] = {10, 20, 15, 12, 11, 50};
    int n = sizeof(arr)/sizeof(arr[0]);
 
    cout << "Given Array is \n";
    printArr(arr, n);
 
    convert(arr , n);
 
    cout << "\n\nConverted Array is \n";
    printArr(arr, n);
 
    return 0;
}

Time complexity: nlogn
Space complexity: linear
// C++ program to output the maximum occurring character
// in a string
#include <bits/stdc++.h>
#define ASCII_SIZE 256
using namespace std;
 
char getMaxOccurringChar(char* str)
{
    // Create array to keep the count of individual
    // characters and initialize the array as 0
    int count[ASCII_SIZE] = { 0 };
 
    // Construct character count array from the input
    // string.
    int len = strlen(str);
    int max = 0; // Initialize max count
    char result; // Initialize result
 
    // Traversing through the string and maintaining
    // the count of each character
    for (int i = 0; i < len; i++) {
        count[str[i]]++;
        if (max < count[str[i]]) {
            max = count[str[i]];
            result = str[i];
        }
    }
 
    return result;
}
 
// Driver program to test the above function
int main()
{
    char str[] = "sample string";
    cout << "Max occurring character is "
         << getMaxOccurringChar(str);
}

Time complexity: linear
Space complexity: constant
// C++ program to print all words that have
// the same unique character set
#include<bits/stdc++.h>
using namespace std;
#define MAX_CHAR 26
 
// Generates a key from given string. The key
// contains all unique characters of given string in
// sorted order consisting of only distinct elements.
string getKey(string &str)
{
    bool visited[MAX_CHAR] = { false };
 
    // store all unique characters of current
    // word in key
    for (int j = 0; j < str.length(); j++)
        visited[str[j] - 'a'] = true ;
    string key = "";
    for (int j=0; j < MAX_CHAR; j++)
        if (visited[j])
            key = key + (char)('a'+j);
    return key;
}
 
// Print all words together with same character sets.
void wordsWithSameCharSet(string words[], int n)
{
    // Stores indexes of all words that have same
    // set of unique characters.
    unordered_map <string, vector <int> > Hash;
 
    // Traverse all words
    for (int i=0; i<n; i++)
    {
        string key = getKey(words[i]);
        Hash[key].push_back(i);
    }
 
    // print all words that have the same unique character set
    for (auto it = Hash.begin(); it!=Hash.end(); it++)
    {
      for (auto v=(*it).second.begin(); v!=(*it).second.end(); v++)
          cout << words[*v] << ", ";
      cout << endl;
    }
}
 
// Driver program to test above function
int main()
{
    string words[] = { "may", "student", "students", "dog",
                 "studentssess", "god", "cat", "act", "tab",
                 "bat", "flow", "wolf", "lambs", "amy", "yam",
                 "balms", "looped", "poodle"};
    int n = sizeof(words)/sizeof(words[0]);
    wordsWithSameCharSet(words, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find out the second
// most repeated word
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the word
string secMostRepeated(vector<string> seq)
{
 
    // Store all the words with its occurrence
    unordered_map<string, int> occ;
    for (int i = 0; i < seq.size(); i++)
        occ[seq[i]]++;
 
    // find the second largest occurrence
    int first_max = INT_MIN, sec_max = INT_MIN;
    for (auto it = occ.begin(); it != occ.end(); it++) {
        if (it->second > first_max) {
            sec_max = first_max;
            first_max = it->second;
        }
 
        else if (it->second > sec_max &&
                 it->second != first_max)
            sec_max = it->second;
    }
 
    // Return string with occurrence equals
    // to sec_max
    for (auto it = occ.begin(); it != occ.end(); it++)
        if (it->second == sec_max)
            return it->first;
}
 
// Driver program
int main()
{
    vector<string> seq = { "ccc", "aaa", "ccc",
                          "ddd", "aaa", "aaa" };
    cout << secMostRepeated(seq);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find the smallest element
// with frequency exactly k.
#include <bits/stdc++.h>
using namespace std;
 
int smallestKFreq(int a[], int n, int k)
{
    unordered_map<int, int> m;
 
    // Map is used to store the count of
    // elements present in the array
    for (int i = 0; i < n; i++)
        m[a[i]]++;
 
    // Traverse the map and find minimum
    // element with frequency k.
    int res = INT_MAX;
    for (auto it = m.begin(); it != m.end(); ++it)
        if (it->second == k)
           res = min(res, it->first);
 
    return (res != INT_MAX)? res : -1;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 2, 1, 3, 1 };
    int k = 2;
    int n = sizeof(arr) / (sizeof(arr[0]));
    cout << smallestKFreq(arr, n, k);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ code to find number
// occurring prime number
// of times with frequency >= k
#include <bits/stdc++.h>
using namespace std;
 
// Check if the number of
// occurrences are primes
// or not
bool isPrime(int n)
{
    // Corner case
    if (n <= 1) return false;
 
    // Check from 2 to n-1
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;
 
    return true;
}
 
// Function to find number
// with prime occurrences
void primeOccurrences(int arr[], int k)
{
    unordered_map<int, int> map;
     
    // Insert values and
    // their frequencies
    for (int i = 0; i < 12; i++)
        map[arr[i]]++;
 
    // Traverse map and find
    // elements with prime
    // frequencies and frequency
    // at least k
    for (auto x : map)
    {
        if (isPrime(x.second) &&
                    x.second >= k)
            cout << x.first << endl;
    }
}
 
// Driver code
int main()
{
    int arr[] = {11, 11, 11, 23,
                 11, 37, 37, 51,
                 51, 51, 51, 51};
    int k = 2;
 
    primeOccurrences(arr, k);
    return 0;
}
 
// This code is contributed by
// Manish Shaw(manishshaw1)

Time complexity: linear
Space complexity: linear
// C++ implementation to find k numbers with most
// occurrences in the given array
#include <bits/stdc++.h>
using namespace std;
 
// Comparison function to sort the 'freq_arr[]'
bool compare(pair<int, int> p1, pair<int, int> p2)
{
    // If frequencies of two elements are same
    // then the larger number should come first
    if (p1.second == p2.second)
        return p1.first > p2.first;
 
    // Sort on the basis of decreasing order
    // of frequencies
    return p1.second > p2.second;
}
 
// Function to print the k numbers with most occurrences
void print_N_mostFrequentNumber(int arr[], int N, int K)
{
    // unordered_map 'mp' implemented as frequency hash
    // table
    unordered_map<int, int> mp;
    for (int i = 0; i < N; i++)
        mp[arr[i]]++;
 
    // store the elements of 'mp' in the vector 'freq_arr'
    vector<pair<int, int> > freq_arr(mp.begin(), mp.end());
 
    // Sort the vector 'freq_arr' on the basis of the
    // 'compare' function
    sort(freq_arr.begin(), freq_arr.end(), compare);
 
    // display the top k numbers
    cout << K << " numbers with most occurrences are:\n";
    for (int i = 0; i < K; i++)
        cout << freq_arr[i].first << " ";
}
 
// Driver's code
int main()
{
    int arr[] = { 3, 1, 4, 4, 5, 2, 6, 1 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 2;
 
    // Function call
    print_N_mostFrequentNumber(arr, N, K);
 
    return 0;
}

Time complexity: nlogn
Space complexity: logn
/* C++ program to find first repeating element in arr[] */
#include <bits/stdc++.h>
using namespace std;
 
// This function prints the first repeating element in arr[]
void printFirstRepeating(int arr[], int n)
{
    // Initialize index of first repeating element
    int min = -1;
 
    // Creates an empty hashset
    set<int> myset;
 
    // Traverse the input array from right to left
    for (int i = n - 1; i >= 0; i--) {
        // If element is already in hash set, update min
        if (myset.find(arr[i]) != myset.end())
            min = i;
 
        else // Else add element to hash set
            myset.insert(arr[i]);
    }
 
    // Print the result
    if (min != -1)
        cout << "The first repeating element is "
             << arr[min];
    else
        cout << "There are no repeating elements";
}
 
// Driver Code
int main()
{
    int arr[] = { 10, 5, 3, 4, 3, 5, 6 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
    printFirstRepeating(arr, n);
}
// This article is contributed by Chhavi

Time complexity: linear
Space complexity: linear