Banana-Leopard/TASTY huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
int main()
{
    vector<int> V = { 11, 65, 193, 36, 209, 664, 32 };
    partial_sort(V.begin(), V.begin() + 3, V.end(), greater<int>());
 
    cout << "first = " << V[0] << "\n";
    cout << "second = " << V[1] << "\n";
    cout << "third = " << V[2] << "\n";
    return 0;
}

Time complexity: nlogn
Space complexity: constant
// Simple C++ program to find
// all elements in array which
// have at-least two greater
// elements itself.
#include<bits/stdc++.h>
using namespace std;
 
void findElements(int arr[], int n)
{
    // Pick elements one by one and
    // count greater elements. If
    // count is more than 2, print
    // that element.
    for (int i = 0; i < n; i++)
    {
        int count = 0;
        for (int j = 0; j < n; j++)
            if (arr[j] > arr[i])
                count++;
 
        if (count >= 2)
            cout << arr[i] << " ";
    }
}
 
// Driver code
int main()
{
    int arr[] = { 2, -6 ,3 , 5, 1};
    int n = sizeof(arr) / sizeof(arr[0]);
    findElements(arr, n);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// Sorting based C++ program to
// find all elements in array
// which have atleast two greater
// elements itself.
#include<bits/stdc++.h>
using namespace std;
 
void findElements(int arr[], int n)
{
    sort(arr, arr + n);
 
    for (int i = 0; i < n - 2; i++)
    cout << arr[i] << " ";
}
 
// Driver Code
int main()
{
    int arr[] = { 2, -6 ,3 , 5, 1};
    int n = sizeof(arr) / sizeof(arr[0]);
    findElements(arr, n);
    return 0;
}

Time complexity: nlogn
Space complexity: constant
// C++ program to find all elements
// in array which have atleast two
// greater elements itself.
#include<bits/stdc++.h>
using namespace std;
 
void findElements(int arr[], int n)
{
    int first = INT_MIN,
        second = INT_MIN;
    for (int i = 0; i < n; i++)
    {
        /* If current element is smaller
        than first then update both first
        and second */
        if (arr[i] > first)
        {
            second = first;
            first = arr[i];
        }
 
        /* If arr[i] is in between first
        and second then update second */
        else if (arr[i] > second)
            second = arr[i];
    }
 
    for (int i = 0; i < n; i++)
        if (arr[i] < second)
            cout << arr[i] << " ";
}
 
// Driver code
int main()
{
    int arr[] = { 2, -6, 3, 5, 1};
    int n = sizeof(arr) / sizeof(arr[0]);
    findElements(arr, n);
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP program to find mean and median of
// an array
#include <bits/stdc++.h>
using namespace std;
 
// Function for calculating mean
double findMean(int a[], int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += a[i];
 
    return (double)sum / (double)n;
}
 
// Function for calculating median
double findMedian(int a[], int n)
{
    // First we sort the array
    sort(a, a + n);
 
    // check for even case
    if (n % 2 != 0)
        return (double)a[n / 2];
 
    return (double)(a[(n - 1) / 2] + a[n / 2]) / 2.0;
}
 
// Driver code
int main()
{
    int a[] = { 1, 3, 4, 2, 7, 5, 8, 6 };
    int N = sizeof(a) / sizeof(a[0]);
   
    // Function call
    cout << "Mean = " << findMean(a, N) << endl;
    cout << "Median = " << findMedian(a, N) << endl;
    return 0;
}

Time complexity: nlogn
Space complexity: constant
// C++ program to find med in
// stream of running integers
#include<bits/stdc++.h>
using namespace std;
  
// function to calculate med of stream
void printMedians(double arr[], int n)
{
    // max heap to store the smaller half elements
    priority_queue<double> s;
  
    // min heap to store the greater half elements
    priority_queue<double,vector<double>,greater<double> > g;
  
    double med = arr[0];
    s.push(arr[0]);
  
    cout << med << endl;
  
    // reading elements of stream one by one
    /*  At any time we try to make heaps balanced and
        their sizes differ by at-most 1. If heaps are
        balanced,then we declare median as average of
        min_heap_right.top() and max_heap_left.top()
        If heaps are unbalanced,then median is defined
        as the top element of heap of larger size  */
    for (int i=1; i < n; i++)
    {
        double x = arr[i];
  
        // case1(left side heap has more elements)
        if (s.size() > g.size())
        {
            if (x < med)
            {
                g.push(s.top());
                s.pop();
                s.push(x);
            }
            else
                g.push(x);
  
            med = (s.top() + g.top())/2.0;
        }
  
        // case2(both heaps are balanced)
        else if (s.size()==g.size())
        {
            if (x < med)
            {
                s.push(x);
                med = (double)s.top();
            }
            else
            {
                g.push(x);
                med = (double)g.top();
            }
        }
  
        // case3(right side heap has more elements)
        else
        {
            if (x > med)
            {
                s.push(g.top());
                g.pop();
                g.push(x);
            }
            else
                s.push(x);
  
            med = (s.top() + g.top())/2.0;
        }
  
        cout << med << endl;
    }
}
  
// Driver program to test above functions
int main()
{
    // stream of integers
    double arr[] = {5, 15, 10, 20, 3};
    int n = sizeof(arr)/sizeof(arr[0]);
    printMedians(arr, n);
    return 0;
}

Time complexity: nlogn
Space complexity: linear
// CPP program to find minimum product of
// k elements in an array
#include <bits/stdc++.h>
using namespace std;
 
int minProduct(int arr[], int n, int k)
{
    priority_queue<int, vector<int>, greater<int> > pq;
    for (int i = 0; i < n; i++)
        pq.push(arr[i]);
 
    int count = 0, ans = 1;
 
    // One by one extract items from max heap
    while (pq.empty() == false && count < k) {
        ans = ans * pq.top();
        pq.pop();
        count++;
    }
 
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = { 198, 76, 544, 123, 154, 675 };
    int k = 2;
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Minimum product is " << minProduct(arr, n, k);
    return 0;
}

Time complexity: nlogn
Space complexity: linear
// A simple C++ program to find N maximum
// combinations from two arrays,
#include <bits/stdc++.h>
using namespace std;
 
// function to display first N maximum sum
// combinations
void KMaxCombinations(int A[], int B[],
                      int N, int K)
{
    // max heap.
    priority_queue<int> pq;
 
    // insert all the possible combinations
    // in max heap.
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            pq.push(A[i] + B[j]);
 
    // pop first N elements from max heap
    // and display them.
    int count = 0;
    while (count < K) {
        cout << pq.top() << endl;
        pq.pop();
        count++;
    }
}
 
// Driver Code.
int main()
{
    int A[] = { 4, 2, 5, 1 };
    int B[] = { 8, 0, 5, 3 };
    int N = sizeof(A) / sizeof(A[0]);
    int K = 3;
   
    // Function call
    KMaxCombinations(A, B, N, K);
    return 0;
}

Time complexity: quadratic
Space complexity: quadratic
// An efficient C++ program to find top K elements
// from two arrays.
#include <bits/stdc++.h>
using namespace std;
 
// Function prints k maximum possible combinations
void KMaxCombinations(vector<int>& A,
                      vector<int>& B, int K)
{
    // sort both arrays A and B
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
 
    int N = A.size();
 
    // Max heap which contains tuple of the format
    // (sum, (i, j)) i and j are the indices
    // of the elements from array A
    // and array B which make up the sum.
    priority_queue<pair<int, pair<int, int> > > pq;
 
    // my_set is used to store the indices of
    // the  pair(i, j) we use my_set to make sure
    // the indices does not repeat inside max heap.
    set<pair<int, int> > my_set;
 
    // initialize the heap with the maximum sum
    // combination ie (A[N - 1] + B[N - 1])
    // and also push indices (N - 1, N - 1) along
    // with sum.
    pq.push(make_pair(A[N - 1] + B[N - 1],
                      make_pair(N - 1, N - 1)));
 
    my_set.insert(make_pair(N - 1, N - 1));
 
    // iterate upto K
    for (int count = 0; count < K; count++)
    {
        // tuple format (sum, (i, j)).
        pair<int, pair<int, int> > temp = pq.top();
        pq.pop();
 
        cout << temp.first << endl;
 
        int i = temp.second.first;
        int j = temp.second.second;
 
        int sum = A[i - 1] + B[j];
 
        // insert (A[i - 1] + B[j], (i - 1, j))
        // into max heap.
        pair<int, int> temp1 = make_pair(i - 1, j);
 
        // insert only if the pair (i - 1, j) is
        // not already present inside the map i.e.
        // no repeating pair should be present inside
        // the heap.
        if (my_set.find(temp1) == my_set.end())
        {
            pq.push(make_pair(sum, temp1));
            my_set.insert(temp1);
        }
 
        // insert (A[i] + B[j - 1], (i, j - 1))
        // into max heap.
        sum = A[i] + B[j - 1];
        temp1 = make_pair(i, j - 1);
 
        // insert only if the pair (i, j - 1)
        // is not present inside the heap.
        if (my_set.find(temp1) == my_set.end())
        {
            pq.push(make_pair(sum, temp1));
            my_set.insert(temp1);
        }
    }
}
 
// Driver Code.
int main()
{
    vector<int> A = { 1, 4, 2, 3 };
    vector<int> B = { 2, 5, 1, 6 };
    int K = 4;
   
    // Function call
    KMaxCombinations(A, B, K);
    return 0;
}

Time complexity: nlogn
Space complexity: linear
// CPP for printing smallest k numbers in order
 
#include <algorithm>
#include <iostream>
using namespace std;
 
// Function to print smallest k numbers
// in arr[0..n-1]
void printSmall(int arr[], int n, int k)
{
    // For each arr[i] find whether
    // it is a part of n-smallest
    // with insertion sort concept
    for (int i = k; i < n; ++i) {
 
        // find largest from first k-elements
        int max_var = arr[k - 1];
        int pos = k - 1;
 
        for (int j = k - 2; j >= 0; j--) {
            if (arr[j] > max_var) {
                max_var = arr[j];
                pos = j;
            }
        }
 
        // if largest is greater than arr[i]
        // shift all element one place left
        if (max_var > arr[i]) {
 
            int j = pos;
            while (j < k - 1) {
                arr[j] = arr[j + 1];
                j++;
            }
 
            // make arr[k-1] = arr[i]
            arr[k - 1] = arr[i];
        }
    }
 
    // print result
    for (int i = 0; i < k; i++)
        cout << arr[i] << " ";
}
 
// Driver program
int main()
{
    int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 5;
    printSmall(arr, n, k);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program to prints first k pairs with least sum from two
// arrays.
#include<bits/stdc++.h>
 
using namespace std;
 
// Function to find k pairs with least sum such
// that one element of a pair is from arr1[] and
// other element is from arr2[]
void kSmallestPair(int arr1[], int n1, int arr2[],
                                   int n2, int k)
{
    if (k > n1*n2)
    {
        cout << "k pairs don't exist";
        return ;
    }
 
    // Stores current index in arr2[] for
    // every element of arr1[]. Initially
    // all values are considered 0.
    // Here current index is the index before
    // which all elements are considered as
    // part of output.
    int index2[n1];
    memset(index2, 0, sizeof(index2));
 
    while (k > 0)
    {
        // Initialize current pair sum as infinite
        int min_sum = INT_MAX;
        int min_index = 0;
 
        // To pick next pair, traverse for all elements
        // of arr1[], for every element, find corresponding
        // current element in arr2[] and pick minimum of
        // all formed pairs.
        for (int i1 = 0; i1 < n1; i1++)
        {
            // Check if current element of arr1[] plus
            // element of array2 to be used gives minimum
            // sum
            if (index2[i1] < n2 &&
                arr1[i1] + arr2[index2[i1]] < min_sum)
            {
                // Update index that gives minimum
                min_index = i1;
 
                // update minimum sum
                min_sum = arr1[i1] + arr2[index2[i1]];
            }
        }
 
        cout << "(" << arr1[min_index] << ", "
             << arr2[index2[min_index]] << ") ";
 
        index2[min_index]++;
 
        k--;
    }
}
 
// Driver code
int main()
{
    int arr1[] = {1, 3, 11};
    int n1 = sizeof(arr1) / sizeof(arr1[0]);
 
    int arr2[] = {2, 4, 8};
    int n2 = sizeof(arr2) / sizeof(arr2[0]);
 
    int k = 4;
    kSmallestPair( arr1, n1, arr2, n2, k);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to Prints
// first k pairs with
// least sum from two arrays.
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find k pairs
// with least sum such
// that one element of a pair
// is from arr1[] and
// other element is from arr2[]
void kSmallestPair(vector<int> A, vector<int> B, int K)
{
 
    int n = A.size();
 
    // Min heap which contains tuple of the format
    // (sum, (i, j)) i and j are the indices
    // of the elements from array A
    // and array B which make up the sum.
 
    priority_queue<pair<int, pair<int, int> >,
                   vector<pair<int, pair<int, int> > >,
                   greater<pair<int, pair<int, int> > > >
        pq;
 
    // my_set is used to store the indices of
    // the  pair(i, j) we use my_set to make sure
    // the indices does not repeat inside min heap.
 
    set<pair<int, int> > my_set;
 
    // initialize the heap with the minimum sum
    // combination i.e. (A[0] + B[0])
    // and also push indices (0,0) along
    // with sum.
 
    pq.push(make_pair(A[0] + B[0], make_pair(0, 0)));
 
    my_set.insert(make_pair(0, 0));
 
    // iterate upto K
    int flag = 1;
    for (int count = 0; count < K && flag; count++) {
 
        // tuple format (sum, i, j).
        pair<int, pair<int, int> > temp = pq.top();
        pq.pop();
 
        int i = temp.second.first;
        int j = temp.second.second;
 
        cout << "(" << A[i] << ", " << B[j] << ")"
             << endl; // Extracting pair with least sum such
                      // that one element is from arr1 and
                      // another is from arr2
 
        // check if i+1 is in the range of our first array A
        flag = 0;
        if (i + 1 < A.size()) {
            int sum = A[i + 1] + B[j];
            // insert (A[i + 1] + B[j], (i + 1, j))
            // into min heap.
            pair<int, int> temp1 = make_pair(i + 1, j);
 
            // insert only if the pair (i + 1, j) is
            // not already present inside the map i.e.
            // no repeating pair should be present inside
            // the heap.
 
            if (my_set.find(temp1) == my_set.end()) {
                pq.push(make_pair(sum, temp1));
                my_set.insert(temp1);
            }
            flag = 1;
        }
        // check if j+1 is in the range of our second array
        // B
        if (j + 1 < B.size()) {
            // insert (A[i] + B[j + 1], (i, j + 1))
            // into min heap.
 
            int sum = A[i] + B[j + 1];
            pair<int, int> temp1 = make_pair(i, j + 1);
 
            // insert only if the pair (i, j + 1)
            // is not present inside the heap.
 
            if (my_set.find(temp1) == my_set.end()) {
                pq.push(make_pair(sum, temp1));
                my_set.insert(temp1);
            }
            flag = 1;
        }
    }
}
 
// Driver Code.
int main()
{
    vector<int> A = { 1 };
    vector<int> B = { 2, 4, 5, 9 };
    int K = 8;
    kSmallestPair(A, B, K);
    return 0;
}
 
// This code is contributed by Dhairya.

Time complexity: nlogn
Space complexity: linear
// C++ program to find k-th absolute difference
// between two elements
#include<bits/stdc++.h>
using namespace std;
 
// returns number of pairs with absolute difference
// less than or equal to mid.
int countPairs(int *a, int n, int mid)
{
    int res = 0;
    for (int i = 0; i < n; ++i)
 
        // Upper bound returns pointer to position
        // of next higher number than a[i]+mid in
        // a[i..n-1]. We subtract (a + i + 1) from
        // this position to count
        res += upper_bound(a+i, a+n, a[i] + mid) -
                                    (a + i + 1);
    return res;
}
 
// Returns k-th absolute difference
int kthDiff(int a[], int n, int k)
{
    // Sort array
    sort(a, a+n);
 
    // Minimum absolute difference
    int low = a[1] - a[0];
    for (int i = 1; i <= n-2; ++i)
        low = min(low, a[i+1] - a[i]);
 
    // Maximum absolute difference
    int high = a[n-1] - a[0];
 
    // Do binary search for k-th absolute difference
    while (low < high)
    {
        int mid = (low+high)>>1;
        if (countPairs(a, n, mid) < k)
            low = mid + 1;
        else
            high = mid;
    }
 
    return low;
}
 
// Driver code
int main()
{
    int k = 3;
    int a[] = {1, 2, 3, 4};
    int n = sizeof(a)/sizeof(a[0]);
    cout << kthDiff(a, n, k);
    return 0;
}

Time complexity: nlogn
Space complexity: constant
// C++ program to find second largest element in an array
 
#include <bits/stdc++.h>
using namespace std;
 
/* Function to print the second largest elements */
void print2largest(int arr[], int arr_size)
{
    int i, first, second;
    /* There should be atleast two elements */
    if (arr_size < 2) {
        printf(" Invalid Input ");
        return;
    }
    // sort the array
    sort(arr, arr + arr_size);
    // start from second last element as the largest element
    // is at last
    for (i = arr_size - 2; i >= 0; i--) {
        // if the element is not equal to largest element
        if (arr[i] != arr[arr_size - 1]) {
            printf("The second largest element is %d\n",arr[i]);
            return;
        }
    }
    printf("There is no second largest element\n");
}
 
/* Driver program to test above function */
int main()
{
    int arr[] = { 12, 35, 1, 10, 34, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    print2largest(arr, n);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Time complexity: nlogn
Space complexity: constant
// C++ program to find the second largest element in the array
#include <iostream>
using namespace std;
 
 
int secondLargest(int arr[], int n) {
    int largest = 0, secondLargest = -1;
 
    // finding the largest element in the array
    for (int i = 1; i < n; i++) {
        if (arr[i] > arr[largest])
            largest = i;
    }
 
    // finding the largest element in the array excluding
    // the largest element calculated above
    for (int i = 0; i < n; i++) {
        if (arr[i] != arr[largest]) {
            // first change the value of second largest
            // as soon as the next element is found
            if (secondLargest == -1)
                secondLargest = i;
            else if (arr[i] > arr[secondLargest])
                secondLargest = i;
        }
    }
    return secondLargest;
}
 
 
int main() {
    int arr[] = {10, 12, 20, 4};
    int n = sizeof(arr)/sizeof(arr[0]);
    int second_Largest = secondLargest(arr, n);
    if (second_Largest == -1)
        cout << "Second largest didn't exit\n";
    else
        cout << "Second largest : " << arr[second_Largest];
}

Time complexity: linear
Space complexity: constant
// C++ program to find the second largest element
 
#include <iostream>
using namespace std;
 
// returns the index of second largest
// if second largest didn't exist return -1
int secondLargest(int arr[], int n) {
    int first = 0, second = -1;
    for (int i = 1; i < n; i++) {
        if (arr[i] > arr[first]) {
            second = first;
            first = i;
        }
        else if (arr[i] < arr[first]) {
            if (second == -1 || arr[second] < arr[i])
                second = i;
        }
    }
    return second;
}
 
int main() {
    int arr[] = {10, 12, 20, 4};
    int index = secondLargest(arr, sizeof(arr)/sizeof(arr[0]));
    if (index == -1)
        cout << "Second Largest didn't exist";
    else
        cout << "Second largest : " << arr[index];
}

Time complexity: linear
Space complexity: constant
// 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: linear
// C++ program to find k numbers with most
// occurrences in the given array
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the k numbers with most occurrences
void print_N_mostFrequentNumber(int arr[], int N, int K)
{
    // HashMap to store count of the elements
    unordered_map<int, int> elementCount;
    for (int i = 0; i < N; i++) {
        elementCount[arr[i]]++;
    }
 
    // Array to store the elements according
    // to their frequency
    vector<vector<int> > frequency(N + 1);
 
    // Inserting elements in the frequency array
    for (auto element : elementCount) {
        frequency[element.second].push_back(element.first);
    }
 
    int count = 0;
    cout << K << " numbers with most occurrences are:\n";
 
    for (int i = frequency.size() - 1; i >= 0; i--) {
 
        for (auto element : frequency[i]) {
            count++;
            cout << element << " ";
        }
 
        // if K elements have been printed
        if (count == K)
            return;
    }
 
    return;
}
 
// 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: linear
Space complexity: linear
//C++ simple approach to print smallest
//and second smallest element.
#include<bits/stdc++.h>
using namespace std;
int main() {
int arr[]={111, 13, 25, 9, 34, 1};
int n=sizeof(arr)/sizeof(arr[0]);
//sorting the array using
//in-built sort function
sort(arr,arr+n);
//printing the desired element
cout<<"smallest element is "<<arr[0]<<endl;
cout<<"second smallest element is "<<arr[1];
return 0;
}
 
//this code is contributed by Machhaliya Muhammad

Time complexity: nlogn
Space complexity: constant