giulio98/xlcost-single-prompt huggingface dataset:
# maximum number of groups that can receive fresh donuts distributed in batches of size k | recursive function to find the maximum number of groups that will receive fresh donuts ; store the result for the current state ; check if the leftover donuts from the previous batch is 0 ; if true , then one by one give the fresh donuts to each group ; decrement arr [ i ] ; update the maximum number of groups ; increment arr [ i ] ; otherwise , traverse the given array , arr [ ] ; decrement arr [ i ] ; update the maximum number of groups ; increment arr [ i ] ; return the value of q ; function to find the maximum number of groups that will receive fresh donuts ; stores count of remainder by k ; traverse the array arr [ ] ; stores maximum number of groups ; return the answer ; 
def dfs(arr, left, K):
    q = 0
    if (left == 0):
        for i in range(1, K, 1):
            if (arr[i] > 0):
                arr[i] -= 1
                q = max(q, 1 + dfs(arr, K - i, K))
                arr[i] += 1
    else:
        for i in range(1, K, 1):
            if (arr[i] > 0):
                arr[i] -= 1
                nleft = left - i if i <= left else K + left - i
                q = max(q, dfs(arr, nleft, K))
                arr[i] += 1
    return q


def maxGroups(K, arr, n):
    V = [0 for i in range(K)]
    for x in range(n):
        V[arr[x] % K] += 1
    ans = V[0] + dfs(V, 0, K)
    return ans


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

# Expected output:
4
# maximum jumps to reach end of array with condition that index i can make arr [ i ] jumps | function to find the maximum jumps to reach end of array ; stores the jumps needed to reach end from each index ; traverse the array ; check if j is less than n ; add dp [ j ] to the value of dp [ i ] ; update the value of ans ; print the value of ans ; 
def findMaxJumps(arr, N):
    dp = [0 for i in range(N)]
    ans = 0
    i = N - 1
    while (i >= 0):
        dp[i] = arr[i]
        j = i + arr[i]
        if (j < N):
            dp[i] = dp[i] + dp[j]
        ans = max(ans, dp[i])
        i -= 1
    print(ans)


if __name__ == '__main__':
    arr = [2, 3, 5, 7, 9]
    N = len(arr)
    findMaxJumps(arr, N)

# Expected output:
12
# longest subsequence with non negative prefix sum at each position | function to find the length of the longest subsequence with non negative prefix sum at each position ; initialize dp array with - 1 ; maximum subsequence sum by including no elements till position 'i ; maximum subsequence sum by including first element at first position ; iterate over all the remaining positions ; if the current element is excluded ; if current element is included and if total sum is positive or not ; select the maximum j by which a non negative prefix sum subsequence can be obtained ; print the answer ; 
def longestSubsequence(arr, N):
    dp = [[-1 for i in range(N + 1)] for i in range(N)]
    for i in range(N):
        dp[i][0] = 0
    dp[0][1] = arr[0] if arr[0] >= 0 else -1
    for i in range(1, N):

        for j in range(1, i + 2):
            if (dp[i - 1][j] != -1):
                dp[i][j] = max(dp[i][j], dp[i - 1][j])
            if (dp[i - 1][j - 1] >= 0 and dp[i - 1][j - 1] + arr[i] >= 0):

                dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + arr[i])

    ans = 0
    for j in range(N + 1):
        if (dp[N - 1][j] >= 0):
            ans = j
    print(ans)


arr = [4, -4, 1, -3, 1, -3]
N = len(arr)
longestSubsequence(arr, N)

# Expected output:
5
# count of strictly increasing n | declaration of dp table ; function to find the count of all n digit numbers such that all the digit is less than its adjacent digits ; base case : if i = n , then return 1 as valid number has been formed ; if the state has already been computed , then return it ; stores the total count of ways for the current recursive call ; if i = 0 , any digit from [ 1 - 9 ] can be placed and also if n = 1 , then 0 can also be placed ; if i = 1 , any digit from [ 0 - 9 ] can be placed such that digit is not equal to previous digit ; if the current digit is not same as the prev ; place the current digit such that it is less than the previous digit ; place current digit such that it is more than the previous digit ; return the resultant total count ; function to find all n - digit numbers satisfying the given criteria ; function call to count all possible ways ; 

dp = [[[-1 for x in range(2)]for y in range(10)]for z in range(100)]


def solve(i, n, prev, sign):
    if (i == n):
        return 1
    val = dp[i][prev][sign]
    if (val != -1):
        return val
    val = 0
    if (i == 0):
        if (n == 1):
            digit = 0
        else:
            digit = 1
        while digit <= 9:
            val += solve(i + 1, n, digit, sign)
            digit += 1
    elif (i == 1):
        for digit in range(10):
            if (digit != prev):
                val += solve(i + 1, n, digit, (digit > prev))
    else:
        if (sign == 1):
            for digit in range(prev - 1, -1, -1):
                val += solve(i + 1, n, digit, 0)
        else:
            for digit in range(prev + 1, 10):
                val += solve(i + 1, n, digit, 1)
    return val


def countNdigitNumber(N):
    print(solve(0, N, 0, 0))


if __name__ == "__main__":
    N = 3
    countNdigitNumber(N)

# Expected output:
525
# total count of sorted numbers upto n digits in range [ l , r ] ( magnificent necklace combinatorics problem ) | function to count total number of ways ; stores all dp - states ; stores the result ; traverse the range [ 0 , n ] ; traverse the range [ 1 , r - l ] ; update dp [ i ] [ j ] ; assign dp [ 0 ] [ r - l ] to ans ; traverse the range [ 1 , n ] ; traverse the range [ 1 , r - l ] ; update dp [ i ] [ j ] ; increment ans by dp [ i - 1 ] [ j ] ; return ans ; 
def Count(N, L, R):
    dp = [[0 for i in range(R - L + 1)]for i in range(N)]
    ans = 0
    for i in range(N):
        dp[i][0] = 1
    for i in range(1, len(dp[0])):
        dp[0][i] = dp[0][i - 1] + 1
    ans = dp[0][R - L]
    for i in range(1, N):
        for j in range(1, len(dp[0])):
            dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
        ans += dp[i][R - L]
    return ans


if __name__ == '__main__':
    N = 3
    L = 6
    R = 9
    print(Count(N, L, R))

# Expected output:
34
# length of largest common subarray in all the rows of given matrix | function to find longest common subarray in all the rows of the matrix ; array to store the position of element in every row ; traverse the matrix ; store the position of every element in every row ; variable to store length of largest common subarray ; traverse through the matrix column ; variable to check if every row has arr [ i ] [ j ] next to arr [ i - 1 ] [ j ] or not ; traverse through the matrix rows ; check if arr [ i ] [ j ] is next to arr [ i ] [ j - 1 ] in every row or not ; if every row has arr [ 0 ] [ j ] next to arr [ 0 ] [ j - 1 ] increment len by 1 and update the value of ans ; 
def largestCommonSubarray(arr, n, m):
    dp = [[0 for i in range(m + 1)]for j in range(n)]
    for i in range(n):
        for j in range(m):
            dp[i][arr[i][j]] = j
    ans = 1
    len1 = 1
    for i in range(1, m, 1):
        check = True
        for j in range(1, n, 1):
            if (dp[j][arr[0][i - 1]] + 1 != dp[j][arr[0][i]]):
                check = False
                break
        if (check):
            len1 += 1
            ans = max(ans, len1)
        else:
            len1 = 1
    return ans


if __name__ == '__main__':
    n = 4
    m = 5
    arr = [[4, 5, 1, 2, 3, 6, 7], [1, 2, 4, 5, 7, 6, 3], [2, 7, 3, 4, 5, 1, 6]]
    N = len(arr)
    M = len(arr[0])
    print(largestCommonSubarray(arr, N, M))

# Expected output:
2
# maximize sum that can be obtained from two given arrays based on given conditions | function to find the maximum sum that can be obtained from two given arrays based on the following conditions ; stores the maximum sum from 0 to i ; initialize the value of dp [ 0 ] [ 0 ] and dp [ 0 ] [ 1 ] ; traverse the array a [ ] and b [ ] ; if a [ i ] is considered ; if b [ i ] is not considered ; if b [ i ] is considered ; if i = 1 , then consider the value of dp [ i ] [ 1 ] as b [ i ] ; return maximum sum ; 
def MaximumSum(a, b, n):
    dp = [[-1 for j in range(2)]for i in range(n)]
    dp[0][0] = a[0]
    dp[0][1] = b[0]
    for i in range(1, n):
        dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]) + a[i]
        dp[i][1] = max(dp[i - 1][0], dp[i - 1][1])
        if (i - 2 >= 0):
            dp[i][1] = max(dp[i][1], max(dp[i - 2][0], dp[i - 2][1]) + b[i])
        else:
            dp[i][1] = max(dp[i][1], b[i])
    return max(dp[n - 1][0], dp[n - 1][1])


if __name__ == '__main__':
    A = [10, 1, 10, 10]
    B = [5, 50, 1, 5]
    N = len(A)
    print(MaximumSum(A, B, N))

# Expected output:
70
# lexicographically kth | python3 program for the above approach ; function to fill dp array ; initialize all the entries with 0 ; update dp [ 0 ] [ 0 ] to 1 ; traverse the dp array ; update the value of dp [ i ] [ j ] ; recursive function to find the kth lexicographical smallest string ; handle the base cases ; if there are more than or equal to k strings which start with a , then the first character is 'a ; otherwise the first character of the resultant string is 'b ; function to find the kth lexicographical smallest string ; function call to fill the dp array ; print the resultant ; 

from typing import Mapping

MAX = 30


def findNumString(X, Y, dp):
    for i in range(0, MAX):
        for j in range(0, MAX):
            dp[i][j] = 0
    dp[0][0] = 1
    for i in range(0, X + 1):
        for j in range(0, Y + 1):
            if (i > 0):
                dp[i][j] += dp[i - 1][j]

            if (j > 0):
                dp[i][j] += dp[i][j - 1]


def kthString(X, Y, K, dp):
    x1 = ""
    y1 = ""

    for i in range(0, Y):
        x1 += 'b'
    for i in range(0, X):
        y1 += 'a'

    if (X == 0):
        return x1
    if (Y == 0):
        return y1
    if (K <= dp[X - 1][Y]):
        return "a" + kthString(X - 1, Y, K, dp)
    else:
        return "b" + kthString(X, Y - 1,
                               K - dp[X - 1][Y], dp)


def kthStringUtil(X, Y, K):

    dp = [[0 for i in range(MAX)]
          for col in range(MAX)]
    findNumString(X, Y, dp)
    print(kthString(X, Y, K, dp))


X = 4
Y = 3
K = 4
kthStringUtil(X, Y, K)

# Expected output:
aaabbba
# maximum tip calculator | function that finds the maximum tips from the given arrays as per the given conditions ; base condition ; if both have non - zero count then return max element from both array ; traverse first array , as y count has become 0 ; traverse 2 nd array , as x count has become 0 ; 
def maximumTip(arr1, arr2, n, x, y):
    if n == 0:
        return 0
    if x != 0 and y != 0:
        return max(arr1[n - 1] + maximumTip(arr1, arr2, n - 1, x - 1, y),
                   arr2[n - 1] + maximumTip(arr1, arr2, n - 1, x, y - 1))
    if y == 0:
        return arr1[n - 1] + maximumTip(arr1, arr2, n - 1, x - 1, y)
    else:
        return arr2[n - 1] + maximumTip(arr1, arr2, n - 1, x, y - 1)


N = 5
X = 3
Y = 3
A = [1, 2, 3, 4, 5]
B = [5, 4, 3, 2, 1]
print(maximumTip(A, B, N, X, Y))

# Expected output:
21
# number of ways such that only k bars are visible from the left | function to calculate the number of bars that are visible from the left for a particular arrangement ; if current element is greater than the last greater element , it is visible ; function to calculate the number of rearrangements where k bars are visiblef from the left ; vector to store current permutation ; check for all permutations ; if current permutation meets the conditions , increment answer ; 
def noOfbarsVisibleFromLeft(v):
    last = 0
    ans = 0
    for u in v:
        if (last < u):
            ans += 1
            last = u
    return ans


def nextPermutation(nums):
    i = len(nums) - 2
    while i > -1:
        if nums[i] < nums[i + 1]:
            j = len(nums) - 1
            while j > i:
                if nums[j] > nums[i]:
                    break
                j -= 1
            nums[i], nums[j] = nums[j], nums[i]
            break
        i -= 1
    nums[i + 1:] = reversed(nums[i + 1:])
    return nums


def KvisibleFromLeft(N, K):
    v = [0] * (N)
    for i in range(N):
        v[i] = i + 1
    ans = 0
    temp = list(v)
    while True:
        if (noOfbarsVisibleFromLeft(v) == K):
            ans += 1
        v = nextPermutation(v)
        if v == temp:
            break
    return ans


if __name__ == '__main__':
    N = 5
    K = 2
    print(KvisibleFromLeft(N, K))

# Expected output:
50
# number of ways such that only k bars are visible from the left | function to calculate the number of permutations of n , where only k bars are visible from the left . ; only ascending order is possible ; n is placed at the first position the nest n - 1 are arranged in ( n - 1 ) ! ways ; recursing ; 
def KvisibleFromLeft(N, K):
    if (N == K):
        return 1
    if (K == 1):
        ans = 1
        for i in range(1, N, 1):
            ans *= i
        return ans
    return KvisibleFromLeft(N - 1, K - 1) + (N - 1) * \
        KvisibleFromLeft(N - 1, K)


if __name__ == '__main__':
    N = 5
    K = 2
    print(KvisibleFromLeft(N, K))

# Expected output:
50
# minimum deletions in array to make difference of adjacent elements non | function for finding minimum deletions so that the array becomes non decreasing and the difference between adjacent elements also becomes non decreasing ; initialize answer to a large value ; generating all subsets ; checking the first condition ; checking the second condition ; if both conditions are satisfied consider the answer for minimum ; 
def minimumDeletions(A, N):
    ans = 10 ** 8
    for i in range(1, (1 << N)):
        temp = []
        for j in range(N):
            if ((i & (1 << j)) != 0):
                temp .append(A[j])
        flag = 0
        for j in range(1, len(temp)):
            if (temp[j] < temp[j - 1]):
                flag = 1
        for j in range(1, len(temp) - 1):
            if (temp[j] - temp[j - 1] > temp[j + 1] - temp[j]):
                flag = 1
        if (flag == 0):
            ans = min(ans, N - len(temp))
    return ans


if __name__ == '__main__':
    A = [1, 4, 5, 7, 20, 21]
    N = len(A)
    print(minimumDeletions(A, N))

# Expected output:
2
# find maximum subset | function to calculate maximum sum possible by taking at most k elements that is divisibly by d ; variable to store final answer ; traverse all subsets ; update ans if necessary conditions are satisfied ; 
def maximumSum(A, N, K, D):
    ans = 0
    for i in range((1 << N)):
        sum = 0
        c = 0
        for j in range(N):
            if (i >> j & 1):
                sum += A[j]
                c += 1
        if (sum % D == 0 and c <= K):
            ans = max(ans, sum)
    return ans


if __name__ == '__main__':
    N = 5
    K = 3
    D = 7
    A = [1, 11, 5, 5, 18]
    print(maximumSum(A, N, K, D))

# Expected output:
28
# find the longest subsequence of a string that is a substring of another string | function to find the longest subsequence that matches with the substring of other string ; stores the lengths of strings x and y ; create a matrix ; initialize the matrix ; fill all the remaining rows ; if the characters are equal ; if not equal , then just move to next in subsequence string ; find maximum length of the longest subsequence matching substring of other string ; iterate through the last row of matrix ; store the required string ; backtrack from the cell ; if equal , then add the character to res string ; return the required string ; 
def longestSubsequence(X, Y):
    n = len(X)
    m = len(Y)
    mat = [[0 for i in range(m + 1)]for j in range(n + 1)]
    for i in range(0, n + 1):
        for j in range(0, m + 1):
            if (i == 0 or j == 0):
                mat[i][j] = 0
    for i in range(1, n + 1):
        for j in range(1, m + 1):
            if (X[i - 1] == Y[j - 1]):
                mat[i][j] = 1 + mat[i - 1][j - 1]
            else:
                mat[i][j] = mat[i - 1][j]
    len1 = 0
    col = 0
    for i in range(0, m + 1):
        if (mat[n][i] > len1):
            len1 = mat[n][i]
            col = i
    res = ""
    i = n
    j = col
    while (len1 > 0):
        if (X[i - 1] == Y[j - 1]):
            res = X[i - 1] + res
            i -= 1
            j -= 1
            len1 -= 1
        else:
            i -= 1
    return res


X = "ABCD"
Y = "ACDBDCD"
print(longestSubsequence(X, Y))

# Expected output:
ACD
# divide chocolate bar into pieces , minimizing the area of invalid pieces | python3 program for the above approach ; store valid dimensions ; stores memoization ; utility function to calculate minimum invalid area for chocolate piece having dimension ( l , r ) ; check whether current piece is valid or not if it is , then return zero for current dimension ; making all possible horizontal cuts , one by one and calculating the sum of minimum invalid area for both the resulting pieces ; making all possible vertical cuts , one by one and calculating the sum of minimum invalid area for both the resulting pieces ; store the computed result ; function to calculate minimum invalid area for chocolate piece having dimension ( l , r ) ; total number of valid dimensions ; storing valid dimensions as for every ( x , y ) both ( x , y ) and ( y , x ) are valid ; fill dp [ ] [ ] table with - 1 , indicating that results are not computed yet ; stores minimum area ; prminarea as the output ; 

sz = 1001
ok = [[0 for i in range(sz)]for i in range(sz)]
dp = [[0 for i in range(sz)]for i in range(sz)]


def minInvalidAreaUtil(l, b):
    global dp, ok
    if (dp[l][b] == -1):
        if (ok[l][b]):
            dp[l][b] = 0
            return dp[l][b]
        ans = l * b
        for i in range(1, b):
            ans = min(
                ans,
                minInvalidAreaUtil(
                    l,
                    i) +
                minInvalidAreaUtil(
                    l,
                    b -
                    i))
        for i in range(1, l):
            ans = min(
                ans,
                minInvalidAreaUtil(
                    i,
                    b) +
                minInvalidAreaUtil(
                    l -
                    i,
                    b))
        dp[l][b] = ans
    return dp[l][b]


def minInvalidArea(N, M, dimensions):
    global dp, ok
    K = len(dimensions)
    for i in range(K):
        ok[dimensions[i][0]][dimensions[i][1]] = 1
        ok[dimensions[i][1]][dimensions[i][0]] = 1
    for i in range(sz):
        for j in range(sz):
            dp[i][j] = -1
    minArea = minInvalidAreaUtil(N, M)
    print(minArea)


if __name__ == '__main__':
    N, M = 10, 10
    dimensions = [[3, 5]]
    minInvalidArea(N, M, dimensions)

# Expected output:
10
# count unordered pairs of equal elements for all subarrays | function to count all pairs ( i , j ) such that arr [ i ] equals arr [ j ] in all possible subarrays of the array ; stores the size of the array ; stores the positions of all the distinct elements ; append index corresponding to arr [ i ] in the map ; traverse the map m ; traverse the array ; update the value of ans ; update the value of the sum ; print the value of ans ; 
def countPairs(arr):
    N = len(arr)
    ans = 0
    M = {}
    for i in range(len(arr)):
        if arr[i] in M:
            M[arr[i]].append(i)
        else:
            M[arr[i]] = [i]
    for key, value in M .items():
        v = value
        sum1 = 0
        for j in range(len(v)):
            ans += (sum1 * (N - v[j]))
            sum1 += v[j] + 1
    print(ans)


if __name__ == '__main__':
    arr = [1, 2, 1, 1]
    countPairs(arr)

# Expected output:
6
# number of alternating substrings from a given binary string | function to count number of alternating substrings from a given binary string ; initialize dp array , where dp [ i ] [ j ] stores the number of alternating strings starts with i and having j elements . ; traverse the string from the end ; if i is equal to n - 1 ; otherwise , ; increment count of substrings starting at i and has 0 in the beginning ; increment count of substrings starting at i and has 1 in the beginning ; stores the result ; iterate in the range [ 0 , n - 1 ] ; update ans ; return the ans ; 
def countAlternatingSubstrings(S, N):
    dp = [[0 for i in range(N)]for i in range(2)]
    for i in range(N - 1, -1, -1):
        if (i == N - 1):
            if (S[i] == '1'):
                dp[1][i] = 1
            else:
                dp[0][i] = 1
        else:
            if (S[i] == '0'):
                dp[0][i] = 1 + dp[1][i + 1]
            else:
                dp[1][i] = 1 + dp[0][i + 1]
    ans = 0
    for i in range(N):
        ans += max(dp[0][i], dp[1][i])
    return ans


if __name__ == '__main__':
    S = "0010"
    N = len(S)
    print(countAlternatingSubstrings(S, N))

# Expected output:
7
# count ways to place ' + ' and ' | function to count number of ways ' + ' and ' - ' operators can be placed in front of array elements to make the sum of array elements equal to k ; stores sum of the array ; stores count of 0 s in a [ ] ; traverse the array ; update sum ; update count of 0 s ; conditions where no arrangements are possible which adds up to k ; required sum ; dp array ; base cases ; fill the dp array ; return answer ; 
def solve(A, N, K):
    sum = 0
    c = 0
    for i in range(N):
        sum += A[i]
        if (A[i] == 0):
            c += 1
    if (K > sum or (sum + K) % 2):
        return 0
    sum = (sum + K) // 2
    dp = [[0 for i in range(sum + 1)]for j in range(N + 1)]
    for i in range(sum + 1):
        dp[0][i] = 0
    for i in range(N + 1):
        dp[i][0] = 1
    for i in range(1, N + 1, 1):
        for j in range(1, sum + 1, 1):
            if (A[i - 1] <= j and A[i - 1]):
                dp[i][j] = (dp[i - 1][j] + dp[i - 1][j - A[i - 1]])
            else:
                dp[i][j] = dp[i - 1][j]
    return dp[N][sum] + pow(2, c)


if __name__ == '__main__':
    A = [1, 1, 2, 3]
    N = len(A)
    K = 3
    print(solve(A, N, K))

# Expected output:
3
# rearrange an array to maximize sum of bitwise and of same | function to implement recursive dp ; if i is equal to n ; if dp [ i ] [ mask ] is not equal to - 1 ; iterate over the array b [ ] ; if current element is not yet selected ; update dp [ i ] [ mask ] ; return dp [ i ] [ mask ] ; function to obtain maximum sum of bitwise and of same - indexed elements from the arrays a [ ] and b [ ] ; stores all dp - states ; returns the maximum value returned by the function maximizeand ( ) ; 
def maximizeAnd(i, mask, A, B, N, dp):
    if (i == N):
        return 0
    if (dp[i][mask] != -1):
        return dp[i][mask]
    for j in range(N):
        if ((mask & (1 << j)) == 0):
            dp[i][mask] = max(
                dp[i][mask],
                (A[i] & B[j]) +
                maximizeAnd(
                    i +
                    1,
                    mask | (
                        1 << j),
                    A,
                    B,
                    N,
                    dp))
    return dp[i][mask]


def maximizeAndUtil(A, B, N):
    temp = [-1 for i in range(1 << N + 1)]
    dp = [temp for i in range(N)]
    return maximizeAnd(0, 0, A, B, N, dp)


if __name__ == '__main__':
    A = [3, 5, 7, 11]
    B = [2, 6, 10, 12]
    N = len(A)
    print(maximizeAndUtil(A, B, N))

# Expected output:
22