Banana-Leopard/TASTY huggingface dataset:
// C++ program to find minimum difference between
// any pair in an unsorted array
#include <bits/stdc++.h>
using namespace std;
 
// Returns minimum difference between any pair
int findMinDiff(int arr[], int n)
{
    // Sort array in non-decreasing order
    sort(arr, arr + n);
 
    // Initialize difference as infinite
    int diff = INT_MAX;
 
    // Find the min diff by comparing adjacent
    // pairs in sorted array
    for (int i = 0; i < n - 1; i++)
        if (arr[i + 1] - arr[i] < diff)
            diff = arr[i + 1] - arr[i];
 
    // Return min diff
    return diff;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 5, 3, 19, 18, 25 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    cout << "Minimum difference is " << findMinDiff(arr, n);
    return 0;
}

Time complexity: nlogn
Space complexity: constant
// A Simple C++ program to find longest common
// subarray of two binary arrays with same sum
#include<bits/stdc++.h>
using namespace std;
 
// Returns length of the longest common subarray
// with same sum
int longestCommonSum(bool arr1[], bool arr2[], int n)
{
    // Initialize result
    int maxLen = 0;
 
    // One by one pick all possible starting points
    // of subarrays
    for (int i=0; i<n; i++)
    {
       // Initialize sums of current subarrays
       int sum1 = 0, sum2 = 0;
 
       // Consider all points for starting with arr[i]
       for (int j=i; j<n; j++)
       {
           // Update sums
           sum1 += arr1[j];
           sum2 += arr2[j];
 
           // If sums are same and current length is
           // more than maxLen, update maxLen
           if (sum1 == sum2)
           {
             int len = j-i+1;
             if (len > maxLen)
                maxLen = len;
           }
       }
    }
    return maxLen;
}
 
// Driver program to test above function
int main()
{
    bool  arr1[] = {0, 1, 0, 1, 1, 1, 1};
    bool  arr2[] = {1, 1, 1, 1, 1, 0, 1};
    int n = sizeof(arr1)/sizeof(arr1[0]);
    cout << "Length of the longest common span with same "
            "sum is "<< longestCommonSum(arr1, arr2, n);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// A O(n) and O(n) extra space C++ program to find
// longest common subarray of two binary arrays with
// same sum
#include<bits/stdc++.h>
using namespace std;
 
// Returns length of the longest common sum in arr1[]
// and arr2[]. Both are of same size n.
int longestCommonSum(bool arr1[], bool arr2[], int n)
{
    // Initialize result
    int maxLen = 0;
 
    // Initialize prefix sums of two arrays
    int preSum1 = 0, preSum2 = 0;
 
    // Create an array to store starting and ending
    // indexes of all possible diff values. diff[i]
    // would store starting and ending points for
    // difference "i-n"
    int diff[2*n+1];
 
    // Initialize all starting and ending values as -1.
    memset(diff, -1, sizeof(diff));
 
    // Traverse both arrays
    for (int i=0; i<n; i++)
    {
        // Update prefix sums
        preSum1 += arr1[i];
        preSum2 += arr2[i];
 
        // Compute current diff and index to be used
        // in diff array. Note that diff can be negative
        // and can have minimum value as -1.
        int curr_diff = preSum1 - preSum2;
        int diffIndex = n + curr_diff;
 
        // If current diff is 0, then there are same number
        // of 1's so far in both arrays, i.e., (i+1) is
        // maximum length.
        if (curr_diff == 0)
            maxLen = i+1;
 
        // If current diff is seen first time, then update
        // starting index of diff.
        else if ( diff[diffIndex] == -1)
            diff[diffIndex] = i;
 
        // Current diff is already seen
        else
        {
            // Find length of this same sum common span
            int len = i - diff[diffIndex];
 
            // Update max len if needed
            if (len > maxLen)
                maxLen = len;
        }
    }
    return maxLen;
}
 
// Driver code
int main()
{
    bool  arr1[] = {0, 1, 0, 1, 1, 1, 1};
    bool  arr2[] = {1, 1, 1, 1, 1, 0, 1};
    int n = sizeof(arr1)/sizeof(arr1[0]);
    cout << "Length of the longest common span with same "
            "sum is "<< longestCommonSum(arr1, arr2, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find largest subarray
// with equal number of 0's and 1's.
#include <bits/stdc++.h>
using namespace std;
 
// Returns largest common subarray with equal
// number of 0s and 1s in both of t
int longestCommonSum(bool arr1[], bool arr2[], int n)
{
    // Find difference between the two
    int arr[n];
    for (int i=0; i<n; i++)
      arr[i] = arr1[i] - arr2[i];
     
    // Creates an empty hashMap hM
    unordered_map<int, int> hM;
 
    int sum = 0;     // Initialize sum of elements
    int max_len = 0; // Initialize result
 
    // Traverse through the given array
    for (int i = 0; i < n; i++)
    {
        // Add current element to sum
        sum += arr[i];
 
        // To handle sum=0 at last index
        if (sum == 0)
            max_len = i + 1;
 
        // If this sum is seen before,
        // then update max_len if required
        if (hM.find(sum) != hM.end())
          max_len = max(max_len, i - hM[sum]);
           
        else // Else put this sum in hash table
            hM[sum] = i;
    }
 
    return max_len;
}
 
// Driver program to test above function
int main()
{
    bool  arr1[] = {0, 1, 0, 1, 1, 1, 1};
    bool  arr2[] = {1, 1, 1, 1, 1, 0, 1};
    int n = sizeof(arr1)/sizeof(arr1[0]);
    cout << longestCommonSum(arr1, arr2, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to print an array in alternate
// sorted manner.
#include <bits/stdc++.h>
using namespace std;
 
// Function to print alternate sorted values
void alternateSort(int arr[], int n)
{
    // Sorting the array
    sort(arr, arr+n);
 
    // Printing the last element of array
    // first and then first element and then
    // second last element and then second
    // element and so on.
    int i = 0, j = n-1;
    while (i < j) {
        cout << arr[j--] << " ";
        cout << arr[i++] << " ";
    }
 
    // If the total element in array is odd
    // then print the last middle element.
    if (n % 2 != 0)
        cout << arr[i];
}
 
// Driver code
int main()
{
    int arr[] = {1, 12, 4, 6, 7, 10};
    int n = sizeof(arr)/sizeof(arr[0]);
    alternateSort(arr, n);
    return 0;
}

Time complexity: nlogn
Space complexity: constant
// A STL based C++ program to sort a nearly sorted array.
#include <bits/stdc++.h>
using namespace std;
 
// Given an array of size n, where every element
// is k away from its target position, sorts the
// array in O(n logk) time.
void sortK(int arr[], int n, int k)
{
     
    // Insert first k+1 items in a priority queue (or min
    // heap)
    //(A O(k) operation). We assume, k < n.
  //if size of array = k i.e k away from its target position
  //then
    int size;
    size=(n==k)?k:k+1;
    priority_queue<int, vector<int>, greater<int> > pq(arr, arr +size);
 
    // i is index for remaining elements in arr[] and index
    // is target index of for current minimum element in
    // Min Heap 'pq'.
    int index = 0;
    for (int i = k + 1; i < n; i++) {
        arr[index++] = pq.top();
        pq.pop();
        pq.push(arr[i]);
    }
 
    while (pq.empty() == false) {
        arr[index++] = pq.top();
        pq.pop();
    }
}
 
// A utility function to print array elements
void printArray(int arr[], int size)
{
    for (int i = 0; i < size; i++)
        cout << arr[i] << " ";
    cout << endl;
}
 
// Driver program to test above functions
int main()
{
    int k = 3;
    int arr[] = { 2, 6, 3, 12, 56, 8 };
    int n = sizeof(arr) / sizeof(arr[0]);
    sortK(arr, n, k);
 
    cout << "Following is sorted array" << endl;
    printArray(arr, n);
 
    return 0;
}

Time complexity: constant
Space complexity: constant
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
 
int sort(vector<int>& array, int l, int h, int k)
{
    int mid = l + (h - l) / 2; //Choose middle element as pivot
    int i = max(l, mid - k), j = i, end = min(mid + k, h); // Set appropriate range
    swap(array[mid], array[end]); //Swap middle and last element to avoid extra complications
    while (j < end) {
        if (array[j] < array[end]) {
            swap(array[i++], array[j]);
        }
        j = j + 1;
    }
    swap(array[end], array[i]);
    return i;
}
 
void ksorter(vector<int>& array, int l, int h, int k)
{
    if (l < h) {
        int q = sort(array, l, h, k);
        ksorter(array, l, q - 1, k);
        ksorter(array, q + 1, h, k);
    }
}
 
int main()
{
    vector<int> array(
        { 3, 3, 2, 1, 6, 4, 4, 5, 9, 7, 8, 11, 12 });
    int k = 3;
    cout << "Array before k sort\n";
    for (int& num : array)
        cout << num << ' ';
    cout << endl;
    ksorter(array, 0, array.size() - 1, k);
    cout << "Array after k sort\n";
    for (int& num : array)
        cout << num << ' ';
    return 0;
}

Time complexity: nlogn
Space complexity: logn
// A C++ program to sort an array in wave form using
// a sorting function
#include<iostream>
#include<algorithm>
using namespace std;
 
// A utility method to swap two numbers.
void swap(int *x, int *y)
{
    int temp = *x;
    *x = *y;
    *y = temp;
}
 
// This function sorts arr[0..n-1] in wave form, i.e.,
// arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= arr[5]..
void sortInWave(int arr[], int n)
{
    // Sort the input array
    sort(arr, arr+n);
 
    // Swap adjacent elements
    for (int i=0; i<n-1; i += 2)
        swap(&arr[i], &arr[i+1]);
}
 
// Driver program to test above function
int main()
{
    int arr[] = {10, 90, 49, 2, 1, 5, 23};
    int n = sizeof(arr)/sizeof(arr[0]);
    sortInWave(arr, n);
    for (int i=0; i<n; i++)
       cout << arr[i] << " ";
    return 0;
}

Time complexity: nlogn
Space complexity: constant
// A O(n) program to sort an input array in wave form
#include<iostream>
using namespace std;
 
// A utility method to swap two numbers.
void swap(int *x, int *y)
{
    int temp = *x;
    *x = *y;
    *y = temp;
}
 
// This function sorts arr[0..n-1] in wave form, i.e., arr[0] >=
// arr[1] <= arr[2] >= arr[3] <= arr[4] >= arr[5] ....
void sortInWave(int arr[], int n)
{
    // Traverse all even elements
    for (int i = 0; i < n; i+=2)
    {
        // If current even element is smaller than previous
        if (i>0 && arr[i-1] > arr[i] )
            swap(&arr[i], &arr[i-1]);
 
        // If current even element is smaller than next
        if (i<n-1 && arr[i] < arr[i+1] )
            swap(&arr[i], &arr[i + 1]);
    }
}
 
// Driver program to test above function
int main()
{
    int arr[] = {10, 90, 49, 2, 1, 5, 23};
    int n = sizeof(arr)/sizeof(arr[0]);
    sortInWave(arr, n);
    for (int i=0; i<n; i++)
       cout << arr[i] << " ";
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to Merge an array of
// size n into another array of size m + n
#include <bits/stdc++.h>
using namespace std;
 
/* Assuming -1 is filled for the places
   where element is not available */
#define NA -1
 
/* Function to move m elements at
   the end of array mPlusN[] */
void moveToEnd(int mPlusN[], int size)
{
   int j = size - 1;
   for (int i = size - 1; i >= 0; i--)
     if (mPlusN[i] != NA)
     {
        mPlusN[j] = mPlusN[i];
        j--;
     }
}
 
/* Merges array N[] of size n into
   array mPlusN[] of size m+n*/
int merge(int mPlusN[], int N[], int m, int n)
{
   int i = n; /* Current index of i/p part of mPlusN[]*/
   int j = 0; /* Current index of N[]*/
   int k = 0; /* Current index of output mPlusN[]*/
   while (k < (m + n))
   {
    /* Take an element from mPlusN[] if
    a) value of the picked element is smaller
       and we have not reached end of it
    b) We have reached end of N[] */
    if ((j == n)||(i < (m + n) && mPlusN[i] <= N[j]))
    {
        mPlusN[k] = mPlusN[i];
        k++;
        i++;
    }
    else // Otherwise take element from N[]
    {
       mPlusN[k] = N[j];
       k++;
       j++;
    }
   }
}
 
/* Utility that prints out an array on a line */
void printArray(int arr[], int size)
{
   for (int i = 0; i < size; i++)
   cout << arr[i] << " ";
 
   cout << endl;
}
 
/* Driver code */
int main()
{
   /* Initialize arrays */
   int mPlusN[] = {2, 8, NA, NA, NA, 13, NA, 15, 20};
   int N[] = {5, 7, 9, 25};
    
   int n = sizeof(N) / sizeof(N[0]);
   int m = sizeof(mPlusN) / sizeof(mPlusN[0]) - n;
 
   /*Move the m elements at the end of mPlusN*/
   moveToEnd(mPlusN, m + n);
 
   /*Merge N[] into mPlusN[] */
   merge(mPlusN, N, m, n);
 
   /* Print the resultant mPlusN */
   printArray(mPlusN, m+n);
 
   return 0;
}

Time complexity: linear
Space complexity: constant
// CPP program to test whether array
// can be sorted by swapping adjacent
// elements using boolean array
#include <bits/stdc++.h>
using namespace std;
 
// Return true if array can be
// sorted otherwise false
bool sortedAfterSwap(int A[], bool B[], int n)
{
    int i, j;
 
    // Check bool array B and sorts
    // elements for continuous sequence of 1
    for (i = 0; i < n - 1; i++) {
        if (B[i]) {
            j = i;
            while (B[j])
                j++;
 
            // Sort array A from i to j
            sort(A + i, A + 1 + j);
            i = j;
        }
    }
 
    // Check if array is sorted or not
    for (i = 0; i < n; i++) {
        if (A[i] != i + 1)
            return false;
    }
 
    return true;
}
 
// Driver program to test sortedAfterSwap()
int main()
{
    int A[] = { 1, 2, 5, 3, 4, 6 };
    bool B[] = { 0, 1, 1, 1, 0 };
    int n = sizeof(A) / sizeof(A[0]);
 
    if (sortedAfterSwap(A, B, n))
        cout << "A can be sorted\n";
    else
        cout << "A can not be sorted\n";
 
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// CPP program to test whether array
// can be sorted by swapping adjacent
// elements using boolean array
#include <bits/stdc++.h>
using namespace std;
 
// Return true if array can be
// sorted otherwise false
bool sortedAfterSwap(int A[], bool B[], int n)
{
    for (int i = 0; i < n - 1; i++) {
        if (B[i]) {
            if (A[i] != i + 1)
                swap(A[i], A[i + 1]);
        }
    }
 
    // Check if array is sorted or not
    for (int i = 0; i < n; i++) {
        if (A[i] != i + 1)
            return false;
    }
 
    return true;
}
 
// Driver program to test sortedAfterSwap()
int main()
{
    int A[] = { 1, 2, 5, 3, 4, 6 };
    bool B[] = { 0, 1, 1, 1, 0 };
    int n = sizeof(A) / sizeof(A[0]);
 
    if (sortedAfterSwap(A, B, n))
        cout << "A can be sorted\n";
    else
        cout << "A can not be sorted\n";
 
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP program to sort an array with two types
// of values in one traversal.
#include <bits/stdc++.h>
using namespace std;
 
/* Method for segregation 0 and 1 given
   input array */
void segregate0and1(int arr[], int n)
{
    int type0 = 0;
    int type1 = n - 1;
 
    while (type0 < type1) {
        if (arr[type0] == 1) {
            swap(arr[type0], arr[type1]);
            type1--;
        }
        else {
            type0++;
        }
    }
}
 
// Driver program
int main()
{
    int arr[] = { 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1 };
    int n = sizeof(arr)/sizeof(arr[0]);
    segregate0and1(arr, n);
    for (int a : arr)
        cout << a << " ";
}

Time complexity: linear
Space complexity: constant
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Used for sorting
struct ele {
    int count, index, val;
};
 
// Used for sorting by value
bool mycomp(struct ele a, struct ele b)
{
    return (a.val < b.val);
}
 
// Used for sorting by frequency. And if frequency is same,
// then by appearance
bool mycomp2(struct ele a, struct ele b)
{
    if (a.count != b.count)
        return (a.count < b.count);
    else
        return a.index > b.index;
}
 
void sortByFrequency(int arr[], int n)
{
    struct ele element[n];
    for (int i = 0; i < n; i++) {
 
        // Fill Indexes
        element[i].index = i;
 
        // Initialize counts as 0
        element[i].count = 0;
 
        // Fill values in structure
        // elements
        element[i].val = arr[i];
    }
 
    /* Sort the structure elements according to value,
       we used stable sort so relative order is maintained.
     */
    stable_sort(element, element + n, mycomp);
 
    /* initialize count of first element as 1 */
    element[0].count = 1;
 
    /* Count occurrences of remaining elements */
    for (int i = 1; i < n; i++) {
 
        if (element[i].val == element[i - 1].val) {
            element[i].count += element[i - 1].count + 1;
 
            /* Set count of previous element as -1, we are
               doing this because we'll again sort on the
               basis of counts (if counts are equal than on
               the basis of index)*/
            element[i - 1].count = -1;
 
            /* Retain the first index (Remember first index
               is always present in the first duplicate we
               used stable sort. */
            element[i].index = element[i - 1].index;
        }
 
        /* Else If previous element is not equal to current
          so set the count to 1 */
        else
            element[i].count = 1;
    }
 
    /* Now we have counts and first index for each element
       so now sort on the basis of count and in case of tie
       use index to sort.*/
    stable_sort(element, element + n, mycomp2);
    for (int i = n - 1, index = 0; i >= 0; i--)
        if (element[i].count != -1)
            for (int j = 0; j < element[i].count; j++)
                arr[index++] = element[i].val;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 5, 2, 6, -1, 9999999, 5, 8, 8, 8 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    sortByFrequency(arr, n);
 
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    return 0;
}

Time complexity: nlogn
Space complexity: linear
// CPP program for above approach
#include <bits/stdc++.h>
using namespace std;
 
// Compare function
bool fcompare(pair<int, pair<int, int> > p,
              pair<int, pair<int, int> > p1)
{
    if (p.second.second != p1.second.second)
        return (p.second.second > p1.second.second);
    else
        return (p.second.first < p1.second.first);
}
void sortByFrequency(int arr[], int n)
{
    unordered_map<int, pair<int, int> > hash; // hash map
    for (int i = 0; i < n; i++) {
        if (hash.find(arr[i]) != hash.end())
            hash[arr[i]].second++;
        else
            hash[arr[i]] = make_pair(i, 1);
    } // store the count of all the elements in the hashmap
 
    // Iterator to Traverse the Hashmap
    auto it = hash.begin();
 
    // Vector to store the Final Sortted order
    vector<pair<int, pair<int, int> > > b;
    for (it; it != hash.end(); ++it)
        b.push_back(make_pair(it->first, it->second));
 
    sort(b.begin(), b.end(), fcompare);
 
    // Printing the Sorted sequence
    for (int i = 0; i < b.size(); i++) {
        int count = b[i].second.second;
        while (count--)
            cout << b[i].first << " ";
    }
}
 
// Driver code
int main()
{
    int arr[] = { 2, 5, 2, 6, -1, 9999999, 5, 8, 8, 8 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    sortByFrequency(arr, n);
 
    return 0;
}

Time complexity: nlogn
Space complexity: linear
// CPP program to find shortest subarray which is
// unsorted.
#include <bits/stdc++.h>
using namespace std;
 
// bool function for checking an array elements
// are in increasing.
bool increasing(int a[], int n)
{
    for (int i = 0; i < n - 1; i++)
        if (a[i] >= a[i + 1])
            return false;   
    return true;
}
 
// bool function for checking an array
// elements are in decreasing.
bool decreasing(int a[], int n)
{
    for (int i = 0; i < n - 1; i++)
        if (a[i] < a[i + 1])
            return false;   
    return true;
}
 
int shortestUnsorted(int a[], int n)
{
    // increasing and decreasing are two functions.
    // if function return true value then print
    // 0 otherwise 3.
    if (increasing(a, n) == true ||
       decreasing(a, n) == true)
        return 0;
    else
        return 3;
}
 
// Driver code
int main()
{
    int ar[] = { 7, 9, 10, 8, 11 };
    int n = sizeof(ar) / sizeof(ar[0]);
    cout << shortestUnsorted(ar, n);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to find 
// minimum number of swaps
// required to sort an array
#include<bits/stdc++.h>
 
using namespace std;
 
// Function returns the
// minimum number of swaps
// required to sort the array
int minSwaps(int arr[], int n)
{
    // Create an array of
    // pairs where first
    // element is array element
    // and second element
    // is position of first element
    pair<int, int> arrPos[n];
    for (int i = 0; i < n; i++)
    {
        arrPos[i].first = arr[i];
        arrPos[i].second = i;
    }
 
    // Sort the array by array
    // element values to
    // get right position of
    // every element as second
    // element of pair.
    sort(arrPos, arrPos + n);
 
    // To keep track of visited elements.
    // Initialize
    // all elements as not visited or false.
    vector<bool> vis(n, false);
 
    // Initialize result
    int ans = 0;
 
    // Traverse array elements
    for (int i = 0; i < n; i++)
    {
        // already swapped and corrected or
        // already present at correct pos
        if (vis[i] || arrPos[i].second == i)
            continue;
 
        // find out the number of  node in
        // this cycle and add in ans
        int cycle_size = 0;
        int j = i;
        while (!vis[j])
        {
            vis[j] = 1;
 
            // move to next node
            j = arrPos[j].second;
            cycle_size++;
        }
 
        // Update answer by adding current cycle.
        if (cycle_size > 0)
        {
            ans += (cycle_size - 1);
        }
    }
 
    // Return result
    return ans;
}
 
// Driver program to test the above function
int main()
{
    int arr[] = {1, 5, 4, 3, 2};
    int n = (sizeof(arr) / sizeof(int));
    cout << minSwaps(arr, n);
    return 0;
}

Time complexity: nlogn
Space complexity: linear
#include <bits/stdc++.h>
using namespace std;
// Function returns the
// minimum number of swaps
// required to sort the array
int minSwaps(int nums[], int n)
{
    int len = n;
    map<int, int> map;
    for (int i = 0; i < len; i++)
        map[nums[i]] = i;
 
    sort(nums, nums + n);
 
    // To keep track of visited elements. Initialize
    // all elements as not visited or false.
    bool visited[len] = { 0 };
 
    // Initialize result
    int ans = 0;
    for (int i = 0; i < len; i++) {
 
        // already swapped and corrected or
        // already present at correct pos
        if (visited[i] || map[nums[i]] == i)
            continue;
 
        int j = i, cycle_size = 0;
        while (!visited[j]) {
            visited[j] = true;
 
            // move to next node
            j = map[nums[j]];
            cycle_size++;
        }
 
        // Update answer by adding current cycle.
        if (cycle_size > 0) {
            ans += (cycle_size - 1);
        }
    }
    return ans;
}
int main()
{
    // Driver program to test the above function
    int a[] = { 1, 5, 4, 3, 2 };
    int n = 5;
    cout << minSwaps(a, n);
    return 0;
}
 
// This code is contributed by Harshal Khond

Time complexity: nlogn
Space complexity: linear