giulio98/xlcost-single-prompt huggingface dataset:
# remove minimum elements from ends of array so that sum decreases by at least k | o ( n ) | function to return the count of minimum elements to be removed from the ends of the array such that the sum of the array decrease by at least k ; to store the final answer ; maximum possible sum required ; left point ; right pointer ; total current sum ; two pointer loop ; if the sum fits ; update the answer ; update the total sum ; increment the left pointer ; 
def minCount(arr, n, k):
    ans = 0
    sum = 0
    for i in range(n):
        sum += arr[i]
    sum -= k
    l = 0
    r = 0
    tot = 0
    while (l < n):
        if (tot <= sum):
            ans = max(ans, r - l)
            if (r == n):
                break
            tot += arr[r]
            r += 1
        else:
            tot -= arr[l]
            l += 1
    return (n - ans)


if __name__ == "__main__":
    arr = [1, 11, 5, 5]
    n = len(arr)
    k = 11
    print(minCount(arr, n, k))

# Expected output:
2
# minimum cost to merge all elements of list | python3 implementation of the approach ; to store the states of dp ; function to return the minimum merge cost ; base case ; if the state has been solved before ; marking the state as solved ; reference to dp [ i ] [ j ] ; to store the sum of all the elements in the subarray arr [ i ... j ] ; loop to iterate the recurrence ; returning the solved value ; 

import sys
N = 401
dp = [[0 for i in range(N)]for j in range(N)]
v = [[False for i in range(N)]for j in range(N)]


def minMergeCost(i, j, arr):
    if (i == j):
        return 0
    if (v[i][j]):
        return dp[i][j]
    v[i][j] = True
    x = dp[i][j]
    x = sys .maxsize
    tot = 0
    for k in range(i, j + 1):
        tot += arr[k]
    for k in range(i + 1, j + 1):
        x = min(x, tot + minMergeCost(i, k - 1, arr) + minMergeCost(k, j, arr))
    return x


if __name__ == '__main__':
    arr = [1, 3, 7]
    n = len(arr)
    print(minMergeCost(0, n - 1, arr))

# Expected output:
15
# expected number of moves to reach the end of a board | dynamic programming | python3 implementation of the approach ; to store the states of dp ; to determine whether a state has been solved before ; function to return the count ; base cases ; if a state has been solved before it won 't be evaluated again ; recurrence relation ; 

maxSize = 50
dp = [0] * maxSize
v = [0] * maxSize


def expectedSteps(x):
    if (x == 0):
        return 0
    if (x <= 5):
        return 6
    if (v[x]):
        return dp[x]
    v[x] = 1
    dp[x] = 1 + (expectedSteps(x - 1) + expectedSteps(x - 2) + expectedSteps(x - 3) +
                 expectedSteps(x - 4) + expectedSteps(x - 5) + expectedSteps(x - 6)) / 6
    return dp[x]


n = 10
print(round(expectedSteps(n - 1), 5))

# Expected output:
7.36111
# number of subsequences in a given binary string divisible by 2 | function to return the count of the required subsequences ; to store the final answer ; multiplier ; loop to find the answer ; condition to update the answer ; updating multiplier ; 
def countSubSeq(strr, lenn):
    ans = 0
    mul = 1
    for i in range(lenn):
        if (strr[i] == '0'):
            ans += mul
        mul *= 2
    return ans


strr = "10010"
lenn = len(strr)
print(countSubSeq(strr, lenn))

# Expected output:
22
# knapsack with large weights | python3 implementation of the approach ; to store the states of dp ; function to solve the recurrence relation ; base cases ; marking state as solved ; recurrence relation ; function to return the maximum weight ; iterating through all possible values to find the the largest value that can be represented by the given weights ; 

V_SUM_MAX = 1000
N_MAX = 100
W_MAX = 10000000
dp = [[0 for i in range(N_MAX)]for i in range(V_SUM_MAX + 1)]
v = [[0 for i in range(N_MAX)]for i in range(V_SUM_MAX + 1)]


def solveDp(r, i, w, val, n):
    if (r <= 0):
        return 0
    if (i == n):
        return W_MAX
    if (v[r][i]):
        return dp[r][i]
    v[r][i] = 1
    dp[r][i] = min(
        solveDp(
            r,
            i +
            1,
            w,
            val,
            n),
        w[i] +
        solveDp(
            r -
            val[i],
            i +
            1,
            w,
            val,
            n))
    return dp[r][i]


def maxWeight(w, val, n, c):
    for i in range(V_SUM_MAX, -1, -1):
        if (solveDp(i, 0, w, val, n) <= c):
            return i
    return 0


if __name__ == '__main__':
    w = [3, 4, 5]
    val = [30, 50, 60]
    n = len(w)
    C = 8
    print(maxWeight(w, val, n, C))

# Expected output:
90
# number of cells in a matrix that satisfy the given condition | python3 implementation of the approach ; function to return the number of cells in which mirror can be placed ; update the row array where row [ i ] [ j ] will store whether the current row i contains all 1 s in the columns starting from j ; update the column array where col [ i ] [ j ] will store whether the current column j contains all 1 s in the rows starting from i ; to store the required result ; for every cell except the last row and the last column ; if the current cell is not blocked and the light can travel from the next row and the next column then the current cell is valid ; for the last column ; for the last row , note that the last column is not taken into consideration as the bottom right element has already been considered in the last column previously ; 

N = 3


def numberOfCells(mat):
    row = [[False for i in range(N)]for i in range(N)]
    col = [[False for i in range(N)]for i in range(N)]
    for i in range(N):
        for j in range(N - 1, -1, -1):
            if (mat[i][j] == 1):
                if j + 1 < N:
                    row[i][j] = row[i][j + 1]
                else:
                    row[i][j] = True
            else:
                row[i][j] = False
    for j in range(N):
        for i in range(N - 1, -1, -1):
            if (mat[i][j] == 1):
                if i + 1 < N:
                    col[i][j] = col[i + 1][j]
                else:
                    col[i][j] = True
            else:
                col[i][j] = False
    cnt = 0
    for i in range(N - 1):
        for j in range(N - 1):
            if (row[i][j] and col[i][j]):
                cnt += 1
    for i in range(N):
        if (col[i][N - 1]):
            cnt += 1
    for j in range(N - 1):
        if (row[N - 1][j]):
            cnt += 1
    return cnt


mat = [[0, 1, 1], [0, 1, 1], [0, 1, 1]]
print(numberOfCells(mat))

# Expected output:
6
# maximum sum subarray after altering the array | python3 implementation of the approach ; function to return the maximum subarray sum ; function to reverse the subarray arr [ 0. . . i ] ; function to return the maximum subarray sum after performing the given operation at most once ; to store the result ; when no operation is performed ; find the maximum subarray sum after reversing the subarray arr [ 0. . . i ] for all possible values of i ; the complete array is reversed so that the subarray can be processed as arr [ 0. . . i ] instead of arr [ i ... n - 1 ] ; 

import sys


def maxSumSubarray(arr, size):
    max_so_far = -sys .maxsize - 1
    max_ending_here = 0
    for i in range(size):
        max_ending_here = max_ending_here + arr[i]
        if (max_so_far < max_ending_here):
            max_so_far = max_ending_here
        if (max_ending_here < 0):
            max_ending_here = 0
    return max_so_far


def getUpdatedArray(arr, copy, i):
    for j in range((i // 2) + 1):
        copy[j] = arr[i - j]
        copy[i - j] = arr[j]
    return


def maxSum(arr, size):
    resSum = -sys .maxsize - 1
    resSum = max(resSum, maxSumSubarray(arr, size))
    copyArr = []
    copyArr = arr
    for i in range(1, size, 1):
        getUpdatedArray(arr, copyArr, i)
        resSum = max(resSum, maxSumSubarray(copyArr, size))
    arr = arr[::-1]
    copyArr = arr
    for i in range(1, size, 1):
        getUpdatedArray(arr, copyArr, i)
        resSum = max(resSum, maxSumSubarray(copyArr, size))
    resSum += 6
    return resSum


if __name__ == '__main__':
    arr = [-9, 21, 24, 24, -51, -6, 17, -42, -39, 33]
    size = len(arr)
    print(maxSum(arr, size))

# Expected output:
102
# maximum possible gcd after replacing at most one element in the given array | python3 implementation of the approach ; function to return the maximum possible gcd after replacing a single element ; prefix and suffix arrays ; single state dynamic programming relation for storing gcd of first i elements from the left in prefix [ i ] ; initializing suffix array ; single state dynamic programming relation for storing gcd of all the elements having index greater than or equal to i in suffix [ i ] ; if first or last element of the array has to be replaced ; if any other element is replaced ; return the maximized gcd ; 

from math import gcd as __gcd


def MaxGCD(a, n):
    Prefix = [0] * (n + 2)
    Suffix = [0] * (n + 2)
    Prefix[1] = a[0]
    for i in range(2, n + 1):
        Prefix[i] = __gcd(Prefix[i - 1], a[i - 1])
    Suffix[n] = a[n - 1]
    for i in range(n - 1, 0, -1):
        Suffix[i] = __gcd(Suffix[i + 1], a[i - 1])
    ans = max(Suffix[2], Prefix[n - 1])
    for i in range(2, n):
        ans = max(ans, __gcd(Prefix[i - 1], Suffix[i + 1]))
    return ans


if __name__ == "__main__":
    a = [6, 7, 8]
    n = len(a)
    print(MaxGCD(a, n))

# Expected output:
2
# sum of products of all possible k size subsets of the given array | function to return the sum of products of all the possible k size subsets ; initialising all the values to 0 ; to store the answer for current value of k ; for k = 1 , the answer will simply be the sum of all the elements ; filling the table in bottom up manner ; to store the elements of the current row so that we will be able to use this sum for subsequent values of k ; we will subtract previously computed value so as to get the sum of elements from j + 1 to n in the ( i - 1 ) th row ; 
def sumOfProduct(arr, n, k):
    dp = [[0 for x in range(n + 1)]for y in range(n + 1)]
    cur_sum = 0
    for i in range(1, n + 1):
        dp[1][i] = arr[i - 1]
        cur_sum += arr[i - 1]
    for i in range(2, k + 1):
        temp_sum = 0
        for j in range(1, n + 1):
            cur_sum -= dp[i - 1][j]
            dp[i][j] = arr[j - 1] * cur_sum
            temp_sum += dp[i][j]
        cur_sum = temp_sum
    return cur_sum


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

# Expected output:
35
# find the equal pairs of subsequence of s and subsequence of t | python3 implementation of the approach ; function to return the pairs of subsequences from s [ ] and subsequences from t [ ] such that both have the same content ; create dp array ; base values ; base values ; keep previous dp value ; if both elements are same ; return the required answer ; 

import numpy as np
mod = int(1e9 + 7)


def subsequence(S, T, n, m):
    dp = np .zeros((n + 1, m + 1))
    for i in range(n + 1):
        dp[i][0] = 1
    for j in range(m + 1):
        dp[0][j] = 1
    for i in range(1, n + 1):
        for j in range(1, m + 1):
            dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]
            if (S[i - 1] == T[j - 1]):
                dp[i][j] += dp[i - 1][j - 1]
            dp[i][j] += mod
            dp[i][j] %= mod
    return dp[n][m]


if __name__ == "__main__":
    S = [1, 1]
    n = len(S)
    T = [1, 1]
    m = len(T)
    print(subsequence(S, T, n, m))

# Expected output:
6.0
# maximum count of elements divisible on the left for any element | function to return the maximum count of required elements ; for every element in the array starting from the second element ; check all the elements on the left of current element which are divisible by the current element ; 
def findMax(arr, n):
    res = 0
    for i in range(1, n):
        count = 0
        for j in range(0, i):
            if arr[j] % arr[i] == 0:
                count += 1
        res = max(count, res)
    return res


arr = [8, 1, 28, 4, 2, 6, 7]
n = len(arr)
print(findMax(arr, n))

# Expected output:
3
# maximum count of elements divisible on the left for any element | function to return the maximum count of required elements ; divisible [ i ] will store true if arr [ i ] is divisible by any element on its right ; to store the maximum required count ; for every element of the array ; if the current element is divisible by any element on its right ; find the count of element on the left which are divisible by the current element ; if arr [ j ] is divisible then set divisible [ j ] to true ; update the maximum required count ; 
def findMax(arr, n):
    divisible = [False] * n
    res = 0
    for i in range(n - 1, -1, -1):
        if (divisible[i]):
            continue
        cnt = 0
        for j in range(i):
            if ((arr[j] % arr[i]) == 0):
                divisible[j] = True
                cnt += 1
        res = max(res, cnt)
    return res


if __name__ == "__main__":
    arr = [8, 1, 28, 4, 2, 6, 7]
    n = len(arr)
    print(findMax(arr, n))

# Expected output:
3
# number of subsets with sum divisible by m | set 2 | python3 implementation of the approach ; to store the states of dp ; function to find the required count ; base case ; if the state has been solved before return the value of the state ; setting the state as solved ; recurrence relation ; 

maxN = 20
maxM = 10
dp = [[0 for i in range(maxN)]for i in range(maxM)]
v = [[0 for i in range(maxN)]for i in range(maxM)]


def findCnt(arr, i, curr, n, m):
    if (i == n):
        if (curr == 0):
            return 1
        else:
            return 0
    if (v[i][curr]):
        return dp[i][curr]
    v[i][curr] = 1
    dp[i][curr] = findCnt(arr, i + 1, curr, n, m) + \
        findCnt(arr, i + 1, (curr + arr[i]) % m, n, m)
    return dp[i][curr]


arr = [3, 3, 3, 3]
n = len(arr)
m = 6
print(findCnt(arr, 0, 0, n, m) - 1)

# Expected output:
7
# longest subsequence with a given or value : dynamic programming approach | python3 implementation of the approach ; to store the states of dp ; function to return the required length ; base case ; if the state has been solved before return the value of the state ; setting the state as solved ; recurrence relation ; 

import numpy as np
maxN = 20
maxM = 64
dp = np .zeros((maxN, maxM))
v = np .zeros((maxN, maxM))


def findLen(arr, i, curr, n, m):
    if (i == n):
        if (curr == m):
            return 0
        else:
            return -1
    if (v[i][curr]):
        return dp[i][curr]
    v[i][curr] = 1
    l = findLen(arr, i + 1, curr, n, m)
    r = findLen(arr, i + 1, curr | arr[i], n, m)
    dp[i][curr] = l
    if (r != -1):
        dp[i][curr] = max(dp[i][curr], r + 1)
    return dp[i][curr]


if __name__ == "__main__":
    arr = [3, 7, 2, 3]
    n = len(arr)
    m = 3
    ans = findLen(arr, 0, 0, n, m)
    if (ans == -1):
        print(0)
    else:
        print(ans)

# Expected output:
3.0
# longest subsequence with a given and value | python3 implementation of the approach ; to store the states of dp ; function to return the required length ; base case ; if the state has been solved before return the value of the state ; setting the state as solved ; recurrence relation ; 

import numpy as np
maxN = 20
maxM = 256
dp = np .zeros((maxN, maxM))
v = np .zeros((maxN, maxM))


def findLen(arr, i, curr, n, m):
    if (i == n):
        if (curr == m):
            return 0
        else:
            return -1
    if (v[i][curr]):
        return dp[i][curr]
    v[i][curr] = 1
    l = findLen(arr, i + 1, curr, n, m)
    r = findLen(arr, i + 1, curr & arr[i], n, m)
    dp[i][curr] = l
    if (r != -1):
        dp[i][curr] = max(dp[i][curr], r + 1)
    return dp[i][curr]


if __name__ == "__main__":
    arr = [3, 7, 2, 3]
    n = len(arr)
    m = 3
    ans = findLen(arr, 0, ((1 << 8) - 1), n, m)
    if (ans == -1):
        print(0)
    else:
        print(ans)

# Expected output:
3.0
# longest subsequence whose sum is divisible by a given number | python3 implementation of the approach ; to store the states of dp ; function to return the length of the longest subsequence whose sum is divisible by m ; base case ; if the state has been solved before return the value of the state ; setting the state as solved ; recurrence relation ; 

import numpy as np
maxN = 20
maxM = 64
dp = np .zeros((maxN, maxM))
v = np .zeros((maxN, maxM))


def findLen(arr, i, curr, n, m):
    if (i == n):
        if (not curr):
            return 0
        else:
            return -1
    if (v[i][curr]):
        return dp[i][curr]
    v[i][curr] = 1
    l = findLen(arr, i + 1, curr, n, m)
    r = findLen(arr, i + 1, (curr + arr[i]) % m, n, m)
    dp[i][curr] = l
    if (r != -1):
        dp[i][curr] = max(dp[i][curr], r + 1)
    return dp[i][curr]


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

# Expected output:
3.0
# count of subsets with sum equal to x | python3 implementation of the approach ; to store the states of dp ; function to return the required count ; base case ; if the state has been solved before return the value of the state ; setting the state as solved ; recurrence relation ; 

import numpy as np
maxN = 20
maxSum = 50
minSum = 50
base = 50
dp = np .zeros((maxN, maxSum + minSum))
v = np .zeros((maxN, maxSum + minSum))


def findCnt(arr, i, required_sum, n):
    if (i == n):
        if (required_sum == 0):
            return 1
        else:
            return 0
    if (v[i][required_sum + base]):
        return dp[i][required_sum + base]
    v[i][required_sum + base] = 1
    dp[i][required_sum + base] = findCnt(arr,
                                         i + 1,
                                         required_sum,
                                         n) + findCnt(arr,
                                                      i + 1,
                                                      required_sum - arr[i],
                                                      n)
    return dp[i][required_sum + base]


if __name__ == "__main__":
    arr = [3, 3, 3, 3]
    n = len(arr)
    x = 6
    print(findCnt(arr, 0, x, n))

# Expected output:
6.0
# number of subsets with a given or value | python3 implementation of the approach ; to store states of dp ; function to return the required count ; base case ; if the state has been solved before return the value of the state ; setting the state as solved ; recurrence relation ; 

import numpy as np
maxN = 20
maxM = 64
dp = np .zeros((maxN, maxM))
v = np .zeros((maxN, maxM))


def findCnt(arr, i, curr, n, m):
    if (i == n):
        return (curr == m)
    if (v[i][curr]):
        return dp[i][curr]
    v[i][curr] = 1
    dp[i][curr] = findCnt(arr, i + 1, curr, n, m) + \
        findCnt(arr, i + 1, (curr | arr[i]), n, m)
    return dp[i][curr]


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

# Expected output:
4.0
# number of subsets with a given and value | python3 implementation of the approach ; to store states of dp ; function to return the required count ; base case ; if the state has been solved before return the value of the state ; setting the state as solved ; recurrence relation ; 

import numpy as np
maxN = 20
maxM = 64
dp1 = np .zeros((maxN, maxM))
v1 = np .zeros((maxN, maxM))


def findCnt(arr, i, curr, n, m):
    if (i == n):
        return (curr == m)
    if (v1[i][curr]):
        return dp1[i][curr]
    v1[i][curr] = 1
    dp1[i][curr] = findCnt(arr, i + 1, curr, n, m) + \
        findCnt(arr, i + 1, (curr & arr[i]), n, m)
    return dp1[i][curr]


if __name__ == "__main__":
    arr = [0, 0, 0]
    n = len(arr)
    m = 0
    print(findCnt(arr, 0, ((1 << 6) - 1), n, m))

# Expected output:
7.0
# count of integers obtained by replacing ? in the given string that give remainder 5 when divided by 13 | python3 implementation of the approach ; function to find the count of integers obtained by replacing ' ? ' in a given string such that formed integer gives remainder 5 when it is divided by 13 ; initialise ; place digit j at ? position ; get the remainder ; return the required answer ; 

import numpy as np
MOD = (int)(1e9 + 7)


def modulo_13(s, n):
    dp = np .zeros((n + 1, 13))
    dp[0][0] = 1
    for i in range(n):
        for j in range(10):
            nxt = ord(s[i]) - ord('0')
            if (s[i] == '?'):
                nxt = j
            for k in range(13):
                rem = (10 * k + nxt) % 13
                dp[i + 1][rem] += dp[i][k]
                dp[i + 1][rem] %= MOD
            if (s[i] != '?'):
                break
    return int(dp[n][5])


if __name__ == "__main__":
    s = "?44"
    n = len(s)
    print(modulo_13(s, n))

# Expected output:
1
# longest increasing subsequence using longest common subsequence algorithm | function to return the size of the longest increasing subsequence ; create an 2d array of integer for tabulation ; take the second sequence as the sorted sequence of the given sequence ; classical dynamic programming algorithm for longest common subsequence ; return the ans ; 
def LISusingLCS(seq):
    n = len(seq)
    L = [[0 for i in range(n + 1)]for i in range(n + 1)]
    sortedseq = sorted(seq)
    for i in range(n + 1):
        for j in range(n + 1):
            if (i == 0 or j == 0):
                L[i][j] = 0
            elif (seq[i - 1] == sortedseq[j - 1]):
                L[i][j] = L[i - 1][j - 1] + 1
            else:
                L[i][j] = max(L[i - 1][j], L[i][j - 1])
    return L[n][n]


sequence = [12, 34, 1, 5, 40, 80]
print(LISusingLCS(sequence))

# Expected output:
4
# count of n | function to return the count of n - digit numbers that satisfy the given conditions ; base case ; if 0 wasn 't chosen previously ; if 0 wasn 't chosen previously ; 
def count_numbers(k, n, flag):
    if (n == 1):
        if (flag):
            return (k - 1)
        else:
            return 1
    if (flag):
        return (k - 1) * (count_numbers(k, n - 1, 0) +
                          count_numbers(k, n - 1, 1))
    else:
        return count_numbers(k, n - 1, 1)


n = 3
k = 10
print(count_numbers(k, n, True))

# Expected output:
891
# count of n | function to return the count of n - digit numbers that satisfy the given conditions ; dp array to store the pre - caluclated states ; base cases ; i - digit numbers ending with 0 can be formed by concatenating 0 in the end of all the ( i - 1 ) - digit number ending at a non - zero digit ; i - digit numbers ending with non - zero can be formed by concatenating any non - zero digit in the end of all the ( i - 1 ) - digit number ending with any digit ; n - digit number ending with and ending with non - zero ; 
def count_numbers(k, n):
    dp = [[0 for i in range(2)]for i in range(n + 1)]
    dp[1][0] = 0
    dp[1][1] = k - 1
    for i in range(2, n + 1):
        dp[i][0] = dp[i - 1][1]
        dp[i][1] = (dp[i - 1][0] + dp[i - 1][1]) * (k - 1)
    return dp[n][0] + dp[n][1]


k = 10
n = 3
print(count_numbers(k, n))

# Expected output:
891
# divide an array into k subarray with the given condition | function to divide an array into k parts such that the summ of difference of every element with the maximum element of that part is minimum ; dp to store the values ; fill up the dp table ; intitilize maximum value ; max element and the summ ; run a loop from i to n ; find the maximum number from i to l and the summ from i to l ; find the summ of difference of every element with the maximum element ; if the array can be divided ; returns the minimum summ in k parts ; 
def divideArray(arr, n, k):
    dp = [[0 for i in range(500)]for i in range(500)]
    k -= 1
    for i in range(n - 1, -1, -1):
        for j in range(0, k + 1):
            dp[i][j] = 10 ** 9
            max_ = -1
            summ = 0
            for l in range(i, n):
                max_ = max(max_, arr[l])
                summ += arr[l]
                diff = (l - i + 1) * max_ - summ
                if (j > 0):
                    dp[i][j] = min(dp[i][j], diff + dp[l + 1][j - 1])
                else:
                    dp[i][j] = diff
    return dp[0][k]


arr = [2, 9, 5, 4, 8, 3, 6]
n = len(arr)
k = 2
print(divideArray(arr, n, k))

# Expected output:
19