giulio98/xlcost-single-prompt huggingface dataset:
# trapping rain water | function to return the maximum water that can be stored ; to store the maximum water that can be stored ; for every element of the array ; find the maximum element on its left ; find the maximum element on its right ; update the maximum water ; 
def maxWater(arr, n):
    res = 0
    for i in range(1, n - 1):
        left = arr[i]
        for j in range(i):
            left = max(left, arr[j])
        right = arr[i]
        for j in range(i + 1, n):
            right = max(right, arr[j])
        res = res + (min(left, right) - arr[i])
    return res


if __name__ == "__main__":
    arr = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
    n = len(arr)
    print(maxWater(arr, n))

# Expected output:
6
# trapping rain water | python program to find maximum amount of water that can be trapped within given set of bars . ; left [ i ] contains height of tallest bar to the left of i 'th bar including itself ; right [ i ] contains height of tallest bar to the right of ith bar including itself ; initialize result ; fill left array ; fill right array ; calculate the accumulated water element by element consider the amount of water on i 'th bar, the  amount of water accumulated on this particular  bar will be equal to min(left[i], right[i]) - arr[i] . ; 
def findWater(arr, n):
    left = [0] * n
    right = [0] * n
    water = 0
    left[0] = arr[0]
    for i in range(1, n):
        left[i] = max(left[i - 1], arr[i])
    right[n - 1] = arr[n - 1]
    for i in range(n - 2, -1, -1):
        right[i] = max(right[i + 1], arr[i])
    for i in range(0, n):
        water += min(left[i], right[i]) - arr[i]
    return water


arr = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
n = len(arr)
print("Maximum water that can be accumulated is", findWater(arr, n))

# Expected output:
Maximum water that can be accumulated is 6
# trapping rain water | python program to find maximum amount of water that can be trapped within given set of bars . space complexity : o ( 1 ) ; initialize output ; maximum element on left and right ; indices to traverse the array ; update max in left ; water on curr element = max - curr ; update right maximum ; 
def findWater(arr, n):
    result = 0
    left_max = 0
    right_max = 0
    lo = 0
    hi = n - 1
    while (lo <= hi):
        if (arr[lo] < arr[hi]):
            if (arr[lo] > left_max):
                left_max = arr[lo]
            else:
                result += left_max - arr[lo]
            lo += 1
        else:
            if (arr[hi] > right_max):
                right_max = arr[hi]
            else:
                result += right_max - arr[hi]
            hi -= 1
    return result


arr = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
n = len(arr)
print("Maximum water that can be accumulated is ", findWater(arr, n))

# Expected output:
Maximum water that can be accumulated is  6
# trapping rain water | function to return the maximum water that can be stored ; let the first element be stored as previous , we shall loop from index 1 ; to store previous wall 's index ; to store the water until a larger wall is found , if there are no larger walls then delete temp value from water ; if the current wall is taller than the previous wall then make current wall as the previous wall and its index as previous wall 's index  for the subsequent loops ; because larger or same height wall is found ; since current wall is shorter than the previous , we subtract previous wall ' s ▁ height ▁ from ▁ the ▁ current ▁ wall ' s height and add it to the water ; store the same value in temp as well if we dont find any larger wall then we will subtract temp from water ; if the last wall was larger than or equal to the previous wall then prev_index would be equal to size of the array ( last element ) if we didn 't find a wall greater than or equal  to the previous wall from the left then  prev_index must be less than the index  of the last element ; temp would ' ve ▁ stored ▁ the ▁ water ▁ collected ▁ ▁ from ▁ previous ▁ largest ▁ wall ▁ till ▁ the ▁ end ▁ ▁ of ▁ array ▁ if ▁ no ▁ larger ▁ wall ▁ was ▁ found ▁ then ▁ ▁ it ▁ has ▁ excess ▁ water ▁ and ▁ remove ▁ that ▁ ▁ from ▁ ' water ' var ; we start from the end of the array , so previous should be assigned to the last element ; loop from the end of array up to the ' previous ▁ index ' which would contain the " largest ▁ wall ▁ from ▁ the ▁ left " ; right end wall will be definitely smaller than the ' previous ▁ index ' wall ; return the maximum water ; 
def maxWater(arr, n):
    size = n - 1
    prev = arr[0]
    prev_index = 0
    water = 0
    temp = 0
    for i in range(1, size + 1):
        if (arr[i] >= prev):
            prev = arr[i]
            prev_index = i
            temp = 0
        else:
            water += prev - arr[i]
            temp += prev - arr[i]
    if (prev_index < size):
        water -= temp
        prev = arr[size]
        for i in range(size, prev_index - 1, -1):
            if (arr[i] >= prev):
                prev = arr[i]
            else:
                water += prev - arr[i]
    return water


arr = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
n = len(arr)
print(maxWater(arr, n))

# Expected output:
6
# trapping rain water | function to return the maximum water that can be stored ; stores the indices of the bars ; size of the array ; stores the final result ; loop through the each bar ; remove bars from the stack until the condition holds ; store the height of the top and pop it . ; if the stack does not have any bars or the the popped bar has no left boundary ; get the distance between the left and right boundary of popped bar ; calculate the min . height ; if the stack is either empty or height of the current bar is less than or equal to the top bar of stack ; 
def maxWater(height):
    stack = []
    n = len(height)
    ans = 0
    for i in range(n):
        while (len(stack) != 0 and (height[stack[-1]] < height[i])):
            pop_height = height[stack[-1]]
            stack .pop()
            if (len(stack) == 0):
                break
            distance = i - stack[-1] - 1
            min_height = min(height[stack[-1]], height[i]) - pop_height
            ans += distance * min_height
        stack .append(i)
    return ans


arr = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
print(maxWater(arr))

# Expected output:
6
# trapping rain water | function to return the maximum water that can be stored ; indices to traverse the array ; to store left max and right max for two pointers left and right ; to store the total amount of rain water trapped ; we need check for minimum of left and right max for each element ; add the difference between current value and right max at index r ; update right max ; update right pointer ; add the difference between current value and left max at index l ; update left max ; update left pointer ; 
def maxWater(arr, n):
    left = 0
    right = n - 1
    l_max = 0
    r_max = 0
    result = 0
    while (left <= right):
        if r_max <= l_max:
            result += max(0, r_max - arr[right])
            r_max = max(r_max, arr[right])
            right -= 1
        else:
            result += max(0, l_max - arr[left])
            l_max = max(l_max, arr[left])
            left += 1
    return result


arr = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
n = len(arr)
print(maxWater(arr, n))

# Expected output:
6
# median of two sorted arrays with different sizes in o ( log ( min ( n , m ) ) ) | python code for median with case of returning double value when even number of elements are present in both array combinely ; def to find max ; def to find minimum ; def to find median of two sorted arrays ; if i = n , it means that elements from a [ ] in the second half is an empty set . and if j = 0 , it means that elements from b [ ] in the first half is an empty set . so it is necessary to check that , because we compare elements from these two groups . searching on right ; if i = 0 , it means that elements from a [ ] in the first half is an empty set and if j = m , it means that elements from b [ ] in the second half is an empty set . so it is necessary to check that , because we compare elements from these two groups . searching on left ; we have found the desired halves . ; this condition happens when we don 't have any elements  in the first half from a[]  so we returning the last  element in b[] from the  first half. ; and this condition happens when we don 't have any  elements in the first half  from b[] so we returning the  last element in a[] from the  first half. ; calculating the median . if number of elements is odd there is one middle element . ; elements from a [ ] in the second half is an empty set . ; elements from b [ ] in the second half is an empty set . ; 

median = 0
i = 0
j = 0


def maximum(a, b):
    return a if a > b else b


def minimum(a, b):
    return a if a < b else b


def findMedianSortedArrays(a, n, b, m):
    global median, i, j
    min_index = 0
    max_index = n
    while (min_index <= max_index):
        i = int((min_index + max_index) / 2)
        j = int(((n + m + 1) / 2) - i)
        if (i < n and j > 0 and b[j - 1] > a[i]):
            min_index = i + 1
        elif (i > 0 and j < m and b[j] < a[i - 1]):
            max_index = i - 1
        else:
            if (i == 0):
                median = b[j - 1]
            elif (j == 0):
                median = a[i - 1]
            else:
                median = maximum(a[i - 1], b[j - 1])
            break
    if ((n + m) % 2 == 1):
        return median
    if (i == n):
        return ((median + b[j]) / 2.0)
    if (j == m):
        return ((median + a[i]) / 2.0)
    return ((median + minimum(a[i], b[j])) / 2.0)


a = [900]
b = [10, 13, 14]
n = len(a)
m = len(b)
if (n < m):
    print("The median is : {}".format(findMedianSortedArrays(a, n, b, m)))
else:
    echo("The median is : {}".format(findMedianSortedArrays(b, m, a, n)))

# Expected output:
The median is : 13.5
# median of two sorted arrays with different sizes in o ( log ( min ( n , m ) ) ) | function to find median of given two sorted arrays ; if i = n , it means that elements from a [ ] in the second half is an empty set . if j = 0 , it means that elements from b [ ] in the first half is an empty set . so it is necessary to check that , because we compare elements from these two groups . searching on right ; if i = 0 , it means that elements from a [ ] in the first half is an empty set and if j = m , it means that elements from b [ ] in the second half is an a [ ] in the first half is an empty set that , because we compare elements from these two groups . searching on left ; we have found the desired halves . ; this condition happens when we don 't have  any elements in the first half from a[] so  we returning the last element in b[] from  the first half. ; and this condition happens when we don 't  have any elements in the first half from  b[] so we returning the last element in  a[] from the first half. ; function to find maximum ; 
def maximum(a, b):
    return a if a > b else b


def findMedianSortedArrays(a, n, b, m):
    min_index = 0
    max_index = n
    while (min_index <= max_index):
        i = (min_index + max_index) // 2
        j = ((n + m + 1) // 2) - i
        if (i < n and j > 0 and b[j - 1] > a[i]):
            min_index = i + 1
        elif (i > 0 and j < m and b[j] < a[i - 1]):
            max_index = i - 1
        else:
            if (i == 0):
                return b[j - 1]
            if (j == 0):
                return a[i - 1]
            else:
                return maximum(a[i - 1], b[j - 1])
    print("ERROR!!! ", "returning")
    return 0


if __name__ == "__main__":
    a = [900]
    b = [10, 13, 14]
    n = len(a)
    m = len(b)
    if (n < m):
        print("The median is: ", findMedianSortedArrays(a, n, b, m))
    else:
        print("The median is: ", findMedianSortedArrays(b, m, a, n))

# Expected output:
The median is:  13
# print uncommon elements from two sorted arrays | python 3 program to find uncommon elements of two sorted arrays ; if not common , print smaller ; skip common element ; printing remaining elements ; 
def printUncommon(arr1, arr2, n1, n2):
    i = 0
    j = 0
    k = 0
    while (i < n1 and j < n2):
        if (arr1[i] < arr2[j]):
            print(arr1[i], end=" ")
            i = i + 1
            k = k + 1
        elif (arr2[j] < arr1[i]):
            print(arr2[j], end=" ")
            k = k + 1
            j = j + 1
        else:
            i = i + 1
            j = j + 1
    while (i < n1):
        print(arr1[i], end=" ")
        i = i + 1
        k = k + 1
    while (j < n2):
        print(arr2[j], end=" ")
        j = j + 1
        k = k + 1


arr1 = [10, 20, 30]
arr2 = [20, 25, 30, 40, 50]
n1 = len(arr1)
n2 = len(arr2)
printUncommon(arr1, arr2, n1, n2)

# Expected output:
10 25 40 50
# least frequent element in an array | python 3 program to find the least frequent element in an array . ; sort the array ; find the min frequency using linear traversal ; if last element is least frequent ; 
def leastFrequent(arr, n):
    arr .sort()
    min_count = n + 1
    res = -1
    curr_count = 1
    for i in range(1, n):
        if (arr[i] == arr[i - 1]):
            curr_count = curr_count + 1
        else:
            if (curr_count < min_count):
                min_count = curr_count
                res = arr[i - 1]
            curr_count = 1
    if (curr_count < min_count):
        min_count = curr_count
        res = arr[n - 1]
    return res


arr = [1, 3, 2, 1, 2, 2, 3, 1]
n = len(arr)
print(leastFrequent(arr, n))

# Expected output:
3
# least frequent element in an array | python3 program to find the most frequent element in an array . ; insert all elements in hash . ; find the max frequency ; 

import math as mt


def leastFrequent(arr, n):
    Hash = dict()
    for i in range(n):
        if arr[i] in Hash .keys():
            Hash[arr[i]] += 1
        else:
            Hash[arr[i]] = 1
    min_count = n + 1
    res = -1
    for i in Hash:
        if (min_count >= Hash[i]):
            res = i
            min_count = Hash[i]
    return res


arr = [1, 3, 2, 1, 2, 2, 3, 1]
n = len(arr)
print(leastFrequent(arr, n))

# Expected output:
3
# maximum sum of increasing order elements from n arrays | python3 program to find maximum sum by selecting a element from n arrays ; to calculate maximum sum by selecting element from each array ; sort each array ; store maximum element of last array ; selecting maximum element from previoulsy selected element ; j = - 1 means no element is found in a [ i ] so return 0 ; 

M = 4


def maximumSum(a, n):
    global M
    for i in range(0, n):
        a[i].sort()
    sum = a[n - 1][M - 1]
    prev = a[n - 1][M - 1]
    for i in range(n - 2, -1, -1):
        for j in range(M - 1, -1, -1):
            if (a[i][j] < prev):
                prev = a[i][j]
                sum += prev
                break
        if (j == -1):
            return 0
    return sum


arr = [[1, 7, 3, 4], [4, 2, 5, 1], [9, 5, 1, 8]]
n = len(arr)
print(maximumSum(arr, n))

# Expected output:
18
# maximum sum of increasing order elements from n arrays | python3 program to find maximum sum by selecting a element from n arrays ; to calculate maximum sum by selecting element from each array ; store maximum element of last array ; selecting maximum element from previoulsy selected element ; max_smaller equals to int_min means no element is found in a [ i ] so return 0 ; 

M = 4


def maximumSum(a, n):
    prev = max(max(a))
    Sum = prev
    for i in range(n - 2, -1, -1):
        max_smaller = -10 ** 9
        for j in range(M - 1, -1, -1):
            if (a[i][j] < prev and a[i][j] > max_smaller):
                max_smaller = a[i][j]
        if (max_smaller == -10 ** 9):
            return 0
        prev = max_smaller
        Sum += max_smaller
    return Sum


arr = [[1, 7, 3, 4], [4, 2, 5, 1], [9, 5, 1, 8]]
n = len(arr)
print(maximumSum(arr, n))

# Expected output:
18
# pairs such that one is a power multiple of other | program to find pairs count ; function to count the required pairs ; sort the given array ; for each a [ i ] traverse rest array ; count aj such that ai * k ^ x = aj ; increase x till ai * k ^ x <= largest element ; 

import math


def countPairs(A, n, k):
    ans = 0
    A .sort()
    for i in range(0, n):
        for j in range(i + 1, n):
            x = 0
            while ((A[i] * math .pow(k, x)) <= A[j]):
                if ((A[i] * math .pow(k, x)) == A[j]):
                    ans += 1
                    break
                x += 1
    return ans


A = [3, 8, 9, 12, 18, 4, 24, 2, 6]
n = len(A)
k = 3
print(countPairs(A, n, k))

# Expected output:
6
# minimum distance between two occurrences of maximum | function to return min distance ; case a ; case b ; case c ; 
def minDistance(arr, n):
    maximum_element = arr[0]
    min_dis = n
    index = 0
    for i in range(1, n):
        if (maximum_element == arr[i]):
            min_dis = min(min_dis, (i - index))
            index = i
        elif (maximum_element < arr[i]):
            maximum_element = arr[i]
            min_dis = n
            index = i
        else:
            continue
    return min_dis


arr = [6, 3, 1, 3, 6, 4, 6]
n = len(arr)
print("Minimum distance =", minDistance(arr, n))

# Expected output:
Minimum distance = 2
# find final value if we double after every successful search in array | function to find the value of k ; search for k . after every successful search , double k . ; 
def findValue(arr, n, k):
    for i in range(n):
        if (arr[i] == k):
            k = k * 2
    return k


arr = [2, 3, 4, 10, 8, 1]
k = 2
n = len(arr)
print(findValue(arr, n, k))

# Expected output:
16
# last duplicate element in a sorted array | python3 code to print last duplicate element and its index in a sorted array ; if array is null or size is less than equal to 0 return ; compare elements and return last duplicate and its index ; if we reach here , then no duplicate found . ; 
def dupLastIndex(arr, n):
    if (arr is None or n <= 0):
        return
    for i in range(n - 1, 0, -1):
        if (arr[i] == arr[i - 1]):
            print("Last index:", i, "Last",  "duplicateitem:", arr[i])
            return
    print("no duplicate found")


arr = [1, 5, 5, 6, 6, 7, 9]
n = len(arr)
dupLastIndex(arr, n)

# Expected output:
Last index: 4 Last duplicateitem: 6
# find an array element such that all elements are divisible by it | function to find smallest num ; traverse for all elements ; stores the minimum if it divides all ; 
def findSmallest(a, n):
    for i in range(0, n):
        for j in range(0, n):
            if ((a[j] % a[i]) >= 1):
                break
        if (j == n - 1):
            return a[i]
    return -1


a = [25, 20, 5, 10, 100]
n = len(a)
print(findSmallest(a, n))

# Expected output:
5
# find an array element such that all elements are divisible by it | function to find the smallest element ; function to find smallest num ; find the smallest element ; check if all array elements are divisible by smallest . ; 
def min_element(a):
    m = 10000000
    for i in range(0, len(a)):
        if (a[i] < m):
            m = a[i]
    return m


def findSmallest(a, n):
    smallest = min_element(a)
    for i in range(1, n):
        if (a[i] % smallest >= 1):
            return -1
    return smallest


a = [25, 20, 5, 10, 100]
n = len(a)
print(findSmallest(a, n))

# Expected output:
5
# maximum in array which is at | function to find the index of max element that satisfies the condition ; finding index of max of the array ; returns - 1 if the max element is not twice of the i - th element . ; 
def findIndex(arr):
    maxIndex = 0
    for i in range(0, len(arr)):
        if (arr[i] > arr[maxIndex]):
            maxIndex = i
    for i in range(0, len(arr)):
        if (maxIndex != i and arr[maxIndex] < (2 * arr[i])):
            return -1
    return maxIndex


arr = [3, 6, 1, 0]
print(findIndex(arr))

# Expected output:
1
# consecutive steps to roof top | python3 code to find maximum number of consecutive steps ; function to count consecutive steps ; count the number of consecutive increasing height building ; 

import math


def find_consecutive_steps(arr, len):
    count = 0
    maximum = 0
    for index in range(1, len):
        if (arr[index] > arr[index - 1]):
            count += 1
        else:
            maximum = max(maximum, count)
            count = 0
    return max(maximum, count)


arr = [1, 2, 3, 4]
len = len(arr)
print(find_consecutive_steps(arr, len))

# Expected output:
3
# maximum difference between groups of size two | python 3 program to find minimum difference between groups of highest and lowest ; sorting the whole array . ; 
def CalculateMax(arr, n):
    arr .sort()
    min_sum = arr[0] + arr[1]
    max_sum = arr[n - 1] + arr[n - 2]
    return abs(max_sum - min_sum)


arr = [6, 7, 1, 11]
n = len(arr)
print(CalculateMax(arr, n))

# Expected output:
11
# minimum difference between groups of size two | python3 program to find minimum difference between groups of highest and lowest sums . ; sorting the whole array . ; generating sum groups . ; 
def calculate(a, n):
    a .sort()
    s = []
    i = 0
    j = n - 1
    while (i < j):
        s .append((a[i] + a[j]))
        i += 1
        j -= 1
    mini = min(s)
    maxi = max(s)
    return abs(maxi - mini)


a = [2, 6, 4, 3]
n = len(a)
print(calculate(a, n))

# Expected output:
1
# closest numbers from a list of unsorted integers | returns minimum difference between any two pair in arr [ 0. . n - 1 ] ; sort array elements ; compare differences of adjacent pairs to find the minimum difference . ; traverse array again and print all pairs with difference as mindiff . ; 
def printMinDiffPairs(arr, n):
    if n <= 1:
        return
    arr .sort()
    minDiff = arr[1] - arr[0]
    for i in range(2, n):
        minDiff = min(minDiff, arr[i] - arr[i - 1])
    for i in range(1, n):
        if (arr[i] - arr[i - 1]) == minDiff:
            print("(" + str(arr[i - 1]) + ", " + str(arr[i]) + "), ", end='')


arr = [5, 3, 2, 4, 1]
n = len(arr)
printMinDiffPairs(arr, n)

# Expected output:
(1, 2), (2, 3), (3, 4), (4, 5),
# maximum absolute difference of value and index sums | brute force python 3 program to calculate the maximum absolute difference of an array . ; utility function to calculate the value of absolute difference for the pair ( i , j ) . ; function to return maximum absolute difference in brute force . ; variable for storing the maximum absolute distance throughout the traversal of loops . ; iterate through all pairs . ; if the absolute difference of current pair ( i , j ) is greater than the maximum difference calculated till now , update the value of result . ; 
def calculateDiff(i, j, arr):
    return abs(arr[i] - arr[j]) + abs(i - j)


def maxDistance(arr, n):
    result = 0
    for i in range(0, n):
        for j in range(i, n):
            if (calculateDiff(i, j, arr) > result):
                result = calculateDiff(i, j, arr)
    return result


arr = [-70, -64, -6, -56, 64, 61, -57, 16, 48, -98]
n = len(arr)
print(maxDistance(arr, n))

# Expected output:
167
# maximum absolute difference of value and index sums | function to return maximum absolute difference in linear time . ; max and min variables as described in algorithm . ; updating max and min variables as described in algorithm . ; calculating maximum absolute difference . ; 
def maxDistance(array):
    max1 = -2147483648
    min1 = +2147483647
    max2 = -2147483648
    min2 = +2147483647
    for i in range(len(array)):
        max1 = max(max1, array[i] + i)
        min1 = min(min1, array[i] + i)
        max2 = max(max2, array[i] - i)
        min2 = min(min2, array[i] - i)
    return max(max1 - min1, max2 - min2)


array = [-70, -64, -6, -56, 64, 61, -57, 16, 48, -98]
print(maxDistance(array))

# Expected output:
167
# number of local extrema in an array | function to find local extremum ; start loop from position 1 till n - 1 ; check if a [ i ] if greater than both its neighbours , then add 1 to x ; check if a [ i ] if less than both its neighbours , then add 1 to x ; 
def extrema(a, n):
    count = 0
    for i in range(1, n - 1):
        count += (a[i] > a[i - 1] and a[i] > a[i + 1])
        count += (a[i] < a[i - 1] and a[i] < a[i + 1])
    return count


a = [1, 0, 2, 1]
n = len(a)
print(extrema(a, n))

# Expected output:
2