Banana-Leopard/TASTY huggingface dataset:
# Python3 Program to swap diagonal of a matrix
 
# size of square matrix
N = 3
 
# Function to swap diagonal of matrix
def swapDiagonal(matrix):
     
    for i in range(N):
         
        matrix[i][i], matrix[i][N-i-1] = \
            matrix[i][N-i-1], matrix[i][i]
 
 
# Driver Code
matrix = [[0, 1, 2],
          [3, 4, 5],
          [6, 7, 8]]
 
# swap diagonals of matrix
swapDiagonal(matrix);
 
# Displaying modified matrix
for i in range(N):   
    for j in range(N):       
        print(matrix[i][j], end = ' ')       
    print()

Time complexity: quadratic
Space complexity: constant
# Python 3 program for finding max path in matrix
# To calculate max path in matrix
 
def findMaxPath(mat):
   
    for i in range(1, N):
  
        res = -1
        for j in range(M):
  
            # When all paths are possible
            if (j > 0 and j < M - 1):
                mat[i][j] += max(mat[i - 1][j],
                                 max(mat[i - 1][j - 1],
                                     mat[i - 1][j + 1]))
  
            # When diagonal right is not possible
            else if (j > 0):
                mat[i][j] += max(mat[i - 1][j],
                                 mat[i - 1][j - 1])
  
            # When diagonal left is not possible
            else if (j < M - 1):
                mat[i][j] += max(mat[i - 1][j],
                                 mat[i - 1][j + 1])
  
            # Store max path sum
            res = max(mat[i][j], res)
    return res
 
# Driver program to check findMaxPath
N=4
M=6
mat = ([[ 10, 10, 2, 0, 20, 4 ],
        [ 1, 0, 0, 30, 2, 5 ],
        [ 0, 10, 4, 0, 2, 0 ],
        [ 1, 0, 2, 20, 0, 4 ]])
               
print(findMaxPath(mat))
 
# This code is contributed by Azkia Anam.

Time complexity: quadratic
Space complexity: constant
# Python3 program to find number of subarrays
# having product exactly equal to k.
 
# Function to find number of subarrays
# having product equal to 1.
def countOne(arr, n) :
    i = 0
     
    # To store number of ones in
    # current segment of all 1s.
    Len = 0
     
    # To store number of subarrays
    # having product equal to 1.
    ans = 0
     
    while(i < n) :
         
        # If current element is 1, then
        # find length of segment of 1s
        # starting from current element.
        if(arr[i] == 1) :
            Len = 0
            while(i < n and arr[i] == 1) :
                i += 1
                Len += 1
             
            # add number of possible
            # subarrays of 1 to result.
            ans += (Len*(Len+1)) // 2
        i += 1
     
    return ans
 
# Function to find number of subarrays having
# product exactly equal to k.
def findSubarrayCount(arr, n, k) :
 
    start, endval, p, countOnes, res = 0, 0, 1, 0, 0
 
    while (endval < n) :
     
        p = p * (arr[endval])
 
        # If product is greater than k then we need to decrease
        # it. This could be done by shifting starting point of
        # sliding window one place to right at a time and update
        # product accordingly.
        if(p > k) :
         
            while(start <= endval and p > k) :
             
                p = p // arr[start]
                start += 1
                 
        if(p == k) :
         
            # Count number of succeeding ones.
            countOnes = 0
            while endval + 1 < n and arr[endval + 1] == 1 :
              
                countOnes += 1
                endval += 1
 
            # Update result by adding both new subarray
            # and effect of succeeding ones.
            res += (countOnes + 1)
 
            # Update sliding window and result according
            # to change in sliding window. Here preceding
            # 1s have same effect on subarray as succeeding
            # 1s, so simply add.
            while(start <= endval and arr[start] == 1 and k!=1) :
             
                res += (countOnes + 1)
                start += 1
 
            # Move start to correct position to find new
            # subarray and update product accordingly.
            p = p // arr[start]
            start += 1
 
        endval += 1
 
    return res
 
arr1 = [ 2, 1, 1, 1, 3, 1, 1, 4 ]
n1 = len(arr1)
k = 1
 
if(k != 1) :
    print(findSubarrayCount(arr1, n1, k))
else :
    print(countOne(arr1, n1))
 
arr2 = [ 2, 1, 1, 1, 4, 5]
n2 = len(arr2)
k = 4
 
if(k != 1) :
    print(findSubarrayCount(arr2, n2, k))
else :
    print(countOne(arr2, n2))
 
    # This code is contributed by divyesh072019

Time complexity: linear
Space complexity: constant
# The function checks if the array elements
# are consecutive. If elements are consecutive,
# then returns true, else returns false
def areConsecutive(arr, n):
     
    # Sort the array
    arr.sort()
    # checking the adjacent elements
    for i in range (1,n):
        if(arr[i]!=arr[i-1]+1):
            return False;
             
    return True;   
     
# Driver Code
arr = [5, 4, 2, 3, 1, 6]
n = len(arr)
if(areConsecutive(arr, n) == True):
    print("Array elements are consecutive ")
else:
    print("Array elements are not consecutive ")
  
# This code is contributed by Aarti_Rathi

Time complexity: nlogn
Space complexity: constant
# Helper functions to get Minimum and
# Maximum in an array
 
# The function checks if the array elements
# are consecutive. If elements are consecutive,
# then returns true, else returns false
def areConsecutive(arr, n):
 
    if ( n < 1 ):
        return False
     
    # 1) Get the Minimum element in array */
    Min = min(arr)
     
    # 2) Get the Maximum element in array */
    Max = max(arr)
     
    # 3) Max - Min + 1 is equal to n,
    # then only check all elements */
    if (Max - Min + 1 == n):
         
        # Create a temp array to hold visited
        # flag of all elements. Note that, calloc
        # is used here so that all values are
        # initialized as false
        visited = [False for i in range(n)]
     
        for i in range(n):
             
            # If we see an element again,
            # then return false */
            if (visited[arr[i] - Min] != False):
                return False
     
            # If visited first time, then mark
            # the element as visited */
            visited[arr[i] - Min] = True
     
        # If all elements occur once,
        # then return true */
        return True
     
    return False # if (Max - Min + 1 != n)
 
# Driver Code
arr = [5, 4, 2, 3, 1, 6]
n = len(arr)
if(areConsecutive(arr, n) == True):
    print("Array elements are consecutive ")
else:
    print("Array elements are not consecutive ")
 
# This code is contributed by mohit kumar

Time complexity: linear
Space complexity: linear
# Helper functions to get minimum and
# maximum in an array
 
# The function checks if the array
# elements are consecutive. If elements
# are consecutive, then returns true,
# else returns false
def areConsecutive(arr, n):
 
    if ( n < 1 ):
        return False
 
    # 1) Get the minimum element in array
    min = getMin(arr, n)
 
    # 2) Get the maximum element in array
    max = getMax(arr, n)
 
    # 3) max - min + 1 is equal to n
    # then only check all elements
    if (max - min + 1 == n):
 
        for i in range(n):
 
            if (arr[i] < 0):
                j = -arr[i] - min
            else:
                j = arr[i] - min
 
            # if the value at index j is negative
            # then there is repetition
            if (arr[j] > 0):
                arr[j] = -arr[j]
            else:
                return False
 
        # If we do not see a negative value
        # then all elements are distinct
        return True
 
    return False     # if (max - min + 1 != n)
 
# UTILITY FUNCTIONS
def getMin(arr, n):
     
    min = arr[0]
    for i in range(1, n):
        if (arr[i] < min):
            min = arr[i]
    return min
 
def getMax(arr, n):
    max = arr[0]
    for i in range(1, n):
        if (arr[i] > max):
            max = arr[i]
    return max
 
# Driver Code
if __name__ == "__main__":
     
    arr = [1, 4, 5, 3, 2, 6]
    n = len(arr)
    if(areConsecutive(arr, n) == True):
        print(" Array elements are consecutive ")
    else:
        print(" Array elements are not consecutive ")
 
# This code is contributed by ita_c

Time complexity: linear
Space complexity: constant
# Function to Check if array
# elements are consecutive
def areConsecutive(arr, n):
    min_ele = arr.index(min(arr))
    num = 0
    for i in range(0, n):
        num ^= arr[min_ele] ^ arr[i]
        arr[min_ele] += 1
    if num == 0:
        return True
    return False
 
# Driver program to test above
# functions
if __name__ == "__main__":
    arr = [1, 4, 5, 3, 2, 6]
    n = len(arr)
    if areConsecutive(arr, n) == True:
        print(" Array elements are consecutive ", end=' ')
    else:
        print(" Array elements are not consecutive ", end=' ')
 
# This code is contributed by Aarti_Rathi

Time complexity: linear
Space complexity: constant
# Python 3 program to find whether an array
# is subset of another array
 
# Return 1 if arr2[] is a subset of
# arr1[]
 
 
def isSubset(arr1, arr2, m, n):
    i = 0
    j = 0
    for i in range(n):
        for j in range(m):
            if(arr2[i] == arr1[j]):
                break
 
        # If the above inner loop was
        # not broken at all then arr2[i]
        # is not present in arr1[]
        if (j == m):
            return 0
 
    # If we reach here then all
    # elements of arr2[] are present
    # in arr1[]
    return 1
 
 
# Driver code
if __name__ == "__main__":
 
    arr1 = [11, 1, 13, 21, 3, 7]
    arr2 = [11, 3, 7, 1]
 
    m = len(arr1)
    n = len(arr2)
 
    if(isSubset(arr1, arr2, m, n)):
        print("arr2[] is subset of arr1[] ")
    else:
        print("arr2[] is not a subset of arr1[]")
 
# This code is contributed by ita_c

Time complexity: quadratic
Space complexity: constant
# Python3 program to find whether an array
# is subset of another array
 
# Return 1 if arr2[] is a subset of arr1[]
 
 
def isSubset(arr1, arr2, m, n):
    i = 0
 
    quickSort(arr1, 0, m-1)
    for i in range(n):
        if (binarySearch(arr1, 0, m - 1, arr2[i]) == -1):
            return 0
 
    # If we reach here then all elements
    # of arr2[] are present in arr1[]
    return 1
 
# FOLLOWING FUNCTIONS ARE ONLY FOR
# SEARCHING AND SORTING PURPOSE
# Standard Binary Search function
 
 
def binarySearch(arr, low, high, x):
    if(high >= low):
        mid = (low + high)//2
 
        # Check if arr[mid] is the first
        # occurrence of x.
        # arr[mid] is first occurrence if x is
        # one of the following
        # is true:
        # (i) mid == 0 and arr[mid] == x
        # (ii) arr[mid-1] < x and arr[mid] == x
        if((mid == 0 or x > arr[mid-1]) and (arr[mid] == x)):
            return mid
        elif(x > arr[mid]):
            return binarySearch(arr, (mid + 1), high, x)
        else:
            return binarySearch(arr, low, (mid - 1), x)
 
    return -1
 
 
def partition(A, si, ei):
    x = A[ei]
    i = (si - 1)
 
    for j in range(si, ei):
        if(A[j] <= x):
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i + 1], A[ei] = A[ei], A[i + 1]
    return (i + 1)
 
# Implementation of Quick Sort
# A[] --> Array to be sorted
# si --> Starting index
# ei --> Ending index
 
 
def quickSort(A, si, ei):
    # Partitioning index
    if(si < ei):
        pi = partition(A, si, ei)
        quickSort(A, si, pi - 1)
        quickSort(A, pi + 1, ei)
 
 
# Driver code
arr1 = [11, 1, 13, 21, 3, 7]
arr2 = [11, 3, 7, 1]
 
m = len(arr1)
n = len(arr2)
 
if(isSubset(arr1, arr2, m, n)):
    print("arr2[] is subset of arr1[] ")
else:
    print("arr2[] is not a subset of arr1[] ")
 
 
# This code is contributed by chandan_jnu

Time complexity: nlogn
Space complexity: linear
# Python3 program to find whether an array
# is subset of another array
 
# Return 1 if arr2[] is a subset of arr1[] */
 
 
def isSubset(arr1, arr2, m, n):
    i = 0
    j = 0
    if m < n:
        return 0
 
    arr1.sort()
    arr2.sort()
 
    while i < n and j < m:
        if arr1[j] < arr2[i]:
            j += 1
        elif arr1[j] == arr2[i]:
            j += 1
            i += 1
        elif arr1[j] > arr2[i]:
            return 0
    return False if i < n else True
 
 
# Driver code
arr1 = [11, 1, 13, 21, 3, 7]
arr2 = [11, 3, 7, 1]
 
m = len(arr1)
n = len(arr2)
if isSubset(arr1, arr2, m, n) == True:
    print("arr2 is subset of arr1 ")
else:
    printf("arr2 is not a subset of arr1 ")
 
# This code is contributed by Shrikant13

Time complexity: nlogn
Space complexity: constant
# Python3 program to find whether an array
# is subset of another array
 
# Return true if arr2[] is a subset
# of arr1[]
 
 
def isSubset(arr1, m, arr2, n):
 
    # Using STL set for hashing
    hashset = set()
 
    # hset stores all the values of arr1
    for i in range(0, m):
        hashset.add(arr1[i])
 
    # Loop to check if all elements
    # of arr2 also lies in arr1
    for i in range(0, n):
        if arr2[i] in hashset:
            continue
        else:
            return False
 
    return True
 
 
# Driver Code
if __name__ == '__main__':
 
    arr1 = [11, 1, 13, 21, 3, 7]
    arr2 = [11, 3, 7, 1]
 
    m = len(arr1)
    n = len(arr2)
 
    if (isSubset(arr1, m, arr2, n)):
        print("arr2[] is subset of arr1[] ")
    else:
        print("arr2[] is not a subset of arr1[] ")
 
# This code is contributed by akhilsaini

Time complexity: nlogn
Space complexity: linear
# Python3 code
arr1 = [11, 1, 13, 21, 3, 7]
arr2 = [11, 3, 7, 1]
m = len(arr1)
n = len(arr2)
s = set()
for i in range(m):
    s.add(arr1[i])
 
p = len(s)
for i in range(n):
    s.add(arr2[i])
 
if (len(s) == p):
    print("arr2[] is subset of arr1[] ")
 
else:
    print("arr2[] is not subset of arr1[] ")
 
    # This code is contributed by divyeshrabadiya07.

Time complexity: linear
Space complexity: linear
# Python3 program to find whether an array
# is subset of another array
 
# Return true if arr2[] is a subset of arr1[]
 
 
def isSubset(arr1, m, arr2, n):
 
    # Create a Frequency Table using STL
    frequency = {}
 
    # Increase the frequency of each element
    # in the frequency table.
    for i in range(0, m):
        if arr1[i] in frequency:
            frequency[arr1[i]] = frequency[arr1[i]] + 1
        else:
            frequency[arr1[i]] = 1
 
    # Decrease the frequency if the
    # element was found in the frequency
    # table with the frequency more than 0.
    # else return 0 and if loop is
    # completed return 1.
    for i in range(0, n):
        if (frequency[arr2[i]] > 0):
            frequency[arr2[i]] -= 1
        else:
            return False
 
    return True
 
 
# Driver Code
if __name__ == '__main__':
 
    arr1 = [11, 1, 13, 21, 3, 7]
    arr2 = [11, 3, 7, 1]
 
    m = len(arr1)
    n = len(arr2)
 
    if (isSubset(arr1, m, arr2, n)):
        print("arr2[] is subset of arr1[] ")
    else:
        print("arr2[] is not a subset of arr1[] ")
 
# This code is contributed by akhilsaini

Time complexity: linear
Space complexity: linear
# Python program to find all those
# elements of arr1[] that are not
# present in arr2[]
 
def relativeComplement(arr1, arr2, n, m):
  
    i = 0
    j = 0
    while (i < n and j < m):
  
        # If current element in arr2[] is
        # greater, then arr1[i] can't be
        # present in arr2[j..m-1]
        if (arr1[i] < arr2[j]):
            print(arr1[i] , " ", end="")
            i += 1
  
            # Skipping smaller elements of
            # arr2[]
        elif (arr1[i] > arr2[j]):
            j += 1
  
            # Equal elements found (skipping
            # in both arrays)
        elif (arr1[i] == arr2[j]):
            i += 1
            j += 1
     
    # Printing remaining elements of
    # arr1[]
    while (i < n):
        print(arr1[i] , " ", end="")
  
# Driver code
arr1= [3, 6, 10, 12, 15]
arr2 = [1, 3, 5, 10, 16]
n = len(arr1)
m = len(arr2)
relativeComplement(arr1, arr2, n, m)
 
# This code is contributed
# by Anant Agarwal.

Time complexity: linear
Space complexity: constant
# Python3 Program to make all array equal
 
# function for calculating min operations
def minOps(arr, n, k):
 
    # max elements of array
    max1 = max(arr)
    res = 0
 
    # iterate for all elements
    for i in range(0, n):
 
        # check if element can make equal to
        # max or not if not then return -1
        if ((max1 - arr[i]) % k != 0):
            return -1
 
        # else update res for
        # required operations
        else:
            res += (max1 - arr[i]) / k
     
    # return result
    return int(res)
 
# driver program
arr = [21, 33, 9, 45, 63]
n = len(arr)
k = 6
print(minOps(arr, n, k))
 
# This code is contributed by
# Smitha Dinesh Semwal

Time complexity: linear
Space complexity: constant
# python code for above approach.
 
def solve(A, B, C):
 
        # assigning the length -1 value
        # to each of three variables
        i = len(A) - 1
        j = len(B) - 1
        k = len(C) - 1
 
        # calculating min difference
        # from last index of lists
        min_diff = abs(max(A[i], B[j], C[k]) -
        min(A[i], B[j], C[k]))
 
        while i != -1 and j != -1 and k != -1:
            current_diff = abs(max(A[i], B[j],
            C[k]) - min(A[i], B[j], C[k]))
 
            # checking condition
            if current_diff < min_diff:
                min_diff = current_diff
 
            # calculating max term from list
            max_term = max(A[i], B[j], C[k])
 
            # Moving to smaller value in the
            # array with maximum out of three.
            if A[i] == max_term:
                i -= 1
            elif B[j] == max_term:
                j -= 1
            else:
                k -= 1
        return min_diff
 
# driver code
 
A = [ 5, 8, 10, 15 ]
B = [ 6, 9, 15, 78, 89 ]
C = [ 2, 3, 6, 6, 8, 8, 10 ]
print(solve(A, B, C))

Time complexity: linear
Space complexity: constant
# Program to convert binary tree to BST
 
# A binary tree node
class Node:
     
    # Constructor to create a new node
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
 
# Helper function to store the inorder traversal of a tree
def storeInorder(root, inorder):
     
    # Base Case
    if root is None:
        return
     
    # First store the left subtree
    storeInorder(root.left, inorder)
     
    # Copy the root's data
    inorder.append(root.data)
 
    # Finally store the right subtree
    storeInorder(root.right, inorder)
 
# A helper function to count nodes in a binary tree
def countNodes(root):
    if root is None:
        return 0
 
    return countNodes(root.left) + countNodes(root.right) + 1
 
# Helper function that copies contents of sorted array
# to Binary tree
def arrayToBST(arr, root):
 
    # Base Case
    if root is None:
        return
     
    # First update the left subtree
    arrayToBST(arr, root.left)
 
    # now update root's data delete the value from array
    root.data = arr[0]
    arr.pop(0)
 
    # Finally update the right subtree
    arrayToBST(arr, root.right)
 
# This function converts a given binary tree to BST
def binaryTreeToBST(root):
     
    # Base Case: Tree is empty
    if root is None:
        return
     
    # Count the number of nodes in Binary Tree so that
    # we know the size of temporary array to be created
    n = countNodes(root)
 
    # Create the temp array and store the inorder traversal
    # of tree
    arr = []
    storeInorder(root, arr)
     
    # Sort the array
    arr.sort()
 
    # copy array elements back to binary tree
    arrayToBST(arr, root)
 
# Print the inorder traversal of the tree
def printInorder(root):
    if root is None:
        return
    printInorder(root.left)
    print (root.data,end=" ")
    printInorder(root.right)
 
# Driver program to test above function
root = Node(10)
root.left = Node(30)
root.right = Node(15)
root.left.left = Node(20)
root.right.right = Node(5)
 
# Convert binary tree to BST
binaryTreeToBST(root)
 
print ("Following is the inorder traversal of the converted BST")
printInorder(root)
 
# This code is contributed by Nikhil Kumar Singh(nickzuck_007)

Time complexity: nlogn
Space complexity: linear
# Python code to convert a sorted array
# to a balanced Binary Search Tree
 
# binary tree node
class Node:
    def __init__(self, d):
        self.data = d
        self.left = None
        self.right = None
 
# function to convert sorted array to a
# balanced BST
# input : sorted array of integers
# output: root node of balanced BST
def sortedArrayToBST(arr):
     
    if not arr:
        return None
 
    # find middle index
    mid = (len(arr)) // 2
     
    # make the middle element the root
    root = Node(arr[mid])
     
    # left subtree of root has all
    # values <arr[mid]
    root.left = sortedArrayToBST(arr[:mid])
     
    # right subtree of root has all
    # values >arr[mid]
    root.right = sortedArrayToBST(arr[mid+1:])
    return root
 
# A utility function to print the preorder
# traversal of the BST
def preOrder(node):
    if not node:
        return
     
    print node.data,
    preOrder(node.left)
    preOrder(node.right)
 
# driver program to test above function
"""
Constructed balanced BST is
    4
/ \
2 6
/ \ / \
1 3 5 7
"""
 
arr = [1, 2, 3, 4, 5, 6, 7]
root = sortedArrayToBST(arr)
print "PreOrder Traversal of constructed BST ",
preOrder(root)
 
# This code is contributed by Ishita Tripathi

Time complexity: linear
Space complexity: logn
# Python3 program to transform a BST to sum tree
 
class Node:
    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None
 
# Recursive function to transform a BST to sum tree.
# This function traverses the tree in reverse inorder so
# that we have visited all greater key nodes of the currently
# visited node
def transformTreeUtil(root):
   
   # Base case
   if (root == None):
        return
 
   # Recur for right subtree
   transformTreeUtil(root.right)
 
   # Update sum
   global sum
   sum = sum + root.data
 
   # Store old sum in current node
   root.data = sum - root.data
 
   # Recur for left subtree
   transformTreeUtil(root.left)
 
# A wrapper over transformTreeUtil()
def transformTree(root):
 
    # sum = 0 #Initialize sum
    transformTreeUtil(root)
 
# A utility function to prindorder traversal of a
# binary tree
def printInorder(root):
    if (root == None):
        return
 
    printInorder(root.left)
    print(root.data, end = " ")
    printInorder(root.right)
 
# Driver Program to test above functions
if __name__ == '__main__':
 
    sum=0
    root = Node(11)
    root.left = Node(2)
    root.right = Node(29)
    root.left.left = Node(1)
    root.left.right = Node(7)
    root.right.left = Node(15)
    root.right.right = Node(40)
    root.right.right.left = Node(35)
 
    print("Inorder Traversal of given tree")
    printInorder(root)
 
    transformTree(root)
 
    print("\nInorder Traversal of transformed tree")
    printInorder(root)
 
    # This code is contributed by mohit kumar 29

Time complexity: linear
Space complexity: linear