giulio98/xlcost-single-prompt huggingface dataset:
# find smallest number with given digits and sum of digits | function to print minimum integer having only digits p and q and the sum of digits as n ; if q is greater that p then swap the values of p and q ; if p and q are both zero or if q is zero and n is not divisible by p then there is no possible integer which satisfies the given conditions ; loop to find the maximum value of count_p that also satisfy p * count_p + q * count_q = n ; if n is 0 , their is a valid integer possible that satisfies all the requires conditions ; print answer ; 
def printMinInteger(P, Q, N):
    if (Q > P):
        t = P
        P = Q
        Q = t
    if (Q == 0 and (P == 0 or N % P != 0)):
        print("Not Possible")
        return
    count_P = 0
    count_Q = 0
    while (N > 0):
        if (N % P == 0):
            count_P += N / P
            N = 0
        else:
            N = N - Q
            count_Q += 1
    if (N == 0):
        for i in range(count_Q):
            print(Q, end="")
        for i in range(int(count_P)):
            print(P, end="")
    else:
        print("Not Possible")


N = 32
P = 7
Q = 4
printMinInteger(P, Q, N)

# Expected output:
47777
# minimum number of sum and modulo operations using given numbers to reach target | function to find the minimum moves to reach k from n ; initialization of dp vector ; dp [ i ] = minimum pushes required to reach i ; traversing through the buttons ; iterating through all the positions ; if not visited ; next status of lock ; advance to next state ; return the final dp [ target ] ; 
def minPushes(N, K, arr):
    dp = [-1] * 100000
    dp[N] = 0
    for i in range(len(arr)):
        for xx in range(100000):
            x = xx
            if (dp[x] == -1):
                continue
            next = (x + arr[i]) % 100000
            while (dp[next] == -1 or dp[next] > dp[x] + 1):
                dp[next] = dp[x] + 1
                x = next
                next = (next + arr[i]) % 100000
    return dp[K]


N = 99880
K = 89
arr = [100, 3]
print(minPushes(N, K, arr))

# Expected output:
5
# minimum number of apples to be collected from trees to guarantee m red apples | function to minimum no . of apples ; if we get all required apple from south ; if we required trees at east and west ; if we doesn 't have enough  red apples ; 
def minApples():
    if M <= S * K:
        return M
    elif M <= S * K + E + W:
        return S * K + (M - S * K) * K
    else:
        return -1


if __name__ == "__main__":
    M = 10
    K = 15
    N = 0
    S = 1
    W = 0
    E = 0
    ans = minApples()
    print(ans)

# Expected output:
10
# minimum increments or decrements required to signs of prefix sum array elements alternating | function to find the minimum number of increments / decrements of array elements by 1 to make signs of prefix sum array elements alternating ; case 1. neg - pos - neg ; stores the current sign of the prefix sum of array ; stores minimum number of operations for case 1 ; traverse the array ; checking both conditions ; update the current prefix1 to currentprefixsum ; case 2. pos - neg - pos ; stores the prefix sum of array ; stores minimum number of operations for case 1 ; checking both conditions ; update the current prefix2 to currentprefixsum ; 
def minimumOperations(A, N):
    cur_prefix_1 = 0
    parity = -1
    minOperationsCase1 = 0
    for i in range(N):
        cur_prefix_1 += A[i]
        if (cur_prefix_1 == 0 or parity * cur_prefix_1 < 0):
            minOperationsCase1 += abs(parity - cur_prefix_1)
            cur_prefix_1 = parity
        parity *= -1
    cur_prefix_2 = 0
    parity = 1
    minOperationsCase2 = 0
    for i in range(N):
        cur_prefix_2 += A[i]
        if (cur_prefix_2 == 0 or parity * cur_prefix_2 < 0):
            minOperationsCase2 += abs(parity - cur_prefix_2)
            cur_prefix_2 = parity
        parity *= -1
    return min(minOperationsCase1, minOperationsCase2)


A = [1, -3, 1, 0]
N = len(A)
print(minimumOperations(A, N))

# Expected output:
4
# generate an array of maximum sum such that each element exceeds all elements present either on its left or right | function to construct the array having maximum sum satisfying the given criteria ; declaration of the array arra [ ] and ans [ ] ; stores the maximum sum of the resultant array ; initialize the array arra [ ] ; traversing the array arra [ ] ; initialize the array arrb [ ] ; assign the maximum element to the current element ; form the first increasing till every index i ; make the current element as the maximum element ; forming decreasing from the index i + 1 to the index n ; initialize the sum ; find the total sum ; check if the total sum is at least the sum found then make ans as ansb ; print the final array formed ; 
def maximumSumArray(arr, N):
    arrA = [0] * N
    ans = [0] * N
    maxSum = 0
    for i in range(N):
        arrA[i] = arr[i]
    for i in range(N):
        arrB = [0] * N
        maximum = arrA[i]
        arrB[i] = maximum
        temp = 0
        for j in range(i - 1, -1, -1):
            arrB[j] = min(maximum, arrA[j])
            maximum = arrB[j]
            temp = j
        maximum = arrA[i]
        for j in range(i + 1, N):
            arrB[j] = min(maximum, arrA[j])
            maximum = arrB[j]
        sum = 0
        for j in range(N):
            sum += arrB[j]
        if (sum > maxSum):
            maxSum = sum
            ans = arrB
    for val in ans:
        print(val)


A = [10, 6, 8]
N = len(A)
maximumSumArray(A, N)

# Expected output:
10
6
6
# minimize product of two scores possible by at most m reductions | utility function to find the minimum product of r1 and r2 possible ; reaching to its limit ; if m is remaining ; function to find the minimum product of r1 and r2 ; case 1 - r1 reduces first ; case 2 - r2 reduces first ; 
def minProductUtil(R1, B1, R2, B2, M):
    x = min(R1 - B1, M)
    M -= x
    R1 -= x
    if M > 0:
        y = min(R2 - B2, M)
        M -= y
        R2 -= y
    return R1 * R2


def minProduct(R1, B1, R2, B2, M):
    res1 = minProductUtil(R1, B1, R2, B2, M)
    res2 = minProductUtil(R2, B2, R1, B1, M)
    return min(res1, res2)


if __name__ == '__main__':
    R1 = 21
    B1 = 10
    R2 = 13
    B2 = 11
    M = 3
    print(minProduct(R1, B1, R2, B2, M))

# Expected output:
220
# maximize the profit after selling the tickets | set 2 ( for elements in range [ 1 , 10 ^ 6 ] ) | function to find maximum profit after selling k tickets ; frequency array to store freq of every element of the array ; modify the arr [ ] so that the array is sorted in o ( n ) ; variable to store answer ; traverse the array while k > 0 and j >= 0 ; if arr [ i ] > arr [ j ] then ticket can be brought from counter [ j + 1 , n ] ; if arr [ j ] = = arr [ i ] decrement j until arr [ j ] != arr [ i ] ; sell tickets from counter [ j + 1 , n ] ; all elements of array are equal send tickets from each counter 1 time until k > 0. ; converting answer from long long to int ; 
def maxAmount(n, k, arr):
    A = [0 for i in range(1000001)]
    for i in range(n):
        A[arr[i]] += 1
    j = 0
    for j in range(1000001):
        while (A[i] != 0):
            arr[j] = i
            j += 1
            A[i] -= 1
    ans = 6
    mod = 1000000007
    i = n - 1
    j = n - 2
    while (k > 0 and j >= 0):
        if (arr[i] > arr[j]):
            ans = ans + min(k, (i - j)) * arr[i]
            k = k - (i - j)
            arr[i] -= 1
        else:
            while (j >= 0 and arr[j] == arr[i]):
                j -= 1
            if (j < 0):
                break
            ans = ans + min(k, (i - j)) * arr[i]
            k = k - (i - j)
            arr[i] -= 1
    while (k > 0 and arr[i] != 0):
        ans = ans + min(n, k) * arr[i]
        k -= n
        arr[i] -= 1
    ans = ans % mod
    x = ans
    return x


if __name__ == '__main__':
    n = 5
    k = 3
    arr = [4, 3, 6, 2, 4]
    ans = maxAmount(n, k, arr)
    print(ans)

# Expected output:
15
# maximize sum of averages of subsequences of lengths lying in a given range | function to find the maximum sum of average of groups ; sort the given array ; stores the sum of averages ; stores count of array element ; add the current value to the variable sum ; increment the count by 1 ; if the current size is x ; if the remaining elements can 't become a group ; iterate until i is less than n ; update the value of x ; update the average ; find the average ; reset the sum and count ; print maximum sum of averages ; 
def maxAverage(A, N, X, Y):
    A .sort()
    sum = 0
    res = 0
    count = 0
    for i in range(N):
        sum += A[i]
        count += 1
        if (count == X):
            if (N - i - 1 < X):
                i += 1
                cnt = 0
                while (i < N):
                    cnt += 1
                    sum += A[i]
                    i += 1
                X = X + cnt
                res += sum / X
                break
            res += sum / X
            sum = 0
            count = 0
    print(res)


if __name__ == '__main__':
    A = [4, 10, 6, 5]
    N = len(A)
    X = 2
    Y = 3
    maxAverage(A, N, X, Y)

# Expected output:
12.5
# rearrange characters in a sorted string such that no pair of adjacent characters are the same | python 3 program for the above approach ; function to check if a string s contains pair of adjacent characters that are equal or not ; traverse the string s ; if s [ i ] and s [ i + 1 ] are equal ; otherwise , return false ; function to rearrange characters of a string such that no pair of adjacent characters are the same ; initialize 3 variables ; iterate until k < n ; if s [ i ] is not equal to s [ j ] ; increment i and j by 1 ; if j equals k and increment the value of k by 1 ; else ; if s [ j ] equals s [ k ] ; increment k by 1 ; else ; swap ; increment i and j by 1 ; if j equals k ; increment k by 1 ; function to rearrange characters in a string so that no two adjacent characters are same ; if string is already valid ; if size of the string is 2 ; function call ; reversing the string ; function call ; if the string is valid ; otherwise ; 

S = "aaabc"


def isAdjChar(s):
    for i in range(len(s) - 1):
        if (s[i] == s[i + 1]):
            return True
    return False


def rearrangeStringUtil(N):
    global S
    S = list(S)
    i = 0
    j = 1
    k = 2
    while (k < N):
        if (S[i] != S[j]):
            i += 1
            j += 1
            if (j == k):
                k += 1
        else:
            if (S[j] == S[k]):
                k += 1
            else:
                temp = S[k]
                S[k] = S[j]
                S[j] = temp
                i += 1
                j += 1
                if (j == k):
                    k += 1
    S = ''.join(S)


def rearrangeString(N):
    global S
    if (isAdjChar(S) == False):
        return S
    if (len(S) == 2):
        return "-1"
    rearrangeStringUtil(N)
    S = S[::-1]
    rearrangeStringUtil(N)
    if (isAdjChar(S) == False):
        return S
    return "-1"


if __name__ == '__main__':
    N = len(S)
    print(rearrangeString(N))

# Expected output:
acaba
# lexicographically largest string possible by repeatedly appending first character of two given strings | function to make the lexicographically largest string by merging two strings ; stores the resultant string ; if the string word1 is lexographically greater than or equal to word2 ; update the string merge ; erase the first index of the string word1 ; otherwise ; update the string merge ; erase the first index of the string word2 ; return the final string ; 
def largestMerge(word1, word2):
    merge = ""
    while len(word1) != 0 or len(word2) != 0:
        if word1 >= word2:
            merge = merge + word1[0]
            word1 = word1[1:]
        else:
            merge = merge + word2[0]
            word2 = word2[1:]
    return merge


S1 = "xyzxyz"
S2 = "xywzxyx"
print(largestMerge(S1, S2))

# Expected output:
xyzxyzxywzxyx
# maximum rods to put horizontally such that no two rods overlap on x coordinate | python 3 program for the above approach ; function to find the maximum number of rods that can be put horizontally ; stores the result ; stores the last occupied point ; traverse the array arr [ ] ; if the current point can be put on the left side ; increment the ans by 1 ; update prev ; else if the given point can be put on the right side ; increment the ans by 1 ; update prev ; otherwise , ; update prev ; return the ans ; 

import sys


def findMaximumPoints(N, X, H):
    ans = 0
    prev = -sys .maxsize - 1
    for i in range(N):
        if (prev < (X[i] - H[i])):
            ans += 1
            prev = X[i]
        elif (i == N - 1 or (X[i] + H[i]) < X[i + 1]):
            ans += 1
            prev = X[i] + H[i]
        else:
            prev = X[i]
    return ans


if __name__ == '__main__':
    X = [1, 2, 3]
    H = [2, 5, 5]
    N = len(X)
    print(findMaximumPoints(N, X, H))

# Expected output:
2
# maximize the missing values in given time in hh : mm format | function to find the maximum time by replacing ' ? ' by any digits ; if the 0 th index is '? ; if the 1 st index is '? ; if the 3 rd index is '? ; if the 4 th index is '? ; return new string ; 
def maxTime(s):
    s = list(s)

    if (s[0] == '?'):
        if (s[1] <= '3' or s[1] == '?'):
            s[0] = '2'
        else:
            s[0] = '1'
    if (s[1] == '?'):
        if (s[0] != '2'):
            s[1] = 9
        else:
            s[1] = 3
    if (s[3] == '?'):
        s[3] = '5'
    if (s[4] == '?'):
        s[4] = '9'
    print("".join(s))


S = "?4:5?"
maxTime(S)

# Expected output:
14:59
# maximum gcd of two numbers possible by adding same value to them | function to calculate maximum gcd of two numbers possible by adding same value to both a and b ; 
def maxGcd(a, b):
    print(abs(a - b))


a = 2231
b = 343
maxGcd(a, b)

# Expected output:
1888
# count the combination of 4 s and / or 5 s required to make each array element 0 | function to print the count of the combination of 4 or 5 required to make the arr [ i ] for each 0 < i < n ; vector to store the answer ; iterate in the range [ 0 , n - 1 ] ; initialize sum to store the count of numbers and cnt for the current factor of 4 ; iterate in the range [ 0 , arr [ i ] ] with increment of 4 ; check if arr [ i ] - j ( the current factor of 4 ) is divisible by 5 or not ; if sum is not maximum then answer is found ; finally , print the required answer ; 
def sumOfCombinationOf4OR5(arr, N):
    ans = [-1 for i in range(N)]
    for i in range(N):
        if (arr[i] < 4):
            continue
        sum = 10 ** 9
        cnt = 0
        for j in range(0, arr[i] + 1, 4):
            if ((arr[i] - j) % 5 == 0):
                sum = min(sum, cnt + (arr[i] - j) // 5)
            cnt += 1
        if (sum != 10 ** 9):
            ans[i] = sum
    for num in ans:
        print(num, end=" ")


arr = [7, 15, 17, 22]
N = len(arr)
sumOfCombinationOf4OR5(arr, N)

# Expected output:
-1 3 4 5
# find an n | function to find an n - length binary string having maximum sum of elements from all given ranges ; iterate over the range [ 1 , n ] ; if i is odd , then print 0 ; otherwise , print 1 ; 
def printBinaryString(arr, N):
    for i in range(1, N + 1):
        if (i % 2):
            print(0, end="")
        else:
            print(1, end="")


N = 5
M = 3
arr = [[1, 3], [2, 4], [2, 5]]
printBinaryString(arr, N)

# Expected output:
01010
# maximize 0 s in given array after replacing each element a [ i ] with ( a [ i ] * d + b [ i ] ) | python program for the above approach ; function to find the maximum number of 0 s in the array a [ ] after changing the array element to a [ i ] * d + b [ i ] ; stores the frequency of fractions needed to make each element 0 ; stores the maximum number of 0 ; traverse the array ; find the numerator and the denominator ; check if den is not equal to 0 ; divide num and den by their gcd ; check if num is not greater than 0 ; check if both num and den are equal to 0 ; increment the value of { num , den } in the map ; update the value of ans ; prthe value of ans + cnt ; 

from math import gcd


def maxZeroes(A, B):
    mp = {}
    N = len(A)
    ans = 0
    cnt = 0
    for i in range(N):
        num = -B[i]
        den = A[i]
        gc = gcd(num, den)
        if (den != 0):
            num //= gc
            den //= gc
        if (num <= 0):
            num *= -1
            den *= -1
        if (den == 0 and num == 0):
            cnt += 1
        if (den != 0):
            mp[(num, den)] = mp .get((num, den), 0) + 1
            ans = max(mp[(num, den)], ans)
    print(ans + cnt)


if __name__ == '__main__':
    A = [1, 2, -1]
    B = [-6, -12, 6]
    maxZeroes(A, B)

# Expected output:
3
# minimum cost to complete given tasks if cost of 1 , 7 and 30 days are given | function to find the minimum cost to hire the workers for the given days in the array days [ ] ; initialize the array dp ; minimum cost for nth day ; poleter of the array arr [ ] ; traverse from right to left ; if worker is hired for 1 day ; if worker is hired for 7 days ; if worker is hired for 30 days ; update the value of dp [ i ] as minimum of 3 options ; if the day is not at the array arr [ ] ; return the answer ; 
def MinCost(days, cost, N):
    size = days[N - 1] + 1
    dp = [0 for i in range(size)]
    dp[size - 1] = min(cost[0], min(cost[1], cost[2]))
    ptr = N - 2
    for i in range(size - 2, 0, -1):
        if (ptr >= 0 and days[ptr] == i):
            val1 = dp[i + 1] + cost[0]
            val2 = cost[1] + (0 if (i + 7 >= size)else dp[i + 7])
            val3 = cost[2] + (0 if (i + 30 >= size)else dp[i + 30])
            dp[i] = min(val1, min(val2, val3))
            ptr -= 1
        else:
            dp[i] = dp[i + 1]
    return dp[1]


arr = [2, 4, 6, 7, 8, 10, 17]
cost = [3, 8, 20]
N = len(arr)
print(MinCost(arr, cost, N))

# Expected output:
14
# maximum pairs of bracket sequences which can be concatenated to form a regular bracket sequence | function to count the number of pairs whose concatenation results in the regular bracket sequence ; stores the count of opening and closing parenthesis for each string arr [ i ] ; stores maximum count of pairs ; traverse the array arr [ ] ; traverse the string c [ ] ; opening bracket ; otherwise , closing bracket ; count of closing brackets needed to balance string ; count of opening brackets needed to balance string ; add the count of balanced sequences ; traverse the array ; print the resultant count ; 
def countPairs(N, arr):
    open = [0] * 100
    close = [0] * 100
    ans = 0
    for i in range(N):
        c = [i for i in arr[i]]
        d = 0
        minm = 0
        for j in range(len(c)):
            if (c[j] == '('):
                d += 1
            else:
                d -= 1
                if (d < minm):
                    minm = d
        if (d >= 0):
            if (minm == 0):
                close[d] += 1
        elif (d < 0):
            if (minm == d):
                open[-d] += 1
    ans += close[0] // 2
    for i in range(1, 100):
        ans += min(open[i], close[i])
    print(ans)


if __name__ == '__main__':
    N = 7
    list = []
    list .append(")())")
    list .append(")")
    list .append("((")
    list .append("((")
    list .append("(")
    list .append(")")
    list .append(")")
    countPairs(N, list)

# Expected output:
2
# minimum count of elements to be inserted in array to form all values in [ 1 , k ] using subset sum | function to find the count of minimum elements to be inserted to form every number in a range ; stores the count of numbers needed ; stores the numbers upto which every numbers can be formed ; stores the index of the array arr [ ] ; iterate until requiredsum is less than or equal to k ; if i is less than n and requiredsum is greater than or equal to arr [ i ] ; increment requiredsum by arr [ i ] ; increment i by 1 ; otherwise ; increment count by 1 ; increment requiredsum by requiredsum ; return result ; 
def minElements(arr, N, K):
    count = 0
    requiredNum = 1
    i = 0
    while (requiredNum <= K):
        if (i < N and requiredNum >= arr[i]):
            requiredNum += arr[i]
            i += 1
        else:
            count += 1
            requiredNum += requiredNum
    return count


if __name__ == '__main__':
    arr = [1, 3]
    K = 6
    N = len(arr)
    print(minElements(arr, N, K))

# Expected output:
1
# theft at world bank | python3 program for the above approach ; custom comparator ; function to find the maximum profit ; stores the pairs of elements of b and a at the same index ; iterate over the range [ 0 , n ] ; if current integer is perfect square ; push the pair of b [ i ] and a [ i ] in vector v ; sort the vector using the custom comparator ; stores the maximum profit ; traverse the vector v ; if v [ i ] [ 1 ] is less than w ; increment profit by v [ i ] [ 0 ] ; decrement v [ i ] [ 0 ] from w ; otherwise ; update profit ; return the value of profit ; 

import math
from functools import cmp_to_key


def comparator(p1, p2):
    a = p1[0]
    b = p1[1]
    c = p2[0]
    d = p2[1]
    val1 = a / b
    val2 = c / d
    return val1 > val2


def maximumProfit(A, B, N, W):
    V = []
    for i in range(0, N):
        temp = int(math .sqrt(A[i]))
        if temp * temp == A[i]:
            continue
        V .append([B[i], A[i]])
    V = sorted(V, key=cmp_to_key(comparator))
    profit = 0.00
    k = len(V)
    for i in range(k):
        if V[i][1] <= W:
            profit += V[i][0]
            W -= V[i][1]
        else:
            profit += (V[i][0] * W) / V[i][1]
            break
    return profit


if __name__ == '__main__':
    N = 3
    W = 10
    A = [4, 5, 7]
    B = [8, 5, 4]
    print(round(maximumProfit(A, B, N, W), 5))

# Expected output:
7.85714
# maximize the count of adjacent element pairs with even sum by rearranging the array | function to find maximum count pair of adjacent elements with even sum ; stores count of odd numbers ; stores count of even numbers ; traverse the array arr [ ] ; if arr [ i ] % 2 is 1 ; else ; if odd and even both are greater than 0 ; otherwise ; 
def maximumCount(arr, N):
    odd = 0
    even = 0
    for i in range(N):
        if (arr[i] % 2):
            odd += 1
        else:
            even += 1
    if (odd and even):
        return N - 2
    else:
        return N - 1


if __name__ == '__main__':
    arr = [9, 13, 15, 3, 16, 9, 13, 18]
    N = len(arr)
    print(maximumCount(arr, N))

# Expected output:
6
# smallest number possible by repeatedly multiplying with k or 2 exactly n times starting from 1 | function to find the minimum value of x after increment x by k or twice value of x in each of n operations ; iterate over the range [ 1 , n ] ; if the value of x is less than equal to k ; otherwise ; return the minimum value of x ; 
def minPossibleValue(N, K, X):
    for i in range(1, N + 1):
        if (X <= K):
            X = X * 2
        else:
            X = X + K
    return X


N = 7
K = 4
X = 1
print(minPossibleValue(N, K, X))

# Expected output:
24
# smallest number that can replace all | python 3 program for the above approach ; function to find the value of k to minimize the value of maximum absolute difference between adjacent elements ; stores the maximum and minimum among array elements that are adjacent to " - 1" ; traverse the given array arr [ ] ; if arr [ i ] is - 1 & arr [ i + 1 ] is not - 1 ; if arr [ i + 1 ] is - 1 & arr [ i ] is not - 1 ; if all array element is - 1 ; otherwise ; 

import sys


def findMissingValue(arr, N):
    minE = sys .maxsize
    maxE = -sys .maxsize - 1
    for i in range(N - 1):
        if (arr[i] == -1 and arr[i + 1] != -1):
            minE = min(minE, arr[i + 1])
            maxE = max(maxE, arr[i + 1])
        if (arr[i] != -1 and arr[i + 1] == -1):
            minE = min(minE, arr[i])
            maxE = max(maxE, arr[i])
    if (minE == sys .maxsize and maxE == -sys .maxsize - 1):
        print("0")
    else:
        print((minE + maxE) // 2)


if __name__ == '__main__':
    arr = [1, -1, -1, -1, 5]
    N = len(arr)
    findMissingValue(arr, N)

# Expected output:
3