Banana-Leopard/TASTY huggingface dataset:
# Simple Python 3 program to find
# pairs of positive and negative
# values present in an array.
 
# Print pair with negative and
# positive value
 
 
def printPairs(arr, n):
    v = []
 
    # For each element of array.
    for i in range(n):
 
        # Try to find the negative value
        # of arr[i] from i + 1 to n
        for j in range(i + 1, n):
 
            # If absolute values are
            # equal print pair.
            if (abs(arr[i]) == abs(arr[j])):
                v.append(abs(arr[i]))
 
    # If size of vector is 0, therefore
    # there is no element with positive
    # negative value, print "0"
    if (len(v) == 0):
        return
 
    # Print the pair with negative
    # positive value.
    for i in range(len(v)):
        print(-v[i], "", v[i], end=" ")
 
 
# Driver Code
if __name__ == "__main__":
    arr = [4, 8, 9, -4, 1, -1, -8, -9]
    n = len(arr)
 
    # Function call
    printPairs(arr, n)
 
# This code is contributed
# by ChitraNayal

Time complexity: quadratic
Space complexity: linear
# Python3 program to find pairs of
# positive and negative values present in
# an array.
# Print pair with negative and
# positive value
 
 
def printPairs(arr, n):
 
    s = set()
    ret = []
 
    # For each element of array.
    for i in arr:
        if abs(i) in s:
            ret.append(abs(i))
        else:
            s.add(abs(i))
 
    ret.sort()
 
    for i in range(0, len(ret)):
        print(-ret[i], "", ret[i], end=" ")
 
 
# Driver Code
if __name__ == "__main__":
    arr = [4, 8, 9, -4, 1, -1, -8, -9]
    n = len(arr)
 
    # Function call
    printPairs(arr, n)
# This code is contributed by RohitOberoi

Time complexity: linear
Space complexity: linear
# Python3 program to find pairs of
# positive and negative values present in
# an array
 
 
def printPairs(arr, n):
 
    hs = set()
    ans = []
    for i in range(n):
        if (arr[i] * -1) in hs:
 
            if (arr[i] < 0):
                print(arr[i], end=" ")
                print((arr[i] * -1), end=" ")
 
            else:
                print((arr[i] * -1), end=" ")
                print(arr[i], end=" ")
 
        hs.add(arr[i])
 
    return
 
 
# Driver code
arr = [4, 8, 9, -4, 1, -1, -8, -9]
n = len(arr)
 
# Function call
printPairs(arr, n)
 
# This code is contributed by shinjanpatra.

Time complexity: linear
Space complexity: linear
# Python3 program to count
# divisible pairs.
 
def countDivisibles(arr, n) :
 
    res = 0
 
    # Iterate through all pairs
    for i in range(0, n) :
        for j in range(i+1, n) :
             
            # Increment count if one divides
            # other
            if (arr[i] % arr[j] == 0 or
            arr[j] % arr[i] == 0) :
                res+=1
 
    return res
 
# Driver code
if __name__=='__main__':
    a = [1, 2, 3, 9]
    n = len(a)
    print(countDivisibles(a, n) )
 
# this code is contributed by
# Smitha Dinesh Semwal   

Time complexity: quadratic
Space complexity: constant
# Python program to count divisible pairs.
import math
 
# Function to return the total count of pairs such
# that arr[i]%arr[j]==0
def total_count(arr, N):
    count = 0
 
    # Storing the occurrence of every element in array
    # in dictionary
    freq = {}
    for i in range(0, N):
        if arr[i] not in freq:
            freq[arr[i]] = 1
        else:
            freq[arr[i]] += 1
 
    # Iterating through every element and finding all the
    # divisors of that element and then checking how many
    # of them are present in array arr[]
    for i in range(0, N):
        for j in range(1, int(math.sqrt(arr[i]))+1):
            if arr[i] % j == 0:
                if arr[i] == j*j:
                   
                    # If divisors are equal, then take only
                    # one as it will be perfect square root
                    # of arr[i]
                    count += freq[j]
                else:
                    # Else take both j and arr[i]/j as both
                    # will be divisors
                    count += freq[j]+freq[arr[i]/j]
                     
        # As all the elements is divisible by itself and
        # is counted in freq[] so reducing its count
        count = count-1
         
    # returning final count
    return count
 
arr = [1, 2, 3, 9]
N = len(arr)
 
print(total_count(arr, N))
 
# This code is contributed by lokesh (lokeshmvs21).

Time complexity: quadratic
Space complexity: constant
# Python3 implementation to find the
# longest subarray with sum divisible by k
 
# Function to find the longest
# subarray with sum divisible by k
 
 
def longestSubarrWthSumDivByK(arr, n, k):
 
    # unordered map 'um' implemented
    # as hash table
    um = {}
 
    # 'mod_arr[i]' stores (sum[0..i] % k)
    mod_arr = [0 for i in range(n)]
    max_len = 0
    curr_sum = 0
 
    # Traverse arr[] and build up
    # the array 'mod_arr[]'
    for i in range(n):
        curr_sum += arr[i]
 
        # As the sum can be negative,
        # taking modulo twice
        mod_arr[i] = ((curr_sum % k) + k) % k
 
        # If true then sum(0..i) is
        # divisible by k
        if (mod_arr[i] == 0):
 
            # Update 'max_len'
            max_len = i + 1
 
        # If value 'mod_arr[i]' not present in
        # 'um' then store it in 'um' with index
        # of its first occurrence
        elif (mod_arr[i] not in um):
            um[mod_arr[i]] = i
 
        else:
              # If true, then update 'max_len'
            if (max_len < (i - um[mod_arr[i]])):
                max_len = i - um[mod_arr[i]]
 
    # Return the required length of longest subarray
    # with sum divisible by 'k'
    return max_len
 
 
# Driver Code
if __name__ == '__main__':
 
    arr = [2, 7, 6, 1, 4, 5]
    n = len(arr)
    k = 3
 
    print("Length =",
          longestSubarrWthSumDivByK(arr, n, k))
 
# This code is contributed by Surendra_Gangwar, updated by Kshitij Dwivedi

Time complexity: linear
Space complexity: linear
# function to find the longest subarray
#  with sum divisible by k
 
 
def longestSubarrWthSumDivByK(arr, n, k):
 
    # unordered map 'um' implemented as
    # hash table
    um = {}
 
    max_len = 0
    curr_sum = 0
 
    for i in range(n):
 
        curr_sum += arr[i]
        mod = ((curr_sum % k) + k) % k
        # if true then sum(0..i) is divisible by k
 
        if mod == 0:
            # update 'max_len'
            max_len = i + 1
 
        # if value 'mod_arr[i]' not present in 'um'
        # then store it in 'um' with index of its
        # first occurrence
        elif mod in um.keys():
            if max_len < (i - um[mod]):
                max_len = i - um[mod]
 
        else:
            um[mod] = i
 
    # return the required length of longest subarray with
    # sum divisible by 'k'
    return max_len
 
 
arr = [2, 7, 6, 1, 4, 5]
n = len(arr)
k = 3
print("Length =", longestSubarrWthSumDivByK(arr, n, k))
 
# This code is contributed by amreshkumar3, and updated by Kshitij Dwivedi

Time complexity: linear
Space complexity: linear
# Python3 Program to find the subarray with
# no pair sum divisible by K
 
# function to find the subarray with
# no pair sum divisible by k
def subarrayDivisibleByK(arr, n, k) :
     
    # hash table to store the remainders
    # obtained on dividing by K
    mp = [0] * 1000
 
    # s : starting index of the
    # current subarray, e : ending
    # index of the current subarray, maxs :
    # starting index of the maximum
    # size subarray so far, maxe : ending
    # index of the maximum size subarray
    # so far
    s = 0; e = 0; maxs = 0; maxe = 0;
 
    # insert the first element in the set
    mp[arr[0] % k] = mp[arr[0] % k] + 1;
 
    for i in range(1, n):
        mod = arr[i] % k
 
        # Removing starting elements of current
        # subarray while there is an element in
        # set which makes a pair with mod[i] such
        # that the pair sum is divisible.
        while (mp[k - mod] != 0 or (mod == 0
                            and mp[mod] != 0)) :
            mp[arr[s] % k] = mp[arr[s] % k] - 1
            s = s + 1
 
        # include the current element in
        # the current subarray the ending
        # index of the current subarray
        # increments by one
        mp[mod] = mp[mod] + 1
        e = e + 1
 
        # compare the size of the current
        # subarray with the maximum size so
        # far
        if ((e - s) > (maxe - maxs)) :
            maxe = e
            maxs = s
 
    print ("The maximum size is {} and the "
                   " subarray is as follows"
                   .format((maxe - maxs + 1)))
 
    for i in range(maxs, maxe + 1) :
        print ("{} ".format(arr[i]), end="")
 
# Driver Code
k = 3
arr = [5, 10, 15, 20, 25]
n = len(arr)
subarrayDivisibleByK(arr, n, k)
 
# This code is contributed by
# Manish Shaw (manishshaw1)

Time complexity: nlogn
Space complexity: linear
# Python3 program to find special numbers
# in an array
import math as mt
 
# Function to find special numbers
def divisibilityCheck(arr, n):
 
    # Storing all array elements in a hash
    # and finding maximum element in array
    s = dict()
    max_ele = -10**9
    for i in range(n):
        s[arr[i]] = 1
 
        # finding maximum element of array
        max_ele = max(max_ele, arr[i])
     
    # traversing array element and storing
    # the array multiples that are present
    # in s in res.
    res = dict()
    for i in range(n):
 
        # Check for non-zero values only
        if (arr[i] != 0):
 
            # checking the factor of current element
            for j in range(arr[i] * 2,
                           max_ele + 1, arr[i]):
                 
                # if factor is already part of
                # array element then store it
                if (j in s.keys()):
                    res[j] = 1
             
    # displaying elements that are divisible
    # by at least one other in array
    for x in res:
        print(x, end = " ")
 
# Driver code
arr = [ 2, 3, 8, 6, 9, 10]
n = len(arr)
divisibilityCheck(arr, n)
 
# This code is contributed by
# Mohit Kumar 29

Time complexity: quadratic
Space complexity: linear
# Python3 program to find
# three element from different
# three arrays such that
# a + b + c is equal to
# given sum
 
# Function to check if there
# is an element from each
# array such that sum of the
# three elements is equal to
# given sum.
def findTriplet(a1, a2, a3,
                n1, n2, n3, sum):
 
    for i in range(0 , n1):
        for j in range(0 , n2):
            for k in range(0 , n3):
                if (a1[i] + a2[j] +
                    a3[k] == sum):
                    return True
 
    return False
 
# Driver Code
a1 = [ 1 , 2 , 3 , 4 , 5 ]
a2 = [ 2 , 3 , 6 , 1 , 2 ]
a3 = [ 3 , 2 , 4 , 5 , 6 ]
sum = 9
n1 = len(a1)
n2 = len(a2)
n3 = len(a3)
print("Yes") if findTriplet(a1, a2, a3,
                            n1, n2, n3,
                            sum) else print("No")
 
# This code is contributed
# by Smitha

Time complexity: cubic
Space complexity: constant
# Python3 program to find three element
# from different three arrays such
# that a + b + c is equal to
# given sum
 
# Function to check if there is
# an element from each array such
# that sum of the three elements is
# equal to given sum.
def findTriplet(a1, a2, a3,
                n1, n2, n3, sum):
 
    # Store elements of first
    # array in hash
    s = set()
 
    # sum last two arrays element
    # one by one
    for i in range(n1):
        s.add(a1[i])
 
    for i in range(n2):
        for j in range(n3):
 
            # Consider current pair and
            # find if there is an element
            # in a1[] such that these three
            # form a required triplet
            if sum - a2[i] - a3[j] in s:
                return True
    return False
 
# Driver code
a1 = [1, 2, 3, 4, 5]
a2 = [2, 3, 6, 1, 2]
a3 = [3, 24, 5, 6]
n1 = len(a1)
n2 = len(a2)
n3 = len(a3)
sum = 9
if findTriplet(a1, a2, a3,
               n1, n2, n3, sum) == True:
    print("Yes")
else:
    print("No")
 
# This code is contributed by Shrikant13

Time complexity: quadratic
Space complexity: linear
# Python program for the above approach
 
# returns the length
def maxLen(arr):
     
    # initialize result
    max_len = 0
 
    # pick a starting point
    for i in range(len(arr)):
         
        # initialize sum for every starting point
        curr_sum = 0
         
        # try all subarrays starting with 'i'
        for j in range(i, len(arr)):
         
            curr_sum += arr[j]
 
            # if curr_sum becomes 0, then update max_len
            if curr_sum == 0:
                max_len = max(max_len, j-i + 1)
 
    return max_len
 
# Driver's code
if __name__ == "__main__":
# test array
    arr = [15, -2, 2, -8, 1, 7, 10, 13]
     
    # Function call
    print ("Length of the longest 0 sum subarray is % d" % maxLen(arr))

Time complexity: quadratic
Space complexity: constant
# Python program for the above approach
 
# Returns the maximum length
 
 
def maxLen(arr):
 
    # NOTE: Dictionary in python is
    # implemented as Hash Maps
    # Create an empty hash map (dictionary)
    hash_map = {}
 
    # Initialize result
    max_len = 0
 
    # Initialize sum of elements
    curr_sum = 0
 
    # Traverse through the given array
    for i in range(len(arr)):
 
        # Add the current element to the sum
        curr_sum += arr[i]
 
        if curr_sum == 0:
            max_len = i + 1
 
        # NOTE: 'in' operation in dictionary
        # to search key takes O(1). Look if
        # current sum is seen before
        if curr_sum in hash_map:
            max_len = max(max_len, i - hash_map[curr_sum])
        else:
 
            # else put this sum in dictionary
            hash_map[curr_sum] = i
 
    return max_len
 
 
# Driver's code
if __name__ == "__main__":
 
    # test array
    arr = [15, -2, 2, -8, 1, 7, 10, 13]
 
    # Function call
    print("Length of the longest 0 sum subarray is % d" % maxLen(arr))

Time complexity: linear
Space complexity: linear
# Python 3 program to find length of
# the longest increasing subsequence
# whose adjacent element differ by 1
import sys
 
# function that returns the length
# of the longest increasing subsequence
# whose adjacent element differ by 1
def longestSubsequence(a, n):
     
    # stores the index of elements
    mp = {i:0 for i in range(13)}
 
    # stores the length of the longest
    # subsequence that ends with a[i]
    dp = [0 for i in range(n)]
 
    maximum = -sys.maxsize - 1
 
    # iterate for all element
    index = -1
    for i in range(n):
         
        # if a[i]-1 is present before
        # i-th index
        if ((a[i] - 1 ) in mp):
             
            # last index of a[i]-1
            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 curr in range(a[index] - maximum + 1,
                      a[index] + 1, 1):
        print(curr, end = " ")
 
# Driver Code
if __name__ == '__main__':
    a = [3, 10, 3, 11, 4, 5,
                6, 7, 8, 12]
    n = len(a)
    longestSubsequence(a, n)
 
# This code is contributed by
# Surendra_Gangwar

Time complexity: linear
Space complexity: linear
# python program to find length of the
# longest increasing subsequence
# whose adjacent element differ by 1
 
from collections import defaultdict
import sys
 
# function that returns the length of the
# longest increasing subsequence
# whose adjacent element differ by 1
 
def longestSubsequence(a, n):
    mp = defaultdict(lambda:0)
 
    # stores the length of the longest
    # subsequence that ends with a[i]
    dp = [0 for i in range(n)]
    maximum = -sys.maxsize
 
    # iterate for all element
    for i in range(n):
 
        # if a[i]-1 is present before i-th index
        if a[i] - 1 in mp:
 
            # last index of a[i]-1
            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
a = [3, 10, 3, 11, 4, 5, 6, 7, 8, 12]
n = len(a)
print(longestSubsequence(a, n))
 
# This code is contributed by Shrikant13

Time complexity: linear
Space complexity: linear
# Python3 implementation to find longest
# subsequence such that difference between
# adjacents is one
from collections import defaultdict
 
# function to find longest subsequence such
# that difference between adjacents is one
def longLenSub(arr, 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
    um = defaultdict(lambda:0)
    longLen = 0
    for i in range(n):
 
        # / initialize current length
        # for element arr[i] as 0
        len1 = 0
 
        # if 'arr[i]-1' is in 'um' and its length
        # of subsequence is greater than 'len'
        if (arr[i - 1] in um and
            len1 < um[arr[i] - 1]):
            len1 = um[arr[i] - 1]
 
        # f 'arr[i]+1' is in 'um' and its length
        # of subsequence is greater than 'len'    
        if (arr[i] + 1 in um and
            len1 < um[arr[i] + 1]):
            len1 = um[arr[i] + 1]
 
        # update arr[i] subsequence
        # length in 'um'    
        um[arr[i]] = len1 + 1
 
        # update longest length
        if longLen < um[arr[i]]:
            longLen = um[arr[i]]
 
    # required longest length
    # subsequence
    return longLen
 
# Driver code
arr = [1, 2, 3, 4, 5, 3, 2]
n = len(arr)
print("Longest length subsequence =",
                  longLenSub(arr, n))
 
# This code is contributed by Shrikant13

Time complexity: linear
Space complexity: linear
# Python3 program to find longest
# contiguous subsequence
 
# Returns length of the longest
# contiguous subsequence
 
 
def findLongestConseqSubseq(arr, n):
 
    ans = 0
    count = 0
 
    # Sort the array
    arr.sort()
 
    v = []
 
    v.append(arr[0])
 
    # Insert repeated elements only
    # once in the vector
    for i in range(1, n):
        if (arr[i] != arr[i - 1]):
            v.append(arr[i])
 
    # Find the maximum length
    # by traversing the array
    for i in range(len(v)):
 
        # Check if the current element is
        # equal to previous element +1
        if (i > 0 and v[i] == v[i - 1] + 1):
            count += 1
 
        # Reset the count
        else:
            count = 1
 
        # Update the maximum
        ans = max(ans, count)
 
    return ans
 
 
# Driver code
arr = [1, 2, 2, 3]
n = len(arr)
 
print("Length of the Longest contiguous subsequence is",
      findLongestConseqSubseq(arr, n))
 
# This code is contributed by avanitrachhadiya2155

Time complexity: nlogn
Space complexity: linear
# Python program to find longest contiguous subsequence
 
 
def findLongestConseqSubseq(arr, n):
 
    s = set()
    ans = 0
 
    # Hash all the array elements
    for ele in arr:
        s.add(ele)
 
    # check each possible sequence from the start
    # then update optimal length
    for i in range(n):
 
         # if current element is the starting
        # element of a sequence
        if (arr[i]-1) not in s:
 
            # Then check for next elements in the
            # sequence
            j = arr[i]
            while(j in s):
                j += 1
 
            # update  optimal length if this length
            # is more
            ans = max(ans, j-arr[i])
    return ans
 
 
# Driver code
if __name__ == '__main__':
    n = 7
    arr = [1, 9, 3, 10, 4, 20, 2]
    print("Length of the Longest contiguous subsequence is ",
          findLongestConseqSubseq(arr, n))
 
# Contributed by: Harshit Sidhwa

Time complexity: linear
Space complexity: linear
# Python program for the above approach
import bisect
 
 
def findLongestConseqSubseq(arr, N):
    pq = []
    for i in range(N):
 
        # adding element from
        # array to PriorityQueue
        bisect.insort(pq, arr[i])
 
    # Storing the first element
    # of the Priority Queue
    # This first element is also
    # the smallest element
    prev = pq[0]
    pq.pop(0)
 
    # Taking a counter variable with value 1
    c = 1
 
    # Storing value of max as 1
    # as there will always be
    # one element
    max = 1
    while(len(pq)):
        # check if current peek
        # element minus previous
        # element is greater than
        # 1 This is done because
        # if it's greater than 1
        # then the sequence
        # doesn't start or is broken here
        if(pq[0] - prev > 1):
            # Store the value of counter to 1
            # As new sequence may begin
            c = 1
 
            # Update the previous position with the
            # current peek And remove it
            prev = pq[0]
            pq.pop(0)
 
        # Check if the previous
        # element and peek are same
        elif(pq[0] - prev == 0):
            # Update the previous position with the
            # current peek And remove it
            prev = pq[0]
            pq.pop(0)
 
        # If the difference
        # between previous element and peek is 1
        else:
            # Update the counter
            # These are consecutive elements
            c = c + 1
            # Update the previous position
            # with the current peek And remove it
            prev = pq[0]
            pq.pop(0)
 
        # Check if current longest
        # subsequence is the greatest
        if(max < c):
            # Store the current subsequence count as
            # max
            max = c
    return max
 
 
# Driver Code
arr = [1, 9, 3, 10, 4, 20, 2]
n = 7
print("Length of the Longest consecutive subsequence is {}".format(
    findLongestConseqSubseq(arr, n)))
 
 
# This code is contributed by Pushpesh Raj

Time complexity: nlogn
Space complexity: linear
# Python3 implementation of longest
# continuous increasing subsequence
 
# Function for LIS
def findLIS(A, n):
    hash = dict()
 
    # Initialize result
    LIS_size, LIS_index = 1, 0
 
    hash[A[0]] = 1
 
    # iterate through array and find
    # end index of LIS and its Size
    for i in range(1, n):
 
        # If the desired key is not present
        # in dictionary, it will throw key error,
        # to avoid this error this is necessary
        if A[i] - 1 not in hash:
            hash[A[i] - 1] = 0
 
        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
    print("LIS_size =", LIS_size)
 
    # print LIS after setting start element
    print("LIS : ", end = "")
 
    start = LIS_index - LIS_size + 1
    while start <= LIS_index:
        print(start, end = " ")
        start += 1
 
# Driver Code
if __name__ == "__main__":
    A = [ 2, 5, 3, 7, 4, 8, 5, 13, 6 ]
    n = len(A)
    findLIS(A, n)
 
# This code is contributed by sanjeev2552

Time complexity: linear
Space complexity: linear
# python implementation to count subsets having
# even numbers only and all are distinct
 
#function to count the required subsets
def countSubSets(arr, n):
    us = set()
    even_count = 0
 
    # inserting even numbers in the set 'us'
    # single copy of each number is retained
    for i in range(n):
        if arr[i] % 2 == 0:
            us.add(arr[i])
 
    # counting distinct even numbers
    even_count = len(us)
 
    # total count of required subsets
    return pow(2, even_count)-  1
 
 
# Driver program
arr = [4, 2, 1, 9, 2, 6, 5, 3]
n = len(arr)
print("Numbers of subset=", countSubSets(arr,n))
 
# This code is contributed by Shrikant13

Time complexity: linear
Space complexity: linear