Banana-Leopard/TASTY huggingface dataset:
// A C++ program to put all negative
// numbers before positive numbers
#include <bits/stdc++.h>
using namespace std;
 
void rearrange(int arr[], int n)
{
    int j = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] < 0) {
            if (i != j)
                swap(arr[i], arr[j]);
            j++;
        }
    }
}
 
// A utility function to print an array
void printArray(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
}
 
// Driver code
int main()
{
    int arr[] = { -1, 2, -3, 4, 5, 6, -7, 8, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
    rearrange(arr, n);
    printArray(arr, n);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program of the above
// approach
 
#include <iostream>
using namespace std;
 
// Function to shift all the
// negative elements on left side
void shiftall(int arr[], int left,
              int right)
{
   
  // Loop to iterate over the
  // array from left to the right
  while (left<=right)
  {
    // Condition to check if the left
    // and the right elements are
    // negative
    if (arr[left] < 0 && arr[right] < 0)
      left+=1;
     
    // Condition to check if the left
    // pointer element is positive and
    // the right pointer element is negative
    else if (arr[left]>0 && arr[right]<0)
    {
      int temp=arr[left];
      arr[left]=arr[right];
      arr[right]=temp;
      left+=1;
      right-=1;
    }
     
    // Condition to check if both the
    // elements are positive
    else if (arr[left]>0 && arr[right] >0)
      right-=1;
    else{
      left += 1;
      right -= 1;
    }
  }
}
 
// Function to print the array
void display(int arr[], int right){
   
  // Loop to iterate over the element
  // of the given array
  for (int i=0;i<=right;++i){
    cout<<arr[i]<<" ";
  }
  cout<<endl;
}
 
// Driver Code
int main()
{
  int arr[] = {-12, 11, -13, -5,
               6, -7, 5, -3, 11};
  int arr_size = sizeof(arr) /
                sizeof(arr[0]);
   
  // Function Call
  shiftall(arr,0,arr_size-1);
  display(arr,arr_size-1);
  return 0;
}
 
//added by Dhruv Goyal

Time complexity: linear
Space complexity: constant
#include <iostream>
using namespace std;
 
// Swap Function.
void swap(int &a,int &b){
  int temp =a;
  a=b;
  b=temp;
}
   
// Using Dutch National Flag Algorithm.
void reArrange(int arr[],int n){
      int low =0,high = n-1;
      while(low<high){
      if(arr[low]<0){
          low++;
      }else if(arr[high]>0){
          high--;
      }else{
        swap(arr[low],arr[high]);
      }
    }
}
void displayArray(int arr[],int n){
  for(int i=0;i<n;i++){
    cout<<arr[i]<<" ";
  }
  cout<<endl;
}
int main() {
    // Data
    int arr[] = {1, 2,  -4, -5, 2, -7, 3, 2, -6, -8, -9, 3, 2,  1};
      int n = sizeof(arr)/sizeof(arr[0]);
      reArrange(arr,n);
    displayArray(arr,n);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to Move All -ve Element At End
// Without changing order Of Array Element
#include<bits/stdc++.h>
using namespace std;
 
// Moves all -ve element to end of array in
// same order.
void segregateElements(int arr[], int n)
{
    // Create an empty array to store result
    int temp[n];
 
    // Traversal array and store +ve element in
    // temp array
    int j = 0; // index of temp
    for (int i = 0; i < n ; i++)
        if (arr[i] >= 0 )
            temp[j++] = arr[i];
 
    // If array contains all positive or all negative.
    if (j == n || j == 0)
        return;
 
    // Store -ve element in temp array
    for (int i = 0 ; i < n ; i++)
        if (arr[i] < 0)
            temp[j++] = arr[i];
 
    // Copy contents of temp[] to arr[]
    memcpy(arr, temp, sizeof(temp));
}
 
// Driver program
int main()
{
    int arr[] = {1 ,-1 ,-3 , -2, 7, 5, 11, 6 };
    int n = sizeof(arr)/sizeof(arr[0]);
 
    segregateElements(arr, n);
 
    for (int i = 0; i < n; i++)
    cout << arr[i] << " ";
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// CPP code to rearrange an array such that
// even index elements are smaller and odd
// index elements are greater than their
// next.
#include <iostream>
using namespace std;
 
void rearrange(int* arr, int n)
{
    for (int i = 0; i < n - 1; i++) {
        if (i % 2 == 0 && arr[i] > arr[i + 1])
            swap(arr[i], arr[i + 1]);
 
        if (i % 2 != 0 && arr[i] < arr[i + 1])
            swap(arr[i], arr[i + 1]);
    }
}
 
/* Utility that prints out an array in
   a line */
void printArray(int arr[], int size)
{
    for (int i = 0; i < size; i++)
        cout << arr[i] << " ";
 
    cout << endl;
}
 
/* Driver function to test above functions */
int main()
{
    int arr[] = { 6, 4, 2, 1, 8, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << "Before rearranging: \n";
    printArray(arr, n);
 
    rearrange(arr, n);
 
    cout << "After rearranging: \n";
    printArray(arr, n);
 
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to rearrange positive and negative
// numbers
#include <bits/stdc++.h>
using namespace std;
  
void rearrange(int a[], int size)
{
    int positive = 0, negative = 1;
  
    while (true) {
  
        /* Move forward the positive pointer till 
         negative number number not encountered */
        while (positive < size && a[positive] >= 0)
            positive += 2;
  
        /* Move forward the negative pointer till 
         positive number number not encountered   */
        while (negative < size && a[negative] <= 0)
            negative += 2;
  
        // Swap array elements to fix their position.
        if (positive < size && negative < size)
            swap(a[positive], a[negative]);
  
        /* Break from the while loop when any index 
            exceeds the size of the array */
        else
            break;
    }
}
  
// Driver code
int main()
{
    int arr[] = { 1, -3, 5, 6, -3, 6, 7, -4, 9, 10 };
    int n = (sizeof(arr) / sizeof(arr[0]));
  
    rearrange(arr, n); 
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
  
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program to rearrange positive
// and negative numbers
#include<iostream>
using namespace std;
  
// Swap function
void swap(int* a, int i , int j)
{
    int temp = a[i];
    a[i] = a[j];
    a[j] = temp;
    return ;
}
  
// Print array function
void printArray(int* a, int n)
{
    for(int i = 0; i < n; i++)
        cout << a[i] << " ";
    cout << endl;
    return ;
}
  
// Driver code
int main()
{
    int arr[] = { 1, -3, 5, 6, -3, 6, 7, -4, 9, 10 };
    int n = sizeof(arr)/sizeof(arr[0]);
      
    //before modification
    printArray(arr, n);
  
    for(int i = 0; i < n; i++)
    {
        if(arr[i] >= 0 && i % 2 == 1)
        { 
            // out of order positive element
            for(int j = i + 1; j < n; j++)
            {
                if(arr[j] < 0 && j % 2 == 0)
                { 
                    // find out of order negative 
                    // element in remaining array
                    swap(arr, i, j);
                    break ;
                }
            }
        }
        else if(arr[i] < 0 && i % 2 == 0)
        {
            // out of order negative element
            for(int j = i + 1; j < n; j++)
            {
                if(arr[j] >= 0 && j % 2 == 1)
                {
                    // find out of order positive 
                    // element in remaining array
                    swap(arr, i, j);
                    break;
                }
            } 
        }
    }
      
    //after modification
    printArray(arr, n); 
    return 0;
}
  
// This code is contributed by AnitAggarwal

Time complexity: quadratic
Space complexity: constant
// C++ program to update every array element with
// multiplication of previous and next numbers in array
#include<iostream>
using namespace std;
 
void modify(int arr[], int n)
{
    // Nothing to do when array size is 1
    if (n <= 1)
      return;
 
    // store current value of arr[0] and update it
    int prev = arr[0];
    arr[0] = arr[0] * arr[1];
 
    // Update rest of the array elements
    for (int i=1; i<n-1; i++)
    {
        // Store current value of next interaction
        int curr = arr[i];
 
        // Update current value using previous value
        arr[i] = prev * arr[i+1];
 
        // Update previous value
        prev = curr;
    }
 
    // Update last array element
    arr[n-1] = prev * arr[n-1];
}
 
// Driver program
int main()
{
    int arr[] = {2, 3, 4, 5, 6};
    int n = sizeof(arr)/sizeof(arr[0]);
    modify(arr, n);
    for (int i=0; i<n; i++)
      cout << arr[i] << " ";
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ Program to shuffle a given array
#include<bits/stdc++.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
 
// A utility function to swap to integers
void swap (int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}
 
// A utility function to print an array
void printArray (int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << "\n";
}
 
// A function to generate a random
// permutation of arr[]
void randomize (int arr[], int n)
{
    // Use a different seed value so that
    // we don't get same result each time
    // we run this program
    srand (time(NULL));
 
    // Start from the last element and swap
    // one by one. We don't need to run for
    // the first element that's why i > 0
    for (int i = n - 1; i > 0; i--)
    {
        // Pick a random index from 0 to i
        int j = rand() % (i + 1);
 
        // Swap arr[i] with the element
        // at random index
        swap(&arr[i], &arr[j]);
    }
}
 
// Driver Code
int main()
{
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
    int n = sizeof(arr) / sizeof(arr[0]);
    randomize (arr, n);
    printArray(arr, n);
 
    return 0;
}
 
// This code is contributed by
// rathbhupendra

Time complexity: linear
Space complexity: constant
// C++ Implementation of the above approach
#include <iostream>
using namespace std;
void arrayEvenAndOdd(int arr[], int n)
{
    int a[n], index = 0;
    for (int i = 0; i < n; i++)
    {
        if (arr[i] % 2 == 0)
        {
            a[index] = arr[i];
            index++;
        }
    }
    for (int i = 0; i < n; i++)
    {
        if (arr[i] % 2 != 0)
        {
            a[index] = arr[i];
            index++;
        }
    }
    for (int i = 0; i < n; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 3, 2, 4, 7, 6, 9, 10 };
    int n = sizeof(arr) / sizeof(int);
 
    // Function call
    arrayEvenAndOdd(arr, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// CPP code to segregate even odd
// numbers in an array
#include <bits/stdc++.h>
using namespace std;
 
// Function to segregate even odd numbers
void arrayEvenAndOdd(int arr[], int n)
{
 
    int i = -1, j = 0;
    int t;
    while (j != n) {
        if (arr[j] % 2 == 0) {
            i++;
 
            // Swapping even and odd numbers
            swap(arr[i], arr[j]);
        }
        j++;
    }
 
    // Printing segregated array
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// Driver code
int main()
{
    int arr[] = { 1, 3, 2, 4, 7, 6, 9, 10 };
    int n = sizeof(arr) / sizeof(int);
    arrayEvenAndOdd(arr, n);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program of above implementation
 
#include <bits/stdc++.h>
using namespace std;
 
// Standard partition process of QuickSort().
// It considers the last element as pivot and
// oves all smaller element to left of it
// and greater elements to right
int partition(int* arr, int l, int r)
{
    int x = arr[r], i = l;
    for (int j = l; j <= r - 1; j++) {
        if (arr[j] <= x) {
            swap(arr[i], arr[j]);
            i++;
        }
    }
    swap(arr[i], arr[r]);
    return i;
}
 
int randomPartition(int* arr, int l, int r)
{
    int n = r - l + 1;
    int pivot = (rand() % 100 + 1) % n;
    swap(arr[l + pivot], arr[r]);
    return partition(arr, l, r);
}
 
// This function returns k'th smallest
// element in arr[l..r] using
// QuickSort based method. ASSUMPTION:
// ALL ELEMENTS IN ARR[] ARE DISTINCT
int kthSmallest(int* arr, int l, int r, int k)
{
 
    // If k is smaller than number
    // of elements in array
    if (k > 0 && k <= r - l + 1) {
 
        // Partition the array around last
        // element and get position of pivot
        // element in sorted array
        int pos = randomPartition(arr, l, r);
 
        // If position is same as k
        if (pos - l == k - 1) {
            return arr[pos];
        }
 
        // If position is more, recur
        // for left subarray
        if (pos - l > k - 1) {
            return kthSmallest(arr, l, pos - 1, k);
        }
 
        // Else recur for right subarray
        return kthSmallest(arr, pos + 1, r,
                           k - pos + l - 1);
    }
 
    // If k is more than number of
    // elements in array
    return INT_MAX;
}
 
// Driver Code
int main()
{
    int arr[] = { 12, 3, 5, 7, 4, 19, 26 };
    int n = sizeof(arr) / sizeof(arr[0]), k = 3;
    cout << "K'th smallest element is "
         << kthSmallest(arr, 0, n - 1, k);
}

Time complexity: nlogn
Space complexity: constant
// STL based C++ program to find k-th smallest
// element.
#include <bits/stdc++.h>
using namespace std;
 
int kthSmallest(int arr[], int n, int k)
{
 // Insert all elements into the set
 set<int> s;
 for (int i = 0; i < n; i++)
  s.insert(arr[i]);
 
 // Traverse set and print k-th element
 auto it = s.begin();
 for (int i = 0; i < k - 1; i++)
  it++;
 return *it;
}
 
int main()
{
 int arr[] = { 12, 3, 5, 7, 3, 19 };
 int n = sizeof(arr) / sizeof(arr[0]), k = 2;
 cout << "K'th smallest element is "
  << kthSmallest(arr, n, k);
 return 0;
}

Time complexity: nlogn
Space complexity: linear
#include <bits/stdc++.h>
using namespace std;
 
// picks up last element between start and end
int findPivot(int a[], int start, int end)
{
    // Selecting the pivot element
    int pivot = a[end];
    // Initially partition-index will be at starting
    int pIndex = start;
    for (int i = start; i < end; i++) {
        // If an element is lesser than pivot, swap it.
        if (a[i] <= pivot) {
            swap(a[i], a[pIndex]);
            // Incrementing pIndex for further swapping.
            pIndex++;
        }
    }
    // Lastly swapping or the correct position of pivot
    swap(a[pIndex], a[end]);
    return pIndex;
}
 
// THIS PART OF CODE IS CONTRIBUTED BY - rjrachit
// Picks up random pivot element between start and end
int findRandomPivot(int arr[], int start, int end)
{
    int n = end - start + 1;
    // Selecting the random pivot index
    int pivotInd = random() % n;
    swap(arr[end], arr[start + pivotInd]);
    int pivot = arr[end];
    // initialising pivoting point to start index
    pivotInd = start;
    for (int i = start; i < end; i++) {
        // If an element is lesser than pivot, swap it.
        if (arr[i] <= pivot) {
            swap(arr[i], arr[pivotInd]);
            // Incrementing pivotIndex for further swapping.
            pivotInd++;
        }
    }
 
    // Lastly swapping or the correct position of pivot
    swap(arr[pivotInd], arr[end]);
    return pivotInd;
}
// THIS PART OF CODE IS CONTRIBUTED BY - rjrachit
 
void SmallestLargest(int a[], int low, int high, int k,
                     int n)
{
    if (low == high)
        return;
    else {
        int pivotIndex = findRandomPivot(a, low, high);
        if (k == pivotIndex) {
            cout << k << " smallest elements are : ";
            for (int i = 0; i < pivotIndex; i++)
                cout << a[i] << "  ";
            cout << endl;
            cout << k << " largest elements are : ";
            for (int i = (n - pivotIndex); i < n; i++)
                cout << a[i] << "  ";
        }
 
        else if (k < pivotIndex)
            SmallestLargest(a, low, pivotIndex - 1, k, n);
        else if (k > pivotIndex)
            SmallestLargest(a, pivotIndex + 1, high, k, n);
    }
}
 
// Driver Code
int main()
{
    int a[] = { 11, 3, 2, 1, 15, 5, 4, 45, 88, 96, 50, 45 };
    int n = sizeof(a) / sizeof(a[0]);
    int low = 0;
    int high = n - 1;
    // Lets assume k is 3
    int k = 3;
    // Function Call
    SmallestLargest(a, low, high, k, n);
    return 0;
}
 
// This code is contributed by Sania Kumari Gupta

Time complexity: nlogn
Space complexity: constant
// C++ code for k largest/ smallest elements in an array
#include <bits/stdc++.h>
using namespace std;
 
// Function to find k largest array element
void kLargest(vector<int>& v, int N, int K)
{
    // Implementation using
    // a Priority Queue
    priority_queue<int, vector<int>, greater<int> >pq;
  
    for (int i = 0; i < N; ++i) {
  
        // Insert elements into
        // the priority queue
        pq.push(v[i]);
  
        // If size of the priority
        // queue exceeds k
        if (pq.size() > K) {
            pq.pop();
        }
    }
  
    // Print the k largest element
    while(!pq.empty())
    {
        cout << pq.top() <<" ";
        pq.pop();
    }
    cout<<endl;
}
 
// Function to find k smallest array element
void kSmalest(vector<int>& v, int N, int K)
{
    // Implementation using
    // a Priority Queue
    priority_queue<int> pq;
  
    for (int i = 0; i < N; ++i) {
  
        // Insert elements into
        // the priority queue
        pq.push(v[i]);
  
        // If size of the priority
        // queue exceeds k
        if (pq.size() > K) {
            pq.pop();
        }
    }
  
    // Print the k smallest element
    while(!pq.empty())
    {
        cout << pq.top() <<" ";
        pq.pop();
    }
     
}
 
// driver program
int main()
{
    // Given array
    vector<int> arr = { 11, 3, 2, 1, 15, 5, 4, 45, 88, 96, 50, 45 };
    // Size of array
    int n = arr.size();
    int k = 3;
    cout<<k<<" largest elements are : ";
    kLargest(arr, n, k);
    cout<<k<<" smallest elements are : ";
    kSmalest(arr, n, k);
}
 
// This code is contributed by Pushpesh Raj.

Time complexity: nlogn
Space complexity: linear
#include <bits/stdc++.h>
using namespace std;
 
struct Node{
    int data;
    struct Node *left;
    struct Node *right;
};
 
class Tree{
    public:
        Node *root = NULL;
        void addNode(int data){
            Node *newNode = new Node();
            newNode->data = data;
            if (!root){
                root = newNode;
            }
            else{
                Node *cur = root;
                while (cur){
                    if (cur->data > data){
                        if (cur->left){
                            cur = cur->left;
                        }
                        else{
                            cur->left = newNode;
                            return;
                        }
                    }
                    else{
                        if (cur->right){
                            cur = cur->right;
                        }
                        else{
                            cur->right = newNode;
                            return;
                        }
                    }
                }
            }
        }
        void printGreatest(int &K, vector<int> /, Node* node){
            if (!node || K == 0)  return;
            printGreatest(K, sol, node->right);
            if (K <= 0) return;
            sol.push_back(node->data);
            K--;
            printGreatest(K, sol, node->left);
        }
};
 
class Solution{
public:
     
    vector<int> kLargest(int arr[], int n, int k) {
        vector<int> sol;
        Tree tree = Tree();
        for (int i = 0; i < n; i++){
            tree.addNode(arr[i]);
        }
        tree.printGreatest(k, sol, tree.root);
        return sol;
    }
 
};
 
 
int main() {
      int n = 5, k = 2;
      int arr[] = {12, 5, 787, 1, 23};
      Solution ob;
      auto ans = ob.kLargest(arr, n, k);
      cout << "Top " << k << " Elements: ";
    for (auto x : ans) {
      cout << x << " ";
    }
    cout << "\n";
    return 0;
}

Time complexity: nlogn
Space complexity: linear
// C++ program to find maximum
// in arr[] of size n 
#include <bits/stdc++.h>
using namespace std;
  
int largest(int arr[], int n)
{
    int i;
      
    // Initialize maximum element
    int max = arr[0];
  
    // Traverse array elements 
    // from second and compare
    // every element with current max 
    for (i = 1; i < n; i++)
        if (arr[i] > max)
            max = arr[i];
  
    return max;
}
  
// Driver Code
int main()
{
    int arr[] = {10, 324, 45, 90, 9808};
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Largest in given array is " 
         << largest(arr, n);
    return 0;
}
  
// This Code is contributed 
// by Shivi_Aggarwal

Time complexity: linear
Space complexity: constant
// C++ program to find maximum
// in arr[] of size n
#include <bits/stdc++.h>
using namespace std;
  
int largest(int arr[], int n, int i)
{
    // last index
    // return the element
    if (i == n - 1) {
        return arr[i];
    }
  
    // find the maximum from rest of the array
    int recMax = largest(arr, n, i + 1);
  
    // compare with i-th element and return
    return max(recMax, arr[i]);
}
  
// Driver Code
int main()
{
    int arr[] = { 10, 324, 45, 90, 9808 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Largest in given array is "
         << largest(arr, n, 0);
    return 0;
}
  
// This Code is contributed by Rajdeep Mallick

Time complexity: linear
Space complexity: linear
// C++ program to find maximum in arr[] of size n 
#include <bits/stdc++.h>
using namespace std;
  
// returns maximum in arr[] of size n
int largest(int arr[], int n)
{
    return *max_element(arr, arr+n);
}
  
int main()
{
    int arr[] = {10, 324, 45, 90, 9808};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << largest(arr, n);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program for find the largest
// three elements in an array
#include <bits/stdc++.h>
using namespace std;
 
// Function to print three largest elements
void print3largest(int arr[], int arr_size)
{
    int first, second, third;
 
    // There should be atleast three elements
    if (arr_size < 3)
    {
        cout << " Invalid Input ";
        return;
    }
 
    third = first = second = INT_MIN;
    for(int i = 0; i < arr_size; i++)
    {
         
        // If current element is
        // greater than first
        if (arr[i] > first)
        {
            third = second;
            second = first;
            first = arr[i];
        }
 
        // If arr[i] is in between first
        // and second then update second
        else if (arr[i] > second && arr[i] != first)
        {
            third = second;
            second = arr[i];
        }
 
        else if (arr[i] > third && arr[i] != second)
            third = arr[i];
    }
 
    cout << "Three largest elements are "
        << first << " " << second << " "
        << third << endl;
}
 
// Driver code
int main()
{
    int arr[] = { 12, 13, 1, 10, 34, 11 };
    int n = sizeof(arr) / sizeof(arr[0]);
     
    print3largest(arr, n);
    return 0;
}
 
// This code is contributed by Anjali_Chauhan

Time complexity: linear
Space complexity: constant
// C++ code to find largest three elements in an array
#include <bits/stdc++.h>
using namespace std;
 
void find3largest(int arr[], int n)
{
    sort(arr, arr + n); // It uses Tuned Quicksort with
    // avg. case Time complexity = O(nLogn)
 
    int check = 0, count = 1;
    for (int i = 1; i <= n; i++) {
        if (count < 4) {
            if (check != arr[n - i]) {
                // to handle duplicate values
                cout << arr[n - i] << " ";
                check = arr[n - i];
                count++;
            }
        }
        else
            break;
    }
}
 
// Driver code
int main()
{
    int arr[] = { 12, 45, 1, -1, 45, 54, 23, 5, 0, -10 };
    int n = sizeof(arr) / sizeof(arr[0]);
    find3largest(arr, n);
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Time complexity: nlogn
Space complexity: constant