Banana-Leopard/TASTY huggingface dataset:
// C++ program to find minimum number
// of swaps required to sort an array
#include <bits/stdc++.h>
using namespace std;
 
void swap(vector<int> &arr, int i, int j)
{
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
}
 
int indexOf(vector<int> &arr, int ele)
{
    for(int i = 0; i < arr.size(); i++)
    {
        if (arr[i] == ele)
        {
            return i;
        }
    }
    return -1;
}
 
// Return the minimum number
// of swaps required to sort the array
int minSwaps(vector<int> arr, int N)
{
    int ans = 0;
    vector<int> temp(arr.begin(),arr.end());
    sort(temp.begin(),temp.end());
     
    for(int i = 0; i < N; i++)
    {
         
        // This is checking whether
        // the current element is
        // at the right place or not
        if (arr[i] != temp[i])
        {
            ans++;
 
            // Swap the current element
            // with the right index
            // so that arr[0] to arr[i] is sorted
            swap(arr, i, indexOf(arr, temp[i]));
        }
    }
    return ans;
}
 
// Driver Code
int main()
{
 
    vector<int> a = {101, 758, 315, 730,
                   472, 619, 460, 479};
     
    int n = a.size();
     
    // Output will be 5
    cout << minSwaps(a, n);
}
 
// This code is contributed by mohit kumar 29

Time complexity: quadratic
Space complexity: linear
// C++ program to find
// minimum number of swaps
// required to sort an array
#include<bits/stdc++.h>
using namespace std;
 
void swap(vector<int> &arr,
          int i, int j)
{
  int temp = arr[i];
  arr[i] = arr[j];
  arr[j] = temp;
}
// Return the minimum number
// of swaps required to sort
// the array
int minSwaps(vector<int>arr,
             int N)
{
  int ans = 0;
  vector<int>temp = arr;
 
  // Hashmap which stores the
  // indexes of the input array
  map <int, int> h;
 
  sort(temp.begin(), temp.end());
  for (int i = 0; i < N; i++)
  {
    h[arr[i]] = i;
  }
  for (int i = 0; i < N; i++)
  {
    // This is checking whether
    // the current element is
    // at the right place or not
    if (arr[i] != temp[i])
    {
      ans++;
      int init = arr[i];
 
      // If not, swap this element
      // with the index of the
      // element which should come here
      swap(arr, i, h[temp[i]]);
 
      // Update the indexes in
      // the hashmap accordingly
      h[init] = h[temp[i]];
      h[temp[i]] = i;
    }
  }
  return ans;
}
 
// Driver class
int main()
{
  // Driver program to
  // test the above function
  vector <int> a = {101, 758, 315,
                    730, 472, 619,
                    460, 479};
  int n = a.size();
   
  // Output will be 5
  cout << minSwaps(a, n);
}
 
// This code is contributed by Stream_Cipher

Time complexity: nlogn
Space complexity: linear
// C++ program to sort an array
// with 0, 1 and 2 in a single pass
#include <bits/stdc++.h>
using namespace std;
 
// Function to sort the input array,
// the array is assumed
// to have values in {0, 1, 2}
void sort012(int a[], int arr_size)
{
    int lo = 0;
    int hi = arr_size - 1;
    int mid = 0;
 
    // Iterate till all the elements
    // are sorted
    while (mid <= hi) {
        switch (a[mid]) {
 
        // If the element is 0
        case 0:
            swap(a[lo++], a[mid++]);
            break;
 
        // If the element is 1 .
        case 1:
            mid++;
            break;
 
        // If the element is 2
        case 2:
            swap(a[mid], a[hi--]);
            break;
        }
    }
}
 
// Function to print array arr[]
void printArray(int arr[], int arr_size)
{
    // Iterate and print every element
    for (int i = 0; i < arr_size; i++)
        cout << arr[i] << " ";
}
 
// Driver Code
int main()
{
    int arr[] = { 0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    sort012(arr, n);
 
    printArray(arr, n);
 
    return 0;
}
 
// This code is contributed by Shivi_Aggarwal

Time complexity: linear
Space complexity: constant
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Utility function to print the contents of an array
void printArr(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// Function to sort the array of 0s, 1s and 2s
void sortArr(int arr[], int n)
{
    int i, cnt0 = 0, cnt1 = 0, cnt2 = 0;
 
    // Count the number of 0s, 1s and 2s in the array
    for (i = 0; i < n; i++) {
        switch (arr[i]) {
        case 0:
            cnt0++;
            break;
        case 1:
            cnt1++;
            break;
        case 2:
            cnt2++;
            break;
        }
    }
 
    // Update the array
    i = 0;
 
    // Store all the 0s in the beginning
    while (cnt0 > 0) {
        arr[i++] = 0;
        cnt0--;
    }
 
    // Then all the 1s
    while (cnt1 > 0) {
        arr[i++] = 1;
        cnt1--;
    }
 
    // Finally all the 2s
    while (cnt2 > 0) {
        arr[i++] = 2;
        cnt2--;
    }
 
    // Print the sorted array
    printArr(arr, n);
}
 
// Driver code
int main()
{
    int arr[] = { 0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1 };
    int n = sizeof(arr) / sizeof(int);
 
    sortArr(arr, n);
 
    return 0;
}

Time complexity: linear
Space complexity: constant
// This code is contributed by Anjali Saxena
 
#include <bits/stdc++.h>
 
using namespace std;
 
// Function to find position to insert current element of
// stream using binary search
int binarySearch(int arr[], int item, int low, int high)
{
    if (low >= high) {
        return (item > arr[low]) ? (low + 1) : low;
    }
 
    int mid = (low + high) / 2;
 
    if (item == arr[mid])
        return mid + 1;
 
    if (item > arr[mid])
        return binarySearch(arr, item, mid + 1, high);
 
    return binarySearch(arr, item, low, mid - 1);
}
 
// Function to print median of stream of integers
void printMedian(int arr[], int n)
{
    int i, j, pos, num;
    int count = 1;
 
    cout << "Median after reading 1"
         << " element is " << arr[0] << "\n";
 
    for (i = 1; i < n; i++) {
        float median;
        j = i - 1;
        num = arr[i];
 
        // find position to insert current element in sorted
        // part of array
        pos = binarySearch(arr, num, 0, j);
 
        // move elements to right to create space to insert
        // the current element
        while (j >= pos) {
            arr[j + 1] = arr[j];
            j--;
        }
 
        arr[j + 1] = num;
 
        // increment count of sorted elements in array
        count++;
 
        // if odd number of integers are read from stream
        // then middle element in sorted order is median
        // else average of middle elements is median
        if (count % 2 != 0) {
            median = arr[count / 2];
        }
        else {
            median = (arr[(count / 2) - 1] + arr[count / 2])
                     / 2;
        }
 
        cout << "Median after reading " << i + 1
             << " elements is " << median << "\n";
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 5, 15, 1, 3, 2, 8, 7, 9, 10, 6, 11, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    printMedian(arr, n);
 
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the median of stream of data
void streamMed(int A[], int n)
{
    // Declared two max heap
    priority_queue<int> g, s;
   
    for (int i = 0; i < n; i++) {
        s.push(A[i]);
        int temp = s.top();
        s.pop();
       
        // Negation for treating it as min heap
        g.push(-1 * temp);
        if (g.size() > s.size()) {
            temp = g.top();
            g.pop();
            s.push(-1 * temp);
        }
        if (g.size() != s.size())
            cout << (double)s.top() << "\n";
        else
            cout << (double)((s.top() * 1.0
                              - g.top() * 1.0)
                             / 2)
                 << "\n";
    }
}
 
// Driver code
int main()
{
    int A[] = { 5, 15, 1, 3, 2, 8, 7, 9, 10, 6, 11, 4 };
    int N = sizeof(A) / sizeof(A[0]);
   
    // Function call
    streamMed(A, N);
    return 0;
}

Time complexity: nlogn
Space complexity: linear
// C++ code to count the number of possible triangles using
// brute force approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to count all possible triangles with arr[]
// elements
int findNumberOfTriangles(int arr[], int n)
{
    // Count of triangles
    int count = 0;
 
    // The three loops select three different values from
    // array
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
 
            // The innermost loop checks for the triangle
            // property
            for (int k = j + 1; k < n; k++)
 
                // Sum of two sides is greater than the
                // third
                if (arr[i] + arr[j] > arr[k]
                    && arr[i] + arr[k] > arr[j]
                    && arr[k] + arr[j] > arr[i])
                    count++;
        }
    }
    return count;
}
 
// Driver code
int main()
{
    int arr[] = { 10, 21, 22, 100, 101, 200, 300 };
    int size = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    cout << "Total number of triangles possible is "
         << findNumberOfTriangles(arr, size);
    return 0;
}
 
// This code is contributed by Sania Kumari Gupta

Time complexity: cubic
Space complexity: constant
// C++ program to count number of triangles that can be
// formed from given array
#include <bits/stdc++.h>
using namespace std;
 
// Function to count all possible triangles with arr[]
// elements
int findNumberOfTriangles(int arr[], int n)
{
    // Sort the array elements in non-decreasing order
    sort(arr, arr + n);
    // Initialize count of triangles
    int count = 0;
    // Fix the first element. We need to run till n-3
    // as the other two elements are selected from
    // arr[i+1...n-1]
    for (int i = 0; i < n - 2; ++i) {
        // Initialize index of the rightmost third
        // element
        int k = i + 2;
 
        // Fix the second element
        for (int j = i + 1; j < n; ++j) {
            // Find the rightmost element which is smaller
            // than the sum of two fixed elements The
            // important thing to note here is, we use the
            // previous value of k. If value of arr[i] +
            // arr[j-1] was greater than arr[k], then arr[i]
            // + arr[j] must be greater than k, because the
            // array is sorted.
            while (k < n && arr[i] + arr[j] > arr[k])
                ++k;
 
            // Total number of possible triangles that can
            // be formed with the two fixed elements is
            // k - j - 1. The two fixed elements are arr[i]
            // and arr[j]. All elements between arr[j+1]/ to
            // arr[k-1] can form a triangle with arr[i] and
            // arr[j]. One is subtracted from k because k is
            // incremented one extra in above while loop. k
            // will always be greater than j. If j becomes
            // equal to k, then above loop will increment k,
            // because arr[k]
            // + arr[i] is always greater than arr[k]
            if (k > j)
                count += k - j - 1;
        }
    }
 
    return count;
}
 
// Driver code
int main()
{
    int arr[] = { 10, 21, 22, 100, 101, 200, 300 };
    int size = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    cout << "Total number of triangles possible is "
         << findNumberOfTriangles(arr, size);
    return 0;
}
 
// This code is contributed by Sania Kumari Gupta

Time complexity: quadratic
Space complexity: constant
// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
 
void CountTriangles(vector<int> A)
{
 
    int n = A.size();
 
    sort(A.begin(), A.end());
 
    int count = 0;
 
    for (int i = n - 1; i >= 1; i--) {
        int l = 0, r = i - 1;
        while (l < r) {
            if (A[l] + A[r] > A[i]) {
 
                // If it is possible with a[l], a[r]
                // and a[i] then it is also possible
                // with a[l+1]..a[r-1], a[r] and a[i]
                count += r - l;
 
                // checking for more possible solutions
                r--;
            }
            else
 
                // if not possible check for
                // higher values of arr[l]
                l++;
        }
    }
    cout << "No of possible solutions: " << count;
}
 
// Driver code
int main()
{
 
    vector<int> A = { 10, 21, 22, 100, 101, 200, 300 };
 
    // Function call
    CountTriangles(A);
}

Time complexity: quadratic
Space complexity: constant
// C++ program to finds the number of pairs (x, y)
// in an array such that x^y > y^x
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return count of pairs with x as one element
// of the pair. It mainly looks for all values in Y[] where
// x ^ Y[i] > Y[i] ^ x
int count(int x, int Y[], int n, int NoOfY[])
{
    // If x is 0, then there cannot be any value in Y such
    // that x^Y[i] > Y[i]^x
    if (x == 0)
        return 0;
 
    // If x is 1, then the number of pairs is equal to number
    // of zeroes in Y[]
    if (x == 1)
        return NoOfY[0];
 
    // Find number of elements in Y[] with values greater
    // than x upper_bound() gets address of first greater
    // element in Y[0..n-1]
    int* idx = upper_bound(Y, Y + n, x);
    int ans = (Y + n) - idx;
 
    // If we have reached here, then x must be greater than
    // 1, increase number of pairs for y=0 and y=1
    ans += (NoOfY[0] + NoOfY[1]);
 
    // Decrease number of pairs for x=2 and (y=4 or y=3)
    if (x == 2)
        ans -= (NoOfY[3] + NoOfY[4]);
 
    // Increase number of pairs for x=3 and y=2
    if (x == 3)
        ans += NoOfY[2];
 
    return ans;
}
 
// Function to return count of pairs (x, y) such that
// x belongs to X[], y belongs to Y[] and x^y > y^x
int countPairs(int X[], int Y[], int m, int n)
{
    // To store counts of 0, 1, 2, 3 and 4 in array Y
    int NoOfY[5] = { 0 };
    for (int i = 0; i < n; i++)
        if (Y[i] < 5)
            NoOfY[Y[i]]++;
 
    // Sort Y[] so that we can do binary search in it
    sort(Y, Y + n);
 
    int total_pairs = 0; // Initialize result
 
    // Take every element of X and count pairs with it
    for (int i = 0; i < m; i++)
        total_pairs += count(X[i], Y, n, NoOfY);
 
    return total_pairs;
}
 
// Driver program
int main()
{
    int X[] = { 2, 1, 6 };
    int Y[] = { 1, 5 };
 
    int m = sizeof(X) / sizeof(X[0]);
    int n = sizeof(Y) / sizeof(Y[0]);
 
    cout << "Total pairs = " << countPairs(X, Y, m, n);
 
    return 0;
}

Time complexity: nlogn
Space complexity: constant
/* A simple program to count pairs with difference k*/
#include <iostream>
using namespace std;
 
int countPairsWithDiffK(int arr[], int n, int k)
{
    int count = 0;
 
    // Pick all elements one by one
    for (int i = 0; i < n; i++) {
        // See if there is a pair of this picked element
        for (int j = i + 1; j < n; j++)
            if (arr[i] - arr[j] == k
                || arr[j] - arr[i] == k)
                count++;
    }
    return count;
}
 
// Driver program to test above function
int main()
{
    int arr[] = { 1, 5, 3, 4, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 3;
    cout << "Count of pairs with given diff is "
         << countPairsWithDiffK(arr, n, k);
    return 0;
}
 
// This code is contributed by Sania Kumari Gupta

Time complexity: quadratic
Space complexity: constant
/* A sorting based program to count pairs with difference k*/
#include <iostream>
#include <algorithm>
using namespace std;
  
/* Returns count of pairs with difference k in arr[] of size n. */
int countPairsWithDiffK(int arr[], int n, int k)
{
    int count = 0;
    sort(arr, arr+n);  // Sort array elements
 
    int l = 0;
    int r = 0;
    while(r < n)
    {
         if(arr[r] - arr[l] == k)
        {
              count++;
              l++;
              r++;
        }
         else if(arr[r] - arr[l] > k)
              l++;
         else // arr[r] - arr[l] < sum
              r++;
    }  
    return count;
}
 
// Driver program to test above function
int main()
{
    int arr[] =  {1, 5, 3, 4, 2};
    int n = sizeof(arr)/sizeof(arr[0]);
    int k = 3;
    cout << "Count of pairs with given diff is "
         << countPairsWithDiffK(arr, n, k);
    return 0;
}

Time complexity: nlogn
Space complexity: constant
#include <bits/stdc++.h>
using namespace std;
 
int BS(int arr[], int X, int low, int N)
{
    int high = N - 1;
    int ans = N;
    while (low <= high) {
        int mid = low + (high - low) / 2;
        if (arr[mid] >= X) {
            ans = mid;
            high = mid - 1;
        }
        else
            low = mid + 1;
    }
    return ans;
}
int countPairsWithDiffK(int arr[], int N, int k)
{
    int count = 0;
    sort(arr, arr + N);
    for (int i = 0; i < N; ++i) {
        int X = BS(arr, arr[i] + k, i + 1, N);
        if (X != N) {
            int Y = BS(arr, arr[i] + k + 1, i + 1, N);
            count += Y - X;
        }
    }
 
    return count;
}
int main()
{
    int arr[] = { 1, 3, 5, 8, 6, 4, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 2;
    cout << "Count of pairs with given diff is "
         << countPairsWithDiffK(arr, n, k);
 
    return 0;
}
 
// This code is contributed by umadevi9616

Time complexity: nlogn
Space complexity: constant
// C++ program to print all distinct elements in a given array
#include <bits/stdc++.h>
using namespace std;
 
void printDistinct(int arr[], int n)
{
    // Pick all elements one by one
    for (int i=0; i<n; i++)
    {
        // Check if the picked element is already printed
        int j;
        for (j=0; j<i; j++)
           if (arr[i] == arr[j])
               break;
 
        // If not printed earlier, then print it
        if (i == j)
          cout << arr[i] << " ";
    }
}
 
// Driver program to test above function
int main()
{
    int arr[] = {6, 10, 5, 4, 9, 120, 4, 6, 10};
    int n = sizeof(arr)/sizeof(arr[0]);
    printDistinct(arr, n);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program to print all distinct elements in a given array
#include <bits/stdc++.h>
using namespace std;
 
void printDistinct(int arr[], int n)
{
    // First sort the array so that all occurrences become consecutive
    sort(arr, arr + n);
 
    // Traverse the sorted array
    for (int i=0; i<n; i++)
    {
       // Move the index ahead while there are duplicates
       while (i < n-1 && arr[i] == arr[i+1])
          i++;
 
       // print last occurrence of the current element
       cout << arr[i] << " ";
    }
}
 
// Driver program to test above function
int main()
{
    int arr[] = {6, 10, 5, 4, 9, 120, 4, 6, 10};
    int n = sizeof(arr)/sizeof(arr[0]);
    printDistinct(arr, n);
    return 0;
}

Time complexity: nlogn
Space complexity: constant
/* CPP program to print all distinct elements
   of a given array */
#include<bits/stdc++.h>
using namespace std;
 
// This function prints all distinct elements
void printDistinct(int arr[],int n)
{
    // Creates an empty hashset
    unordered_set<int> s;
 
    // Traverse the input array
    for (int i=0; i<n; i++)
    {
        // if element is not present then s.count(element) return 0 else return 1
        // hashtable and print it
        if (!s.count(arr[i]))  // <--- avg O(1) time
        {
            s.insert(arr[i]);
            cout << arr[i] << " ";
        }
    }
}
 
// Driver method to test above method
int main ()
{
    int arr[] = {10, 5, 3, 4, 3, 5, 6};
    int n=7;
    printDistinct(arr,n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ approach
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int ar[] = { 10, 5, 3, 4, 3, 5, 6 };
  map<int  ,int> hm;
  for (int i = 0; i < sizeof(ar)/sizeof(ar[0]); i++) { // total = O(n*logn)
    hm.insert({ar[i], i});  //  time complexity for insert() in map O(logn)
  }
  cout <<"[";
  for (auto const &pair: hm) {
    cout << pair.first << ", ";
  }
  cout <<"]";
}
 
// This code is contributed by Shubham Singh

Time complexity: nlogn
Space complexity: linear
// C++ program to merge two sorted arrays with O(1) extra
// space.
#include <bits/stdc++.h>
using namespace std;
 
// Merge ar1[] and ar2[] with O(1) extra space
void merge(int ar1[], int ar2[], int m, int n)
{
    // Iterate through all elements
    // of ar2[] starting from the last element
    for (int i = n - 1; i >= 0; i--) {
        // Find the smallest element greater than ar2[i].
        // Move all elements one position ahead till the
        // smallest greater element is not found */
        int j, last = ar1[m - 1];
        for (j = m - 2; j >= 0 && ar1[j] > ar2[i]; j--)
            ar1[j + 1] = ar1[j];
 
        // If there was a greater element
        if (last > ar2[i]) {
            ar1[j + 1] = ar2[i];
            ar2[i] = last;
        }
    }
}
 
// Driver program
int main()
{
    int ar1[] = { 1, 5, 9, 10, 15, 20 };
    int ar2[] = { 2, 3, 8, 13 };
    int m = sizeof(ar1) / sizeof(ar1[0]);
    int n = sizeof(ar2) / sizeof(ar2[0]);
    merge(ar1, ar2, m, n);
 
    cout << "After Merging \nFirst Array: ";
    for (int i = 0; i < m; i++)
        cout << ar1[i] << " ";
    cout << "\nSecond Array: ";
    for (int i = 0; i < n; i++)
        cout << ar2[i] << " ";
    return 0;
}

Time complexity: quadratic
Space complexity: constant
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
 
void merge(int arr1[], int arr2[], int n, int m) {
        int i=0;
        // while loop till last element of array 1(sorted) is greater than
          // first element of array 2(sorted)
        while(arr1[n-1]>arr2[0])
        {
            if(arr1[i]>arr2[0])
            {
                // swap arr1[i] with first element
                  // of arr2 and sorting the updated
                // arr2(arr1 is already sorted)
                swap(arr1[i],arr2[0]);
                sort(arr2,arr2+m);
            }
            i++;
        }
    }
 
int main()
{
 
    int ar1[] = { 1, 5, 9, 10, 15, 20 };
    int ar2[] = { 2, 3, 8, 13 };
    int m = sizeof(ar1) / sizeof(ar1[0]);
    int n = sizeof(ar2) / sizeof(ar2[0]);
    merge(ar1, ar2, m, n);
 
    cout << "After Merging \nFirst Array: ";
    for (int i = 0; i < m; i++)
        cout << ar1[i] << " ";
    cout << "\nSecond Array: ";
    for (int i = 0; i < n; i++)
        cout << ar2[i] << " ";
    return 0;
 
}

Time complexity: linear
Space complexity: constant