giulio98/xlcost-single-prompt huggingface dataset:
# shuffle 2 n integers as a1 | function to reverse the array from the position ' start ' to position 'end ; stores mid of start and end ; traverse the array in the range [ start , end ] ; stores arr [ start + i ] ; update arr [ start + i ] ; update arr [ end - i ] ; utility function to shuffle the given array in the of form { a1 , b1 , a2 , b2 , ... . an , bn } ; stores the length of the array ; if length of the array is 2 ; stores mid of the { start , end } ; divide array into two halves of even length ; update mid ; calculate the mid - points of both halves of the array ; reverse the subarray made from mid1 to mid2 ; reverse the subarray made from mid1 to mid ; reverse the subarray made from mid to mid2 ; recursively calls for both the halves of the array ; function to shuffle the given array in the form of { a1 , b1 , a2 , b2 , ... . an , bn } ; function call ; print the modified array ; 
def reverse(arr, start, end):
    mid = (end - start + 1) // 2
    for i in range(mid):
        temp = arr[start + i]
        arr[start + i] = arr[end - i]
        arr[end - i] = temp
    return arr


def shuffleArrayUtil(arr, start, end):
    i = 0
    l = end - start + 1
    if (l == 2):
        return
    mid = start + l // 2
    if (l % 4):
        mid -= 1
    mid1 = start + (mid - start) // 2
    mid2 = mid + (end + 1 - mid) // 2
    arr = reverse(arr, mid1, mid2 - 1)
    arr = reverse(arr, mid1, mid - 1)
    arr = reverse(arr, mid, mid2 - 1)
    shuffleArrayUtil(arr, start, mid - 1)
    shuffleArrayUtil(arr, mid, end)


def shuffleArray(arr, N, start, end):
    shuffleArrayUtil(arr, start, end)
    for i in arr:
        print(i, end=" ")


if __name__ == '__main__':
    arr = [1, 3, 5, 2, 4, 6]
    N = len(arr)
    shuffleArray(arr, N, 0, N - 1)

# Expected output:
1 2 3 4 5 6
# reduce sum of same | function to check if elements of b can be rearranged such that a [ i ] + b [ i ] <= x ; checks the given condition ; sort a in ascending order ; sort b in descending order ; traverse the arrays a and b ; if a [ i ] + b [ i ] exceeds x ; rearrangement not possible , set flag to false ; if flag is true ; otherwise ; 
def rearrange(A, B, N, X):
    flag = True
    A = sorted(A)
    B = sorted(B)[::-1]
    for i in range(N):
        if (A[i] + B[i] > X):
            flag = False
            break
    if (flag):
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    A = [1, 2, 3]
    B = [1, 1, 2]
    X = 4
    N = len(A)
    rearrange(A, B, N, X)

# Expected output:
Yes
# minimum increments or decrements by d required to make all array elements equal | function to find minimum count of operations required to make all array elements equal by incrementing or decrementing array elements by d ; sort the array ; traverse the array ; if difference between two consecutive elements are not divisible by d ; store minimum count of operations required to make all array elements equal by incrementing or decrementing array elements by d ; stores middle element of the array ; traverse the array ; update count ; 
def numOperation(arr, N, D):
    arr .sort()
    for i in range(N - 1):
        if ((arr[i + 1] - arr[i]) % D != 0):
            print("-1")
            return
    count = 0
    mid = arr[N // 2]
    for i in range(N):
        count += abs(mid - arr[i]) // D
    print(count)


if __name__ == "__main__":
    N = 4
    D = 2
    arr = [2, 4, 6, 8]
    numOperation(arr, N, D)

# Expected output:
4
# maximize sum of second minimums of each k length partitions of the array | function to find the maximum sum of second smallest of each partition of size k ; sort the array a in ascending order ; store the maximum sum of second smallest of each partition of size k ; select every ( k - 1 ) th element as second smallest element ; update sum ; print the maximum sum ; 
def findSum(A, N, K):
    A .sort()
    sum = 0
    for i in range(N // K, N, K - 1):
        sum += A[i]
    print(sum)


if __name__ == '__main__':
    K = 4
    A = [2, 3, 1, 4, 7, 5, 6, 1]
    N = len(A)
    findSum(A, N, K)

# Expected output:
7
# minimize remaining array element by removing pairs and replacing them with their average | function to find the smallest element left in the array by removing pairs and inserting their average ; stores array elements such that the largest element present at top of pq ; traverse the array ; insert arr [ i ] into pq ; iterate over elements of pq while count of elements in pq greater than 1 ; stores largest element of pq ; pop the largest element of pq ; stores largest element of pq ; pop the largest element of pq ; insert the ceil value of average of top1 and top2 ; 
def findSmallestNumLeft(arr, N):
    PQ = []
    for i in range(N):
        PQ .append(arr[i])
    PQ = sorted(PQ)
    while (len(PQ) > 1):
        top1 = PQ[-1]
        del PQ[-1]
        top2 = PQ[-1]
        del PQ[-1]
        PQ .append((top1 + top2 + 1) // 2)
        PQ = sorted(PQ)
    return PQ[-1]


if __name__ == '__main__':
    arr = [30, 16, 40]
    N = len(arr)
    print(findSmallestNumLeft(arr, N))

# Expected output:
26
# find the array element from indices not divisible by k having largest composite product of digits | python3 program to implement the above approach ; function to check if a number is a composite number or not ; corner cases ; check if number is divisible by 2 or 3 ; check if number is a multiple of any other prime number ; function to calculate the product of digits of a number ; stores the product of digits ; extract digits of a number ; calculate product of digits ; function to check if the product of digits of a number is a composite number or not ; stores product of digits ; if product of digits is equal to 1 ; if product of digits is not prime ; function to find the number with largest composite product of digits from the indices not divisible by k from the given array ; traverse the array ; if index is divisible by k ; check if product of digits is a composite number or not ; sort the products ; 

from math import ceil, sqrt


def isComposite(n):
    if (n <= 1):
        return False
    if (n <= 3):
        return False
    if (n % 2 == 0 or n % 3 == 0):
        return True
    for i in range(5, ceil(sqrt(n)), 6):
        if (n % i == 0 or n % (i + 2) == 0):
            return True
    return False


def digitProduct(number):
    product = 1
    while (number > 0):
        product *= (number % 10)
        number //= 10
    return product


def compositedigitProduct(num):
    res = digitProduct(num)
    if (res == 1):
        return False
    if (isComposite(res)):
        return True
    return False


def largestCompositeDigitProduct(a, n, k):
    pq = []
    for i in range(n):
        if ((i % k) == 0):
            continue
        if (compositedigitProduct(a[i])):
            b = digitProduct(a[i])
            pq .append([b, a[i]])
    pq = sorted(pq)
    return pq[-1][1]


if __name__ == '__main__':
    arr = [233, 144, 89, 71, 13, 21, 11, 34, 55, 23]
    n = len(arr)
    k = 3
    ans = largestCompositeDigitProduct(arr, n, k)
    print(ans)

# Expected output:
89
# split array into k | function to find the minimum sum of 2 nd smallest elements of each subset ; sort the array ; stores minimum sum of second elements of each subset ; traverse first k 2 nd smallest elements ; update their sum ; prthe sum ; 
def findMinimum(arr, N, K):
    arr = sorted(arr)
    ans = 0
    for i in range(1, 2 * (N // K), 2):
        ans += arr[i]
    print(ans)


if __name__ == '__main__':
    arr = [11, 20, 5, 7, 8, 14, 2, 17, 16, 10]
    N = len(arr)
    K = 5
    findMinimum(arr, N, K)

# Expected output:
13
# find two non | function to check if two non - intersecting subarrays with equal sum exists or not ; sort the given array ; traverse the array ; check for duplicate elements ; if no duplicate element is present in the array ; 
def findSubarrays(arr, N):
    arr .sort()
    i = 0
    for i in range(N - 1):
        if (arr[i] == arr[i + 1]):
            print("YES")
            return
    print("NO")


if __name__ == '__main__':
    arr = [4, 3, 0, 1, 2, 0]
    N = len(arr)
    findSubarrays(arr, N)

# Expected output:
YES
# selection sort vs bubble sort | function for bubble sort ; iterate from 1 to n - 1 ; iterate from 0 to n - i - 1 ; 
def Bubble_Sort(arr, n):
    flag = True
    for i in range(1, n):
        flag = False
        for j in range(n - i):
            if (arr[j] > arr[j + 1]):
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
                flag = True
        if (flag == False):
            break


n = 5
arr = [2, 0, 1, 4, 3]
Bubble_Sort(arr, n)
print("The Sorted Array by using Bubble Sort is : ", end='')
for i in range(n):
    print(arr[i], end=" ")

# Expected output:
The Sorted Array by using Bubble Sort is : 0 1 2 3 4
# print all array elements appearing more than n / k times | function to prall array elements whose frequency is greater than n / k ; sort the array , arr [ ] ; traverse the array ; stores frequency of arr [ i ] ; traverse array elements which is equal to arr [ i ] ; update cnt ; update i ; if frequency of arr [ i ] is greater than ( n / k ) ; 
def NDivKWithFreq(arr, N, K):
    arr = sorted(arr)
    i = 0
    while i < N:
        cnt = 1
        while ((i + 1) < N and arr[i] == arr[i + 1]):
            cnt += 1
            i += 1
        if (cnt > (N // K)):
            print(arr[i], end=" ")
        i += 1


if __name__ == '__main__':
    arr = [1, 2, 2, 6, 6, 6, 6, 7, 10]
    N = len(arr)
    K = 4
    NDivKWithFreq(arr, N, K)

# Expected output:
6
# sort m elements of given circular array starting from index k | function to print the circular array ; print the array ; function to sort m elements of diven circular array starting from index k ; traverse m elements ; iterate from index k to k + m - 1 ; check if the next element in the circular array is less than the current element ; swap current element with the next element ; print the circular array ; 
def printCircularArray(arr, n):
    for i in range(n):
        print(arr[i], end=" ")


def sortCircularArray(arr, n, k, m):
    for i in range(m):
        for j in range(k, k + m - 1):
            if (arr[j % n] > arr[(j + 1) % n]):
                arr[j % n], arr[(j + 1) % n] = (arr[(j + 1) % n], arr[j % n])
    printCircularArray(arr, n)


if __name__ == "__main__":
    arr = [4, 1, 6, 5, 3]
    K = 2
    M = 3
    N = len(arr)
    sortCircularArray(arr, N, K, M)

# Expected output:
4 1 3 5 6
# minimize cost required to complete all processes | function to find minimum cost required to complete all the process ; sort the array on descending order of y ; stores length of array ; stores minimum cost required to complete all the process ; stores minimum cost to initiate any process ; traverse the array ; if mincostinit is less than the cost to initiate the process ; update mincost ; update mincostinit ; update mincostinit ; return mincost ; 
def minimumCostReqToCompthePrcess(arr):
    arr .sort(key=lambda x: x[0] - x[1])
    n = len(arr)
    minCost = 0
    minCostInit = 0
    for i in range(n):
        if arr[i][1] > minCostInit:
            minCost += (arr[i][1] - minCostInit)
            minCostInit = arr[i][1]
        minCostInit -= arr[i][0]
    return minCost


if __name__ == "__main__":
    arr = [[1, 2], [2, 4], [4, 8]]
    print(minimumCostReqToCompthePrcess(arr))

# Expected output:
8
# split array into equal length subsets with maximum sum of kth largest element of each subset | function to find the maximum sum of kth largest element of m equal length partition ; stores sum of k_th largest element of equal length partitions ; if n is not divisible by m ; stores length of each partition ; if k is greater than length of partition ; sort array in descending porder ; traverse the array ; update maxsum ; 
def maximumKthLargestsumPart(arr, N, M, K):
    maxSum = 0
    if (N % M != 0):
        return -1
    sz = (N / M)
    if (K > sz):
        return -1
    arr = sorted(arr)
    for i in range(0, N // 2):
        t = arr[i]
        arr[i] = arr[N - i - 1]
        arr[N - i - 1] = t
    for i in range(1, M + 1):
        maxSum += arr[i * K - 1]
    return maxSum


if __name__ == '__main__':
    arr = [1, 2, 3, 4, 5, 6]
    M = 2
    K = 1
    N = len(arr)
    print(maximumKthLargestsumPart(arr, N, M, K))

# Expected output:
11
# minimize difference between maximum and minimum array elements by exactly k removals | function to minimize the difference of the maximum and minimum array elements by removing k elements ; base condition ; sort the array ; initialize left and right pointers ; iterate for k times ; removing right element to reduce the difference ; removing the left element to reduce the difference ; print the minimum difference ; 
def minimumRange(arr, N, K):
    if (K >= N):
        print(0, end='')
        return
    arr .sort()
    left = 0
    right = N - 1
    for i in range(K):
        if (arr[right - 1] - arr[left] < arr[right] - arr[left + 1]):
            right -= 1
        else:
            left += 1
    print(arr[right] - arr[left], end='')


if __name__ == "__main__":
    arr = [5, 10, 12, 14, 21, 54, 61]
    N = len(arr)
    K = 4
    minimumRange(arr, N, K)

# Expected output:
4
# split array into k subsets to maximize sum of their second largest elements | function to split array into k subsets having maximum sum of their second maximum elements ; sort the array ; stores the maximum possible sum of second maximums ; add second maximum of current subset ; proceed to the second maximum of next subset ; print the maximum sum obtained ; 
def splitArray(arr, n, K):
    arr .sort()
    i = n - 1
    result = 0
    while (K > 0):
        result += arr[i - 1]
        i -= 2
        K -= 1
    print(result)


if __name__ == "__main__":
    arr = [1, 3, 1, 5, 1, 3]
    N = len(arr)
    K = 2
    splitArray(arr, N, K)

# Expected output:
4
# count subsequences for every array element in which they are the maximum | function to merge the subarrays arr [ l . . m ] and arr [ m + 1 , . . r ] based on indices [ ] ; if a [ indices [ l ] ] is less than a [ indices [ j ] ] , add indice [ l ] to temp ; else add indices [ j ] ; add remaining elements ; add remainging elements ; recursive function to divide the array into to parts ; recursive call for elements before mid ; recursive call for elements after mid ; merge the two sorted arrays ; function to find the number of subsequences for each element ; sorting the indices according to array arr [ ] ; array to store output numbers ; initialize subseq ; b [ i ] is 2 ^ i ; doubling the subsequences ; print the final output , array b [ ] ; 
def merge(indices, a, l, mid, r):
    temp_ind = [0] * (r - l + 1)
    j = mid + 1
    i = 0
    temp_l = l
    while (l <= mid and j <= r):
        if (a[indices[l]] < a[indices[j]]):
            temp_ind[i] = indices[l]
            i += 1
            l += 1
        else:
            temp_ind[i] = indices[j]
            i += 1
            j += 1
    while (l <= mid):
        temp_ind[i] = indices[l]
        i += 1
        l += 1
    while (j <= r):
        temp_ind[i] = indices[j]
        i += 1
        j += 1
    for k in range(i):
        indices[temp_l] = temp_ind[k]
        temp_l += 1


def divide(indices, a, l, r):
    if (l >= r):
        return
    mid = l // 2 + r // 2
    divide(indices, a, l, mid)
    divide(indices, a, mid + 1, r)
    merge(indices, a, l, mid, r)


def noOfSubsequences(arr, N):
    indices = N * [0]
    for i in range(N):
        indices[i] = i
    divide(indices, arr, 0, N - 1)
    B = [0] * N
    subseq = 1
    for i in range(N):
        B[indices[i]] = subseq
        subseq *= 2
    for i in range(N):
        print(B[i], end=" ")


if __name__ == "__main__":
    arr = [2, 3, 1]
    N = len(arr)
    noOfSubsequences(arr, N)

# Expected output:
2 4 1
# maximize cost to empty given array by repetitively removing k array elements | function to find the maximum cost to remove all array elements ; stores maximum cost to remove array elements ; sort array in ascending order ; traverse the array ; update maxcost ; 
def maxCostToRemove(arr, N, K):
    maxCost = 0
    arr = sorted(arr)
    for i in range(0, N, K):
        maxCost += arr[i + 1]
    return maxCost


if __name__ == '__main__':
    arr = [1, 3, 4, 1, 5, 1, 5, 3]
    N = len(arr)
    K = 4
    print(maxCostToRemove(arr, N, K))

# Expected output:
5
# print all numbers up to n in words in lexicographical order | function to convert a number to words ; stores the digits ; base cases ; stores strings of unit place ; stores strings for corner cases ; stores strings for ten 's place digits ; stores strings for powers of 10 ; if given number contains a single digit ; iterate over all the digits ; represent first 2 digits in words ; represent last 2 digits in words ; explicitly handle corner cases [ 10 , 19 ] ; explicitly handle corner case 20 ; for rest of the two digit numbers i . e . , 21 to 99 ; function to print all the numbers up to n in lexicographical order ; convert all numbers in words ; sort all strings ; print answer ; 
def convert_to_words(n):
    num = str(n)
    length = len(num)
    ans = ""
    if (length == 0):
        ans += "Empty String"
        return ans
    single_digits = [
        "zero",
        "one",
        "two",
        "three",
        "four",
        "five",
        "six",
        "seven",
        "eight",
        "nine"]
    two_digits = [
        "",
        "ten",
        "eleven",
        "twelve",
        "thirteen",
        "fourteen",
        "fifteen",
        "sixteen",
        "seventeen",
        "eighteen",
        "nineteen"]
    tens_multiple = [
        "",
        "",
        "twenty",
        "thirty",
        "forty",
        "fifty",
        "sixty",
        "seventy",
        "eighty",
        "ninety"]
    tens_power = ["hundred", "thousand"]
    if (length == 1):
        ans += single_digits[ord(num[0]) - ord('0')]
        return ans
    x = 0
    while (x < len(num)):
        if (length >= 3):
            if (num[x] - '0' != 0):
                ans += single_digits[ord(num[x]) - ord('0')]
                ans += " "
                ans += tens_power[len - 3]
                ans += " "
            length -= 1
        else:
            if (ord(num[x]) - ord('0') == 1):
                sum = (ord(num[x]) - ord('0') + ord(num[x]) - ord('0'))
                ans += two_digits[sum]
                return ans
            elif (ord(num[x]) - ord('0') == 2 and ord(num[x + 1]) - ord('0') == 0):
                ans += "twenty"
                return ans
            else:
                i = (ord(num[x]) - ord('0'))
                if (i > 0):
                    ans += tens_multiple[i]
                    ans += " "
                else:
                    ans += ""
                x += 1
                if (ord(num[x]) - ord('0') != 0):
                    ans += single_digits[ord(num[x]) - ord('0')]
        x += 1
    return ""


def lexNumbers(n):
    s = []
    for i in range(1, n + 1):
        s .append(convert_to_words(i))
    s .sort()
    ans = []
    for i in range(n):
        ans .append(s[i])
    for i in range(n - 1):
        print(ans[i], end=", ")
    print(ans[n - 1], end="")


if __name__ == "__main__":
    n = 5
    lexNumbers(n)

# Expected output:
five, four, one, three, two
# minimize cost to convert all array elements to 0 | python3 program for the above approach ; function to calculate the minimum cost of converting all array elements to 0 s ; stores subarrays of 0 s only ; traverse the array ; if consecutive 0 s occur ; increment if needed ; push the current length of consecutive 0 s in a vector ; update lengths as 0 ; sorting vector ; stores the number of subarrays consisting of 1 s ; traverse the array ; if current element is 1 ; otherwise ; increment count of consecutive ones ; stores the minimum cost ; traverse the array ; first element ; traverse the subarray sizes ; update cost ; cost of performing x and y operations ; find the minimum cost ; print the minimum cost ; 

import sys


def minimumCost(binary, n, a, b):
    groupOfZeros = []
    length = 0
    i = 0
    increment_need = True
    while (i < n):
        increment_need = True
        while (i < n and binary[i] == 0):
            length += 1
            i += 1
            increment_need = False
        if (increment_need):
            i += 1
        if (length != 0):
            groupOfZeros .append(length)
        length = 0
    groupOfZeros .sort()
    i = 0
    found_ones = False
    NumOfOnes = 0
    while (i < n):
        found_ones = False
        while (i < n and binary[i] == 1):
            i += 1
            found_ones = True
        if (found_ones == False):
            i += 1
        else:
            NumOfOnes += 1
    ans = sys .maxsize
    for i in range(n):
        curr = 0
        totalOnes = NumOfOnes
        if (i == 0):
            curr = totalOnes * a
        else:
            mark = i
            num_of_changes = 0
            for x in groupOfZeros:
                if (mark >= x):
                    totalOnes -= 1
                    mark -= x
                    num_of_changes += x
                else:
                    break
            curr = ((num_of_changes * b) + (totalOnes * a))
        ans = min(ans, curr)
    print(ans)


if __name__ == "__main__":
    arr = [1, 1, 1, 0, 1, 1]
    N = len(arr)
    X = 10
    Y = 4
    minimumCost(arr, N, X, Y)

# Expected output:
14
# reduce array to longest sorted array possible by removing either half of given array in each operation | function to check if the subarray arr [ i . . j ] is a sorted subarray or not ; traverse all elements of the subarray arr [ i ... j ] ; if the previous element of the subarray exceeds current element ; return 0 ; return 1 ; recursively partition the array into two equal halves ; if atmost one element is left in the subarray arr [ i . . j ] ; checks if subarray arr [ i . . j ] is a sorted subarray or not ; if the subarray arr [ i ... j ] is a sorted subarray ; stores middle element of the subarray arr [ i . . j ] ; recursively partition the current subarray arr [ i . . j ] into equal halves ; 
def isSortedparitions(arr, i, j):
    for k in range(i + 1, j + 1):
        if (arr[k] < arr[k - 1]):
            return 0
    return 1


def partitionsArr(arr, i, j):
    if (i >= j):
        return 1
    flag = int(isSortedparitions(arr, i, j))
    if (flag != 0):
        return (j - i + 1)
    mid = (i + j) // 2
    X = partitionsArr(arr, i, mid)
    Y = partitionsArr(arr, mid + 1, j)
    return max(X, Y)


if __name__ == '__main__':
    arr = [11, 12, 1, 2, 13, 14, 3, 4]
    N = len(arr)
    print(partitionsArr(arr, 0, N - 1))

# Expected output:
2
# check if two sorted arrays can be merged to form a sorted array with no adjacent pair from the same array | function to check if it is possible to merge the two given arrays with given conditions ; stores index of the array a [ ] ; stores index of the array b [ ] ; check if the previous element are from the array a [ ] or from the array b [ ] ; check if it is possible to merge the two given sorted arrays with given conditions ; traverse both the arrays ; if a [ i ] is less than b [ j ] and previous element are not from a [ ] ; update prev ; update i ; if b [ j ] is less than a [ i ] and previous element are not from b [ ] ; update prev ; update j ; if a [ i ] equal to b [ j ] ; if previous element are not from b [ ] ; update prev ; update j ; if previous element is not from a [ ] ; update prev ; update i ; if it is not possible to merge two sorted arrays with given conditions ; update flag ; 
def checkIfPossibleMerge(A, B, N):
    i = 0
    j = 0
    prev = -1
    flag = 1
    while (i < N and j < N):
        if (A[i] < B[j] and prev != 0):
            prev = 0
            i += 1
        elif (B[j] < A[i] and prev != 1):
            prev = 1
            j += 1
        elif (A[i] == B[j]):
            if (prev != 1):
                prev = 1
                j += 1
            else:
                prev = 0
                i += 1
        else:
            flag = 0
            break
    return flag


if __name__ == '__main__':
    A = [3, 5, 8]
    B = [2, 4, 6]
    N = len(A)
    if (checkIfPossibleMerge(A, B, N)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes