Banana-Leopard/TASTY huggingface dataset:
# Python3 program to find equilibrium
# index of an array
 
# function to find the equilibrium index
 
 
def equilibrium(arr):
    leftsum = 0
    rightsum = 0
    n = len(arr)
 
    # Check for indexes one by one
    # until an equilibrium index is found
    for i in range(n):
        leftsum = 0
        rightsum = 0
 
        # get left sum
        for j in range(i):
            leftsum += arr[j]
 
        # get right sum
        for j in range(i + 1, n):
            rightsum += arr[j]
 
        # if leftsum and rightsum are same,
        # then we are done
        if leftsum == rightsum:
            return i
 
    # return -1 if no equilibrium index is found
    return -1
 
 
# Driver code
if __name__ == "__main__":
    arr = [-7, 1, 5, 2, -4, 3, 0]
 
    # Function call
    print(equilibrium(arr))
 
# This code is contributed by Abhishek Sharama

Time complexity: quadratic
Space complexity: constant
# Python program to find the equilibrium
# index of an array
 
# Function to find the equilibrium index
 
 
def equilibrium(arr):
 
    # finding the sum of whole array
    total_sum = sum(arr)
    leftsum = 0
    for i, num in enumerate(arr):
 
        # total_sum is now right sum
        # for index i
        total_sum -= num
 
        if leftsum == total_sum:
            return i
        leftsum += num
 
      # If no equilibrium index found,
      # then return -1
    return -1
 
 
# Driver code
if __name__ == "__main__":
    arr = [-7, 1, 5, 2, -4, 3, 0]
 
    # Function call
    print('First equilibrium index is ',
          equilibrium(arr))
 
# This code is contributed by Abhishek Sharma

Time complexity: linear
Space complexity: constant
# Python3 program to find the equilibrium
# index of an array
 
# Function to find the equilibrium index
 
 
def equilibrium(arr):
    left_sum = []
    right_sum = []
 
    # Iterate from 0 to len(arr)
    for i in range(len(arr)):
 
        # If i is not 0
        if(i):
            left_sum.append(left_sum[i-1]+arr[i])
            right_sum.append(right_sum[i-1]+arr[len(arr)-1-i])
        else:
            left_sum.append(arr[i])
            right_sum.append(arr[len(arr)-1])
 
    # Iterate from 0 to len(arr)
    for i in range(len(arr)):
        if(left_sum[i] == right_sum[len(arr) - 1 - i]):
            return(i)
 
    # If no equilibrium index found,then return -1
    return -1
 
 
# Driver code
if __name__ == "__main__":
    arr = [-7, 1, 5, 2, -4, 3, 0]
 
    # Function call
    print('First equilibrium index is ',
          equilibrium(arr))
 
# This code is contributed by Lokesh Sharma

Time complexity: linear
Space complexity: linear
# Python Function to print leaders in array
 
def printLeaders(arr,size):
     
    for i in range(0, size):
        for j in range(i+1, size):
            if arr[i]<=arr[j]:
                break
        if j == size-1: # If loop didn't break
            print (arr[i],end=' ')
 
# Driver function
arr=[16, 17, 4, 3, 5, 2]
printLeaders(arr, len(arr))
 
# This code is contributed by _Devesh Agrawal__

Time complexity: quadratic
Space complexity: constant
# Python function to print leaders in array
def printLeaders(arr, size):
    
    max_from_right = arr[size-1]  
    print (max_from_right,end=' ')   
    for i in range( size-2, -1, -1):       
        if max_from_right < arr[i]:       
            print (arr[i],end=' ')
            max_from_right = arr[i]
         
# Driver function
arr = [16, 17, 4, 3, 5, 2]
printLeaders(arr, len(arr))
 
# This code contributed by _Devesh Agrawal__

Time complexity: linear
Space complexity: constant
# Python Function to print leaders in an array
def printLaders(arr, size):
    # create stack to store leaders
    sk = []
    sk.append(arr[size - 1])
    for i in range(size - 2, -1, -1):
        if(arr[i] >= sk[len(sk) - 1]):
            sk.append(arr[i])
 
    # print stack elements
    # run loop till stack is not empty
    while(len(sk) != 0):
        print(sk[len(sk)-1],end = ' ')
        sk.pop()
 
# Driver program to test above function
if __name__ == "__main__":
    arr = [16,17,4,3,5,2]
    n = len(arr)
    printLaders(arr,n)
     
    # This code is contributed by ajaymakvana

Time complexity: linear
Space complexity: linear
# Function to get index of ceiling of x in arr[low..high] */
def ceilSearch(arr, low, high, x):
 
    # If x is smaller than or equal to first element,
    # then return the first element */
    if x <= arr[low]:
        return low
 
    # Otherwise, linearly search for ceil value */
    i = low
    for i in range(high):
        if arr[i] == x:
            return i
 
        # if x lies between arr[i] and arr[i+1] including
        # arr[i+1], then return arr[i+1] */
        if arr[i] < x and arr[i+1] >= x:
            return i+1
         
    # If we reach here then x is greater than the last element
    # of the array,  return -1 in this case */
    return -1
 
# Driver program to check above functions */
arr = [1, 2, 8, 10, 10, 12, 19]
n = len(arr)
x = 3
index = ceilSearch(arr, 0, n-1, x);
 
if index == -1:
    print ("Ceiling of %d doesn't exist in array "% x)
else:
    print ("ceiling of %d is %d"%(x, arr[index]))
 
# This code is contributed by Shreyanshi Arun

Time complexity: linear
Space complexity: constant
# Function to get index of ceiling of x in arr[low..high]*/
def ceilSearch(arr, low, high, x):
 
    # If x is smaller than or equal to the first element,
    # then return the first element */
    if x <= arr[low]:
        return low
 
    # If x is greater than the last element, then return -1 */
    if x > arr[high]:
        return -1 
  
    # get the index of middle element of arr[low..high]*/
    mid = (low + high)/2;  # low + (high - low)/2 */
  
    # If x is same as middle element, then return mid */
    if arr[mid] == x:
        return mid
 
    # If x is greater than arr[mid], then either arr[mid + 1]
    # is ceiling of x or ceiling lies in arr[mid+1...high] */
    elif arr[mid] < x:
        if mid + 1 <= high and x <= arr[mid+1]:
            return mid + 1
        else:
            return ceilSearch(arr, mid+1, high, x)
  
    # If x is smaller than arr[mid], then either arr[mid]
    # is ceiling of x or ceiling lies in arr[low...mid-1] */  
    else:
        if mid - 1 >= low and x > arr[mid-1]:
            return mid
        else:
            return ceilSearch(arr, low, mid - 1, x)
  
# Driver program to check above functions
arr = [1, 2, 8, 10, 10, 12, 19]
n = len(arr)
x = 20
index = ceilSearch(arr, 0, n-1, x);
 
if index == -1:
    print ("Ceiling of %d doesn't exist in array "% x)
else:
    print ("ceiling of %d is %d"%(x, arr[index]))
 
# This code is contributed by Shreyanshi Arun

Time complexity: logn
Space complexity: constant
# Function to get index of ceiling of x in arr[low..high]
def ceilSearch(arr, low, high, x):
 
  # base condition if length of arr == 0 then return -1
    if (x == 0):
        return -1
 
    """this while loop function will run until
  condition not break once condition break
       loop will return start and ans is low
       which will be next smallest greater than target
       which is ceiling"""
    while (low <= high):
        mid = low + (high - low) / 2
        mid = int(mid)
        if (arr[mid] == x):
            return mid
        elif (x < arr[mid]):
            high = mid - 1
        else:
            low = mid + 1
 
    return low
 
 
""" step 1 : { low = 1, 2, 8, 10= mid, 10, 12, 19= high};
                if( x < mid) yes set high = mid -1;
      step 2 : { low = 1, 2 = mid, 8 = high, 10, 10, 12, 19};
                if( x < mid) no set low = mid + 1;
      step 3 : {1, 2, 8 = high,low,low,  10, 10, 12, 19};
                 if( x == mid ) yes return mid
                 if(x < mid ) no low = mid + 1
      step 4  : {1, 2, 8 = high,mid, 10 = low, 10, 12, 19};
                check while(low < = high)
                 condition break and return low which will next greater of target """
 
# Driver program to check above functions
arr = [1, 2, 8, 10, 10, 12, 19]
n = len(arr)
x = 8
index = ceilSearch(arr, 0, n - 1, x)
if (index == -1):
    print("Ceiling of", x, "does not exist in an array")
else:
    print("Ceiling of", x, "is", arr[index])

Time complexity: logn
Space complexity: constant
# Python3 program to find Majority
# element in an array
 
# Function to find Majority
# element in an array
 
 
def findMajority(arr, n):
 
    maxCount = 0
    index = -1  # sentinels
    for i in range(n):
 
        count = 0
        for j in range(n):
 
            if(arr[i] == arr[j]):
                count += 1
 
        # update maxCount if count of
        # current element is greater
        if(count > maxCount):
 
            maxCount = count
            index = i
 
    # if maxCount is greater than n/2
    # return the corresponding element
    if (maxCount > n//2):
        print(arr[index])
 
    else:
        print("No Majority Element")
 
 
# Driver code
if __name__ == "__main__":
    arr = [1, 1, 2, 1, 3, 5, 1]
    n = len(arr)
 
    # Function calling
    findMajority(arr, n)
 
# This code is contributed
# by ChitraNayal

Time complexity: quadratic
Space complexity: constant
# Python3 program to demonstrate insert operation in binary
# search tree.
# class for creating node
class Node():
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
        self.count = 1  # count of number of times data is inserted in tree
 
# class for binary search tree
# it initialises tree with None root
# insert function inserts node as per BST rule
# and also checks for majority element
# if no majority element is found yet, it returns None
 
 
class BST():
    def __init__(self):
        self.root = None
 
    def insert(self, data, n):
        out = None
        if (self.root == None):
            self.root = Node(data)
        else:
            out = self.insertNode(self.root, data, n)
        return out
 
    def insertNode(self, currentNode, data, n):
        if (currentNode.data == data):
            currentNode.count += 1
            if (currentNode.count > n//2):
                return currentNode.data
            else:
                return None
        elif (currentNode.data < data):
            if (currentNode.right):
                self.insertNode(currentNode.right, data, n)
            else:
                currentNode.right = Node(data)
        elif (currentNode.data > data):
            if (currentNode.left):
                self.insertNode(currentNode.left, data, n)
            else:
                currentNode.left = Node(data)
 
 
# Driver code
# declaring an array
arr = [3, 2, 3]
n = len(arr)
 
# declaring None tree
tree = BST()
flag = 0
for i in range(n):
    out = tree.insert(arr[i], n)
    if (out != None):
        print(arr[i])
        flag = 1
        break
if (flag == 0):
    print("No Majority Element")

Time complexity: quadratic
Space complexity: linear
# Program for finding out majority element in an array
 
# Function to find the candidate for Majority
 
 
def findCandidate(A):
    maj_index = 0
    count = 1
    for i in range(len(A)):
        if A[maj_index] == A[i]:
            count += 1
        else:
            count -= 1
        if count == 0:
            maj_index = i
            count = 1
    return A[maj_index]
 
# Function to check if the candidate occurs more than n/2 times
 
 
def isMajority(A, cand):
    count = 0
    for i in range(len(A)):
        if A[i] == cand:
            count += 1
    if count > len(A)/2:
        return True
    else:
        return False
 
# Function to print Majority Element
 
 
def printMajority(A):
    # Find the candidate for Majority
    cand = findCandidate(A)
 
    # Print the candidate if it is Majority
    if isMajority(A, cand) == True:
        print(cand)
    else:
        print("No Majority Element")
 
 
# Driver code
A = [1, 3, 3, 1, 2]
 
# Function call
printMajority(A)

Time complexity: linear
Space complexity: constant
# Python3 program for finding out majority
# element in an array
 
def findMajority(arr, size):
    m = {}
    for i in range(size):
        if arr[i] in m:
            m[arr[i]] += 1
        else:
            m[arr[i]] = 1
    count = 0
    for key in m:
        if m[key] > size / 2:
            count = 1
            print("Majority found :-",key)
            break
    if(count == 0):
        print("No Majority element")
 
# Driver code
arr = [2, 2, 2, 2, 5, 5, 2, 3, 3]
n = len(arr)
 
# Function calling
findMajority(arr, n)
 
# This code is contributed by ankush_953

Time complexity: linear
Space complexity: linear
# Python3 program to find Majority
# element in an array
 
# Function to find Majority element
# in an array
# it returns -1 if there is no majority element
def majorityElement(arr, n) :
     
    # sort the array in O(nlogn)
    arr.sort()  
    count, max_ele, temp, f = 1, -1, arr[0], 0
    for i in range(1, n) :
         
        # increases the count if the same element occurs
        # otherwise starts counting new element
        if(temp == arr[i]) :
            count += 1
        else :
            count = 1
            temp = arr[i]
             
        # sets maximum count
        # and stores maximum occurred element so far
        # if maximum count becomes greater than n/2
        # it breaks out setting the flag
        if(max_ele < count) :
            max_ele = count
            ele = arr[i]
             
            if(max_ele > (n//2)) :
                f = 1
                break
             
    # returns maximum occurred element
    # if there is no such element, returns -1
    if f == 1 :
        return ele
    else :
        return -1
 
# Driver code
arr = [1, 1, 2, 1, 3, 5, 1]
n = len(arr)
 
# Function calling
print(majorityElement(arr, n))
 
# This code is contributed by divyeshrabadiya07

Time complexity: nlogn
Space complexity: constant
# Hashing based Python
# program to find if
# there is a majority
# element in input array.
 
# Returns true if there
# is a majority element
# in a[]
def isMajority(a):
     
    # Insert all elements
    # in a hash table
    mp = {}
     
    for i in a:
        if i in mp: mp[i] += 1
        else: mp[i] = 1
     
    # Check if frequency
    # of any element is
    # n/2 or more.
    for x in mp:
        if mp[x] >= len(a)//2:
            return True
    return False
 
# Driver code
a = [ 2, 3, 9, 2, 2 ]
 
print("Yes" if isMajority(a) else "No")
 
#This code is contributed by Ansu Kumari

Time complexity: linear
Space complexity: linear
# A python3 program to find a peak
#  element using divide and conquer
 
# A binary search based function
# that returns index of a peak element
 
 
def findPeakUtil(arr, low, high, n):
 
    # Find index of middle element
    # low + (high - low) / 2
    mid = low + (high - low)/2
    mid = int(mid)
 
    # Compare middle element with its
    # neighbours (if neighbours exist)
    if ((mid == 0 or arr[mid - 1] <= arr[mid]) and
        (mid == n - 1 or arr[mid + 1] <= arr[mid])):
        return mid
 
 
    # If middle element is not peak and
    # its left neighbour is greater
    # than it, then left half must
    # have a peak element
    elif (mid > 0 and arr[mid - 1] > arr[mid]):
        return findPeakUtil(arr, low, (mid - 1), n)
 
    # If middle element is not peak and
    # its right neighbour is greater
    # than it, then right half must
    # have a peak element
    else:
        return findPeakUtil(arr, (mid + 1), high, n)
 
 
# A wrapper over recursive
# function findPeakUtil()
def findPeak(arr, n):
 
    return findPeakUtil(arr, 0, n - 1, n)
 
 
# Driver code
arr = [1, 3, 20, 4, 1, 0]
n = len(arr)
print("Index of a peak point is", findPeak(arr, n))
     
# This code is contributed by
# Smitha Dinesh Semwal

Time complexity: logn
Space complexity: logn
# A Python program to find a peak element
# using divide and conquer
 
# A binary search based function
# that returns index of a peak element
def findPeak(arr, n):
   
    l = 0
    r = n-1
     
    while(l <= r):
 
        # finding mid by binary right shifting.
        mid = (l + r) >> 1
 
        # first case if mid is the answer
        if((mid == 0 or arr[mid - 1] <= arr[mid]) and (mid == n - 1 or arr[mid + 1] <= arr[mid])):
            break
 
        # move the right pointer
        if(mid > 0 and arr[mid - 1] > arr[mid]):
            r = mid - 1
 
        # move the left pointer
        else:
            l = mid + 1
 
    return mid
 
 
# Driver Code
arr = [1, 3, 20, 4, 1, 0]
n = len(arr)
print(f"Index of a peak point is {findPeak(arr, n)}")
 
# This code is contributed by Rajdeep Mallick (rajdeep999)

Time complexity: logn
Space complexity: constant
# Python3 program to Find the two
# repeating elements in a given array
 
 
def printRepeating(arr, size):
 
    print("Repeating elements are", end=' ')
    for i in range(0, size-1):
        for j in range(i + 1, size):
            if arr[i] == arr[j]:
                print(arr[i], end=' ')
 
 
# Driver code
arr = [4, 2, 4, 5, 2, 3, 1]
arr_size = len(arr)
printRepeating(arr, arr_size)
 
# This code is contributed by Smitha Dinesh Semwal

Time complexity: quadratic
Space complexity: constant
# Python3 code for Find the two repeating
# elements in a given array
 
 
def printRepeating(arr, size):
    count = [0] * size
    print(" Repeating elements are ", end="")
    for i in range(0, size):
        if(count[arr[i]] == 1):
            print(arr[i], end=" ")
        else:
            count[arr[i]] = count[arr[i]] + 1
 
 
# Driver code
arr = [4, 2, 4, 5, 2, 3, 1]
arr_size = len(arr)
printRepeating(arr, arr_size)
 
 
# This code is contributed by Nikita Tiwari.

Time complexity: linear
Space complexity: linear
# Python3 code for Find the two repeating
# elements in a given array
import math
 
 
def printRepeating(arr, size):
 
    # S is for sum of elements in arr[]
    S = 0
 
    # P is for product of elements in arr[]
    P = 1
 
    n = size - 2
 
    # Calculate Sum and Product
    # of all elements in arr[]
    for i in range(0, size):
        S = S + arr[i]
        P = P * arr[i]
 
    # S is x + y now
    S = S - n * (n + 1) // 2
 
    # P is x*y now
    P = P // fact(n)
 
    # D is x - y now
    D = math.sqrt(S * S - 4 * P)
 
    x = (D + S) // 2
    y = (S - D) // 2
 
    print("Repeating elements are ",
          (int)(x), " ", (int)(y))
 
 
def fact(n):
    if(n == 0):
        return 1
    else:
        return(n * fact(n - 1))
 
 
# Driver code
arr = [4, 2, 4, 5, 2, 3, 1]
arr_size = len(arr)
printRepeating(arr, arr_size)
 
 
# This code is contributed by Nikita Tiwari.

Time complexity: linear
Space complexity: constant
# Python3 code to Find the
# two repeating elements
# in a given array
 
 
def printRepeating(arr, size):
 
    # Will hold xor
    # of all elements
    xor = arr[0]
    n = size - 2
    x = 0
    y = 0
 
    # Get the xor of all
    # elements in arr[]
    # and 1, 2 .. n
    for i in range(1, size):
        xor ^= arr[i]
    for i in range(1, n + 1):
        xor ^= i
 
    # Get the rightmost set
    # bit in set_bit_no
    set_bit_no = xor & ~(xor-1)
 
    # Now divide elements in two
    # sets by comparing rightmost
    # set bit of xor with bit at
    # same position in each element.
    for i in range(0, size):
 
        if(arr[i] & set_bit_no):
 
            # XOR of first
            # set in arr[]
            x = x ^ arr[i]
        else:
 
            # XOR of second
            # set in arr[]
            y = y ^ arr[i]
 
    for i in range(1, n + 1):
 
        if(i & set_bit_no):
 
            # XOR of first set
            # in arr[] and
            # 1, 2, ...n
            x = x ^ i
        else:
 
             # XOR of second set
             # in arr[] and
             # 1, 2, ...n
            y = y ^ i
 
    print("Repeating elements are", y, x)
 
 
# Driver code
arr = [4, 2, 4,
       5, 2, 3, 1]
arr_size = len(arr)
printRepeating(arr, arr_size)
 
# This code is contributed
# by Smitha

Time complexity: linear
Space complexity: constant
# Python3 code for Find the two repeating
# elements in a given array
 
 
def printRepeating(arr, size):
 
    print("Repeating elements are", end=" ")
 
    for i in range(0, size):
 
        if(arr[abs(arr[i])] > 0):
            arr[abs(arr[i])] = (-1) * arr[abs(arr[i])]
        else:
            print(abs(arr[i]), end=" ")
 
 
# Driver code
arr = [4, 2, 4, 5, 2, 3, 1]
arr_size = len(arr)
printRepeating(arr, arr_size)
 
 
# This code is contributed by Nikita Tiwari.

Time complexity: linear
Space complexity: constant
# Python program for the above approach
def twoRepeated(arr, N):
    m = N - 1
    for i in range(N):
        arr[arr[i] % m - 1] += m
        if ((arr[arr[i] % m - 1] // m) == 2):
            print(arr[i] % m, end=" ")
 
 
# Driver Code
arr = [4, 2, 4, 5, 2, 3, 1]
n = len(arr)
print("Repeating elements are ", end="")
twoRepeated(arr, n)
 
# This code is contributed by Shubham Singh

Time complexity: linear
Space complexity: constant
# Python3 program to find the two repeating
# elements in a given array
 
 
def printRepeating(arr, size):
 
    s = set()
    print("Repeating elements are ", end="")
 
    for i in range(size):
        if (len(s) and arr[i] in s):
            print(arr[i], end="  ")
 
        s.add(arr[i])
 
 
# Driver code
arr = [4, 2, 4, 5, 2, 3, 1]
arr_size = len(arr)
printRepeating(arr, arr_size)
 
# This code is contributed by Shubham Singh

Time complexity: linear
Space complexity: linear
# A simple program to print subarray with sum as given sum
 
# Returns true if the there is a subarray of arr[] with sum equal to 'sum' otherwise returns false. Also, prints the result
def subArraySum(arr, n, sum):
 
    # Pick a starting point
    for i in range(0,n):
        currentSum = arr[i]
        if(currentSum == sum):
            print("Sum found at indexes",i)
            return
        else:
            # Try all subarrays starting with 'i'
            for j in range(i+1,n):
                currentSum += arr[i]
                if(currentSum == sum):
                    print("Sum found between indexes",i,"and",j)
                    return
    print("No Subarray Found")
 
# Driver Code
if __name__ == "__main__":
    arr = [15,2,4,8,9,5,10,23]
    n = len(arr)
    sum = 23
    subArraySum(arr, n, sum)
     
    # This code is contributed by ajaymakvana

Time complexity: quadratic
Space complexity: constant
# An efficient program
# to print subarray
# with sum as given sum
 
# Returns true if the
# there is a subarray
# of arr[] with sum
# equal to 'sum'
# otherwise returns
# false. Also, prints
# the result.
 
 
def subArraySum(arr, n, sum_):
 
    # Initialize currentSum as
    # value of first element
    # and starting point as 0
    currentSum = arr[0]
    start = 0
 
    # Add elements one by
    # one to currentSum and
    # if the currentSum exceeds
    # the sum, then remove
    # starting element
    i = 1
    while i <= n:
 
        # If currentSum exceeds
        # the sum, then remove
        # the starting elements
        while currentSum > sum_ and start < i-1:
 
            currentSum = currentSum - arr[start]
            start += 1
 
        # If currentSum becomes
        # equal to sum, then
        # return true
        if currentSum == sum_:
            print("Sum found between indexes % d and % d" % (start, i-1))
 
            return 1
 
        # Add this element
        # to currentSum
        if i < n:
            currentSum = currentSum + arr[i]
        i += 1
 
    # If we reach here,
    # then no subarray
    print("No subarray found")
    return 0
 
 
# Driver program
if __name__ == '__main__':
    arr = [15, 2, 4, 8, 9, 5, 10, 23]
    n = len(arr)
    sum_ = 23
 
subArraySum(arr, n, sum_)
 
# This code is Contributed by shreyanshi_arun.

Time complexity: linear
Space complexity: constant