Banana-Leopard/TASTY huggingface dataset:
// C++ program to print connected components in
// an undirected graph
#include <bits/stdc++.h>
using namespace std;
 
// Graph class represents a undirected graph
// using adjacency list representation
class Graph {
    int V; // No. of vertices
 
    // Pointer to an array containing adjacency lists
    list<int>* adj;
 
    // A function used by DFS
    void DFSUtil(int v, bool visited[]);
 
public:
    Graph(int V); // Constructor
    ~Graph();
    void addEdge(int v, int w);
    void connectedComponents();
};
 
// Method to print connected components in an
// undirected graph
void Graph::connectedComponents()
{
    // Mark all the vertices as not visited
    bool* visited = new bool[V];
    for (int v = 0; v < V; v++)
        visited[v] = false;
 
    for (int v = 0; v < V; v++) {
        if (visited[v] == false) {
            // print all reachable vertices
            // from v
            DFSUtil(v, visited);
 
            cout << "\n";
        }
    }
    delete[] visited;
}
 
void Graph::DFSUtil(int v, bool visited[])
{
    // Mark the current node as visited and print it
    visited[v] = true;
    cout << v << " ";
 
    // Recur for all the vertices
    // adjacent to this vertex
    list<int>::iterator i;
    for (i = adj[v].begin(); i != adj[v].end(); ++i)
        if (!visited[*i])
            DFSUtil(*i, visited);
}
 
Graph::Graph(int V)
{
    this->V = V;
    adj = new list<int>[V];
}
 
Graph::~Graph() { delete[] adj; }
 
// method to add an undirected edge
void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w);
    adj[w].push_back(v);
}
 
// Driver code
int main()
{
    // Create a graph given in the above diagram
    Graph g(5); // 5 vertices numbered from 0 to 4
    g.addEdge(1, 0);
    g.addEdge(2, 1);
    g.addEdge(3, 4);
 
    cout << "Following are connected components \n";
    g.connectedComponents();
 
    return 0;
}

Time complexity: linear
Space complexity: linear
#include <bits/stdc++.h>
using namespace std;
 
int merge(int* parent, int x)
{
    if (parent[x] == x)
        return x;
    return merge(parent, parent[x]);
}
 
int connectedcomponents(int n, vector<vector<int> >& edges)
{
    int parent[n];
    for (int i = 0; i < n; i++) {
        parent[i] = i;
    }
    for (auto x : edges) {
        parent[merge(parent, x[0])] = merge(parent, x[1]);
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
        ans += (parent[i] == i);
    }
    for (int i = 0; i < n; i++) {
        parent[i] = merge(parent, parent[i]);
    }
    map<int, list<int> > m;
    for (int i = 0; i < n; i++) {
        m[parent[i]].push_back(i);
    }
    for (auto it = m.begin(); it != m.end(); it++) {
        list<int> l = it->second;
        for (auto x : l) {
            cout << x << " ";
        }
        cout << endl;
    }
    return ans;
}
 
int main()
{
    int n = 5;
    vector<int> e1 = { 0, 1 };
    vector<int> e2 = { 2, 1 };
    vector<int> e3 = { 3, 4 };
    vector<vector<int> > e;
    e.push_back(e1);
    e.push_back(e2);
    e.push_back(e3);
 
    cout << "Following are connected components:\n";
    int a = connectedcomponents(n, e);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
int maxindex(int* dist, int n)
{
    int mi = 0;
    for (int i = 0; i < n; i++) {
        if (dist[i] > dist[mi])
            mi = i;
    }
    return mi;
}
 
void selectKcities(int n, int weights[4][4], int k)
{
    int* dist = new int[n];
    vector<int> centers;
    for (int i = 0; i < n; i++) {
        dist[i] = INT_MAX;
    }
 
    // index of city having the
    // maximum distance to it's
    // closest center
    int max = 0;
    for (int i = 0; i < k; i++) {
        centers.push_back(max);
        for (int j = 0; j < n; j++) {
 
            // updating the distance
            // of the cities to their
            // closest centers
            dist[j] = min(dist[j], weights[max][j]);
        }
 
        // updating the index of the
        // city with the maximum
        // distance to it's closest center
        max = maxindex(dist, n);
    }
 
    // Printing the maximum distance
    // of a city to a center
    // that is our answer
    cout << endl << dist[max] << endl;
 
    // Printing the cities that
    // were chosen to be made
    // centers
    for (int i = 0; i < centers.size(); i++) {
        cout << centers[i] << " ";
    }
    cout << endl;
}
 
// Driver Code
int main()
{
    int n = 4;
    int weights[4][4] = { { 0, 4, 8, 5 },
                          { 4, 0, 10, 7 },
                          { 8, 10, 0, 9 },
                          { 5, 7, 9, 0 } };
    int k = 2;
 
    // Function Call
    selectKcities(n, weights, k);
}
// Contributed by Balu Nagar

Time complexity: quadratic
Space complexity: constant
#include <bits/stdc++.h>
 
using namespace std;
 
void print_sieve(int& x)
{
    int i,temp,digit;
    bool check;
     
    for(i=0;i<=x;i++)
    {
        if(i<10)
        {
            cout<<i<<" ";
            continue;
        }
        check=1;
        temp=i;
        digit=temp%10;
        temp/=10;
        while(temp)
        {
            if(abs(digit-temp%10)!=1)
            {
                check=0;
                break;
            }
            digit=temp%10;
            temp/=10;
        }
        if(check)
        cout<<i<<" ";
    }
}
 
int main()
{
    int x=105;
    print_sieve(x);
 
    return 0;
}

Time complexity: linear
Space complexity: constant
// Finds and prints all jumping numbers smaller than or
// equal to x.
#include <bits/stdc++.h>
using namespace std;
 
// Prints all jumping numbers smaller than or equal to x starting
// with 'num'. It mainly does BFS starting from 'num'.
void bfs(int x, int num)
{
    // Create a queue and enqueue 'i' to it
    queue<int> q;
    q.push(num);
 
    // Do BFS starting from i
    while (!q.empty()) {
        num = q.front();
        q.pop();
 
        if (num <= x) {
            cout << num << " ";
            int last_dig = num % 10;
 
            // If last digit is 0, append next digit only
            if (last_dig == 0)
                q.push((num * 10) + (last_dig + 1));
 
            // If last digit is 9, append previous digit only
            else if (last_dig == 9)
                q.push((num * 10) + (last_dig - 1));
 
            // If last digit is neither 0 nor 9, append both
            // previous and next digits
            else {
                q.push((num * 10) + (last_dig - 1));
                q.push((num * 10) + (last_dig + 1));
            }
        }
    }
}
 
// Prints all jumping numbers smaller than or equal to
// a positive number x
void printJumping(int x)
{
    cout << 0 << " ";
    for (int i = 1; i <= 9 && i <= x; i++)
        bfs(x, i);
}
 
// Driver program
int main()
{
    int x = 40;
    printJumping(x);
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP program to find the minimum number of
// operations required to make all elements
// of array equal
#include <bits/stdc++.h>
using namespace std;
 
// function for min operation
int minOperation (int arr[], int n)
{
    // Insert all elements in hash.
    unordered_map<int, int> hash;
    for (int i=0; i<n; i++)
        hash[arr[i]]++;
 
    // find the max frequency
    int max_count = 0;
    for (auto i : hash)
        if (max_count < i.second)
            max_count = i.second;
 
    // return result
    return (n - max_count);
}
 
// driver program
int main()
{
    int arr[] = {1, 5, 2, 1, 3, 2, 1};
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << minOperation(arr, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find maximum distance between two
// same occurrences of a number.
#include<bits/stdc++.h>
using namespace std;
 
// Function to find maximum distance between equal elements
int maxDistance(int arr[], int n)
{
    // Used to store element to first index mapping
    unordered_map<int, int> mp;
 
    // Traverse elements and find maximum distance between
    // same occurrences with the help of map.
    int max_dist = 0;
    for (int i=0; i<n; i++)
    {
        // If this is first occurrence of element, insert its
        // index in map
        if (mp.find(arr[i]) == mp.end())
            mp[arr[i]] = i;
 
        // Else update max distance
        else
            max_dist = max(max_dist, i - mp[arr[i]]);
    }
 
    return max_dist;
}
 
// Driver program to run the case
int main()
{
    int arr[] = {3, 2, 1, 2, 1, 4, 5, 8, 6, 7, 4, 2};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << maxDistance(arr, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
/* C/C++ program to find maximum number of point
which lie on same line */
#include <bits/stdc++.h>
#include <boost/functional/hash.hpp>
 
using namespace std;
 
// method to find maximum collinear point
int maxPointOnSameLine(vector< pair<int, int> > points)
{
    int N = points.size();
    if (N < 2)
        return N;
 
    int maxPoint = 0;
    int curMax, overlapPoints, verticalPoints;
 
    // here since we are using unordered_map
    // which is based on hash function
    //But by default we don't have hash function for pairs
    //so we'll use hash function defined in Boost library
    unordered_map<pair<int, int>, int,boost::
              hash<pair<int, int> > > slopeMap;
 
    // looping for each point
    for (int i = 0; i < N; i++)
    {
        curMax = overlapPoints = verticalPoints = 0;
 
        // looping from i + 1 to ignore same pair again
        for (int j = i + 1; j < N; j++)
        {
            // If both point are equal then just
            // increase overlapPoint count
            if (points[i] == points[j])
                overlapPoints++;
 
            // If x co-ordinate is same, then both
            // point are vertical to each other
            else if (points[i].first == points[j].first)
                verticalPoints++;
 
            else
            {
                int yDif = points[j].second - points[i].second;
                int xDif = points[j].first - points[i].first;
                int g = __gcd(xDif, yDif);
 
                // reducing the difference by their gcd
                yDif /= g;
                xDif /= g;
 
                // increasing the frequency of current slope
                // in map
                slopeMap[make_pair(yDif, xDif)]++;
                curMax = max(curMax, slopeMap[make_pair(yDif, xDif)]);
            }
 
            curMax = max(curMax, verticalPoints);
        }
 
        // updating global maximum by current point's maximum
        maxPoint = max(maxPoint, curMax + overlapPoints + 1);
 
        // printf("maximum collinear point
        // which contains current point
        // are : %d\n", curMax + overlapPoints + 1);
        slopeMap.clear();
    }
 
    return maxPoint;
}
 
// Driver code
int main()
{
    const int N = 6;
    int arr[N][2] = {{-1, 1}, {0, 0}, {1, 1}, {2, 2},
                    {3, 3}, {3, 4}};
 
    vector< pair<int, int> > points;
    for (int i = 0; i < N; i++)
        points.push_back(make_pair(arr[i][0], arr[i][1]));
 
    cout << maxPointOnSameLine(points) << endl;
 
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// C++ implementation of the
// above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the Duplicates,
// if duplicate occurs 2 times or
// more than 2 times in array so,
// it will print duplicate value
// only once at output
void findDuplicates(int arr[], int len)
{
     
    // Initialize ifPresent as false
    bool ifPresent = false;
 
    // ArrayList to store the output
    vector<int> al;
 
    for(int i = 0; i < len - 1; i++)
    {
        for(int j = i + 1; j < len; j++)
        {
            if (arr[i] == arr[j])
            {
                 
                // Checking if element is
                // present in the ArrayList
                // or not if present then break
                auto it = std::find(al.begin(),
                                    al.end(), arr[i]);
                                     
                if (it != al.end())
                {
                    break;
                }
 
                // If element is not present in the
                // ArrayList then add it to ArrayList
                // and make ifPresent at true
                else
                {
                    al.push_back(arr[i]);
                    ifPresent = true;
                }
            }
        }
    }
 
    // If duplicates is present
    // then print ArrayList
    if (ifPresent == true)
    {
        cout << "[" << al[0] << ", ";
        for(int i = 1; i < al.size() - 1; i++)
        {
            cout << al[i] << ", ";
        }
         
        cout << al[al.size() - 1] << "]";
    }
    else
    {
        cout << "No duplicates present in arrays";
    }
}
 
// Driver code
int main()
{
    int arr[] = { 12, 11, 40, 12,
                  5, 6, 5, 12, 11 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    findDuplicates(arr, n);
 
    return 0;
}
 
// This code is contributed by divyeshrabadiya07

Time complexity: quadratic
Space complexity: linear
// C++ program to find
// duplicates in the given array
#include <bits/stdc++.h>
using namespace std;
 
// function to find and print duplicates
void printDuplicates(int arr[], int n)
{
    // unordered_map to store frequencies
    unordered_map<int, int> freq;
    for (int i=0; i<n; i++)
        freq[arr[i]]++;
 
    bool dup = false;
    unordered_map<int, int>:: iterator itr;
    for (itr=freq.begin(); itr!=freq.end(); itr++)
    {
        // if frequency is more than 1
        // print the element
        if (itr->second > 1)
        {
            cout << itr->first << " ";
            dup = true;
        }
    }
 
    // no duplicates present
    if (dup == false)
        cout << "-1";
}
 
// Driver program to test above
int main()
{
    int arr[] = {12, 11, 40, 12, 5, 6, 5, 12, 11};
    int n = sizeof(arr) / sizeof(arr[0]);
    printDuplicates(arr, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find top k elements in a stream
#include <bits/stdc++.h>
using namespace std;
 
// Function to print top k numbers
void kTop(int a[], int n, int k)
{
    // vector of size k+1 to store elements
    vector<int> top(k + 1);
 
    // array to keep track of frequency
    unordered_map<int, int> freq;
 
    // iterate till the end of stream
    for (int m = 0; m < n; m++) {
        // increase the frequency
        freq[a[m]]++;
 
        // store that element in top vector
        top[k] = a[m];
 
        // search in top vector for same element
        auto it = find(top.begin(), top.end() - 1, a[m]);
 
        // iterate from the position of element to zero
        for (int i = distance(top.begin(), it) - 1; i >= 0; --i) {
            // compare the frequency and swap if higher
            // frequency element is stored next to it
            if (freq[top[i]] < freq[top[i + 1]])
                swap(top[i], top[i + 1]);
 
            // if frequency is same compare the elements
            // and swap if next element is high
            else if ((freq[top[i]] == freq[top[i + 1]])
                     && (top[i] > top[i + 1]))
                swap(top[i], top[i + 1]);
            else
                break;
        }
 
        // print top k elements
        for (int i = 0; i < k && top[i] != 0; ++i)
            cout << top[i] << ' ';
    }
    cout << endl;
}
 
// Driver program to test above function
int main()
{
    int k = 4;
    int arr[] = { 5, 2, 1, 3, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    kTop(arr, n, k);
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// CPP program to find the most frequent element
// in an array.
#include <bits/stdc++.h>
using namespace std;
 
int mostFrequent(int arr[], int n)
{
    // Sort the array
    sort(arr, arr + n);
 
    // Find the max frequency using linear traversal
    int max_count = 1, res = arr[0], curr_count = 1;
    for (int i = 1; i < n; i++) {
        if (arr[i] == arr[i - 1])
            curr_count++;
        else
            curr_count = 1;
       
        if (curr_count > max_count) {
            max_count = curr_count;
            res = arr[i - 1];
        }
    }
 
    return res;
}
 
// Driver program
int main()
{
    int arr[] = { 40,50,30,40,50,30,30};
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << mostFrequent(arr, n);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Time complexity: nlogn
Space complexity: constant
// CPP program to find the most frequent element
// in an array.
#include <bits/stdc++.h>
using namespace std;
 
int mostFrequent(int arr[], int n)
{
    // Insert all elements in hash.
    unordered_map<int, int> hash;
    for (int i = 0; i < n; i++)
        hash[arr[i]]++;
 
    // find the max frequency
    int max_count = 0, res = -1;
    for (auto i : hash) {
        if (max_count < i.second) {
            res = i.first;
            max_count = i.second;
        }
    }
 
    return res;
}
 
// driver program
int main()
{
    int arr[] = {40,50,30,40,50,30,30 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << mostFrequent(arr, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
#include <iostream>
using namespace std;
 
int maxFreq(int *arr, int n) {
    //using moore's voting algorithm
    int res = 0;
    int count = 1;
    for(int i = 1; i < n; i++) {
        if(arr[i] == arr[res]) {
            count++;
        } else {
            count--;
        }
         
        if(count == 0) {
            res = i;
            count = 1;
        }
         
    }
     
    return arr[res];
}
 
int main()
{
    int arr[] = {40,50,30,40,50,30,30};
    int n = sizeof(arr) / sizeof(arr[0]);
    int freq =  maxFreq(arr , n);
    int count = 0;
    for(int i = 0; i < n; i++) {
        if(arr[i] == freq) {
            count++;
        }
    }
    cout <<"Element " << maxFreq(arr , n) << " occurs " << count << " times" << endl;;
    return 0;
    //This code is contributed by Ashish Kumar Shakya
}

Time complexity: linear
Space complexity: constant
// C++ implementation to find first
// element occurring k times
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the first element
// occurring k number of times
int firstElement(int arr[], int n, int k)
{
    // This loop is used for selection
    //  of elements
    for (int i = 0; i < n; i++) {
        // Count how many time selected element
        // occurs
        int count = 0;
        for (int j = 0; j < n; j++) {
            if (arr[i] == arr[j])
                count++;
        }
 
        // Check, if it occurs k times or not
        if (count == k)
            return arr[i];
    }
 
    return -1;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 7, 4, 3, 4, 8, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 2;
    cout << firstElement(arr, n, k);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ implementation to find first
// element occurring k times
#include <bits/stdc++.h>
 
using namespace std;
 
// function to find the first element
// occurring k number of times
int firstElement(int arr[], int n, int k)
{
    // unordered_map to count
    // occurrences of each element
    unordered_map<int, int> count_map;
    for (int i=0; i<n; i++)
        count_map[arr[i]]++;
     
    for (int i=0; i<n; i++) 
 
        // if count of element == k ,then
        // it is the required first element
        if (count_map[arr[i]] == k)
            return arr[i];
             
    // no element occurs k times
    return -1;
}
 
// Driver program to test above
int main()
{
    int arr[] = {1, 7, 4, 3, 4, 8, 7};
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 2;
    cout << firstElement(arr, n, k);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ implementation to find first
// element occurring k times
#include <bits/stdc++.h>
using namespace std;
 
// function to find the first element
// occurring k number of times
int firstElement(int arr[], int n, int k)
{
 
  // unordered_map to count
  // occurrences of each element
  map<int, int> count_map;
  for (int i = 0; i < n; i++) {
 
    count_map[arr[i]]++;
  }
  // count_map[arr[i]]++;
 
  for (int i = 0; i < n;
       i++) // if count of element == k ,then
    // it is the required first element
  {
    if (count_map.find(arr[i]) != count_map.end()) {
      if (count_map[arr[i]] == k)
        return arr[i];
    }
  }
 
  // no element occurs k times
  return -1;
}
 
// Driver code
int main()
{
  int arr[] = { 1, 7, 4, 3, 4, 8, 7 };
  int n = sizeof(arr) / sizeof(arr[0]);
  int k = 2;
  cout << (firstElement(arr, n, k));
  return 0;
}
 
// This code is contributed by Rajput-Ji

Time complexity: nlogn
Space complexity: linear
// A C++ program to find all symmetric pairs in a given
// array of pairs
#include <bits/stdc++.h>
using namespace std;
 
 
void findSymPairs(int arr[][2], int row)
{
    // This loop for selection of one pair
    for (int i = 0; i < row; i++) {
         
      // This loop for searching of symmetric pair
        for (int j = i + 1; j < row; j++) {
           
            // Condition of symmetric pair
            if (arr[i][0] == arr[j][1]
                and arr[i][1] == arr[j][0])
            {
                cout << "(" << arr[i][0] << ", "
                     << arr[i][1] << ")" << endl;
            }
        }
    }
}
 
// Driver method
int main()
{
    int arr[5][2];
    arr[0][0] = 11;
    arr[0][1] = 20;
    arr[1][0] = 30;
    arr[1][1] = 40;
    arr[2][0] = 5;
    arr[2][1] = 10;
    arr[3][0] = 40;
    arr[3][1] = 30;
    arr[4][0] = 10;
    arr[4][1] = 5;
    cout << "Following pairs have symmetric pairs\n";
    findSymPairs(arr, 5);
}
 
// This is contributed by Arpit Jain

Time complexity: quadratic
Space complexity: constant
#include<bits/stdc++.h>
using namespace std;
 
// A C++ program to find all symmetric pairs in a given array of pairs
// Print all pairs that have a symmetric counterpart
void findSymPairs(int arr[][2], int row)
{
    // Creates an empty hashMap hM
    unordered_map<int, int> hM;
 
    // Traverse through the given array
    for (int i = 0; i < row; i++)
    {
        // First and second elements of current pair
        int first = arr[i][0];
        int sec   = arr[i][1];
 
        // If found and value in hash matches with first
        // element of this pair, we found symmetry
        if (hM.find(sec) != hM.end() && hM[sec] == first)
            cout << "(" << sec << ", " << first << ")" <<endl;
 
        else  // Else put sec element of this pair in hash
            hM[first] = sec;
    }
}
 
// Driver method
int main()
{
    int arr[5][2];
    arr[0][0] = 11; arr[0][1] = 20;
    arr[1][0] = 30; arr[1][1] = 40;
    arr[2][0] = 5;  arr[2][1] = 10;
    arr[3][0] = 40;  arr[3][1] = 30;
    arr[4][0] = 10;  arr[4][1] = 5;
    cout<<"Following pairs have symmetric pairs\n";
    findSymPairs(arr, 5);
}
 
//This is contributed by Chhavi

Time complexity: linear
Space complexity: linear
// C++ program to find one of the repeating
// elements in a read only array
#include <bits/stdc++.h>
using namespace std;
 
// Function to find one of the repeating
// elements
int findRepeatingNumber(const int arr[], int n)
{
    for (int i = 0; i < n; i++) {
        int count = 0;
        for (int j = 0; j < n; j++) {
            if (arr[i] == arr[j])
                count++;
        }
        if (count > 1)
            return arr[i];
    }
    // If no repeating element exists
    return -1;
}
 
// Driver Code
int main()
{
    // Read only array
    const int arr[] = { 1, 1, 2, 3, 5, 4 };
 
    int n = 5;
 
    cout << "One of the numbers repeated in"
            " the array is: "
         << findRepeatingNumber(arr, n) << endl;
}

Time complexity: quadratic
Space complexity: constant