Banana-Leopard/TASTY huggingface dataset:
# Python3 program to perform range
# queries over range queries.
import math
 
max = 10000
 
# For prefix sum array
def update(arr, l):
     
    arr[l] += arr[l - 1]
 
# This function is used to apply square root
# decomposition in the record array
def record_func(block_size, block,
                record, l, r, value):
 
    # Traversing first block in range
    while (l < r and
           l % block_size != 0 and
           l != 0):
        record[l] += value
        l += 1
 
    # Traversing completely overlapped
    # blocks in range
    while (l + block_size <= r + 1):
        block[l // block_size] += value
        l += block_size
 
    # Traversing last block in range
    while (l <= r):
        record[l] += value
        l += 1
 
# Function to print the resultant array
def print_array(arr, n):
     
    for i in range(n):
        print(arr[i], end = " ")
 
# Driver code
if __name__ == "__main__":
 
    n = 5
    m = 5
    arr = [0] * n
    record = [0] * m
     
    block_size = (int)(math.sqrt(m))
    block = [0] * max
     
    command = [ [ 1, 1, 2 ],
                [ 1, 4, 5 ],
                [ 2, 1, 2 ],
                [ 2, 1, 3 ],
                [ 2, 3, 4 ] ]
 
    for i in range(m - 1, -1, -1):
 
        # If query is of type 2 then function
        # call to record_func
        if (command[i][0] == 2):
            x = i // (block_size)
             
            record_func(block_size, block,
                        record, command[i][1] - 1,
                                command[i][2] - 1,
                        (block[x] + record[i] + 1))
 
        # If query is of type 1 then simply add
        # 1 to the record array
        else:
            record[i] += 1
 
    # Merging the value of the block
    # in the record array
    for i in range(m):
        check = (i // block_size)
        record[i] += block[check]
 
    for i in range(m):
         
        # If query is of type 1 then the array
        # elements are over-written by the record
        # array
        if (command[i][0] == 1):
            arr[command[i][1] - 1] += record[i]
             
            if ((command[i][2] - 1) < n - 1):
                arr[(command[i][2])] -= record[i]
 
    # The prefix sum of the array
    for i in range(1, n):
        update(arr, i)
 
    # Printing the resultant array
    print_array(arr, n)
 
# This code is contributed by chitranayal

Time complexity: logn
Space complexity: linear
# Python 3 program to count the
# number of indexes in range L R
# such that Ai = Ai + 1
 
# function that answers every
# query in O(r-l)
def answer_query(a, n, l, r):
 
    # traverse from l to r and
    # count the required indexes
    count = 0
    for i in range(l, r):
        if (a[i] == a[i + 1]):
            count += 1
 
    return count
 
# Driver Code
a = [1, 2, 2, 2, 3, 3, 4, 4, 4]
n = len(a)
 
# 1-st query
L = 1
R = 8
print(answer_query(a, n, L, R))
 
# 2nd query
L = 0
R = 4
print(answer_query(a, n, L, R))
 
# This code is contributed by
# Smitha Dinesh Semwal

Time complexity: constant
Space complexity: constant
# Python program to count
# the number of indexes in
# range L R such that Ai=Ai+1
N = 1000
 
# array to store count
# of index from 0 to
# i that obey condition
prefixans = [0] * N;
 
# precomputing
# prefixans[] array
def countIndex(a, n) :
 
    global N, prefixans
     
    # traverse to compute
    # the prefixans[] array
    for i in range(0, n - 1) :
 
        if (a[i] == a[i + 1]) :
            prefixans[i] = 1
 
        if (i != 0) :
            prefixans[i] = (prefixans[i] +
                            prefixans[i - 1])
 
# def that answers
# every query in O(1)
def answer_query(l, r) :
 
    global N, prefixans
    if (l == 0) :
        return prefixans[r - 1]
    else :
        return (prefixans[r - 1] -
                prefixans[l - 1])
 
# Driver Code
a = [1, 2, 2, 2,
     3, 3, 4, 4, 4]
n = len(a)
 
# pre-computation
countIndex(a, n)
 
# 1-st query
L = 1
R = 8
 
print (answer_query(L, R))
 
# 2nd query
L = 0
R = 4
print (answer_query(L, R))
 
# This code is contributed by
# Manish Shaw(manishshaw1)

Time complexity: linear
Space complexity: linear
# Python program to find maximum contiguous subarray
 
# Function to find the maximum contiguous subarray
from sys import maxint
 
 
def maxSubArraySum(a, size):
 
    max_so_far = -maxint - 1
    max_ending_here = 0
 
    for i in range(0, size):
        max_ending_here = max_ending_here + a[i]
        if (max_so_far < max_ending_here):
            max_so_far = max_ending_here
 
        if max_ending_here < 0:
            max_ending_here = 0
    return max_so_far
 
# Driver function to check the above function
 
 
a = [-2, -3, 4, -1, -2, 1, 5, -3]
 
print "Maximum contiguous sum is", maxSubArraySum(a, len(a))
 
# This code is contributed by _Devesh Agrawal_

Time complexity: linear
Space complexity: constant
# Python program to print largest contiguous array sum
 
from sys import maxsize
 
# Function to find the maximum contiguous subarray
# and print its starting and end index
 
 
def maxSubArraySum(a, size):
 
    max_so_far = -maxsize - 1
    max_ending_here = 0
    start = 0
    end = 0
    s = 0
 
    for i in range(0, size):
 
        max_ending_here += a[i]
 
        if max_so_far < max_ending_here:
            max_so_far = max_ending_here
            start = s
            end = i
 
        if max_ending_here < 0:
            max_ending_here = 0
            s = i+1
 
    print("Maximum contiguous sum is %d" % (max_so_far))
    print("Starting Index %d" % (start))
    print("Ending Index %d" % (end))
 
 
# Driver program to test maxSubArraySum
a = [-2, -3, 4, -1, -2, 1, 5, -3]
maxSubArraySum(a, len(a))

Time complexity: linear
Space complexity: constant
# Returns maximum profit with
# two transactions on a given
# list of stock prices price[0..n-1]
 
 
def maxProfit(price, n):
 
    # Create profit array and initialize it as 0
    profit = [0]*n
 
    # Get the maximum profit
    # with only one transaction
    # allowed. After this loop,
    # profit[i] contains maximum
    # profit from price[i..n-1]
    # using at most one trans.
    max_price = price[n-1]
 
    for i in range(n-2, 0, -1):
 
        if price[i] > max_price:
            max_price = price[i]
 
        # we can get profit[i] by
        # taking maximum of:
        # a) previous maximum,
        # i.e., profit[i+1]
        # b) profit by buying at
        # price[i] and selling at
        #    max_price
        profit[i] = max(profit[i+1], max_price - price[i])
 
    # Get the maximum profit
    # with two transactions allowed
    # After this loop, profit[n-1]
    # contains the result
    min_price = price[0]
 
    for i in range(1, n):
 
        if price[i] < min_price:
            min_price = price[i]
 
        # Maximum profit is maximum of:
        # a) previous maximum,
        # i.e., profit[i-1]
        # b) (Buy, Sell) at
        # (min_price, A[i]) and add
        #  profit of other trans.
        # stored in profit[i]
        profit[i] = max(profit[i-1], profit[i]+(price[i]-min_price))
 
    result = profit[n-1]
 
    return result
 
 
# Driver function
price = [2, 30, 15, 10, 8, 25, 80]
print ("Maximum profit is", maxProfit(price, len(price)))
 
# This code is contributed by __Devesh Agrawal__

Time complexity: linear
Space complexity: linear
import sys
 
def maxtwobuysell(arr, size):
    first_buy = -sys.maxsize;
    first_sell = 0;
    second_buy = -sys.maxsize;
    second_sell = 0;
 
    for i in range(size):
 
        first_buy = max(first_buy, -arr[i]);
        first_sell = max(first_sell, first_buy + arr[i]);
        second_buy = max(second_buy, first_sell - arr[i]);
        second_sell = max(second_sell, second_buy + arr[i]);
 
     
    return second_sell;
 
if __name__ == '__main__':
    arr = [ 2, 30, 15, 10, 8, 25, 80 ];
    size = len(arr);
    print(maxtwobuysell(arr, size));
 
# This code is contributed by gauravrajput1

Time complexity: linear
Space complexity: constant
# Python3 program to find
# minimum average subarray
 
# Prints beginning and ending
# indexes of subarray of size k
# with minimum average
def findsubarrayleast(arr, k, n):
    min = 999999
    minindex = 0
    for i in range(n-k+1):
        sum = 0
        j = i
        for j in range(i, i+k):
            sum += arr[j]
        if sum < min:
            min = sum
            minindex = i
 
    # printing the desired subarray
    print("subarray with minimum average is: ", end='')
    for i in range(minindex, minindex+k):
        print(arr[i], end=" ")
 
 
# Driver Code
arr = [20, 3, 13, 5, 10, 14, 8, 5, 11, 9, 1, 11]
k = 9  # Subarray size
n = len(arr)
findsubarrayleast(arr, k, n)
 
# This code is contributed by Aarti_Rathi

Time complexity: quadratic
Space complexity: constant
# Python3 program to find
# minimum average subarray
 
# Prints beginning and ending
# indexes of subarray of size k
# with minimum average
def findMinAvgSubarray(arr, n, k):
 
    # k must be smaller than or equal to n
    if (n < k): return 0
 
    # Initialize beginning index of result
    res_index = 0
 
    # Compute sum of first subarray of size k
    curr_sum = 0
    for i in range(k):
        curr_sum += arr[i]
 
    # Initialize minimum sum as current sum
    min_sum = curr_sum
 
    # Traverse from (k + 1)'th
    # element to n'th element
    for i in range(k, n):
     
        # Add current item and remove first
        # item of previous subarray
        curr_sum += arr[i] - arr[i-k]
 
        # Update result if needed
        if (curr_sum < min_sum):
         
            min_sum = curr_sum
            res_index = (i - k + 1)
         
    print("Subarray between [", res_index,
          ", ", (res_index + k - 1),
          "] has minimum average")
 
# Driver Code
arr = [3, 7, 90, 20, 10, 50, 40]
k = 3 # Subarray size
n = len(arr)
findMinAvgSubarray(arr, n, k)
 
# This code is contributed by Anant Agarwal.

Time complexity: linear
Space complexity: constant
# Python3 code to Find the minimum
# distance between two numbers
 
 
def minDist(arr, n, x, y):
    min_dist = 99999999
    for i in range(n):
        for j in range(i + 1, n):
            if (x == arr[i] and y == arr[j] or
                    y == arr[i] and x == arr[j]) and min_dist > abs(i-j):
                min_dist = abs(i-j)
        return min_dist
 
 
# Driver code
arr = [3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3]
n = len(arr)
x = 3
y = 6
print("Minimum distance between ", x, " and ",
      y, "is", minDist(arr, n, x, y))
 
# This code is contributed by "Abhishek Sharma 44"

Time complexity: quadratic
Space complexity: constant
import sys
 
def minDist(arr, n, x, y):
     
    #previous index and min distance
    i=0
    p=-1
    min_dist = sys.maxsize;
     
    for i in range(n):
     
        if(arr[i] ==x or arr[i] == y):
         
            #we will check if p is not equal to -1 and
            #If the element at current index matches with
            #the element at index p , If yes then update
            #the minimum distance if needed
            if(p != -1 and arr[i] != arr[p]):
                min_dist = min(min_dist,i-p)
              
            #update the previous index
            p=i
         
     
    #If distance is equal to int max
    if(min_dist == sys.maxsize):
       return -1
    return min_dist
 
  
# Driver program to test above function */
arr = [3, 5, 4, 2, 6, 3, 0, 0, 5, 4, 8, 3]
n = len(arr)
x = 3
y = 6
print ("Minimum distance between %d and %d is %d\n"%( x, y,minDist(arr, n, x, y)));
 
# This code is contributed by Shreyanshi Arun.

Time complexity: linear
Space complexity: constant
# Python program to Find the minimum
# distance between two numbers
import sys
 
def minDist(arr, n, x, y) :
     
    # idx1 and idx2 will store indices of
    # x or y and min_dist will store the minimum difference
    idx1=-1; idx2=-1; min_dist = sys.maxsize;
    for i in range(n) :
       # if current element is x then change idx1
       if arr[i]==x :
          idx1=i
           
       # if current element is y then change idx2
       elif arr[i]==y :
          idx2=i
        
       # if x and y both found in array
       # then only find the difference and store it in min_dist
       if idx1!=-1 and idx2!=-1 :
           min_dist=min(min_dist,abs(idx1-idx2));
     
    # if left or right did not found in array
    # then return -1
    if idx1==-1 or idx2==-1 :
        return -1
    # return the minimum distance
    else :
        return min_dist
   
# Driver code
if __name__ == "__main__" :
     
    arr = [ 3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3]
    n = len(arr)
    x = 3
    y = 6
    print ("Minimum distance between %d and %d is %d\n"%( x, y,minDist(arr, n, x, y)));
 
# this code is contributed by aditya942003patil

Time complexity: linear
Space complexity: constant
# User function Template
def getMinDiff(arr, n, k):
    arr.sort()
    ans = arr[n - 1] - arr[0]  # Maximum possible height difference
  
    tempmin = arr[0]
    tempmax = arr[n - 1]
  
    for i in range(1, n):
        if arr[i] < k:
            continue
        tempmin = min(arr[0] + k, arr[i] - k)
  
        # Minimum element when we
        # add k to whole array
        # Maximum element when we
        tempmax = max(arr[i - 1] + k, arr[n - 1] - k)
  
        # subtract k from whole array
        ans = min(ans, tempmax - tempmin)
  
    return ans
  
  
# Driver Code Starts
k = 6
n = 6
arr = [7, 4, 8, 8, 8, 9]
ans = getMinDiff(arr, n, k)
print(ans)
  
# This code is contributed by ninja_hattori.

Time complexity: nlogn
Space complexity: constant
# Python3 program to find Minimum
# number of jumps to reach end
 
# Returns minimum number of jumps
# to reach arr[h] from arr[l]
 
 
def minJumps(arr, l, h):
 
    # Base case: when source and
    # destination are same
    if (h == l):
        return 0
 
    # when nothing is reachable
    # from the given source
    if (arr[l] == 0):
        return float('inf')
 
    # Traverse through all the points
    # reachable from arr[l]. Recursively
    # get the minimum number of jumps
    # needed to reach arr[h] from
    # these reachable points.
    min = float('inf')
    for i in range(l + 1, h + 1):
        if (i < l + arr[l] + 1):
            jumps = minJumps(arr, i, h)
            if (jumps != float('inf') and
                    jumps + 1 < min):
                min = jumps + 1
 
    return min
 
 
# Driver program to test above function
arr = [1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9]
n = len(arr)
print('Minimum number of jumps to reach',
      'end is', minJumps(arr, 0, n-1))
 
# This code is contributed by Soumen Ghosh

Time complexity: np
Space complexity: linear
# Python3 program to find Minimum
# number of jumps to reach end
 
# Returns minimum number of jumps
# to reach arr[n-1] from arr[0]
 
 
def minJumps(arr, n):
    jumps = [0 for i in range(n)]
 
    if (n == 0) or (arr[0] == 0):
        return float('inf')
 
    jumps[0] = 0
 
    # Find the minimum number of
    # jumps to reach arr[i] from
    # arr[0] and assign this
    # value to jumps[i]
    for i in range(1, n):
        jumps[i] = float('inf')
        for j in range(i):
            if (i <= j + arr[j]) and (jumps[j] != float('inf')):
                jumps[i] = min(jumps[i], jumps[j] + 1)
                break
    return jumps[n-1]
 
 
# Driver Program to test above function
arr = [1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9]
size = len(arr)
print('Minimum number of jumps to reach',
      'end is', minJumps(arr, size))
 
# This code is contributed by Soumen Ghosh

Time complexity: np
Space complexity: linear
# Python3 program to find Minimum
# number of jumps to reach end
 
# Returns Minimum number of
# jumps to reach end
 
 
def minJumps(arr, n):
 
    # jumps[0] will hold the result
    jumps = [0 for i in range(n)]
 
    # Minimum number of jumps needed
    # to reach last element from
    # last elements itself is always 0
    # jumps[n-1] is also initialized to 0
 
    # Start from the second element,
    # move from right to left and
    # construct the jumps[] array where
    # jumps[i] represents minimum number
    # of jumps needed to reach arr[m-1]
    # form arr[i]
    for i in range(n-2, -1, -1):
 
        # If arr[i] is 0 then arr[n-1]
        # can't be reached from here
        if (arr[i] == 0):
            jumps[i] = float('inf')
 
        # If we can directly reach to
        # the end point from here then
        # jumps[i] is 1
        elif (arr[i] >= n - i - 1):
            jumps[i] = 1
 
        # Otherwise, to find out the
        # minimum number of jumps
        # needed to reach arr[n-1],
        # check all the points
        # reachable from here and
        # jumps[] value for those points
        else:
            # initialize min value
            min = float('inf')
 
            # following loop checks with
            # all reachable points and
            # takes the minimum
            for j in range(i + 1, n):
                if (j <= arr[i] + i):
                    if (min > jumps[j]):
                        min = jumps[j]
 
            # Handle overflow
            if (min != float('inf')):
                jumps[i] = min + 1
            else:
                # or INT_MAX
                jumps[i] = min
 
    return jumps[0]
 
 
# Driver program to test above function
arr = [1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9]
n = len(arr)
print('Minimum number of jumps to reach',
      'end is', minJumps(arr, n-1))
 
# This code is contributed by Soumen Ghosh

Time complexity: quadratic
Space complexity: linear
# Dynamic Programming based Python
# implementation of Maximum Sum
# Increasing Subsequence (MSIS)
# problem
 
# maxSumIS() returns the maximum
# sum of increasing subsequence
# in arr[] of size n
def maxSumIS(arr, n):
    max = 0
    msis = [0 for x in range(n)]
 
    # Initialize msis values
    # for all indexes
    for i in range(n):
        msis[i] = arr[i]
 
    # Compute maximum sum
    # values in bottom up manner
    for i in range(1, n):
        for j in range(i):
            if (arr[i] > arr[j] and
                msis[i] < msis[j] + arr[i]):
                msis[i] = msis[j] + arr[i]
 
    # Pick maximum of
    # all msis values
    for i in range(n):
        if max < msis[i]:
            max = msis[i]
 
    return max
 
# Driver Code
arr = [1, 101, 2, 3, 100, 4, 5]
n = len(arr)
print("Sum of maximum sum increasing " +
                     "subsequence is " +
                  str(maxSumIS(arr, n)))
 
# This code is contributed
# by Bhavya Jain

Time complexity: quadratic
Space complexity: linear
# Python3 program to find Smallest
# subarray with sum greater
# than a given value
 
# Returns length of smallest subarray
# with sum greater than x. If there
# is no subarray with given sum,
# then returns n+1
def smallestSubWithSum(arr, n, x):
 
    # Initialize length of smallest
    # subarray as n+1
    min_len = n + 1
 
    # Pick every element as starting point
    for start in range(0,n):
     
        # Initialize sum starting
        # with current start
        curr_sum = arr[start]
 
        # If first element itself is greater
        if (curr_sum > x):
            return 1
 
        # Try different ending points
        # for curremt start
        for end in range(start+1,n):
         
            # add last element to current sum
            curr_sum += arr[end]
 
            # If sum becomes more than x
            # and length of this subarray
            # is smaller than current smallest
            # length, update the smallest
            # length (or result)
            if curr_sum > x and (end - start + 1) < min_len:
                min_len = (end - start + 1)
         
    return min_len;
 
 
# Driver program to test above function */
arr1 = [1, 4, 45, 6, 10, 19]
x = 51
n1 = len(arr1)
res1 = smallestSubWithSum(arr1, n1, x);
if res1 == n1+1:
    print("Not possible")
else:
    print(res1)
 
arr2 = [1, 10, 5, 2, 7]
n2 = len(arr2)
x = 9
res2 = smallestSubWithSum(arr2, n2, x);
if res2 == n2+1:
    print("Not possible")
else:
    print(res2)
 
arr3 = [1, 11, 100, 1, 0, 200, 3, 2, 1, 250]
n3 = len(arr3)
x = 280
res3 = smallestSubWithSum(arr3, n3, x)
if res3 == n3+1:
    print("Not possible")
else:
    print(res3)
     
# This code is contributed by Smitha Dinesh Semwal

Time complexity: quadratic
Space complexity: constant
# O(n) solution for finding smallest
# subarray with sum greater than x
 
# Returns length of smallest subarray
# with sum greater than x. If there
# is no subarray with given sum, then
# returns n + 1
 
 
def smallestSubWithSum(arr, n, x):
 
    # Initialize current sum and minimum length
    curr_sum = 0
    min_len = n + 1
 
    # Initialize starting and ending indexes
    start = 0
    end = 0
    while (end < n):
 
        # Keep adding array elements while current
        # sum is smaller than or equal to x
        while (curr_sum <= x and end < n):
            curr_sum += arr[end]
            end += 1
 
        # If current sum becomes greater than x.
        while (curr_sum > x and start < n):
 
            # Update minimum length if needed
            if (end - start < min_len):
                min_len = end - start
 
            # remove starting elements
            curr_sum -= arr[start]
            start += 1
 
    return min_len
 
 
# Driver program
arr1 = [1, 4, 45, 6, 10, 19]
x = 51
n1 = len(arr1)
res1 = smallestSubWithSum(arr1, n1, x)
print("Not possible") if (res1 == n1 + 1) else print(res1)
 
arr2 = [1, 10, 5, 2, 7]
n2 = len(arr2)
x = 9
res2 = smallestSubWithSum(arr2, n2, x)
print("Not possible") if (res2 == n2 + 1) else print(res2)
 
arr3 = [1, 11, 100, 1, 0, 200, 3, 2, 1, 250]
n3 = len(arr3)
x = 280
res3 = smallestSubWithSum(arr3, n3, x)
print("Not possible") if (res3 == n3 + 1) else print(res3)
 
# This code is contributed by
# Smitha Dinesh Semwal

Time complexity: linear
Space complexity: constant
# Python program to find maximum average subarray
# of given length.
 
# Returns beginning index of maximum average
# subarray of length 'k'
def findMaxAverage(arr, n, k):
    # Check if 'k' is valid
    if k > n:
        return -1
 
    # Create and fill array to store cumulative
    # sum. csum[i] stores sum of arr[0] to arr[i]
    csum = [0]*n
    csum[0] = arr[0]
    for i in range(1, n):
        csum[i] = csum[i-1] + arr[i];
 
    # Initialize max_sm as sum of first subarray
    max_sum = csum[k-1]
    max_end = k-1
 
    # Find sum of other subarrays and update
    # max_sum if required.
    for i in range(k, n):
     
        curr_sum = csum[i] - csum[i-k]
        if curr_sum > max_sum:
         
            max_sum = curr_sum
            max_end = i
         
    # Return starting index
    return max_end - k + 1
 
# Driver program
arr = [1, 12, -5, -6, 50, 3]
k = 4
n = len(arr)
print("The maximum average subarray of length",k,
"begins at index",findMaxAverage(arr, n, k))
 
#This code is contributed by
#Smitha Dinesh Semwal

Time complexity: linear
Space complexity: linear
# Python 3 program to find maximum
# average subarray of given length.
 
# Returns beginning index of maximum
# average subarray of length 'k'
def findMaxAverage(arr, n, k):
 
    # Check if 'k' is valid
    if (k > n):
        return -1
 
    # Compute sum of first 'k' elements
    sum = arr[0]
     
    for i in range(1, k):
        sum += arr[i]
 
    max_sum = sum
    max_end = k - 1
 
    # Compute sum of remaining subarrays
    for i in range(k, n):
     
        sum = sum + arr[i] - arr[i - k]
         
        if (sum > max_sum):
         
            max_sum = sum
            max_end = i
         
    # Return starting index
    return max_end - k + 1
 
# Driver program
arr = [1, 12, -5, -6, 50, 3]
k = 4
n = len(arr)
 
print("The maximum average subarray of length", k,
                                "begins at index",
                        findMaxAverage(arr, n, k))
 
# This code is contributed by
# Smitha Dinesh Semwal

Time complexity: linear
Space complexity: constant