Banana-Leopard/TASTY huggingface dataset:
// CPP code to find the difference between highest
// and least frequencies
#include <bits/stdc++.h>
using namespace std;
 
int findDiff(int arr[], int n)
{
    // sort the array
    sort(arr, arr + n);
 
    int count = 0, max_count = 0, min_count = n;
    for (int i = 0; i < (n - 1); i++) {
 
        // checking consecutive elements
        if (arr[i] == arr[i + 1]) {
            count += 1;
            continue;
        }
        else {
            max_count = max(max_count, count);
            min_count = min(min_count, count);
            count = 0;
        }
    }
 
    return (max_count - min_count);
}
 
// Driver code
int main()
{
    int arr[] = { 7, 8, 4, 5, 4, 1, 1, 7, 7, 2, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << findDiff(arr, n) << "\n";
    return 0;
}

Time complexity: nlogn
Space complexity: constant
// CPP code to find the difference between highest
// and least frequencies
#include <bits/stdc++.h>
using namespace std;
 
int findDiff(int arr[], int n)
{
    // Put all elements in a hash map
    unordered_map<int, int> hm;
    for (int i = 0; i < n; i++)
        hm[arr[i]]++;
 
    // Find counts of maximum and minimum
    // frequent elements
    int max_count = 0, min_count = n;
    for (auto x : hm) {
        max_count = max(max_count, x.second);
        min_count = min(min_count, x.second);
    }
 
    return (max_count - min_count);
}
 
// Driver
int main()
{
    int arr[] = { 7, 8, 4, 5, 4, 1, 1, 7, 7, 2, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << findDiff(arr, n) << "\n";
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find minimum range that
// contains exactly k distinct numbers.
#include <bits/stdc++.h>
using namespace std;
 
// Prints the minimum range that contains exactly
// k distinct numbers.
void minRange(int arr[], int n, int k)
{
    // Starting and ending  index of resultant subarray
    int start = 0, end = n;
 
    // Selecting each element as the start index for
    // subarray
    for (int i = 0; i < n; i++) {
        // Initialize a set to store all distinct elements
        unordered_set<int> set;
 
        // Selecting the end index for subarray
        int j;
        for (j = i; j < n; j++) {
            set.insert(arr[j]);
 
            /*
            If set contains exactly k elements,
            then check subarray[i, j] is smaller in size
            than the current resultant subarray
            */
            if (set.size() == k) {
                if (j - i < end - start) {
                    start = i;
                    end = j;
                }
 
                // There are already k distinct elements
                // now, no need to consider further elements
                break;
            }
        }
 
        // If there are no k distinct elements
        // left in the array starting from index i we will
        // break
        if (j == n) {
            break;
        }
    }
 
    // If no window found then print -1
    if (start == 0 && end == n)
        cout << "Invalid k";
 
    else
        cout << start << " " << end;
}
 
// Driver code for above function.
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 3;
    minRange(arr, n, k);
    return 0;
}
 
// This code is contributed by Rajdeep

Time complexity: quadratic
Space complexity: linear
// C++ program to find minimum range that
// contains exactly k distinct numbers.
#include <bits/stdc++.h>
using namespace std;
 
// prints the minimum range that contains exactly
// k distinct numbers.
void minRange(int arr[], int n, int k)
{
    /*
        start = starting index of resultant subarray
        end  = ending index of resultant subarray
    */
    int start = 0, end = n;
 
    unordered_map<int, int> map;
 
    /*
        i = starting index of the window (on left side)
        j = ending index of the window (on right side)
    */
    int i = 0, j = 0;
 
    while (j < n) {
        // Add the current element to the map
        map[arr[j]]++;
        j++;
 
        // Nothing to do when having less element
        if (map.size() < k)
            continue;
 
        /*
                If map contains exactly k elements,
                consider subarray[i, j - 1] keep removing
                left most elements
        */
 
        while (map.size() == k) {
            // as considering the (j-1)th and i-th index
            int windowLen = (j - 1) - i + 1;
            int subArrayLen = end - start + 1;
 
            if (subArrayLen > windowLen) {
                start = i;
                end = j - 1;
            }
 
            // Remove elements from left
 
            // If freq == 1 then totally erase
            if (map[arr[i]] == 1)
                map.erase(arr[i]);
 
            // decrease freq by 1
            else
                map[arr[i]]--;
 
            // move the starting index of window
            i++;
        }
    }
 
    if (start == 0 && end == n)
        cout << "Invalid k" << endl;
 
    else
        cout << start << " " << end << endl;
}
 
// Driver code for above function.
int main()
{
    int arr[] = { 1, 1, 2, 2, 3, 3, 4, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 3;
    minRange(arr, n, k);
    return 0;
}
 
// This code is contributed by Rajdeep

Time complexity: linear
Space complexity: linear
// CPP program to find longest subarray with
// k or less distinct elements.
#include <bits/stdc++.h>
using namespace std;
 
// function to print the longest sub-array
void longest(int a[], int n, int k)
{
    unordered_map<int, int> freq;
 
    int start = 0, end = 0, now = 0, l = 0;
    for (int i = 0; i < n; i++) {
 
        // mark the element visited
        freq[a[i]]++;
 
        // if its visited first time, then increase
        // the counter of distinct elements by 1
        if (freq[a[i]] == 1)
            now++;
 
        // When the counter of distinct elements
        // increases from k, then reduce it to k
        while (now > k) {
 
            // from the left, reduce the number of
            // time of visit
            freq[a[l]]--;
 
            // if the reduced visited time element
            // is not present in further segment
            // then decrease the count of distinct
            // elements
            if (freq[a[l]] == 0)
                now--;
 
            // increase the subsegment mark
            l++;
        }
 
        // check length of longest sub-segment
        // when greater than previous best
        // then change it
        if (i - l + 1 >= end - start + 1)
            end = i, start = l;
    }
 
    // print the longest sub-segment
    for (int i = start; i <= end; i++)
        cout << a[i] << " ";
}
 
// driver program to test the above function
int main()
{
    int a[] = { 6, 5, 1, 2, 3, 2, 1, 4, 5 };
    int n = sizeof(a) / sizeof(a[0]);
    int k = 3;
    longest(a, n, k);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find number of pairs in an array such that
// their XOR is 0
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate the count
int calculate(int a[], int n)
{
  // Sorting the list using built in function
  sort(a, a + n);
  int count = 1;
  int answer = 0;
 
  // Traversing through the elements
  for (int i = 1; i < n; i++) {
    if (a[i] == a[i - 1])
      // Counting frequency of each elements
      count += 1;
    else {
      // Adding the contribution of the frequency to
      // the answer
      answer = answer + (count * (count - 1)) / 2;
      count = 1;
    }
  }
  answer = answer + (count * (count - 1)) / 2;
  return answer;
}
 
// Driver Code
int main()
{
  int a[] = { 1, 2, 1, 2, 4 };
  int n = sizeof(a) / sizeof(a[0]);
  cout << calculate(a, n);
  return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Time complexity: nlogn
Space complexity: constant
// C++ program to find number of pairs
// in an array such that their XOR is 0
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate the answer
int calculate(int a[], int n){
     
    // Finding the maximum of the array
    int *maximum = max_element(a, a + n);
 
    // Creating frequency array
    // With initial value 0
    int frequency[*maximum + 1] = {0};
     
    // Traversing through the array
    for(int i = 0; i < n; i++)
    {
        // Counting frequency
        frequency[a[i]] += 1;
    }
    int answer = 0;
     
    // Traversing through the frequency array
    for(int i = 0; i < (*maximum)+1; i++)
    {
        // Calculating answer
        answer = answer + frequency[i] * (frequency[i] - 1) ;
    }
    return answer/2;
}
 
// Driver Code
int main()
{
   int a[] = {1, 2, 1, 2, 4};
   int n = sizeof(a) / sizeof(a[0]);
    
   // Function calling
   cout << (calculate(a,n));
}
 
// This code is contributed by Smitha

Time complexity: linear
Space complexity: linear
// C++ implementation to count subarrays with
// equal number of 1's and 0's
#include <bits/stdc++.h>
 
using namespace std;
 
// function to count subarrays with
// equal number of 1's and 0's
int countSubarrWithEqualZeroAndOne(int arr[], int n)
{
    // 'um' implemented as hash table to store
    // frequency of values obtained through
    // cumulative sum
    unordered_map<int, int> um;
    int curr_sum = 0;
 
    // Traverse original array and compute cumulative
    // sum and increase count by 1 for this sum
    // in 'um'. Adds '-1' when arr[i] == 0
    for (int i = 0; i < n; i++) {
        curr_sum += (arr[i] == 0) ? -1 : arr[i];
        um[curr_sum]++;
    }
 
    int count = 0;
    // traverse the hash table 'um'
    for (auto itr = um.begin(); itr != um.end(); itr++) {
 
        // If there are more than one prefix subarrays
        // with a particular sum
        if (itr->second > 1)
            count
                += ((itr->second * (itr->second - 1)) / 2);
    }
 
    // add the subarrays starting from 1st element and
    // have equal number of 1's and 0's
    if (um.find(0) != um.end())
        count += um[0];
 
    // required count of subarrays
    return count;
}
 
// Driver program to test above
int main()
{
    int arr[] = { 1, 0, 0, 1, 0, 1, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Count = "
         << countSubarrWithEqualZeroAndOne(arr, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
#include <bits/stdc++.h>
 
using namespace std;
 
int countSubarrWithEqualZeroAndOne(int arr[], int n)
{
    unordered_map<int, int> mp;
    int sum = 0;
    int count = 0;
    for (int i = 0; i < n; i++) {
        // Replacing 0's in array with -1
        if (arr[i] == 0)
            arr[i] = -1;
 
        sum += arr[i];
 
        // If sum = 0, it implies number of 0's and 1's are
        // equal from arr[0]..arr[i]
        if (sum == 0)
            count++;
 
        // if mp[sum] exists then add "frequency-1" to count
        if (mp[sum])
            count += mp[sum];
 
        // if frequency of "sum" is zero then we initialize
        // that frequency to 1 if its not 0, we increment it
        if (mp[sum] == 0)
            mp[sum] = 1;
        else
            mp[sum]++;
    }
    return count;
}
 
int main()
{
    int arr[] = { 1, 0, 0, 1, 0, 1, 1 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << "count="
         << countSubarrWithEqualZeroAndOne(arr, n);
}

Time complexity: linear
Space complexity: linear
// C++ implementation to find the length of
// longest subarray having count of 1's one
// more than count of 0's
#include <bits/stdc++.h>
using namespace std;
 
// function to find the length of longest
// subarray having count of 1's one more
// than count of 0's
int lenOfLongSubarr(int arr[], int n)
{
    // unordered_map 'um' implemented as
    // hash table
    unordered_map<int, int> um;
    int sum = 0, maxLen = 0;
 
    // traverse the given array
    for (int i = 0; i < n; i++) {
 
        // consider '0' as '-1'
        sum += arr[i] == 0 ? -1 : 1;
 
        // when subarray starts form index '0'
        if (sum == 1)
            maxLen = i + 1;
 
        // make an entry for 'sum' if it is
        // not present in 'um'
        else if (um.find(sum) == um.end())
            um[sum] = i;
 
        // check if 'sum-1' is present in 'um'
        // or not
        if (um.find(sum - 1) != um.end()) {
 
            // update maxLength
            if (maxLen < (i - um[sum - 1]))
                maxLen = i - um[sum - 1];
        }
    }
 
    // required maximum length
    return maxLen;
}
 
// Driver program to test above
int main()
{
    int arr[] = { 0, 1, 1, 0, 0, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Length = " << lenOfLongSubarr(arr, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to print all triplets in given
// array that form Arithmetic Progression
// C++ program to print all triplets in given
// array that form Arithmetic Progression
#include <bits/stdc++.h>
using namespace std;
  
// Function to print all triplets in
// given sorted array that forms AP
void printAllAPTriplets(int arr[], int n)
{
    unordered_set<int> s;
    for (int i = 0; i < n - 1; i++)
    {
    for (int j = i + 1; j < n; j++)
    {
        // Use hash to find if there is
        // a previous element with difference
        // equal to arr[j] - arr[i]
        int diff = arr[j] - arr[i];
        if (s.find(arr[i] - diff) != s.end())
            cout << arr[i] - diff << " " << arr[i]
                 << " " << arr[j] << endl;
    }
    s.insert(arr[i]);
    }
}
  
// Driver code
int main()
{
    int arr[] = { 2, 6, 9, 12, 17,
                 22, 31, 32, 35, 42 };
    int n = sizeof(arr) / sizeof(arr[0]);
    printAllAPTriplets(arr, n);
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// C++ program to print all triplets in given
// array that form Arithmetic Progression
#include <bits/stdc++.h>
using namespace std;
  
// Function to print all triplets in
// given sorted array that forms AP
void printAllAPTriplets(int arr[], int n)
{
    for (int i = 1; i < n - 1; i++)
    {
  
        // Search other two elements of
        // AP with arr[i] as middle.
        for (int j = i - 1, k = i + 1; j >= 0 && k < n;)
        {
  
            // if a triplet is found
            if (arr[j] + arr[k] == 2 * arr[i])
            {
                cout << arr[j] << " " << arr[i]
                     << " " << arr[k] << endl;
  
                // Since elements are distinct,
                // arr[k] and arr[j] cannot form
                // any more triplets with arr[i]
                k++;
                j--;
            }
  
            // If middle element is more move to
            // higher side, else move lower side.
            else if (arr[j] + arr[k] < 2 * arr[i])
                k++;        
            else
                j--;        
        }
    }
}
  
// Driver code
int main()
{
    int arr[] = { 2, 6, 9, 12, 17,
                  22, 31, 32, 35, 42 };
    int n = sizeof(arr) / sizeof(arr[0]);
    printAllAPTriplets(arr, n);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program to count triplets with given
// product m
#include <iostream>
using namespace std;
 
// Function to count such triplets
int countTriplets(int arr[], int n, int m)
{
    int count = 0;
 
    // Consider all triplets and count if
    // their product is equal to m
    for (int i = 0; i < n - 2; i++)
        for (int j = i + 1; j < n - 1; j++)
            for (int k = j + 1; k < n; k++)
                if (arr[i] * arr[j] * arr[k] == m)
                    count++;
 
    return count;
}
 
// Drivers code
int main()
{
    int arr[] = { 1, 4, 6, 2, 3, 8 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int m = 24;
 
    cout << countTriplets(arr, n, m);
 
    return 0;
}

Time complexity: cubic
Space complexity: constant
// C++ program to count triplets with given
// product m
#include <bits/stdc++.h>
using namespace std;
 
// Function to count such triplets
int countTriplets(int arr[], int n, int m)
{
    // Store all the elements in a set
    unordered_map<int, int> occ;
    for (int i = 0; i < n; i++)
        occ[arr[i]] = i;
 
    int count = 0;
 
    // Consider all pairs and check for a
    // third number so their product is equal to m
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            // Check if current pair divides m or not
            // If yes, then search for (m / arr[i]*arr[j])
            if ((arr[i] * arr[j] <= m) && (arr[i] * arr[j] != 0) && (m % (arr[i] * arr[j]) == 0)) {
                int check = m / (arr[i] * arr[j]);
                auto it = occ.find(check);
 
                // Check if the third number is present
                // in the map and it is not equal to any
                // other two elements and also check if
                // this triplet is not counted already
                // using their indexes
                if (check != arr[i] && check != arr[j]
                    && it != occ.end() && it->second > i
                    && it->second > j)
                    count++;
            }
        }
    }
 
    // Return number of triplets
    return count;
}
 
// Drivers code
int main()
{
    int arr[] = { 1, 4, 6, 2, 3, 8 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int m = 24;
 
    cout << countTriplets(arr, n, m);
 
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// C++ program to count of pairs with equal
// elements in an array.
#include<bits/stdc++.h>
using namespace std;
 
// Return the number of pairs with equal
// values.
int countPairs(int arr[], int n)
{
    int ans = 0;
 
    // for each index i and j
    for (int i = 0; i < n; i++)
        for (int j = i+1; j < n; j++)
 
            // finding the index with same
            // value but different index.
            if (arr[i] == arr[j])
                ans++;
    return ans;
}
 
// Driven Program
int main()
{
    int arr[] = { 1, 1, 2 };
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << countPairs(arr, n) << endl;
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program to count of index pairs with
// equal elements in an array.
#include<bits/stdc++.h>
using namespace std;
 
// Return the number of pairs with equal
// values.
int countPairs(int arr[], int n)
{
    unordered_map<int, int> mp;
 
    // Finding frequency of each number.
    for (int i = 0; i < n; i++)
        mp[arr[i]]++;
 
    // Calculating pairs of each value.
    int ans = 0;
    for (auto it=mp.begin(); it!=mp.end(); it++)
    {
        int count = it->second;
        ans += (count * (count - 1))/2;
    }
 
    return ans;
}
 
// Driven Program
int main()
{
    int arr[] = {1, 1, 2};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << countPairs(arr, n) << endl;
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to finds out smallest range that includes
// elements from each of the given sorted lists.
 
#include <bits/stdc++.h>
using namespace std;
 
// array for storing the current index of list i
int ptr[501];
 
// This function takes an k sorted lists in the form of
// 2D array as an argument. It finds out smallest range
// that includes elements from each of the k lists.
void findSmallestRange(vector<vector<int> >& arr, int N,
                       int K)
{
    int i, minval, maxval, minrange, minel, maxel, flag,
        minind;
 
    // initializing to 0 index;
    for (i = 0; i <= K; i++)
        ptr[i] = 0;
 
    minrange = INT_MAX;
 
    while (1) {
        // for maintaining the index of list containing the
        // minimum element
        minind = -1;
        minval = INT_MAX;
        maxval = INT_MIN;
        flag = 0;
 
        // iterating over all the list
        for (i = 0; i < K; i++) {
            // if every element of list[i] is traversed then
            // break the loop
            if (ptr[i] == N) {
                flag = 1;
                break;
            }
            // find minimum value among all the list
            // elements pointing by the ptr[] array
            if (ptr[i] < N && arr[i][ptr[i]] < minval) {
                minind = i; // update the index of the list
                minval = arr[i][ptr[i]];
            }
            // find maximum value among all the list
            // elements pointing by the ptr[] array
            if (ptr[i] < N && arr[i][ptr[i]] > maxval) {
                maxval = arr[i][ptr[i]];
            }
        }
 
        // if any list exhaust we will not get any better
        // answer, so break the while loop
        if (flag)
            break;
 
        ptr[minind]++;
 
        // updating the minrange
        if ((maxval - minval) < minrange) {
            minel = minval;
            maxel = maxval;
            minrange = maxel - minel;
        }
    }
 
    printf("The smallest range is [%d, %d]\n", minel,
           maxel);
}
 
// Driver's code
int main()
{
    vector<vector<int> > arr = { { 4, 7, 9, 12, 15 },
                                 { 0, 8, 10, 14, 20 },
                                 { 6, 12, 16, 30, 50 } };
 
    int K = arr.size();
    int N = arr[0].size();
 
    // Function call
    findSmallestRange(arr, N, K);
 
    return 0;
}
// This code is contributed by Aditya Krishna Namdeo

Time complexity: linear
Space complexity: linear
// C++ program for above implementation
#include <bits/stdc++.h>
using namespace std;
 
// Function to count numbers to be added
int countNum(int arr[], int n)
{
    int count = 0;
 
    // Sort the array
    sort(arr, arr + n);
 
    // Check if elements are consecutive
    //  or not. If not, update count
    for (int i = 0; i < n - 1; i++)
        if (arr[i] != arr[i+1] &&
            arr[i] != arr[i + 1] - 1)
            count += arr[i + 1] - arr[i] - 1;
 
    return count;
}
 
// Drivers code
int main()
{
    int arr[] = { 3, 5, 8, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << countNum(arr, n) << endl;
    return 0;
}

Time complexity: nlogn
Space complexity: constant
// C++ program for above implementation
#include <bits/stdc++.h>
using namespace std;
 
// Function to count numbers to be added
int countNum(int arr[], int n)
{
    unordered_set<int> s;
    int count = 0, maxm = INT_MIN, minm = INT_MAX;
 
    // Make a hash of elements
    // and store minimum and maximum element
    for (int i = 0; i < n; i++) {
        s.insert(arr[i]);
        if (arr[i] < minm)
            minm = arr[i];
        if (arr[i] > maxm)
            maxm = arr[i];
    }
 
    // Traverse all elements from minimum
    // to maximum and count if it is not
    // in the hash
    for (int i = minm; i <= maxm; i++)
        if (s.find(arr[i]) == s.end())
            count++;
    return count;
}
 
// Drivers code
int main()
{
    int arr[] = { 3, 5, 8, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << countNum(arr, n) << endl;
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to calculate sum of lengths of subarrays
// of distinct elements.
#include<bits/stdc++.h>
using namespace std;
 
// Returns sum of lengths of all subarrays with distinct
// elements.
int sumoflength(int arr[], int n)
{
    // For maintaining distinct elements.
    unordered_set<int> s;
 
    // Initialize ending point and result
    int j = 0, ans = 0;
 
    // Fix starting point
    for (int i=0; i<n; i++)
    {
        // Find ending point for current subarray with
        // distinct elements.
        while (j < n && s.find(arr[j]) == s.end())
        {
            s.insert(arr[j]);
            j++;
        }
 
        // Calculating and adding all possible length
        // subarrays in arr[i..j]
        ans += ((j - i) * (j - i + 1))/2;
 
        // Remove arr[i] as we pick new starting point
        // from next
        s.erase(arr[i]);
    }
 
    return ans;
}
 
// Driven Code
int main()
{
    int arr[] = {1, 2, 3, 4};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << sumoflength(arr, n) << endl;
    return 0;
}

Time complexity: linear
Space complexity: linear