Banana-Leopard/TASTY huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
 
void swap(int& a, int& b)
{
    int temp = a;
    a = b;
    b = temp;
}
 
void rotate(int a[], int n, int idx)
{
    int i;
    for (i = 0; i < idx / 2; i++)
        swap(a[i], a[idx - 1 - i]);
    for (i = idx; i < (n + idx) / 2; i++)
        swap(a[i], a[n - 1 - (i - idx)]);
    for (i = 0; i < n / 2; i++)
        swap(a[i], a[n - 1 - i]);
}
 
void sol(int a1[], int a2[], int n, int m)
{
    int l = 0, h = n - 1, idx = 0;
    //---------------------------------------------------------
    while (l <= h) {
        // select the median of the remaining subarray
        int c1 = (l + h) / 2;
        // select the first elements from the larger array
        // equal to the size of remaining portion to the
        // right of the smaller array
        int c2 = n - c1 - 1;
        int l1 = a1[c1];
        int l2 = a2[c2 - 1];
        int r1 = c1 == n - 1 ? INT_MAX : a1[c1 + 1];
        int r2 = c2 == m ? INT_MAX : a2[c2];
        // compare the border elements and check for the
        // target index
        if (l1 > r2) {
            h = c1 - 1;
            if (h == -1)
                idx = 0;
        }
        else if (l2 > r1) {
            l = c1 + 1;
            if (l == n - 1)
                idx = n;
        }
        else {
            idx = c1 + 1;
            break;
        }
    }
 
    for (int i = idx; i < n; i++)
        swap(a1[i], a2[i - idx]);
 
    sort(a1, a1 + n);
 
    sort(a2, a2 + m);
}
 
void merge(int arr1[], int arr2[], int n, int m)
{
    // code here
    if (n > m) {
        sol(arr2, arr1, m, n);
        rotate(arr1, n, n - m);
 
        for (int i = 0; i < m; i++)
            swap(arr2[i], arr1[i]);
    }
    else {
        sol(arr1, arr2, n, m);
    }
}
 
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: nlogn
Space complexity: constant
#include <bits/stdc++.h>
using namespace std;
 
void merge(int arr1[], int arr2[], int n, int m)
{
    // two pointer to iterate
    int i = 0;
    int j = 0;
    while (i < n && j < m) {
        // if arr1[i] <= arr2[j] then both array is already
        // sorted
        if (arr1[i] <= arr2[j]) {
            i++;
        }
        else if (arr1[i] > arr2[j]) {
            // if arr1[i]>arr2[j] then first we swap both
            // element so that arr1[i] become smaller means
            // arr1[] become sorted then we check that
            // arr2[j] is smaller than all other element in
            // right side of arr2[j] if arr2[] is not sorted
            // then we linearly do sorting
            // means while adjacent element are less than
            // new arr2[j] we do sorting like by changing
            // position of element by shifting one position
            // toward left
            swap(arr1[i], arr2[j]);
            i++;
            if (j < m - 1 && arr2[j + 1] < arr2[j]) {
                int temp = arr2[j];
                int tempj = j + 1;
                while (arr2[tempj] < temp && tempj < m) {
                    arr2[tempj - 1] = arr2[tempj];
                    tempj++;
                }
                arr2[tempj - 1] = temp;
            }
        }
    }
}
 
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
// C++ program to calculate the
// product of max element of
// first array and min element
// of second array
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate
// the product
int minMaxProduct(int arr1[],
                  int arr2[],
                  int n1,
                  int n2)
{
    // Sort the arrays to find
    // the maximum and minimum
    // elements in given arrays
    sort(arr1, arr1 + n1);
    sort(arr2, arr2 + n2);
 
    // Return product of
    // maximum and minimum.
    return arr1[n1 - 1] * arr2[0];
}
 
// Driven code
int main()
{
    int arr1[] = { 10, 2, 3, 6, 4, 1 };
    int arr2[] = { 5, 1, 4, 2, 6, 9 };
    int n1 = sizeof(arr1) / sizeof(arr1[0]);
    int n2 = sizeof(arr1) / sizeof(arr1[0]);
    cout << minMaxProduct(arr1, arr2, n1, n2);
    return 0;
}

Time complexity: nlogn
Space complexity: constant
// C++ program to find the to
// calculate the product of
// max element of first array
// and min element of second array
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate the product
int minMaxProduct(int arr1[], int arr2[],
                  int n1, int n2)
{
    // Initialize max of first array
    int max = arr1[0];
 
    // initialize min of second array
    int min = arr2[0];
 
    int i;
    for (i = 1; i < n1 && i < n2; ++i)
    {
 
        // To find the maximum
        // element in first array
        if (arr1[i] > max)
            max = arr1[i];
 
        // To find the minimum
        // element in second array
        if (arr2[i] < min)
            min = arr2[i];
    }
 
    // Process remaining elements
    while (i < n1)
    {
        if (arr1[i] > max)
        max = arr1[i];
        i++;
    }
    while (i < n2)
    {
        if (arr2[i] < min)
        min = arr2[i];
        i++;
    }
 
    return max * min;
}
 
// Driven code
int main()
{
    int arr1[] = { 10, 2, 3, 6, 4, 1 };
    int arr2[] = { 5, 1, 4, 2, 6, 9 };
    int n1 = sizeof(arr1) / sizeof(arr1[0]);
    int n2 = sizeof(arr1) / sizeof(arr1[0]);
    cout << minMaxProduct(arr1, arr2, n1, n2)
         << endl;
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to implement linear
// search in unsorted array
#include <bits/stdc++.h>
using namespace std;
 
// Function to implement search operation
int findElement(int arr[], int n, int key)
{
    int i;
    for (i = 0; i < n; i++)
        if (arr[i] == key)
            return i;
     
      // If the key is not found
    return -1;
}
 
// Driver's Code
int main()
{
    int arr[] = { 12, 34, 10, 6, 40 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Using a last element as search element
    int key = 40;
   
      // Function call
    int position = findElement(arr, n, key);
 
    if (position == -1)
        cout << "Element not found";
    else
        cout << "Element Found at Position: "
             << position + 1;
 
    return 0;
}
 
// This code is contributed
// by Akanksha Rai

Time complexity: linear
Space complexity: constant
// C++ program to implement insert
// operation in an unsorted array.
#include <iostream>
using namespace std;
 
// Inserts a key in arr[] of given capacity.
// n is current size of arr[]. This
// function returns n + 1 if insertion
// is successful, else n.
int insertSorted(int arr[], int n, int key, int capacity)
{
 
    // Cannot insert more elements if n is
    // already more than or equal to capacity
    if (n >= capacity)
        return n;
 
    arr[n] = key;
 
    return (n + 1);
}
 
// Driver Code
int main()
{
    int arr[20] = { 12, 16, 20, 40, 50, 70 };
    int capacity = sizeof(arr) / sizeof(arr[0]);
    int n = 6;
    int i, key = 26;
 
    cout << "\n Before Insertion: ";
    for (i = 0; i < n; i++)
        cout << arr[i] << " ";
 
    // Inserting key
    n = insertSorted(arr, n, key, capacity);
 
    cout << "\n After Insertion: ";
    for (i = 0; i < n; i++)
        cout << arr[i] << " ";
 
    return 0;
}
 
// This code is contributed by SHUBHAMSINGH10

Time complexity: constant
Space complexity: constant
// C Program to Insert an element
// at a specific position in an Array
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to insert element
// at a specific position
void insertElement(int arr[], int n, int x, int pos)
{
    // shift elements to the right
    // which are on the right side of pos
    for (int i = n - 1; i >= pos; i--)
        arr[i + 1] = arr[i];
 
    arr[pos] = x;
}
 
// Driver's code
int main()
{
    int arr[15] = { 2, 4, 1, 8, 5 };
    int n = 5;
 
    cout<<"Before insertion : ";
    for (int i = 0; i < n; i++)
        cout<<arr[i]<<" ";
 
    cout<<endl;
 
    int x = 10, pos = 2;
   
      // Function call
    insertElement(arr, n, x, pos);
    n++;
 
    cout<<"After insertion : ";
    for (int i = 0; i < n; i++)
        cout<<arr[i]<<" ";
 
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to implement delete operation in a
// unsorted array
#include <iostream>
using namespace std;
 
// To search a key to be deleted
int findElement(int arr[], int n, int key);
 
// Function to delete an element
int deleteElement(int arr[], int n, int key)
{
    // Find position of element to be deleted
    int pos = findElement(arr, n, key);
 
    if (pos == -1) {
        cout << "Element not found";
        return n;
    }
 
    // Deleting element
    int i;
    for (i = pos; i < n - 1; i++)
        arr[i] = arr[i + 1];
 
    return n - 1;
}
 
// Function to implement search operation
int findElement(int arr[], int n, int key)
{
    int i;
    for (i = 0; i < n; i++)
        if (arr[i] == key)
            return i;
 
    return -1;
}
 
// Driver's code
int main()
{
    int i;
    int arr[] = { 10, 50, 30, 40, 20 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
    int key = 30;
 
    cout << "Array before deletion\n";
    for (i = 0; i < n; i++)
        cout << arr[i] << " ";
     
   
      // Function call
    n = deleteElement(arr, n, key);
 
    cout << "\n\nArray after deletion\n";
    for (i = 0; i < n; i++)
        cout << arr[i] << " ";
 
    return 0;
}
 
// This code is contributed by shubhamsingh10

Time complexity: linear
Space complexity: constant
// C++ program to implement binary search in sorted array
#include <bits/stdc++.h>
using namespace std;
 
int binarySearch(int arr[], int low, int high, int key)
{
    if (high < low)
        return -1;
    int mid = (low + high) / 2; /*low + (high - low)/2;*/
    if (key == arr[mid])
        return mid;
    if (key > arr[mid])
        return binarySearch(arr, (mid + 1), high, key);
    return binarySearch(arr, low, (mid - 1), key);
}
 
/* Driver code */
int main()
{
    // Let us search 3 in below array
    int arr[] = { 5, 6, 7, 8, 9, 10 };
    int n, key;
 
    n = sizeof(arr) / sizeof(arr[0]);
    key = 10;
 
    // Function call
    cout << "Index: " << binarySearch(arr, 0, n - 1, key)
         << endl;
    return 0;
}
 
// This code is contributed by NamrataSrivastava1

Time complexity: logn
Space complexity: logn
// C++ program to implement insert operation in
// an sorted array.
#include <bits/stdc++.h>
using namespace std;
 
// Inserts a key in arr[] of given capacity. n is current
// size of arr[]. This function returns n+1 if insertion
// is successful, else n.
int insertSorted(int arr[], int n, int key, int capacity)
{
    // Cannot insert more elements if n is already
    // more than or equal to capacity
    if (n >= capacity)
        return n;
 
    int i;
    for (i = n - 1; (i >= 0 && arr[i] > key); i--)
        arr[i + 1] = arr[i];
 
    arr[i + 1] = key;
 
    return (n + 1);
}
 
/* Driver code */
int main()
{
    int arr[20] = { 12, 16, 20, 40, 50, 70 };
    int capacity = sizeof(arr) / sizeof(arr[0]);
    int n = 6;
    int i, key = 26;
 
    cout << "\nBefore Insertion: ";
    for (i = 0; i < n; i++)
        cout << arr[i] << " ";
 
    // Function call
    n = insertSorted(arr, n, key, capacity);
 
    cout << "\nAfter Insertion: ";
    for (i = 0; i < n; i++)
        cout << arr[i] << " ";
 
    return 0;
}
 
// This code is contributed by SHUBHAMSINGH10

Time complexity: linear
Space complexity: constant
// C++ program to implement delete operation in a
// sorted array
#include <bits/stdc++.h>
using namespace std;
 
// To search a key to be deleted
int binarySearch(int arr[], int low, int high, int key);
 
/* Function to delete an element */
int deleteElement(int arr[], int n, int key)
{
    // Find position of element to be deleted
    int pos = binarySearch(arr, 0, n - 1, key);
 
    if (pos == -1) {
        cout << "Element not found";
        return n;
    }
 
    // Deleting element
    int i;
    for (i = pos; i < n - 1; i++)
        arr[i] = arr[i + 1];
 
    return n - 1;
}
 
int binarySearch(int arr[], int low, int high, int key)
{
    if (high < low)
        return -1;
    int mid = (low + high) / 2;
    if (key == arr[mid])
        return mid;
    if (key > arr[mid])
        return binarySearch(arr, (mid + 1), high, key);
    return binarySearch(arr, low, (mid - 1), key);
}
 
// Driver code
int main()
{
    int i;
    int arr[] = { 10, 20, 30, 40, 50 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
    int key = 30;
 
    cout << "Array before deletion\n";
    for (i = 0; i < n; i++)
        cout << arr[i] << " ";
 
    // Function call
    n = deleteElement(arr, n, key);
 
    cout << "\n\nArray after deletion\n";
    for (i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// This code is contributed by shubhamsingh10

Time complexity: linear
Space complexity: logn
// C++ program for the above approach
#include <bits/stdc++.h>
 
using namespace std;
 
// Function to find and print pair
bool chkPair(int A[], int size, int x)
{
    for (int i = 0; i < (size - 1); i++) {
        for (int j = (i + 1); j < size; j++) {
            if (A[i] + A[j] == x) {
                return 1;
            }
        }
    }
 
    return 0;
}
 
// Driver code
int main()
{
    int A[] = { 0, -1, 2, -3, 1 };
    int x = -2;
    int size = sizeof(A) / sizeof(A[0]);
 
    if (chkPair(A, size, x)) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << x << endl;
    }
 
    return 0;
}
 
// This code is contributed by Samim Hossain Mondal.

Time complexity: quadratic
Space complexity: constant
// C++ program to check if given array
// has 2 elements whose sum is equal
// to the given value
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if array has 2 elements
// whose sum is equal to the given value
bool hasArrayTwoCandidates(int A[], int arr_size, int sum)
{
    int l, r;
 
    /* Sort the elements */
    sort(A, A + arr_size);
 
    /* Now look for the two candidates in
       the sorted array*/
    l = 0;
    r = arr_size - 1;
    while (l < r) {
        if (A[l] + A[r] == sum)
            return 1;
        else if (A[l] + A[r] < sum)
            l++;
        else // A[l] + A[r] > sum
            r--;
    }
    return 0;
}
 
/* Driver program to test above function */
int main()
{
    int A[] = { 1, 4, 45, 6, 10, -8 };
    int n = 16;
    int arr_size = sizeof(A) / sizeof(A[0]);
 
    // Function calling
    if (hasArrayTwoCandidates(A, arr_size, n))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}

Time complexity: nlogn
Space complexity: constant
// C++ program to check if given array
// has 2 elements whose sum is equal
// to the given value
 
#include <bits/stdc++.h>
using namespace std;
 
bool binarySearch(int A[], int low, int high, int searchKey)
{
 
    while (low <= high) {
        int m = low + (high - low) / 2;
 
        // Check if searchKey is present at mid
        if (A[m] == searchKey)
            return true;
 
        // If searchKey greater, ignore left half
        if (A[m] < searchKey)
            low = m + 1;
 
        // If searchKey is smaller, ignore right half
        else
            high = m - 1;
    }
 
    // if we reach here, then element was
    // not present
    return false;
}
 
bool checkTwoSum(int A[], int arr_size, int sum)
{
    int l, r;
 
    /* Sort the elements */
    sort(A, A + arr_size);
 
    // Traversing all element in an array search for
    // searchKey
    for (int i = 0; i < arr_size - 1; i++) {
 
        int searchKey = sum - A[i];
        // calling binarySearch function
        if (binarySearch(A, i + 1, arr_size - 1, searchKey)
            == true) {
            return true;
        }
    }
    return false;
}
 
/* Driver program to test above function */
int main()
{
    int A[] = { 1, 4, 45, 6, 10, -8 };
    int n = 14;
    int arr_size = sizeof(A) / sizeof(A[0]);
 
    // Function calling
    if (checkTwoSum(A, arr_size, n))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}

Time complexity: nlogn
Space complexity: constant
// C++ program to check if given array
// has 2 elements whose sum is equal
// to the given value
#include <bits/stdc++.h>
 
using namespace std;
 
void printPairs(int arr[], int arr_size, int sum)
{
    unordered_set<int> s;
    for (int i = 0; i < arr_size; i++) {
        int temp = sum - arr[i];
 
        if (s.find(temp) != s.end()) {
            cout << "Yes" << endl;
            return;
        }
        s.insert(arr[i]);
    }
    cout << "No" << endl;
}
 
/* Driver Code */
int main()
{
    int A[] = { 1, 4, 45, 6, 10, 8 };
    int n = 16;
    int arr_size = sizeof(A) / sizeof(A[0]);
 
    // Function calling
    printPairs(A, arr_size, n);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// Code in cpp to tell if there exists a pair in array whose
// sum results in x.
#include <iostream>
using namespace std;
 
// Function to print pairs
void printPairs(int a[], int n, int x)
{
    int i;
    int rem[x];
    // initializing the rem values with 0's.
    for (i = 0; i < x; i++)
        rem[i] = 0;
    // Perform the remainder operation only if the element
    // is x, as numbers greater than x can't be used to get
    // a sum x. Updating the count of remainders.
    for (i = 0; i < n; i++)
        if (a[i] < x)
            rem[a[i] % x]++;
 
    // Traversing the remainder list from start to middle to
    // find pairs
    for (i = 1; i < x / 2; i++) {
        if (rem[i] > 0 && rem[x - i] > 0) {
            // The elements with remainders i and x-i will
            // result to a sum of x. Once we get two
            // elements which add up to x , we print x and
            // break.
            cout << "Yes\n";
            break;
        }
    }
 
    // Once we reach middle of remainder array, we have to
    // do operations based on x.
    if (i >= x / 2) {
        if (x % 2 == 0) {
            // if x is even and we have more than 1 elements
            // with remainder x/2, then we will have two
            // distinct elements which add up to x. if we
            // dont have more than 1 element, print "No".
            if (rem[x / 2] > 1)
                cout << "Yes\n";
            else
                cout << "No\n";
        }
        else {
            // When x is odd we continue the same process
            // which we did in previous loop.
            if (rem[x / 2] > 0 && rem[x - x / 2] > 0)
                cout << "Yes\n";
            else
                cout << "No\n";
        }
    }
}
 
/* Driver Code */
int main()
{
    int A[] = { 1, 4, 45, 6, 10, 8 };
    int n = 16;
    int arr_size = sizeof(A) / sizeof(A[0]);
 
    // Function calling
    printPairs(A, arr_size, n);
 
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Time complexity: linear
Space complexity: linear
// C++ program to search an element in an array
// where difference between adjacent elements is atmost k
#include<bits/stdc++.h>
using namespace std;
 
// x is the element to be searched in arr[0..n-1]
// such that all elements differ by at-most k.
int search(int arr[], int n, int x, int k)
{
    // Traverse the given array starting from
    // leftmost element
    int i = 0;
    while (i < n)
    {
        // If x is found at index i
        if (arr[i] == x)
            return i;
 
        // Jump the difference between current
        // array element and x divided by k
        // We use max here to make sure that i
        // moves at-least one step ahead.
        i = i + max(1, abs(arr[i]-x)/k);
    }
 
    cout << "number is not present!";
    return -1;
}
 
// Driver program to test above function
int main()
{
    int arr[] = {2, 4, 5, 7, 7, 6};
    int x = 6;
    int k = 2;
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << "Element " << x  << " is present at index "
         << search(arr, n, x, k);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to print common elements in three arrays
#include <bits/stdc++.h>
using namespace std;
 
// This function prints common elements in ar1
void findCommon(int ar1[], int ar2[], int ar3[], int n1,
                int n2, int n3)
{
    // Initialize starting indexes for ar1[], ar2[] and
    // ar3[]
    int i = 0, j = 0, k = 0;
 
    // Iterate through three arrays while all arrays have
    // elements
    while (i < n1 && j < n2 && k < n3) {
        // If x = y and y = z, print any of them and move
        // ahead in all arrays
        if (ar1[i] == ar2[j] && ar2[j] == ar3[k]) {
            cout << ar1[i] << " ";
            i++;
            j++;
            k++;
        }
 
        // x < y
        else if (ar1[i] < ar2[j])
            i++;
 
        // y < z
        else if (ar2[j] < ar3[k])
            j++;
 
        // We reach here when x > y and z < y, i.e., z is
        // smallest
        else
            k++;
    }
}
 
// Driver code
int main()
{
    int ar1[] = { 1, 5, 10, 20, 40, 80 };
    int ar2[] = { 6, 7, 20, 80, 100 };
    int ar3[] = { 3, 4, 15, 20, 30, 70, 80, 120 };
    int n1 = sizeof(ar1) / sizeof(ar1[0]);
    int n2 = sizeof(ar2) / sizeof(ar2[0]);
    int n3 = sizeof(ar3) / sizeof(ar3[0]);
 
    cout << "Common Elements are ";
    findCommon(ar1, ar2, ar3, n1, n2, n3);
    return 0;
}
 
// This code is contributed by Sania Kumari Gupta (kriSania804)

Time complexity: linear
Space complexity: constant
// C++ program to print common elements in three arrays
#include <bits/stdc++.h>
using namespace std;
 
// This function prints common elements in ar1
void findCommon(int ar1[], int ar2[], int ar3[], int n1, int n2, int n3)
{
    // Initialize starting indexes for ar1[], ar2[] and ar3[]
    int i = 0, j = 0, k = 0;
 
    // Declare three variables prev1, prev2, prev3 to track
    // previous element
    int prev1, prev2, prev3;
 
    // Initialize prev1, prev2, prev3 with INT_MIN
    prev1 = prev2 = prev3 = INT_MIN;
 
    // Iterate through three arrays while all arrays have
    // elements
    while (i < n1 && j < n2 && k < n3) {
 
        // If ar1[i] = prev1 and i < n1, keep incrementing i
        while (ar1[i] == prev1 && i < n1)
            i++;
 
        // If ar2[j] = prev2 and j < n2, keep incrementing j
        while (ar2[j] == prev2 && j < n2)
            j++;
 
        // If ar3[k] = prev3 and k < n3, keep incrementing k
        while (ar3[k] == prev3 && k < n3)
            k++;
 
        // If x = y and y = z, print any of them, update
        // prev1 prev2, prev3 and move ahead in each array
        if (ar1[i] == ar2[j] && ar2[j] == ar3[k]) {
            cout << ar1[i] << " ";
            prev1 = ar1[i++];
            prev2 = ar2[j++];
            prev3 = ar3[k++];
        }
 
        // If x < y, update prev1 and increment i
        else if (ar1[i] < ar2[j])
            prev1 = ar1[i++];
 
        // If y < z, update prev2 and increment j
        else if (ar2[j] < ar3[k])
            prev2 = ar2[j++];
 
        // We reach here when x > y and z < y, i.e., z is
        // smallest update prev3 and increment k
        else
            prev3 = ar3[k++];
    }
}
 
// Driver code
int main()
{
    int ar1[] = { 1, 5, 10, 20, 40, 80, 80 };
    int ar2[] = { 6, 7, 20, 80, 80, 100 };
    int ar3[] = { 3, 4, 15, 20, 30, 70, 80, 80, 120 };
    int n1 = sizeof(ar1) / sizeof(ar1[0]);
    int n2 = sizeof(ar2) / sizeof(ar2[0]);
    int n3 = sizeof(ar3) / sizeof(ar3[0]);
 
    cout << "Common Elements are ";
    findCommon(ar1, ar2, ar3, n1, n2, n3);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Time complexity: linear
Space complexity: constant