Banana-Leopard/TASTY huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
 
// Function to print common strings with minimum index sum
void find(vector<string> list1, vector<string> list2)
{
    vector<string> res; // resultant list
    int max_possible_sum = list1.size() + list2.size() - 2;
 
    // iterating over sum in ascending order
    for (int sum = 0; sum <= max_possible_sum ; sum++)
    {
        // iterating over one list and check index
        // (Corresponding to given sum) in other list
        for (int i = 0; i <= sum; i++)
         
            // put common strings in resultant list 
            if (i < list1.size() &&
                (sum - i) < list2.size() &&
                list1[i] == list2[sum - i])
                res.push_back(list1[i]);        
 
        // if common string found then break as we are
        // considering index sums in increasing order.
        if (res.size() > 0)
            break;
    }
 
    // print the resultant list
    for (int i = 0; i < res.size(); i++)
        cout << res[i] << " ";
}
 
// Driver code
int main()
{
    // Creating list1
    vector<string> list1;
    list1.push_back("GeeksforGeeks");
    list1.push_back("Udemy");
    list1.push_back("Coursera");
    list1.push_back("edX");
 
    // Creating list2
    vector<string> list2;
    list2.push_back("Codecademy");
    list2.push_back("Khan Academy");
    list2.push_back("GeeksforGeeks");
 
    find(list1, list2);
    return 0;
}

Time complexity: linear
Space complexity: quadratic
// Hashing based C++ program to find common elements
// with minimum index sum.
#include <bits/stdc++.h>
using namespace std;
 
// Function to print common strings with minimum index sum
void find(vector<string> list1, vector<string> list2)
{
    // mapping strings to their indices
    unordered_map<string, int> map;
    for (int i = 0; i < list1.size(); i++)
        map[list1[i]] = i;
 
    vector<string> res; // resultant list
 
    int minsum = INT_MAX;
    for (int j = 0; j < list2.size(); j++)
    {
        if (map.count(list2[j]))
        {
            // If current sum is smaller than minsum
            int sum = j + map[list2[j]];
            if (sum < minsum)
            {
                minsum = sum;
                res.clear();
                res.push_back(list2[j]);
            }
 
            // if index sum is same then put this
            // string in resultant list as well 
            else if (sum == minsum)
                res.push_back(list2[j]);
        }
    }
 
    // Print result
    for (int i = 0; i < res.size(); i++)
        cout << res[i] << " ";
}
 
// Driver code
int main()
{
    // Creating list1
    vector<string> list1;
    list1.push_back("GeeksforGeeks");
    list1.push_back("Udemy");
    list1.push_back("Coursera");
    list1.push_back("edX");
 
    // Creating list2
    vector<string> list2;
    list2.push_back("Codecademy");
    list2.push_back("Khan Academy");
    list2.push_back("GeeksforGeeks");
 
    find(list1, list2);
    return 0;
}

Time complexity: linear
Space complexity: quadratic
// C++ program to find a pair with given sum such that
// every element of pair is in different rows.
#include<bits/stdc++.h>
using namespace std;
 
const int MAX = 100;
 
// Function to find pair for given sum in matrix
// mat[][] --> given matrix
// n --> order of matrix
// sum --> given sum for which we need to find pair
void pairSum(int mat[][MAX], int n, int sum)
{
    // First sort all the rows in ascending order
    for (int i=0; i<n; i++)
        sort(mat[i], mat[i]+n);
 
    // Select i'th row and find pair for element in i'th
    // row in j'th row whose summation is equal to given sum
    for (int i=0; i<n-1; i++)
    {
        for (int j=i+1; j<n; j++)
        {
            int left = 0, right = n-1;
            while (left<n && right>=0)
            {
                if ((mat[i][left] + mat[j][right]) == sum)
                {
                    cout << "(" << mat[i][left]
                         << ", "<< mat[j][right] << "), ";
                    left++;
                    right--;
                }
                else
                {
                    if ((mat[i][left] + mat[j][right]) < sum)
                        left++;
                    else
                        right--;
                }
            }
        }
    }
}
 
// Driver program to run the case
int main()
{
    int n = 4, sum = 11;
    int mat[][MAX] = {{1, 3, 2, 4},
                      {5, 8, 7, 6},
                      {9, 10, 13, 11},
                      {12, 0, 14, 15}};
    pairSum(mat, n, sum);
    return 0;
}

Time complexity: cubic
Space complexity: constant
// A Program to prints common element in all
// rows of matrix
#include <bits/stdc++.h>
using namespace std;
 
// Specify number of rows and columns
#define M 4
#define N 5
 
// prints common element in all rows of matrix
void printCommonElements(int mat[M][N])
{
    unordered_map<int, int> mp;
 
    // initialize 1st row elements with value 1
    for (int j = 0; j < N; j++)
        mp[mat[0][j]] = 1;
 
    // traverse the matrix
    for (int i = 1; i < M; i++)
    {
        for (int j = 0; j < N; j++)
        {
            // If element is present in the map and
            // is not duplicated in current row.
            if (mp[mat[i][j]] == i)
            {
               // we increment count of the element
               // in map by 1
                mp[mat[i][j]] = i + 1;
 
                // If this is last row
                if (i==M-1 && mp[mat[i][j]]==M)
                  cout << mat[i][j] << " ";
            }
        }
    }
}
 
// driver program to test above function
int main()
{
    int mat[M][N] =
    {
        {1, 2, 1, 4, 8},
        {3, 7, 8, 5, 1},
        {8, 7, 7, 3, 1},
        {8, 1, 2, 7, 9},
    };
 
    printCommonElements(mat);
 
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// C++ program to find all permutations of a given row
#include<bits/stdc++.h>
#define MAX 100
 
using namespace std;
 
// Function to find all permuted rows of a given row r
void permutatedRows(int mat[][MAX], int m, int n, int r)
{
    // Creating an empty set
    unordered_set<int> s;
 
    // Count frequencies of elements in given row r
    for (int j=0; j<n; j++)
        s.insert(mat[r][j]);
 
    // Traverse through all remaining rows
    for (int i=0; i<m; i++)
    {
        // we do not need to check for given row r
        if (i==r)
            continue;
 
        // initialize hash i.e; count frequencies
        // of elements in row i
        int j;
        for (j=0; j<n; j++)
            if (s.find(mat[i][j]) == s.end())
                break;
        if (j != n)
           continue;
 
        cout << i << ", ";
    }
}
 
// Driver program to run the case
int main()
{
    int m = 4, n = 4,r = 3;
    int mat[][MAX] = {{3, 1, 4, 2},
                      {1, 6, 9, 3},
                      {1, 2, 3, 4},
                      {4, 3, 2, 1}};
    permutatedRows(mat, m, n, r);
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// CPP program to make a permutation of numbers
// from 1 to n using minimum changes.
#include <bits/stdc++.h>
using namespace std;
 
void makePermutation(int a[], int n)
{
    // Store counts of all elements.
    unordered_map<int, int> count;
    for (int i = 0; i < n; i++)
        count[a[i]]++;
 
    int next_missing = 1;
    for (int i = 0; i < n; i++) {
        if (count[a[i]] != 1 || a[i] > n || a[i] < 1) {
            count[a[i]]--;
 
            // Find next missing element to put
            // in place of current element.
            while (count.find(next_missing) != count.end())
                next_missing++;
 
            // Replace with next missing and insert the
            // missing element in hash.
            a[i] = next_missing;
            count[next_missing] = 1;
        }
    }
}
 
// Driver Code
int main()
{
    int A[] = { 2, 2, 3, 3 };
    int n = sizeof(A) / sizeof(A[0]);
    makePermutation(A, n);
    for (int i = 0; i < n; i++)
        cout << A[i] << " ";
    return 0;
}

Time complexity: nlogn
Space complexity: linear
// C++ implementation of simple method to find count of
// pairs with given sum.
#include <bits/stdc++.h>
using namespace std;
 
// Returns number of pairs in arr[0..n-1] with sum equal
// to 'sum'
int getPairsCount(int arr[], int n, int sum)
{
    int count = 0; // Initialize result
 
    // Consider all possible pairs and check their sums
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++)
            if (arr[i] + arr[j] == sum)
                count++;
 
    return count;
}
 
// Driver function to test the above function
int main()
{
    int arr[] = { 1, 5, 7, -1, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int sum = 6;
    cout << "Count of pairs is "
         << getPairsCount(arr, n, sum);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Time complexity: quadratic
Space complexity: constant
// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the count of pairs
int getPairsCount(int arr[], int n, int k)
{
    sort(arr, arr + n);
    int x = 0, c = 0, y, z;
    for (int i = 0; i < n - 1; i++) {
        x = k - arr[i];
 
        // Lower bound from i+1
        int y = lower_bound(arr + i + 1, arr + n, x) - arr;
 
        // Upper bound from i+1
        int z = upper_bound(arr + i + 1, arr + n, x) - arr;
        c = c + z - y;
    }
    return c;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 5, 7, -1, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 6;
 
    // Function call
    cout << "Count of pairs is "
         << getPairsCount(arr, n, k);
    return 0;
}

Time complexity: nlogn
Space complexity: constant
// C++ implementation of simple method to find count of
// pairs with given sum.
#include <bits/stdc++.h>
using namespace std;
 
// Returns number of pairs in arr[0..n-1] with sum equal
// to 'sum'
int getPairsCount(int arr[], int n, int sum)
{
    unordered_map<int, int> m;
 
    // Store counts of all elements in map m
    for (int i = 0; i < n; i++)
        m[arr[i]]++;
 
    int twice_count = 0;
 
    // iterate through each element and increment the
    // count (Notice that every pair is counted twice)
    for (int i = 0; i < n; i++) {
        twice_count += m[sum - arr[i]];
 
        // if (arr[i], arr[i]) pair satisfies the condition,
        // then we need to ensure that the count is
        // decreased by one such that the (arr[i], arr[i])
        // pair is not considered
        if (sum - arr[i] == arr[i])
            twice_count--;
    }
 
    // return the half of twice_count
    return twice_count / 2;
}
 
// Driver function to test the above function
int main()
{
    int arr[] = { 1, 5, 7, -1, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int sum = 6;
    cout << "Count of pairs is "
         << getPairsCount(arr, n, sum);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ implementation of simple method to
//  find count of pairs with given sum.
#include <bits/stdc++.h>
using namespace std;
 
// Returns number of pairs in arr[0..n-1] with sum equal
// to 'sum'
int getPairsCount(int arr[], int n, int k)
{
    unordered_map<int, int> m;
    int count = 0;
    for (int i = 0; i < n; i++) {
        if (m.find(k - arr[i]) != m.end()) {
            count += m[k - arr[i]];
        }
        m[arr[i]]++;
    }
    return count;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 5, 7, -1, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int sum = 6;
 
    // Function Call
    cout << "Count of pairs is "
         << getPairsCount(arr, n, sum);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ implementation to count pairs from both linked 
// lists  whose sum is equal to a given value
#include <bits/stdc++.h>
using namespace std;
  
/* A Linked list node */
struct Node
{
  int data;
  struct Node* next;
};
  
// function to insert a node at the
// beginning of the linked list
void push(struct Node** head_ref, int new_data)
{
  /* allocate node */
  struct Node* new_node =
          (struct Node*) malloc(sizeof(struct Node));
   
  /* put in the data  */
  new_node->data  = new_data;
   
  /* link the old list to the new node */
  new_node->next = (*head_ref);
   
  /* move the head to point to the new node */
  (*head_ref)    = new_node;
}
  
// function to count all pairs from both the linked lists
// whose sum is equal to a given value
int countPairs(struct Node* head1, struct Node* head2, int x)
{
    int count = 0;
      
    struct Node *p1, *p2;
      
    // traverse the 1st linked list
    for (p1 = head1; p1 != NULL; p1 = p1->next)
  
        // for each node of 1st list
        // traverse the 2nd list
  
        for (p2 = head2; p2 != NULL; p2 = p2->next)
  
            // if sum of pair is equal to 'x'
            // increment count
            if ((p1->data + p2->data) == x)
                count++;            
          
    // required count of pairs     
    return count;
}
  
// Driver program to test above
int main()
{
    struct Node* head1 = NULL;
    struct Node* head2 = NULL;
      
    // create linked list1 3->1->5->7
    push(&head1, 7);
    push(&head1, 5);
    push(&head1, 1);
    push(&head1, 3);    
      
    // create linked list2 8->2->5->3
    push(&head2, 3);
    push(&head2, 5);
    push(&head2, 2);
    push(&head2, 8);
      
    int x = 10;
      
    cout << "Count = "
         << countPairs(head1, head2, x);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ implementation to count pairs from both linked 
// lists  whose sum is equal to a given value
#include <bits/stdc++.h>
using namespace std;
  
/* A Linked list node */
struct Node
{
  int data;
  struct Node* next;
};
  
// function to insert a node at the
// beginning of the linked list
void push(struct Node** head_ref, int new_data)
{
  /* allocate node */
  struct Node* new_node =
          (struct Node*) malloc(sizeof(struct Node));
   
  /* put in the data  */
  new_node->data  = new_data;
   
  /* link the old list to the new node */
  new_node->next = (*head_ref);
   
  /* move the head to point to the new node */
  (*head_ref)    = new_node;
}
  
// function to count all pairs from both the linked 
// lists whose sum is equal to a given value
int countPairs(struct Node* head1, struct Node* head2,
                                              int x)
{
    int count = 0;
      
    // sort head1 in ascending order and
    // head2 in descending order
    // sort (head1), sort (head2)
    // For simplicity both lists are considered to be 
    // sorted in the respective orders
      
    // traverse both the lists from left to right
    while (head1 != NULL && head2 != NULL)
    {
        // if this sum is equal to 'x', then move both 
        // the lists to next nodes and increment 'count'
        if ((head1->data + head2->data) == x)
        {
            head1 = head1->next;
            head2 = head2->next;
            count++;    
        }    
          
        // if this sum is greater than x, then
        // move head2 to next node
        else if ((head1->data + head2->data) > x)
            head2 = head2->next;
              
        // else move head1 to next node    
        else
            head1 = head1->next;
    }        
          
    // required count of pairs     
    return count;
}
  
// Driver program to test above
int main()
{
    struct Node* head1 = NULL;
    struct Node* head2 = NULL;
      
    // create linked list1 1->3->5->7
    // assumed to be in ascending order
    push(&head1, 7);
    push(&head1, 5);
    push(&head1, 3);
    push(&head1, 1);    
      
    // create linked list2 8->5->3->2
    // assumed to be in descending order
    push(&head2, 2);
    push(&head2, 3);
    push(&head2, 5);
    push(&head2, 8);
      
    int x = 10;
      
    cout << "Count = "
         << countPairs(head1, head2, x);
    return 0;
}

Time complexity: nlogn
Space complexity: constant
// C++ implementation to count pairs from both linked  
// lists whose sum is equal to a given value
#include <bits/stdc++.h>
using namespace std;
  
/* A Linked list node */
struct Node
{
  int data;
  struct Node* next;
};
  
// function to insert a node at the
// beginning of the linked list
void push(struct Node** head_ref, int new_data)
{
  /* allocate node */
  struct Node* new_node =
          (struct Node*) malloc(sizeof(struct Node));
   
  /* put in the data  */
  new_node->data  = new_data;
   
  /* link the old list to the new node */
  new_node->next = (*head_ref);
   
  /* move the head to point to the new node */
  (*head_ref)    = new_node;
}
  
// function to count all pairs from both the linked 
// lists whose sum is equal to a given value
int countPairs(struct Node* head1, struct Node* head2, 
                                               int x)
{
    int count = 0;
      
    unordered_set<int> us;
      
    // insert all the elements of 1st list
    // in the hash table(unordered_set 'us')
    while (head1 != NULL)
    {
        us.insert(head1->data);    
          
        // move to next node    
        head1 = head1->next;
    }
      
    // for each element of 2nd list
    while (head2 != NULL)    
    {
        // find (x - head2->data) in 'us'
        if (us.find(x - head2->data) != us.end())
            count++;
          
        // move to next node
        head2 = head2->next;    
    }
    // required count of pairs     
    return count;
}
  
// Driver program to test above
int main()
{
    struct Node* head1 = NULL;
    struct Node* head2 = NULL;
      
    // create linked list1 3->1->5->7
    push(&head1, 7);
    push(&head1, 5);
    push(&head1, 1);
    push(&head1, 3);    
      
    // create linked list2 8->2->5->3
    push(&head2, 3);
    push(&head2, 5);
    push(&head2, 2);
    push(&head2, 8);
      
    int x = 10;
      
    cout << "Count = "
         << countPairs(head1, head2, x);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ implementation to count quadruples from four sorted arrays
// whose sum is equal to a given value x
#include <bits/stdc++.h>
 
using namespace std;
 
// function to count all quadruples from
// four sorted arrays whose sum is equal
// to a given value x
int countQuadruples(int arr1[], int arr2[],
                    int arr3[], int arr4[], int n, int x)
{
    int count = 0;
 
    // generate all possible quadruples from
    // the four sorted arrays
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            for (int k = 0; k < n; k++)
                for (int l = 0; l < n; l++)
                    // check whether elements of
                    // quadruple sum up to x or not
                    if ((arr1[i] + arr2[j] + arr3[k] + arr4[l]) == x)
                        count++;
 
    // required count of quadruples
    return count;
}
 
// Driver program to test above
int main()
{
    // four sorted arrays each of size 'n'
    int arr1[] = { 1, 4, 5, 6 };
    int arr2[] = { 2, 3, 7, 8 };
    int arr3[] = { 1, 4, 6, 10 };
    int arr4[] = { 2, 4, 7, 8 };
 
    int n = sizeof(arr1) / sizeof(arr1[0]);
    int x = 30;
    cout << "Count = "
         << countQuadruples(arr1, arr2, arr3,
                            arr4, n, x);
    return 0;
}

Time complexity: np
Space complexity: constant
// C++ implementation to count quadruples from
// four sorted arrays whose sum is equal to a
// given value x
#include <bits/stdc++.h>
 
using namespace std;
 
// find the 'value' in the given array 'arr[]'
// binary search technique is applied
bool isPresent(int arr[], int low, int high, int value)
{
    while (low <= high) {
        int mid = (low + high) / 2;
 
        // 'value' found
        if (arr[mid] == value)
            return true;
        else if (arr[mid] > value)
            high = mid - 1;
        else
            low = mid + 1;
    }
 
    // 'value' not found
    return false;
}
 
// function to count all quadruples from four
// sorted arrays whose sum is equal to a given value x
int countQuadruples(int arr1[], int arr2[], int arr3[],
                    int arr4[], int n, int x)
{
    int count = 0;
 
    // generate all triplets from the 1st three arrays
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            for (int k = 0; k < n; k++) {
 
                // calculate the sum of elements in
                // the triplet so generated
                int T = arr1[i] + arr2[j] + arr3[k];
 
                // check if 'x-T' is present in 4th
                // array or not
                if (isPresent(arr4, 0, n, x - T))
 
                    // increment count
                    count++;
            }
 
    // required count of quadruples
    return count;
}
 
// Driver program to test above
int main()
{
    // four sorted arrays each of size 'n'
    int arr1[] = { 1, 4, 5, 6 };
    int arr2[] = { 2, 3, 7, 8 };
    int arr3[] = { 1, 4, 6, 10 };
    int arr4[] = { 2, 4, 7, 8 };
 
    int n = sizeof(arr1) / sizeof(arr1[0]);
    int x = 30;
    cout << "Count = "
         << countQuadruples(arr1, arr2, arr3, arr4, n, x);
    return 0;
}

Time complexity: cubic
Space complexity: constant
// C++ implementation to count quadruples from
// four sorted arrays whose sum is equal to a
// given value x
#include <bits/stdc++.h>
 
using namespace std;
 
// count pairs from the two sorted array whose sum
// is equal to the given 'value'
int countPairs(int arr1[], int arr2[], int n, int value)
{
    int count = 0;
    int l = 0, r = n - 1;
 
    // traverse 'arr1[]' from left to right
    // traverse 'arr2[]' from right to left
    while (l < n & amp; &r >= 0) {
        int sum = arr1[l] + arr2[r];
 
        // if the 'sum' is equal to 'value', then
        // increment 'l', decrement 'r' and
        // increment 'count'
        if (sum == value) {
            l++, r--;
            count++;
        }
 
        // if the 'sum' is greater than 'value', then
        // decrement r
        else if (sum > value)
            r--;
 
        // else increment l
        else
            l++;
    }
 
    // required count of pairs
    return count;
}
 
// function to count all quadruples from four sorted arrays
// whose sum is equal to a given value x
int countQuadruples(int arr1[], int arr2[], int arr3[],
                    int arr4[], int n, int x)
{
    int count = 0;
 
    // generate all pairs from arr1[] and arr2[]
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) {
            // calculate the sum of elements in
            // the pair so generated
            int p_sum = arr1[i] + arr2[j];
 
            // count pairs in the 3rd and 4th array
            // having value 'x-p_sum' and then
            // accumulate it to 'count'
            count += countPairs(arr3, arr4, n, x - p_sum);
        }
 
    // required count of quadruples
    return count;
}
 
// Driver program to test above
int main()
{
    // four sorted arrays each of size 'n'
    int arr1[] = { 1, 4, 5, 6 };
    int arr2[] = { 2, 3, 7, 8 };
    int arr3[] = { 1, 4, 6, 10 };
    int arr4[] = { 2, 4, 7, 8 };
 
    int n = sizeof(arr1) / sizeof(arr1[0]);
    int x = 30;
    cout << "Count = "
         << countQuadruples(arr1, arr2, arr3,
                            arr4, n, x);
    return 0;
}

Time complexity: cubic
Space complexity: constant