Banana-Leopard/TASTY huggingface dataset:
/* 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)
{
 
    // This will set k=1, if any
    // repeating element found
    int k = 0;
 
    // max = maximum from (all elements & n)
    int max = n;
    for (int i = 0; i < n; i++)
        if (max < arr[i])
            max = arr[i];
 
    // Array a is for storing
    // 1st time occurrence of element
    // initialized by 0
    int a[max + 1] = {};
 
    // Store 1 in array b
    // if element is duplicate
    // initialized by 0
    int b[max + 1] = {};
 
    for (int i = 0; i < n; i++) {
 
        // Duplicate element found
        if (a[arr[i]]) {
            b[arr[i]] = 1;
            k = 1;
            continue;
        }
        else
            // storing 1st occurrence of arr[i]
            a[arr[i]] = i + 1;
    }
 
    if (k == 0)
        cout << "No repeating element found" << endl;
    else {
        int min = max + 1;
 
        // trace array a & find repeating element
        // with min index
        for (int i = 0; i < max + 1; i++)
            if (a[i] && min > a[i] && b[i])
                min = a[i];
        cout << arr[min - 1];
    }
    cout << endl;
}
 
// Driver method to test above method
int main()
{
    int arr[] = { 10, 5, 3, 4, 3, 5, 6 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
    printFirstRepeating(arr, N);
}

Time complexity: linear
Space complexity: linear
// Simple CPP program to find first non-
// repeating element.
#include <bits/stdc++.h>
using namespace std;
 
int firstNonRepeating(int arr[], int n)
{
    // Loop for checking each element
    for (int i = 0; i < n; i++) {
        int j;
        // Checking if ith element is present in array
        for (j = 0; j < n; j++)
            if (i != j && arr[i] == arr[j])
                break;
        // if ith element is not present in array
        // except at ith index then return element
        if (j == n)
            return arr[i];
    }
    return -1;
}
 
// Driver code
int main()
{
    int arr[] = { 9, 4, 9, 6, 7, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << firstNonRepeating(arr, n);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// Efficient CPP program to find first non-
// repeating element.
#include <bits/stdc++.h>
using namespace std;
 
int firstNonRepeating(int arr[], int n)
{
    // Insert all array elements in hash
    // table
    unordered_map<int, int> mp;
    for (int i = 0; i < n; i++)
        mp[arr[i]]++;
 
    // Traverse array again and return
    // first element with count 1.
    for (int i = 0; i < n; i++)
        if (mp[arr[i]] == 1)
            return arr[i];
    return -1;
}
 
// Driver code
int main()
{
    int arr[] = { 9, 4, 9, 6, 7, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << firstNonRepeating(arr, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to print k-th distinct
// element in a given array
#include <bits/stdc++.h>
using namespace std;
 
// Returns k-th distinct
// element in arr.
int printKDistinct(int arr[], int n,
                              int k)
{
    int dist_count = 0;
    for (int i = 0; i < n; i++)
    {
        // Check if current element is
        // present somewhere else.
        int j;
        for (j = 0; j < n; j++)
            if (i != j && arr[j] == arr[i])
                break;
 
        // If element is unique
        if (j == n)
            dist_count++;
 
        if (dist_count == k)
            return arr[i];
    }
 
    return -1;
}
 
// Driver Code
int main ()
{
    int ar[] = {1, 2, 1, 3, 4, 2};
    int n = sizeof(ar) / sizeof(ar[0]);
    int k = 2;
    cout << printKDistinct(ar, n, k);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program to print k-th
// distinct element in a
// given array
#include <bits/stdc++.h>
using namespace std;
 
// Returns k-th distinct
// element in arr
int printKDistinct(int arr[],
                   int n, int k)
{
    // Traverse input array and
    // store counts if individual
    // elements.
    unordered_map<int, int> h;
    for (int i = 0; i < n; i++)
        h[arr[i]]++;
 
    // If size of hash is
    // less than k.
    if (h.size() < k)
        return -1;
 
    // Traverse array again and
    // find k-th element with
    // count as 1.
    int dist_count = 0;
    for (int i = 0; i < n; i++)
    {
        if (h[arr[i]] == 1)
            dist_count++;
        if (dist_count == k)
            return arr[i];
    }
 
    return -1;
}
 
// Driver Code
int main ()
{
    int ar[] = {1, 2, 1, 3, 4, 2};
    int n = sizeof(ar) / sizeof(ar[0]);
    cout << printKDistinct(ar, n, 2);
    return 0;
}

Time complexity: linear
Space complexity: linear
// Simple CPP program to find pairs of positive
// and negative values present in an array.
#include <bits/stdc++.h>
using namespace std;
 
// Print pair with negative and positive value
void printPairs(int arr[], int n)
{
    vector<int> v;
 
    // For each element of array.
    for (int i = 0; i < n; i++)
 
        // Try to find the negative value of
        // arr[i] from i + 1 to n
        for (int j = i + 1; j < n; j++)
 
            // If absolute values are equal print pair.
            if (abs(arr[i]) == abs(arr[j]))
                v.push_back(abs(arr[i]));
 
    // If size of vector is 0, therefore there is no
    // element with positive negative value, print "0"
    if (v.size() == 0)
        return;
 
    // Print the pair with negative positive value.
    for (int i = 0; i < v.size(); i++)
        cout << -v[i] << " " << v[i] << " ";
}
 
// Driver code
int main()
{
    int arr[] = { 4, 8, 9, -4, 1, -1, -8, -9 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    printPairs(arr, n);
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// CPP program to find pairs of
// positive and negative values present in
// an array.
#include <bits/stdc++.h>
using namespace std;
 
// Print pair with negative and positive value
void printPairs(int arr[], int n)
{
    vector<int> v;
    unordered_map<int, bool> cnt;
 
    // For each element of array.
    for (int i = 0; i < n; i++) {
 
        // If element has not encounter early,
        // mark it on cnt array.
        if (cnt[abs(arr[i])] == 0)
            cnt[abs(arr[i])] = 1;
 
        // If seen before, push it in vector (
        // given that elements are distinct)
        else {
            v.push_back(abs(arr[i]));
            cnt[abs(arr[i])] = 0;
        }
    }
 
    if (v.size() == 0)
        return;
 
    for (int i = 0; i < v.size(); i++)
        cout << "-" << v[i] << " " << v[i] << " ";
}
 
// Driver code
int main()
{
    int arr[] = { 4, 8, 9, -4, 1, -1, -8, -9 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    printPairs(arr, n);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// CPP program to find pairs of
// positive and negative values present in
// an array.
#include <bits/stdc++.h>
using namespace std;
 
// Print pair with negative and positive value
void printPairs(int arr[], int n)
{
    unordered_set<int> hs;
    vector<int> ans;
    for (int i = 0; i < n; i++) {
        if (hs.find((arr[i]) * -1) != hs.end()) {
            if (arr[i] < 0) {
                cout << arr[i] << " ";
                cout << (arr[i] * -1) << " ";
            }
            else {
                cout << (arr[i] * -1) << " ";
                cout << arr[i] << " ";
            }
        }
        hs.insert(arr[i]);
    }
 
    return;
}
 
// Driver code
int main()
{
    int arr[] = { 4, 8, 9, -4, 1, -1, -8, -9 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    printPairs(arr, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// CPP program to count divisible pairs.
#include <bits/stdc++.h>
using namespace std;
 
int countDivisibles(int arr[], int n)
{
    int res = 0;
 
    // Iterate through all pairs
    for (int i=0; i<n; i++)
      for (int j=i+1; j<n; j++)
           
         // Increment count if one divides
         // other
         if (arr[i] % arr[j] == 0 ||
             arr[j] % arr[i] == 0)
               res++;
 
    return res;
}
 
int main()
{
    int a[] = {1, 2, 3, 9};
    int n = sizeof(a) / sizeof(a[0]);
    cout << countDivisibles(a, n);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ code to implement above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the total
// count of pairs such that
// arr[i]%arr[j]==0
int total_count(int arr[], int n)
{
    int count = 0;
     
    // Storing the occurrence of
    // every element in array in
    // unordered_map
    unordered_map<int, int> freq;
  
    for(int i=0;i<n;i++)
    {
        freq[arr[i]]++;
    }
     
    // Iterating through every element
    // and finding all the divisors of
    // that element and then checking
    // how many of them are present
    // in array arr[]
    for(int i=0;i<n;i++)
    {
        for (int j=1;j<=sqrt(arr[i]);j++)
        {
            if(arr[i]%j==0)
            {
                if(arr[i]==j*j)
                {   // If divisors are equal,
                    // then take only one as
                    // it will be perfect square
                    // root of arr[i]
                    count+=freq[j];
                }
                else
                {
                    // Else take both j and arr[i]/j
                    // as both will be divisors
                    count+=freq[j]+ freq[arr[i]/j];
                }
            }
        }
                // As all the elements is divisible
                // by itself and is counted in freq[]
                // so reducing its count
                count=count-1;
    }
     
    // Returning final count
    return count;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 9 };
     int N = sizeof(arr) / sizeof(arr[0]);
      
    cout<<total_count(arr,N);
    return 0;
}
 
// This code is contributed by Pushpesh Raj

Time complexity: quadratic
Space complexity: constant
// A C++ program to check if arr[0..n-1]
// can be divided in pairs such that
// every pair is divisible by k
 
#include <bits/stdc++.h>
using namespace std;
 
bool canPairs(int nums[], int n, int k)
{
    // Array with odd length
    // cannot be divided
    if (n % 2 == 1)
        return false;
 
    // Initialize count = 0
    int count = 0;
 
    vector<int> vis(n, -1);
 
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if ((nums[i] + nums[j]) % k == 0
                and vis[i] == -1 and vis[j] == -1) {
 
                // if pair is divisible increment
                // the count and mark elements
                // as visited
                count++;
                vis[i] = 1;
                vis[j] = 1;
            }
        }
    }
 
    if (count == n / 2)
        return true;
    else
        return false;
}
 
// Driver code
int main()
{
    int arr[] = { 92, 75, 65, 48, 45, 35 };
    int k = 10;
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    canPairs(arr, n, k) ? cout << "True" : cout << "False";
    return 0;
}
 
// This code is contributed by Arpit Jain

Time complexity: quadratic
Space complexity: linear
// A C++ program to check if arr[0..n-1] can be divided
// in pairs such that every pair is divisible by k.
#include <bits/stdc++.h>
using namespace std;
 
// Returns true if arr[0..n-1] can be divided into pairs
// with sum divisible by k.
bool canPairs(int arr[], int n, int k)
{
    // An odd length array cannot be divided into pairs
    if (n & 1)
        return false;
 
    // Create a frequency array to count occurrences
    // of all remainders when divided by k.
    unordered_map<int, int> freq;
 
    // Count occurrences of all remainders
    for (int i = 0; i < n; i++)
        freq[((arr[i] % k) + k) % k]++;
 
    // Traverse input array and use freq[] to decide
    // if given array can be divided in pairs
    for (int i = 0; i < n; i++) {
        // Remainder of current element
        int rem = ((arr[i] % k) + k) % k;
 
        // If remainder with current element divides
        // k into two halves.
        if (2 * rem == k) {
            // Then there must be even occurrences of
            // such remainder
            if (freq[rem] % 2 != 0)
                return false;
        }
 
        // If remainder is 0, then there must be even
        // number of elements with 0 remainder
        else if (rem == 0) {
            if (freq[rem] & 1)
                return false;
        }
 
        // Else number of occurrences of remainder
        // must be equal to number of occurrences of
        // k - remainder
        else if (freq[rem] != freq[k - rem])
            return false;
    }
    return true;
}
 
// Driver code
int main()
{
    int arr[] = { 92, 75, 65, 48, 45, 35 };
    int k = 10;
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    canPairs(arr, n, k) ? cout << "True" : cout << "False";
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ implementation to find the longest subarray
// with sum divisible by k
 
#include <bits/stdc++.h>
using namespace std;
 
// function to find the longest subarray
// with sum divisible by k
 
int longestSubarrWthSumDivByK(int arr[], int n, int k)
{
    // unordered map 'um' implemented as
    // hash table
    unordered_map<int, int> um;
 
    // 'mod_arr[i]' stores (sum[0..i] % k)
    int mod_arr[n], max_len = 0;
    int curr_sum = 0;
 
    // traverse arr[] and build up the
    // array 'mod_arr[]'
    for (int i = 0; i < n; i++) {
        curr_sum += arr[i];
 
        // as the sum can be negative, taking modulo twice
        mod_arr[i] = ((curr_sum % k) + k) % k;
 
        // if true then sum(0..i) is divisible by k
        if (mod_arr[i] == 0)
            // update 'max'
            max_len = i + 1;
 
        // if value 'mod_arr[i]' not present in 'um'
        // then store it in 'um' with index of its
        // first occurrence
        else if (um.find(mod_arr[i]) == um.end())
            um[mod_arr[i]] = i;
 
        else
            // if true, then update 'max'
            if (max_len < (i - um[mod_arr[i]]))
            max_len = i - um[mod_arr[i]];
    }
 
    // return the required length of longest subarray
    // with sum divisible by 'k'
    return max_len;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 7, 6, 1, 4, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 3;
 
    cout << "Length = "
         << longestSubarrWthSumDivByK(arr, n, k);
 
    return 0;
}
 
// Code updated by Kshitij Dwivedi

Time complexity: linear
Space complexity: linear
#include <bits/stdc++.h>
 
using namespace std;
 
// function to find the longest subarray
// with sum divisible by k
int longestSubarrWthSumDivByK(int arr[], int n, int k)
{
    // unordered map 'um' implemented as
    // hash table
    unordered_map<int, int> um;
 
    int max_len = 0;
    int curr_sum = 0;
 
    for (int i = 0; i < n; i++) {
        curr_sum += arr[i];
 
        int mod = ((curr_sum % k) + k) % k;
 
        // if true then sum(0..i) is divisible
        // by k
        if (mod == 0)
            // update 'max_len'
            max_len = i + 1;
 
        // if value 'mod_arr[i]' not present in 'um'
        // then store it in 'um' with index of its
        // first occurrence
        else if (um.find(mod) == um.end())
            um[mod] = i;
 
        else
            // if true, then update 'max_len'
            if (max_len < (i - um[mod]))
            max_len = i - um[mod];
    }
 
    // return the required length of longest subarray with
    // sum divisible by 'k'
    return max_len;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 7, 6, 1, 4, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 3;
 
    cout << "Length = "
         << longestSubarrWthSumDivByK(arr, n, k);
 
    return 0;
}
 
// Code Updated by Kshitij Dwivedi

Time complexity: linear
Space complexity: linear
// CPP code to find the subarray with
// no pair sum divisible by K
#include<bits/stdc++.h>
using namespace std;
 
// function to find the subarray with
// no pair sum divisible by k
void subarrayDivisibleByK(int arr[], int n, int k)
{
    // hash table to store the remainders
    // obtained on dividing by K
    map<int,int> mp;
 
    // s : starting index of the
    // current subarray, e : ending
    // index of the current subarray, maxs :
    // starting index of the maximum
    // size subarray so far, maxe : ending
    // index of the maximum size subarray
    // so far
    int s = 0, e = 0, maxs = 0, maxe = 0;
 
    // insert the first element in the set
    mp[arr[0] % k]++;
 
    for (int i = 1; i < n; i++)
    {
        int mod = arr[i] % k;
 
        // Removing starting elements of current
        // subarray while there is an element in
        // set which makes a pair with mod[i] such
        // that the pair sum is divisible.
        while (mp[k - mod] != 0 ||
              (mod == 0 && mp[mod] != 0))
        {
            mp[arr[s] % k]--;
            s++;
        }
 
        // include the current element in
        // the current subarray the ending
        // index of the current subarray
        // increments by one
        mp[mod]++;
        e++;
 
        // compare the size of the current
        // subarray with the maximum size so
        // far
        if ((e - s) > (maxe - maxs))
        {
            maxe = e;
            maxs = s;
        }
 
    }
 
    cout << "The maximum size is "
         << maxe - maxs + 1 << " and "
         "the subarray is as follows\n";
 
    for (int i=maxs; i<=maxe; i++)
        cout << arr[i] << " ";
}
 
int main()
{
    int k = 3;
    int arr[] = {5, 10, 15, 20, 25};
    int n = sizeof(arr)/sizeof(arr[0]);
    subarrayDivisibleByK(arr, n, k);
    return 0;
}

Time complexity: nlogn
Space complexity: linear
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find special numbers
void divisibilityCheck(int arr[], int n)
{
 
    // Storing all array elements in a hash
    // and finding maximum element in the array
    unordered_set<int> s;
    int max_ele = INT_MIN;
    for (int i = 0; i < n; i++) {
        s.insert(arr[i]);
 
        // Update the maximum element of the array
        max_ele = max(max_ele, arr[i]);
    }
 
    // Traversing the array elements and storing the array
    // multiples that are present in s in res
    unordered_set<int> res;
    for (int i = 0; i < n; i++) {
 
        // Check for non-zero values only
        if (arr[i] != 0) {
 
            // Checking the factors of current element
            for (int j = arr[i] * 2; j <= max_ele; j += arr[i]) {
 
                // If current factor is already part
                // of the array then store it
                if (s.find(j) != s.end())
                    res.insert(j);
            }
        }
    }
 
    // For non-distinct elmments
    // To store the frequency of elements
    unordered_map<int, int> mp;
    for (int i = 0; i < n; i++)
        mp[arr[i]]++;
 
    unordered_map<int, int>::iterator it;
    vector<int> ans;
    for (it = mp.begin(); it != mp.end(); it++) {
 
        // If frequency is at least 2
        if (it->second >= 2) {
            if (res.find(it->first) == res.end()) {
 
                // If frequency is greater than 1 and
                // the number is not divisible by
                // any other number
                int val = it->second;
 
                // Then we push the element number of
                // times it is present in the vector
                while (val--)
                    ans.push_back(it->first);
            }
        }
 
        // If frequency is greater than 1 and the number
        // is divisible by any other number
        if (res.find(it->first) != res.end()) {
            int val = it->second;
 
            // Then we push the element number of
            // times it is present in the vector
            while (val--)
                ans.push_back(it->first);
        }
    }
 
    // Print the elements that are divisible by
    // at least one other element from the array
    for (auto x : ans)
        cout << x << " ";
}
 
// Driver code
int main()
{
    int arr[] = { 2, 3, 8, 6, 9, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    divisibilityCheck(arr, n);
 
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// C++ program to find three element
// from different three arrays such
// that a + b + c is equal to
// given sum
#include<bits/stdc++.h>
using namespace std;
 
// Function to check if there is
// an element from each array such
// that sum of the three elements
// is equal to given sum.
bool findTriplet(int a1[], int a2[],
                 int a3[], int n1,
                 int n2, int n3, int sum)
{
    for (int i = 0; i < n1; i++)
    for (int j = 0; j < n2; j++)
        for (int k = 0; k < n3; k++)
            if (a1[i] + a2[j] + a3[k] == sum)
            return true;
 
    return false;
}
 
// Driver Code
int main()
{
    int a1[] = { 1 , 2 , 3 , 4 , 5 };
    int a2[] = { 2 , 3 , 6 , 1 , 2 };
    int a3[] = { 3 , 2 , 4 , 5 , 6 };
    int sum = 9;
    int n1 = sizeof(a1) / sizeof(a1[0]);
    int n2 = sizeof(a2) / sizeof(a2[0]);
    int n3 = sizeof(a3) / sizeof(a3[0]);
    findTriplet(a1, a2, a3, n1, n2, n3, sum)?
                cout << "Yes" : cout << "No";
    return 0;
}

Time complexity: cubic
Space complexity: constant
// C++ program to find three element
// from different three arrays such
// that a + b + c is equal to
// given sum
#include<bits/stdc++.h>
using namespace std;
 
// Function to check if there is
// an element from each array such
// that sum of the three elements is
// equal to given sum.
bool findTriplet(int a1[], int a2[],
                 int a3[], int n1,
                 int n2, int n3,
                 int sum)
{
    // Store elements of
    // first array in hash
    unordered_set <int> s;
    for (int i = 0; i < n1; i++)
        s.insert(a1[i]);
 
    // sum last two arrays
    // element one by one
    for (int i = 0; i < n2; i++)
    {
        for (int j = 0; j < n3; j++)
        {
            // Consider current pair and
            // find if there is an element
            // in a1[] such that these three
            // form a required triplet
            if (s.find(sum - a2[i] - a3[j]) !=
                                       s.end())
                return true;
        }
    }
 
    return false;
}
 
// Driver Code
int main()
{
    int a1[] = { 1 , 2 , 3 , 4 , 5 };
    int a2[] = { 2 , 3 , 6 , 1 , 2 };
    int a3[] = { 3 , 2 , 4 , 5 , 6 };
    int sum = 9;
    int n1 = sizeof(a1) / sizeof(a1[0]);
    int n2 = sizeof(a2) / sizeof(a2[0]);
    int n3 = sizeof(a3) / sizeof(a3[0]);
    findTriplet(a1, a2, a3, n1, n2, n3, sum)?
    cout << "Yes" : cout << "No";
 
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Returns length of the largest
// subarray with 0 sum
int maxLen(int arr[], int N)
{
    // Initialize result
    int max_len = 0;
 
    // Pick a starting point
    for (int i = 0; i < N; i++) {
 
        // Initialize currr_sum for
        // every starting point
        int curr_sum = 0;
 
        // Try all subarrays starting with 'i'
        for (int j = i; j < N; j++) {
            curr_sum += arr[j];
 
            // If curr_sum becomes 0,
            // then update max_len
            // if required
            if (curr_sum == 0)
                max_len = max(max_len, j - i + 1);
        }
    }
    return max_len;
}
 
// Driver's Code
int main()
{
    int arr[] = {15, -2, 2, -8, 1, 7, 10, 23};
    int N = sizeof(arr) / sizeof(arr[0]);
   
  // Function call
    cout << "Length of the longest 0 sum subarray is "
         << maxLen(arr, N);
    return 0;
}

Time complexity: quadratic
Space complexity: constant