Banana-Leopard/TASTY huggingface dataset:
// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Returns Length of the required subarray
int maxLen(int arr[], int N)
{
    // Map to store the previous sums
    unordered_map<int, int> presum;
 
    int sum = 0; // Initialize the 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];
        if (sum == 0)
            max_len = i + 1;
 
        // Look for this sum in Hash table
        if (presum.find(sum) != presum.end()) {
 
            // If this sum is seen before, then update
            // max_len
            max_len = max(max_len, i - presum[sum]);
        }
        else {
            // Else insert this sum with index
            // in hash table
            presum[sum] = i;
        }
    }
 
    return max_len;
}
 
// Driver's Code
int main()
{
    int arr[] = { 15, -2, 2, -8, 1, 7, 10, 23 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    cout << "Length of the longest 0 sum subarray is "
         << maxLen(arr, N);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// CPP program to find length of the
// longest increasing subsequence
// whose adjacent element differ by 1
#include <bits/stdc++.h>
using namespace std;
 
// function that returns the length of the
// longest increasing subsequence
// whose adjacent element differ by 1
void longestSubsequence(int a[], int n)
{
    // stores the index of elements
    unordered_map<int, int> mp;
 
    // stores the length of the longest
    // subsequence that ends with a[i]
    int dp[n];
    memset(dp, 0, sizeof(dp));
 
    int maximum = INT_MIN;
 
    // iterate for all element
    int index = -1;
    for (int i = 0; i < n; i++) {
 
        // if a[i]-1 is present before i-th index
        if (mp.find(a[i] - 1) != mp.end()) {
 
            // last index of a[i]-1
            int lastIndex = mp[a[i] - 1] - 1;
 
            // relation
            dp[i] = 1 + dp[lastIndex];
        }
        else
            dp[i] = 1;
 
        // stores the index as 1-index as we need to
        // check for occurrence, hence 0-th index
        // will not be possible to check
        mp[a[i]] = i + 1;
 
        // stores the longest length
        if (maximum < dp[i]) {
            maximum = dp[i];
            index = i;
        }
    }
 
    // We know last element of sequence is
    // a[index]. We also know that length
    // of subsequence is "maximum". So We
    // print these many consecutive elements
    // starting from "a[index] - maximum + 1"
    // to a[index].
    for (int curr = a[index] - maximum + 1;
         curr <= a[index]; curr++)
        cout << curr << " ";
}
 
// Driver Code
int main()
{
    int a[] = { 3, 10, 3, 11, 4, 5, 6, 7, 8, 12 };
    int n = sizeof(a) / sizeof(a[0]);
    longestSubsequence(a, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// CPP program to find length of the
// longest increasing subsequence
// whose adjacent element differ by 1
#include <bits/stdc++.h>
using namespace std;
 
// function that returns the length of the
// longest increasing subsequence
// whose adjacent element differ by 1
int longestSubsequence(int a[], int n)
{
    // stores the index of elements
    unordered_map<int, int> mp;
 
    // stores the length of the longest
    // subsequence that ends with a[i]
    int dp[n];
    memset(dp, 0, sizeof(dp));
 
    int maximum = INT_MIN;
 
    // iterate for all element
    for (int i = 0; i < n; i++) {
 
        // if a[i]-1 is present before i-th index
        if (mp.find(a[i] - 1) != mp.end()) {
 
            // last index of a[i]-1
            int lastIndex = mp[a[i] - 1] - 1;
 
            // relation
            dp[i] = 1 + dp[lastIndex];
        }
        else
            dp[i] = 1;
 
        // stores the index as 1-index as we need to
        // check for occurrence, hence 0-th index
        // will not be possible to check
        mp[a[i]] = i + 1;
 
        // stores the longest length
        maximum = max(maximum, dp[i]);
    }
 
    return maximum;
}
 
// Driver Code
int main()
{
    int a[] = { 3, 10, 3, 11, 4, 5, 6, 7, 8, 12 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << longestSubsequence(a, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ implementation to find longest subsequence
// such that difference between adjacents is one
#include <bits/stdc++.h>
using namespace std;
   
// function to find longest subsequence such
// that difference between adjacents is one
int longLenSub(int arr[], int n)
{
    // hash table to map the array element with the
    // length of the longest subsequence of which
    // it is a part of and is the last element of
    // that subsequence
    unordered_map<int, int> um;
      
    // to store the longest length subsequence
    int longLen = 0;
      
    // traverse the array elements
    for (int i=0; i<n; i++)
    {
        // initialize current length
        // for element arr[i] as 0
        int len = 0;
          
        // if 'arr[i]-1' is in 'um' and its length 
        // of subsequence is greater than 'len'
        if (um.find(arr[i]-1) != um.end() &&
            len < um[arr[i]-1])
            len = um[arr[i]-1];
          
        // if 'arr[i]+1' is in 'um' and its length 
        // of subsequence is greater than 'len'       
        if (um.find(arr[i]+1) != um.end() &&
            len < um[arr[i]+1])
            len = um[arr[i]+1];   
          
        // update arr[i] subsequence length in 'um'   
        um[arr[i]] = len + 1;
          
        // update longest length
        if (longLen < um[arr[i]])   
            longLen = um[arr[i]];
    }
       
    // required longest length subsequence
    return longLen;       
}
   
// Driver program to test above
int main()
{
    int arr[] = {1, 2, 3, 4, 5, 3, 2};
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Longest length subsequence = "
         << longLenSub(arr, n);
    return 0;
}  

Time complexity: linear
Space complexity: linear
// C++ implementation of longest continuous increasing
// subsequence
#include <bits/stdc++.h>
using namespace std;
 
// Function for LIS
int findLIS(int A[], int n)
{
    unordered_map<int, int> hash;
 
    // Initialize result
    int LIS_size = 1;
    int LIS_index = 0;
 
    hash[A[0]] = 1;
 
    // iterate through array and find
    // end index of LIS and its Size
    for (int i = 1; i < n; i++) {
        hash[A[i]] = hash[A[i] - 1] + 1;
        if (LIS_size < hash[A[i]]) {
            LIS_size = hash[A[i]];
            LIS_index = A[i];
        }
    }
 
    // print LIS size
    cout << "LIS_size = " << LIS_size << "\n";
 
    // print LIS after setting start element
    cout << "LIS : ";
    int start = LIS_index - LIS_size + 1;
    while (start <= LIS_index) {
        cout << start << " ";
        start++;
    }
}
 
// driver
int main()
{
    int A[] = { 2, 5, 3, 7, 4, 8, 5, 13, 6 };
    int n = sizeof(A) / sizeof(A[0]);
    findLIS(A, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ implementation to count subsets having
// even numbers only and all are distinct
#include <bits/stdc++.h>
using namespace std;
 
// function to count the
// required subsets
int countSubsets(int arr[], int n)
{
    unordered_set<int> us;
    int even_count = 0;
         
    // inserting even numbers in the set 'us'
    // single copy of each number is retained
    for (int i=0; i<n; i++)
        if (arr[i] % 2 == 0)
            us.insert(arr[i]);
      
    unordered_set<int>:: iterator itr;
     
    // distinct even numbers
    even_count = us.size();
     
    // total count of required subsets
    return (pow(2, even_count) - 1);
}
 
// Driver program to test above
int main()
{
    int arr[] = {4, 2, 1, 9, 2, 6, 5, 3};
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Number of subsets = "
         << countSubsets(arr, n);
    return 0;    
} 

Time complexity: linear
Space complexity: linear
// C++ program to count distinct
// elements in every window of size K
  
#include <bits/stdc++.h>
using namespace std;
  
// Counts distinct elements in window of size K
int countWindowDistinct(int win[], int K)
{
    int dist_count = 0;
  
    // Traverse the window
    for (int i = 0; i < K; i++) {
        // Check if element arr[i] exists in arr[0..i-1]
        int j;
        for (j = 0; j < i; j++)
            if (win[i] == win[j])
                break;
        if (j == i)
            dist_count++;
    }
    return dist_count;
}
  
// Counts distinct elements in all windows of size k
void countDistinct(int arr[], int N, int K)
{
    // Traverse through every window
    for (int i = 0; i <= N - K; i++)
        cout << countWindowDistinct(arr + i, K) << endl;
}
  
// Driver's code
int main()
{
    int arr[] = {1, 2, 1, 3, 4, 2, 3}, K = 4;
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Function call
    countDistinct(arr, N, K);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program for the above approach
  
#include <iostream>
#include <unordered_map>
using namespace std;
  
void countDistinct(int arr[], int K, int N)
{
    // Creates an empty hashmap hm
    unordered_map<int, int> hm;
  
    // initialize distinct element count for current window
    int dist_count = 0;
  
    // Traverse the first window and store count
    // of every element in hash map
    for (int i = 0; i < K; i++) {
        if (hm[arr[i]] == 0) {
            dist_count++;
        }
        
        hm[arr[i]] += 1;
    }
  
    // Print count of first window
    cout << dist_count << endl;
  
    // Traverse through the remaining array
    for (int i = K; i < N; i++) {
        // Remove first element of previous window
        // If there was only one occurrence, then reduce distinct count.
        if (hm[arr[i - K]] == 1) {
            dist_count--;
        }
        // reduce count of the removed element
        hm[arr[i - K]] -= 1;
  
        // Add new element of current window
        // If this element appears first time,
        // increment distinct element count
  
        if (hm[arr[i]] == 0) {
            dist_count++;
        }
        hm[arr[i]] += 1;
  
        // Print count of current window
        cout << dist_count << endl;
    }
}
  
// Driver's code
int main()
{
    int arr[] = {1, 2, 1, 3, 4, 2, 3};
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 4;
    
      // Function call
    countDistinct(arr, K, N);
  
    return 0;
}
// This solution is contributed by Aditya Goel

Time complexity: linear
Space complexity: linear
// C++ program to find the maximum
// possible sum of a window in one
// array such that elements in same
// window of other array are unique.
#include <bits/stdc++.h>
using namespace std;
 
// Function to return maximum sum of window
// in B[] according to given constraints.
int returnMaxSum(int A[], int B[], int n)
{
    // Map is used to store elements
    // and their counts.
    unordered_set<int> mp;
 
    int result = 0; // Initialize result
 
    // calculating the maximum possible
    // sum for each subarray containing
    // unique elements.
    int curr_sum = 0, curr_begin = 0;
    for (int i = 0; i < n; ++i) {
 
        // Remove all duplicate
        // instances of A[i] in
        // current window.
        while (mp.find(A[i]) != mp.end()) {
            mp.erase(A[curr_begin]);
            curr_sum -= B[curr_begin];
            curr_begin++;
        }
 
        // Add current instance of A[i]
        // to map and to current sum.
        mp.insert(A[i]);
        curr_sum += B[i];
 
        // Update result if current
        // sum is more.
        result = max(result, curr_sum);
    }
 
    return result;
}
 
// Driver code
int main()
{
    int A[] = { 0, 1, 2, 3, 0, 1, 4 };
    int B[] = { 9, 8, 1, 2, 3, 4, 5 };
    int n = sizeof(A)/sizeof(A[0]);
    cout << returnMaxSum(A, B, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find if
// there is a zero sum subarray
 
#include <bits/stdc++.h>
using namespace std;
 
bool subArrayExists(int arr[], int N)
{
    unordered_set<int> sumSet;
 
    // Traverse through array
    // and store prefix sums
    int sum = 0;
    for (int i = 0; i < N; i++) {
        sum += arr[i];
 
        // If prefix sum is 0 or
        // it is already present
        if (sum == 0 || sumSet.find(sum) != sumSet.end())
            return true;
 
        sumSet.insert(sum);
    }
    return false;
}
 
// Driver's code
int main()
{
    int arr[] = {-3, 2, 3, 1, 6};
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    if (subArrayExists(arr, N))
        cout << "Found a subarray with 0 sum";
    else
        cout << "No Such Sub Array Exists!";
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to print all subarrays
// in the array which has sum 0
#include <bits/stdc++.h>
using namespace std;
 
vector<pair<int, int> > findSubArrays(int arr[], int n)
{
 
    // Array to store all the start and end
    // indices of subarrays with 0 sum
    vector<pair<int, int> > out;
    for (int i = 0; i < n; i++) {
        int prefix = 0;
        for (int j = i; j < n; j++) {
            prefix += arr[j];
            if (prefix == 0)
                out.push_back({ i, j });
        }
    }
 
    return out;
}
 
// Function to print all subarrays with 0 sum
void print(vector<pair<int, int> > out)
{
    for (auto it = out.begin(); it != out.end(); it++)
        cout << "Subarray found from Index " << it->first
             << " to " << it->second << endl;
}
 
// Driver code
int main()
{
 
    // Given array
    int arr[] = { 6, 3, -1, -3, 4, -2, 2, 4, 6, -12, -7 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    vector<pair<int, int> > out = findSubArrays(arr, n);
 
    // if we didn’t find any subarray with 0 sum,
    // then subarray doesn’t exists
    if (out.size() == 0) {
        cout << "No subarray exists";
    }
    else {
        print(out);
    }
    return 0;
}
 
// This article is contributed by Arpit Jain

Time complexity: quadratic
Space complexity: constant
// C++ program to print all subarrays
// in the array which has sum 0
#include <bits/stdc++.h>
using namespace std;
  
// Function to print all subarrays in the array which
// has sum 0
vector< pair<int, int> > findSubArrays(int arr[], int n)
{
    // create an empty map
    unordered_map<int, vector<int> > map;
  
    // create an empty vector of pairs to store
    // subarray starting and ending index
    vector <pair<int, int>> out;
  
    // Maintains sum of elements so far
    int sum = 0;
  
    for (int i = 0; i < n; i++)
    {
        // add current element to sum
        sum += arr[i];
  
        // if sum is 0, we found a subarray starting
        // from index 0 and ending at index i
        if (sum == 0)
            out.push_back(make_pair(0, i));
  
        // If sum already exists in the map there exists
        // at-least one subarray ending at index i with
        // 0 sum
        if (map.find(sum) != map.end())
        {
            // map[sum] stores starting index of all subarrays
            vector<int> vc = map[sum];
            for (auto it = vc.begin(); it != vc.end(); it++)
                out.push_back(make_pair(*it + 1, i));
        }
  
        // Important - no else
        map[sum].push_back(i);
    }
  
    // return output vector
    return out;
}
  
// Utility function to print all subarrays with sum 0
void print(vector<pair<int, int>> out)
{
    for (auto it = out.begin(); it != out.end(); it++)
        cout << "Subarray found from Index " <<
            it->first << " to " << it->second << endl;
}
  
// Driver code
int main()
{
    int arr[] = {6, 3, -1, -3, 4, -2, 2, 4, 6, -12, -7};
    int n = sizeof(arr)/sizeof(arr[0]);
  
    vector<pair<int, int> > out = findSubArrays(arr, n);
  
    // if we didn’t find any subarray with 0 sum,
    // then subarray doesn’t exists
    if (out.size() == 0)
        cout << "No subarray exists";
    else
        print(out);
  
    return 0;
}

Time complexity: linear
Space complexity: linear
/* A simple program to print subarray
with sum as given sum */
#include <bits/stdc++.h>
using namespace std;
 
/* Returns true if the there is a subarray
of arr[] with sum equal to 'sum' otherwise
returns false. Also, prints the result */
int subArraySum(int arr[], int n, int sum)
{
    int curr_sum, i, j;
 
    // Pick a starting point
    for (i = 0; i < n; i++) {
        curr_sum = 0;
 
        // try all subarrays starting with 'i'
        for (j = i; j < n; j++) {
            curr_sum = curr_sum + arr[j];
 
            if (curr_sum == sum) {
                cout << "Sum found between indexes " << i
                     << " and " << j;
                return 1;
            }
        }
    }
 
    cout << "No subarray found";
    return 0;
}
 
// Driver Code
int main()
{
    int arr[] = { 15, 2, 4, 8, 9, 5, 10, 23 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int sum = 23;
 
    // Function call
    subArraySum(arr, n, sum);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program to print subarray with sum as given sum
#include <bits/stdc++.h>
using namespace std;
 
// Function to print subarray with sum as given sum
void subArraySum(int arr[], int n, int sum)
{
    // create an empty map
    unordered_map<int, int> map;
 
    // Maintains sum of elements so far
    int curr_sum = 0;
 
    for (int i = 0; i < n; i++) {
        // add current element to curr_sum
        curr_sum = curr_sum + arr[i];
 
        // if curr_sum is equal to target sum
        // we found a subarray starting from index 0
        // and ending at index i
        if (curr_sum == sum) {
            cout << "Sum found between indexes " << 0
                 << " to " << i << endl;
            return;
        }
 
        // If curr_sum - sum already exists in map
        // we have found a subarray with target sum
        if (map.find(curr_sum - sum) != map.end()) {
            cout << "Sum found between indexes "
                 << map[curr_sum - sum] + 1 << " to " << i
                 << endl;
            return;
        }
 
        map[curr_sum] = i;
    }
 
    // If we reach here, then no subarray exists
    cout << "No subarray with given sum exists";
}
 
// Driver code
int main()
{
    int arr[] = { 10, 2, -2, -20, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int sum = -10;
 
    // Function call
    subArraySum(arr, n, sum);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// CPP program to find minimum number
// of insertions to make a string
// palindrome
#include <bits/stdc++.h>
using namespace std;
 
// Function will return number of
// characters to be added
int minInsertion(string str)
{
    // To store string length
    int n = str.length();
 
    // To store number of characters
    // occurring odd number of times
    int res = 0;
 
    // To store count of each
    // character
    int count[26] = { 0 };
 
    // To store occurrence of each
    // character
    for (int i = 0; i < n; i++)
        count[str[i] - 'a']++;
 
    // To count characters with odd
    // occurrence
    for (int i = 0; i < 26; i++)
        if (count[i] % 2 == 1)
            res++;
 
    // As one character can be odd return
    // res - 1 but if string is already
    // palindrome return 0
    return (res == 0) ? 0 : res - 1;
}
 
// Driver program
int main()
{
    string str = "geeksforgeeks";
    cout << minInsertion(str);
 
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ implementation to find maximum length
// subsequence with difference between adjacent
// elements as either 0 or 1
#include <bits/stdc++.h>
using namespace std;
  
// function to find maximum length subsequence
// with difference between adjacent elements as
// either 0 or 1
int maxLenSub(int arr[], int n)
{
    // hash table to map the array element with the
    // length of the longest subsequence of which
    // it is a part of and is the last element of
    // that subsequence
    unordered_map<int, int> um;
     
    // to store the maximum length subsequence
    int maxLen = 0;
     
    // traverse the array elements
    for (int i=0; i<n; i++)
    {
        // initialize current length
        // for element arr[i] as 0
        int len = 0;
         
        // if 'arr[i]-1' is in 'um' and its length of
        // subsequence is greater than 'len'
        if (um.find(arr[i]-1) != um.end() && len < um[arr[i]-1])
            len = um[arr[i]-1];
         
        // if 'arr[i]' is in 'um' and its length of
        // subsequence is greater than 'len'   
        if (um.find(arr[i]) != um.end() && len < um[arr[i]])
            len = um[arr[i]];
             
        // if 'arr[i]+1' is in 'um' and its length of
        // subsequence is greater than 'len'       
        if (um.find(arr[i]+1) != um.end() && len < um[arr[i]+1])
            len = um[arr[i]+1];   
         
        // update arr[i] subsequence length in 'um'   
        um[arr[i]] = len + 1;
         
        // update maximum length
        if (maxLen < um[arr[i]])   
            maxLen = um[arr[i]];
    }
      
    // required maximum length subsequence
    return maxLen;       
}
  
// Driver program to test above
int main()
{
    int arr[] = {2, 5, 6, 3, 7, 6, 5, 8};
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Maximum length subsequence = "
         << maxLenSub(arr, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find maximum difference
// between frequency of any two element
// such that element with greater frequency
// is also greater in value.
#include<bits/stdc++.h>
using namespace std;
 
// Return the maximum difference between
// frequencies of any two elements such that
// element with greater frequency is also
// greater in value.
int maxdiff(int arr[], int n)
{
    unordered_map<int, int> freq;
 
    // Finding the frequency of each element.
    for (int i = 0; i < n; i++)
        freq[arr[i]]++;
 
    int ans = 0;
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<n; j++)
        {
            // finding difference such that element
            // having greater frequency is also
            // greater in value.
            if (freq[arr[i]] > freq[arr[j]] &&
                arr[i] > arr[j] )
                ans = max(ans, freq[arr[i]]-freq[arr[j]]);
            else if (freq[arr[i]] < freq[arr[j]] &&
                      arr[i] < arr[j] )
                ans = max(ans, freq[arr[j]]-freq[arr[i]]);
        }
    }
 
    return ans;
}
 
// Driven Program
int main()
{
    int arr[] = { 3, 1, 3, 2, 3, 2 };
    int n = sizeof(arr)/sizeof(arr[0]);
 
    cout << maxdiff(arr, n) << endl;
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// Efficient C++ program to find maximum
// difference between frequency of any two
// elements such that element with greater
// frequency is also greater in value.
#include<bits/stdc++.h>
using namespace std;
 
// Return the maximum difference between
// frequencies of any two elements such that
// element with greater frequency is also
// greater in value.
int maxdiff(int arr[], int n)
{
    unordered_map<int, int> freq;
 
    int dist[n];
 
    // Finding the frequency of each element.
    int j = 0;
    for (int i = 0; i < n; i++)
    {
        if (freq.find(arr[i]) == freq.end())
            dist[j++] = arr[i];
 
        freq[arr[i]]++;
    }
 
    // Sorting the distinct element
    sort(dist, dist + j);
 
    int min_freq = n+1;
 
    // Iterate through all sorted distinct elements.
    // For each distinct element, maintaining the
    // element with minimum frequency than that
    // element and also finding the maximum
    // frequency difference
    int ans = 0;
    for (int i=0; i<j; i++)
    {
        int cur_freq = freq[dist[i]];
        ans = max(ans, cur_freq - min_freq);
        min_freq = min(min_freq, cur_freq);
    }
 
    return ans;
}
 
// Driven Program
int main()
{
    int arr[] = { 3, 1, 3, 2, 3, 2 };
    int n = sizeof(arr)/sizeof(arr[0]);
 
    cout << maxdiff(arr, n) << endl;
    return 0;
}

Time complexity: nlogn
Space complexity: linear