giulio98/xlcost-single-prompt huggingface dataset:
# minimum cost to select k strictly increasing elements | python3 program for the above approach ; function to calculate min cost to choose k increasing elements ; if k elements are counted return 0 ; if all elements of array has been traversed then return max_value ; to check if this is already calculated ; when i 'th elements  is not considered ; when the ith element is greater than previous element check if adding its cost makes total cost minimum ; 

N = 1005
K = 20
n = 0
k = 0
dp = [[[-1 for k in range(K + 1)]for j in range(N + 1)]for i in range(N + 1)]


def minCost(i, prev, cnt, ele, cost):
    if (cnt == k + 1):
        return 0
    if (i == n + 1):
        return 100000
    ans = dp[i][prev][cnt]
    if (ans != -1):
        return ans
    ans = minCost(i + 1, prev, cnt, ele, cost)
    if (ele[i] > ele[prev]):
        ans = min(ans, cost[i] + minCost(i + 1, i, cnt + 1, ele, cost))
    return ans


if __name__ == '__main__':
    n = 4
    k = 2
    ele = [0, 2, 6, 4, 8]
    cost = [0, 40, 20, 30, 10]
    ans = minCost(1, 0, 1, ele, cost)
    if (ans == 100000):
        ans = -1
    print(ans)

# Expected output:
30
# maximize the subarray sum after multiplying all elements of any subarray with x | python3 implementation of the approach ; function to return the maximum sum ; base case ; if already calculated ; if no elements have been chosen ; do not choose any element and use kadane 's algorithm by taking max ; choose the sub - array and multiply x ; choose the sub - array and multiply x ; end the sub - array multiplication ; no more multiplication ; memoize and return the answer ; function to get the maximum sum ; initialize dp with - 1 ; iterate from every position and find the maximum sum which is possible ; 

N = 5


def func(idx, cur, a, dp, n, x):
    if (idx == n):
        return 0
    if (dp[idx][cur] != -1):
        return dp[idx][cur]
    ans = 0
    if (cur == 0):
        ans = max(ans, a[idx] + func(idx + 1, 0, a, dp, n, x))
        ans = max(ans, x * a[idx] + func(idx + 1, 1, a, dp, n, x))
    elif (cur == 1):
        ans = max(ans, x * a[idx] + func(idx + 1, 1, a, dp, n, x))
        ans = max(ans, a[idx] + func(idx + 1, 2, a, dp, n, x))
    else:
        ans = max(ans, a[idx] + func(idx + 1, 2, a, dp, n, x))
    dp[idx][cur] = ans
    return dp[idx][cur]


def getMaximumSum(a, n, x):
    dp = [[-1 for i in range(3)]for j in range(n)]
    maxi = 0
    for i in range(0, n):
        maxi = max(maxi, func(i, 0, a, dp, n, x))
    return maxi


a = [-3, 8, -2, 1, -6]
n = len(a)
x = -1
print(getMaximumSum(a, n, x))

# Expected output:
15
# count pairs of non | python3 implementation of the approach ; pre - processing function ; get the size of the string ; initially mark every position as false ; for the length ; iterate for every index with length j ; if the length is less than 2 ; if characters are equal ; check for equal ; function to return the number of pairs ; create the dp table initially ; declare the left array ; declare the right array ; initially left [ 0 ] is 1 ; count the number of palindrome pairs to the left ; initially right most as 1 ; count the number of palindrome pairs to the right ; count the number of pairs ; 

N = 100


def pre_process(dp, s):
    n = len(s)
    for i in range(n):
        for j in range(n):
            dp[i][j] = False
    for j in range(1, n + 1):
        for i in range(n - j + 1):
            if (j <= 2):
                if (s[i] == s[i + j - 1]):
                    dp[i][i + j - 1] = True
            elif (s[i] == s[i + j - 1]):
                dp[i][i + j - 1] = dp[i + 1][i + j - 2]


def countPairs(s):
    dp = [[False for i in range(N)]for j in range(N)]
    pre_process(dp, s)
    n = len(s)
    left = [0 for i in range(n)]
    right = [0 for i in range(n)]
    left[0] = 1
    for i in range(1, n):
        for j in range(i + 1):
            if (dp[j][i] == 1):
                left[i] += 1
    right[n - 1] = 1
    for i in range(n - 2, -1, -1):
        right[i] = right[i + 1]
        for j in range(n - 1, i - 1, -1):
            if (dp[i][j] == 1):
                right[i] += 1
    ans = 0
    for i in range(n - 1):
        ans += left[i] * right[i + 1]
    return ans


s = "abacaba"
print(countPairs(s))

# Expected output:
36
# length of the longest increasing subsequence such that no two adjacent elements are coprime | python3 program to find the length of the longest increasing sub sequence from the given array such that no two adjacent elements are co prime ; function to find the length of the longest increasing sub sequence from the given array such that no two adjacent elements are co prime ; to store dp and d value ; to store required answer ; for all elements in the array ; initially answer is one ; for all it 's divisors ; update the dp value ; update the divisor value ; check for required answer ; update divisor of a [ i ] ; return required answer ; 

N = 100005


def LIS(a, n):
    dp = [0 for i in range(N)]
    d = [0 for i in range(N)]
    ans = 0
    for i in range(n):
        dp[a[i]] = 1
        for j in range(2, a[i]):
            if j * j > a[i]:
                break
            if (a[i] % j == 0):
                dp[a[i]] = max(dp[a[i]], dp[d[j]] + 1)
                dp[a[i]] = max(dp[a[i]], dp[d[a[i] // j]] + 1)
                d[j] = a[i]
                d[a[i] // j] = a[i]
        ans = max(ans, dp[a[i]])
        d[a[i]] = a[i]
    return ans


a = [1, 2, 3, 4, 5, 6]
n = len(a)
print(LIS(a, n))

# Expected output:
3
# find the number of binary strings of length n with at least 3 consecutive 1 s | function that returns true if s contains three consecutive 1 's ; function to return the count of required strings ; 
def check(s):
    n = len(s)
    for i in range(2, n):
        if (s[i] == '1' and s[i - 1] == '1' and s[i - 2] == '1'):
            return 1


def countStr(i, s):
    if (i < 0):
        if (check(s)):
            return 1
        return 0
    s[i] = '0'
    ans = countStr(i - 1, s)
    s[i] = '1'
    ans += countStr(i - 1, s)
    s[i] = '0'
    return ans


if __name__ == "__main__":
    N = 4
    s = list('0' * N)
    print(countStr(N - 1, s))

# Expected output:
3
# find the number of binary strings of length n with at least 3 consecutive 1 s | function to return the count of required strings ; '0' at ith position ; '1' at ith position ; 
def solve(i, x, dp):
    if (i < 0):
        return x == 3
    if (dp[i][x] != -1):
        return dp[i][x]
    dp[i][x] = solve(i - 1, 0, dp)
    dp[i][x] += solve(i - 1, x + 1, dp)
    return dp[i][x]


if __name__ == "__main__":
    n = 4
    dp = [[0 for i in range(n)]for j in range(4)]
    for i in range(n):
        for j in range(4):
            dp[i][j] = -1
    for i in range(n):
        dp[i][3] = (1 << (i + 1))
    print(solve(n - 1, 0, dp))

# Expected output:
3
# find the sum of the diagonal elements of the given n x n spiral matrix | function to return the sum of both the diagonal elements of the required matrix ; array to store sum of diagonal elements ; base cases ; computing the value of dp ; 
def findSum(n):
    dp = [0 for i in range(n + 1)]
    dp[1] = 1
    dp[0] = 0
    for i in range(2, n + 1, 1):
        dp[i] = ((4 * (i * i)) - 6 * (i - 1) + dp[i - 2])
    return dp[n]


if __name__ == '__main__':
    n = 4
    print(findSum(n))

# Expected output:
56
# count number of ways to reach a given score in a matrix | python3 implementation of the approach ; to store the states of dp ; to check whether a particular state of dp has been solved ; function to find the ways using memoization ; base cases ; if required score becomes negative ; if current state has been reached before ; set current state to visited ; 

n = 3
MAX = 60
dp = [[[0 for i in range(30)]for i in range(30)]for i in range(MAX + 1)]
v = [[[0 for i in range(30)]for i in range(30)]for i in range(MAX + 1)]


def findCount(mat, i, j, m):
    if (i == 0 and j == 0):
        if (m == mat[0][0]):
            return 1
        else:
            return 0
    if (m < 0):
        return 0
    if (i < 0 or j < 0):
        return 0
    if (v[i][j][m] > 0):
        return dp[i][j][m]
    v[i][j][m] = True
    dp[i][j][m] = (
        findCount(
            mat,
            i -
            1,
            j,
            m -
            mat[i][j]) +
        findCount(
            mat,
            i,
            j -
            1,
            m -
            mat[i][j]))
    return dp[i][j][m]


mat = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
m = 5
print(findCount(mat, n - 1, n - 1, m))

# Expected output:
6
# number of ways of scoring r runs in b balls with at most w wickets | python3 implementation of the approach ; function to return the number of ways to score r runs in b balls with at most w wickets ; if the wickets lost are more ; if runs scored are more ; if condition is met ; if no run got scored ; already visited state ; if scored 0 run ; if scored 1 run ; if scored 2 runs ; if scored 3 runs ; if scored 4 runs ; if scored 6 runs ; if scored no run and lost a wicket ; memoize and return ; 

mod = 1000000007
RUNMAX = 300
BALLMAX = 50
WICKETMAX = 10


def CountWays(r, b, l, R, B, W, dp):
    if (l > W):
        return 0
    if (r > R):
        return 0
    if (b == B and r == R):
        return 1
    if (b == B):
        return 0
    if (dp[r][b][l] != -1):
        return dp[r][b][l]
    ans = 0
    ans += CountWays(r, b + 1, l, R, B, W, dp)
    ans = ans % mod
    ans += CountWays(r + 1, b + 1, l, R, B, W, dp)
    ans = ans % mod
    ans += CountWays(r + 2, b + 1, l, R, B, W, dp)
    ans = ans % mod
    ans += CountWays(r + 3, b + 1, l, R, B, W, dp)
    ans = ans % mod
    ans += CountWays(r + 4, b + 1, l, R, B, W, dp)
    ans = ans % mod
    ans += CountWays(r + 6, b + 1, l, R, B, W, dp)
    ans = ans % mod
    ans += CountWays(r, b + 1, l + 1, R, B, W, dp)
    ans = ans % mod
    dp[r][b][l] = ans
    return ans


if __name__ == "__main__":
    R = 40
    B = 10
    W = 40
    dp = [[[-1 for k in range(WICKETMAX)]
           for j in range(BALLMAX)]for i in range(RUNMAX)]
    print(CountWays(0, 0, 0, R, B, W, dp))

# Expected output:
653263
# minimum steps to delete a string by deleting substring comprising of same characters | function to return the minimum number of delete operations ; when a single character is deleted ; when a group of consecutive characters are deleted if any of them matches ; when both the characters are same then delete the letters in between them ; memoize ; 
def findMinimumDeletion(l, r, dp, s):
    if l > r:
        return 0
    if l == r:
        return 1
    if dp[l][r] != -1:
        return dp[l][r]
    res = 1 + findMinimumDeletion(l + 1, r, dp, s)
    for i in range(l + 1, r + 1):
        if s[l] == s[i]:
            res = min(
                res,
                findMinimumDeletion(
                    l +
                    1,
                    i -
                    1,
                    dp,
                    s) +
                findMinimumDeletion(
                    i,
                    r,
                    dp,
                    s))
    dp[l][r] = res
    return res


if __name__ == "__main__":
    s = "abcddcba"
    n = len(s)
    N = 10
    dp = [[-1 for i in range(N)]for j in range(N)]
    print(findMinimumDeletion(0, n - 1, dp, s))

# Expected output:
4
# minimal product subsequence where adjacent elements are separated by a maximum distance of k | python3 implementation of the above approach . ; function to get the minimum product of subsequence such that adjacent elements are separated by a max distance of k ; multiset will hold pairs pair = ( log value of product , dp [ j ] value ) dp [ j ] = minimum product % mod multiset will be sorted according to log values therefore , corresponding to the minimum log value dp [ j ] value can be obtained . ; for the first k - sized window . ; update log value by adding previous minimum log value ; update dp [ i ] ; insert it again into the multiset since it is within the k - size window ; eliminate previous value which falls out of the k - sized window ; insert newest value to enter in the k - sized window . ; dp [ n - 1 ] will have minimum product % mod such that adjacent elements are separated by a max distance k ; 

import math
mod = 1000000007
MAX = 100005


def minimumProductSubsequence(arr, n, k):
    s = []
    dp = [0 for i in range(MAX)]
    p = [0.0 for i in range(MAX)]
    dp[0] = arr[0]
    p[0] = math .log(arr[0])
    s .append([p[0], dp[0]])
    s .sort()
    for i in range(1, k):
        l = s[0][0]
        min = s[0][1]
        p[i] = math .log(arr[i]) + l
        dp[i] = (arr[i] * min) % mod
        s .append([p[i], dp[i]])
        s .sort()
    for i in range(k, n):
        l = s[0][0]
        min = s[0][1]
        p[i] = math .log(arr[i]) + l
        dp[i] = (arr[i] * min) % mod
        if [p[i - k], dp[i - k]] in s:
            s .pop(s .index([p[i - k], dp[i - k]]))
        s .append([p[i], dp[i]])
        s .sort()
    return dp[n - 1]


if __name__ == '__main__':
    arr = [1, 2, 3, 4]
    n = len(arr)
    k = 2
    print(minimumProductSubsequence(arr, n, k))

# Expected output:
8
# ways to form a group from three groups with given constraints | python3 program to find the number of ways to form the group of peopls ; function to pre - compute the combination using dp ; calculate value of binomial coefficient in bottom up manner ; base cases ; calculate value using previously stored values ; function to find the number of ways ; function to pre - compute ; sum the zci ; iterate for second position ; iterate for first position ; multiply the common combination value ; 

C = [[0 for i in range(1000)]for i in range(1000)]


def binomialCoeff(n):
    i, j = 0, 0
    for i in range(n + 1):
        for j in range(i + 1):
            if (j == 0 or j == i):
                C[i][j] = 1
            else:
                C[i][j] = C[i - 1][j - 1] + C[i - 1][j]


def numberOfWays(x, y, z):
    binomialCoeff(max(x, max(y, z)))
    sum = 0
    for i in range(1, z + 1):
        sum = (sum + C[z][i])
    sum1 = 0
    for i in range(1, y + 1):
        for j in range(i + 1, x + 1):
            sum1 = (sum1 + (C[y][i] * C[x][j]))
    sum1 = (sum * sum1)
    return sum1


x = 3
y = 2
z = 1
print(numberOfWays(x, y, z))

# Expected output:
9
# longest subsequence such that adjacent elements have at least one common digit | returns length of maximum length subsequence ; to store the length of the maximum length subsequence ; to store current element arr [ i ] ; to store the length of the sub - sequence ending at index i and having common digit d ; to store digits present in current element ; to store length of maximum length subsequence ending at index i ; for first element maximum length is 1 for each digit ; find digits of each element , then find length of subsequence for each digit and then find local maximum ; find digits in current element ; for each digit present find length of subsequence and find local maximum ; update value of dp [ i ] [ d ] for each digit present in current element to local maximum found . ; update maximum length with local maximum ; 
def findSubsequence(arr, n):
    Len = 1
    tmp = 0
    i, j, d = 0, 0, 0
    dp = [[0 for i in range(10)]for i in range(n)]
    cnt = [0 for i in range(10)]
    locMax = 0
    tmp = arr[0]
    while (tmp > 0):
        dp[0][tmp % 10] = 1
        tmp //= 10
    for i in range(1, n):
        tmp = arr[i]
        locMax = 1
        cnt = [0 for i in range(10)]
        while (tmp > 0):
            cnt[tmp % 10] = 1
            tmp //= 10
        for d in range(10):
            if (cnt[d]):
                dp[i][d] = 1
                for j in range(i):
                    dp[i][d] = max(dp[i][d], dp[j][d] + 1)
                    locMax = max(dp[i][d], locMax)
        for d in range(10):
            if (cnt[d]):
                dp[i][d] = locMax
        Len = max(Len, locMax)
    return Len


arr = [1, 12, 44, 29, 33, 96, 89]
n = len(arr)
print(findSubsequence(arr, n))

# Expected output:
5
# count numbers in range with difference between sum of digits at even and odd positions as prime | python implementation of the above approach ; prime numbers upto 100 ; function to return the count of required numbers from 0 to num ; base case ; check if the difference is equal to any prime number ; if this result is already computed simply return it ; maximum limit upto which we can place digit . if tight is 1 , means number has already become smaller so we can place any digit , otherwise num [ pos ] ; if the current position is odd add it to currodd , otherwise to curreven ; function to convert x into its digit vector and uses count ( ) function to return the required count ; initialize dp ; 

M = 18
prime = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
         43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]


def count(pos, even, odd, tight, num):
    if pos == len(num):
        if len(num) & 1:
            odd, even = even, odd
        d = even - odd
        for i in range(24):
            if d == prime[i]:
                return 1
        return 0
    if dp[pos][even][odd][tight] != -1:
        return dp[pos][even][odd][tight]
    ans = 0
    limit = 9 if tight else num[pos]
    for d in range(limit + 1):
        currF = tight
        currEven = even
        currOdd = odd
        if d < num[pos]:
            currF = 1
        if pos & 1:
            currOdd += d
        else:
            currEven += d
        ans += count(pos + 1, currEven, currOdd, currF, num)
    dp[pos][even][odd][tight] = ans
    return dp[pos][even][odd][tight]


def solve(x):
    global dp
    num = []
    while x:
        num .append(x % 10)
        x //= 10
    num .reverse()
    dp = [[[[-1, -1]for i in range(90)]for j in range(90)]for k in range(M)]
    return count(0, 0, 0, 0, num)


if __name__ == "__main__":
    dp = []
    L = 1
    R = 50
    print(solve(R) - solve(L - 1))
    L = 50
    R = 100
    print(solve(R) - solve(L - 1))

# Expected output:
6
18
# sum of xor of all subarrays | function to calculate the sum of xor of all subarrays ; variable to store the final sum ; multiplier ; variable to store number of sub - arrays with odd number of elements with ith bits starting from the first element to the end of the array ; variable to check the status of the odd - even count while calculating c_odd ; loop to calculate initial value of c_odd ; loop to iterate through all the elements of the array and update sum ; updating the multiplier ; returning the sum ; 
def findXorSum(arr, n):
    Sum = 0
    mul = 1
    for i in range(30):
        c_odd = 0
        odd = 0
        for j in range(n):
            if ((arr[j] & (1 << i)) > 0):
                odd = (~odd)
            if (odd):
                c_odd += 1
        for j in range(n):
            Sum += (mul * c_odd)
            if ((arr[j] & (1 << i)) > 0):
                c_odd = (n - j - c_odd)
        mul *= 2
    return Sum


arr = [3, 8, 13]
n = len(arr)
print(findXorSum(arr, n))

# Expected output:
46
# find probability of selecting element from kth column after n iterations | python3 implementation of the above approach ; function to calculate probability ; declare dp [ ] [ ] and sum [ ] ; precalculate the first row ; calculate the probability for each element and update dp table ; return result ; 

n = 4
m = 4


def calcProbability(M, k):
    dp = [[0 for i in range(n)]for i in range(m)]
    Sum = [0 for i in range(n)]
    for j in range(n):
        dp[0][j] = M[0][j]
        Sum[0] += dp[0][j]
    for i in range(1, m):
        for j in range(n):
            dp[i][j] += (dp[i - 1][j] / Sum[i - 1] + M[i][j])
            Sum[i] += dp[i][j]
    return dp[n - 1][k - 1] / Sum[n - 1]


M = [[1, 1, 0, 3], [2, 3, 2, 3], [9, 3, 0, 2], [2, 3, 2, 2]]
k = 3
print(calcProbability(M, k))

# Expected output:
0.20121212121212123
# possible cuts of a number such that maximum parts are divisible by 3 | python3 program to find the maximum number of numbers divisible by 3 in a large number ; function to find the maximum number of numbers divisible by 3 in a large number ; store size of the string ; stores last index of a remainder ; last visited place of remainder zero is at 0. ; to store result from 0 to i ; get the remainder ; get maximum res [ i ] value ; 

import math as mt


def MaximumNumbers(string):
    n = len(string)
    remIndex = [-1 for i in range(3)]
    remIndex[0] = 0
    res = [-1 for i in range(n + 1)]
    r = 0
    for i in range(n + 1):
        r = (r + ord(string[i - 1]) - ord('0')) % 3
        res[i] = res[i - 1]
        if (remIndex[r] != -1):
            res[i] = max(res[i], res[remIndex[r]] + 1)
        remIndex[r] = i + 1
    return res[n]


s = "12345"
print(MaximumNumbers(s))

# Expected output:
3
# minimum steps required to convert x to y where a binary matrix represents the possible conversions | pyton3 implementation of the above approach ; dist [ ] [ ] will be the output matrix that will finally have the shortest distances between every pair of numbers ; initially same as mat ; add all numbers one by one to the set of intermediate numbers . before start of an iteration , we have shortest distances between all pairs of numbers such that the shortest distances consider only the numbers in set { 0 , 1 , 2 , . . k - 1 } as intermediate numbers . after the end of an iteration , vertex no . k is added to the set of intermediate numbers and the set becomes { 0 , 1 , 2 , . . k } ; pick all numbers as source one by one ; pick all numbers as destination for the above picked source ; if number k is on the shortest path from i to j , then update the value of dist [ i ] [ j ] ; if no path ; 

INF = 99999
size = 10


def findMinimumSteps(mat, x, y, n):
    dist = [[0 for i in range(n)]for i in range(n)]
    i, j, k = 0, 0, 0
    for i in range(n):
        for j in range(n):
            if (mat[i][j] == 0):
                dist[i][j] = INF
            else:
                dist[i][j] = 1
            if (i == j):
                dist[i][j] = 1
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if (dist[i][k] + dist[k][j] < dist[i][j]):
                    dist[i][j] = dist[i][k] + dist[k][j]
    if (dist[x][y] < INF):
        return dist[x][y]
    else:
        return -1


mat = [
    [
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [
                0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [
                        0, 0, 0, 1, 0, 0, 0, 0, 0, 0], [
                            0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [
                                0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [
                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [
                                        0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [
                                            0, 0, 0, 0, 0, 0, 0, 0, 0, 1]]
x, y = 2, 3
print(findMinimumSteps(mat, x, y, size))

# Expected output:
2
# count number of paths whose weight is exactly x and has at | python3 program to count the number of paths ; function to find the number of paths ; if the summation is more than x ; if exactly x weights have reached ; already visited ; count paths ; traverse in all paths ; if the edge weight is m ; else : edge 's weight is not m ; 

Max = 4
c = 2


def countPaths(Sum, get, m, n, dp):
    if (Sum < 0):
        return 0
    if (Sum == 0):
        return get
    if (dp[Sum][get] != -1):
        return dp[Sum][get]
    res = 0
    for i in range(1, n + 1):
        if (i == m):
            res += countPaths(Sum - i, 1, m, n, dp)
            res += countPaths(Sum - i, get, m, n, dp)
    dp[Sum][get] = res
    return dp[Sum][get]


n = 3
m = 2
x = 3
dp = [[-1 for i in range(2)]for i in range(Max + 1)]
for i in range(Max + 1):
    for j in range(2):
        dp[i][j] = -1
print(countPaths(x, 0, m, n, dp))

# Expected output:
0