giulio98/xlcost-single-prompt huggingface dataset:
# find the winner of a game of removing at most 3 stones from a pile in each turn | function to find the maximum score of player 1 ; base case ; if the result is already computed , then return the result ; variable to store maximum score ; pick one stone ; pick 2 stones ; pick 3 stones ; return the score of the player ; function to find the winner of the game ; create a 1d table , dp of size n ; store the result ; player 1 wins ; player 2 wins ; tie ; 
def maximumStonesUtil(arr, n, i, dp):
    if (i >= n):
        return 0
    ans = dp[i]
    if (ans != -1):
        return ans
    ans = -2 ** 31
    ans = max(ans, arr[i] - maximumStonesUtil(arr, n, i + 1, dp))
    if (i + 1 < n):
        ans = max(ans, arr[i] + arr[i + 1] -
                  maximumStonesUtil(arr, n, i + 2, dp))
    if (i + 2 < n):
        ans = max(ans, arr[i] + arr[i + 1] + arr[i + 2] -
                  maximumStonesUtil(arr, n, i + 3, dp))
    return ans


def maximumStones(arr, n):
    dp = [-1] * n
    res = maximumStonesUtil(arr, n, 0, dp)
    if (res > 0):
        return "Player1"
    elif (res < 0):
        return "Player2"
    else:
        return "Tie"


arr = [1, 2, 3, 7]
n = len(arr)
print(maximumStones(arr, n))

# Expected output:
Player2
# count ways to reach the nth station | function to find the number of ways to reach nth station ; declares the dp [ ] array ; only 1 way to reach station 1 ; find the remaining states from the 2 nd station ; if the train a is present at station i - 1 ; if the train b is present at station i - 2 ; if train c is present at station i - 3 ; the total number of ways to reach station i ; return the total count of ways ; 
def numberOfWays(N):
    DP = [[0 for i in range(5)]for i in range(N + 1)]
    DP[1][1] = 1
    DP[1][2] = 1
    DP[1][3] = 1
    DP[1][4] = 1
    for i in range(2, N + 1):
        if (i - 1 > 0 and DP[i - 1][1] > 0):
            DP[i][1] = DP[i - 1][4]
        if (i - 2 > 0 and DP[i - 2][2] > 0):
            DP[i][2] = DP[i - 2][4]
        if (i - 3 > 0 and DP[i - 3][3] > 0):
            DP[i][3] = DP[i - 3][4]
        DP[i][4] = (DP[i][1] + DP[i][2] + DP[i][3])
    return DP[N][4]


if __name__ == '__main__':
    N = 15
    print(numberOfWays(N))

# Expected output:
338
# number of m | function to find the number of m - length sorted arrays possible using numbers from the range [ 1 , n ] ; if size becomes equal to m , that means an array is found ; include current element , increase size by 1 and remain on the same element as it can be included again ; exclude current element ; return the sum obtained in both the cases ; 
def countSortedArrays(start, m, size, n):
    if (size == m):
        return 1
    if (start > n):
        return 0
    notTaken, taken = 0, 0
    taken = countSortedArrays(start, m, size + 1, n)
    notTaken = countSortedArrays(start + 1, m, size, n)
    return taken + notTaken


if __name__ == '__main__':
    n, m = 2, 3
    print(countSortedArrays(1, m, 0, n))

# Expected output:
4
# maximum subarray sum possible after removing at most one subarray | function to find maximum subarray sum possible after removing at most one subarray ; calculate the presum [ ] array ; update the value of sum ; update the value of maxsum ; update the value of presum [ i ] ; calculate the postsum [ ] array ; update the value of sum ; update the value of maxsum ; update the value of postsum [ i ] ; stores the resultant maximum sum of subarray ; update the value of ans ; print resultant sum ; 
def maximumSum(arr, n):
    preSum = [0] * n
    sum = 0
    maxSum = 0
    for i in range(n):
        sum = max(arr[i], sum + arr[i])
        maxSum = max(maxSum, sum)
        preSum[i] = maxSum
    sum = 0
    maxSum = 0
    postSum = [0] * (n + 1)
    for i in range(n - 1, -1, -1):
        sum = max(arr[i], sum + arr[i])
        maxSum = max(maxSum, sum)
        postSum[i] = maxSum
    ans = 0
    for i in range(n):
        ans = max(ans, preSum[i] + postSum[i + 1])
    print(ans)


arr = [7, 6, -1, -4, -5, 7, 1]
N = len(arr)
maximumSum(arr, N)

# Expected output:
21
# count all unique outcomes possible by performing s flips on n coins | function to recursively count the number of unique outcomes possible s flips are performed on n coins ; base cases ; recursive calls ; 
def numberOfUniqueOutcomes(N, S):
    if (S < N):
        return 0
    if (N == 1 or N == S):
        return 1
    return (
        numberOfUniqueOutcomes(
            N -
            1,
            S -
            1) +
        numberOfUniqueOutcomes(
            N -
            1,
            S -
            2))


if __name__ == '__main__':
    N, S = 3, 4
    print(numberOfUniqueOutcomes(N, S))

# Expected output:
3
# length of longest increasing subsequence in a string | function to find length of longest increasing subsequence in a string ; stores at every i - th index , the length of the longest increasing subsequence ending with character i ; size of string ; stores the length of lis ; iterate over each character of the string ; store position of the current character ; stores the length of lis ending with current character ; check for all characters less then current character ; include current character ; update length of longest increasing subsequence ; updating lis for current character ; return the length of lis ; 
def lisOtimised(s):
    dp = [0] * 30
    N = len(s)
    lis = -10 ** 9
    for i in range(N):
        val = ord(s[i]) - ord('a')
        curr = 0
        for j in range(val):
            curr = max(curr, dp[j])
        curr += 1
        lis = max(lis, curr)
        dp[val] = max(dp[val], curr)
    return lis


if __name__ == '__main__':
    s = "fdryutiaghfse"
    print(lisOtimised(s))

# Expected output:
4
# sum of length of two smallest subsets possible from a given array with sum at least k | python3 program for the above approach ; function to calculate sum of lengths of two smallest subsets with sum >= k ; sort the array in ascending order ; stores suffix sum of the array ; update the suffix sum array ; stores all dp - states ; initialize all dp - states with a maximum possible value ; base case ; traverse the array arr [ ] ; iterate over the range [ 0 , k ] ; if a [ i ] is equal to at least the required sum j for the current state ; if the next possible state doesn 't exist ; otherwise , update the current state to the minimum of the next state and state including the current element a [ i ] ; traverse the suffix sum array ; if suffix [ i ] - dp [ i ] [ k ] >= k ; sum of lengths of the two smallest subsets is obtained ; return - 1 , if there doesn 't  exist any subset of sum >= k ; 

MAX = 1e9


def MinimumLength(A, N, K):
    A .sort()
    suffix = [0] * (N + 1)
    for i in range(N - 1, -1, -1):
        suffix[i] = suffix[i + 1] + A[i]
    dp = [[0] * (K + 1)] * (N + 1)
    for i in range(N + 1):
        for j in range(K + 1):
            dp[i][j] = MAX
    dp[N][0] = 0
    for i in range(N - 1, -1, -1):
        for j in range(K, -1, -1):
            if (j <= A[i]):
                dp[i][j] = A[i]
                continue
            if (dp[i + 1][j - A[i]] == MAX):
                dp[i][j] = MAX
            else:
                dp[i][j] = min(dp[i + 1][j], dp[i + 1][j - A[i]] + A[i])
    for i in range(N - 1, -1, -1):
        if (suffix[i] - dp[i][K] >= K):
            return N - i
    return -1


arr = [7, 4, 5, 6, 8]
K = 13
N = len(arr)
print(MinimumLength(arr, N, K))

# Expected output:
4
# count distinct possible bitwise xor values of subsets of an array | stores the mask of the vector ; stores the current 20 of dp [ ] ; function to store the mask of given integer ; iterate over the range [ 0 , 20 ] ; if i - th bit 0 ; if dp [ i ] is zero ; store the position in dp ; increment the answer ; return from the loop ; mask = mask xor dp [ i ] ; function to find the 20 of the set having bitwise xor of all the subset of the given array ; traverse the array ; prthe answer ; 

dp = [0] * 20
ans = 0


def insertVector(mask):
    global dp, ans
    for i in range(20):
        if ((mask & 1 << i) == 0):
            continue
        if (not dp[i]):
            dp[i] = mask
            ans += 1
            return
        mask ^= dp[i]


def maxSizeSet(arr, N):
    for i in range(N):
        insertVector(arr[i])
    print((1 << ans))


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

# Expected output:
8
# maximize sum possible from an array by jumps of length i + k * arr [ i ] from any ith index | function to find the maximum sum possible by jumps of length i + k * arr [ i ] from any i - th index ; initialize an array dp [ ] ; stores the maximum sum ; iterate over the range [ n - 1 , 0 ] ; if length of the jump exceeds n ; set dp [ i ] as arr [ i ] ; otherwise , update dp [ i ] as sum of dp [ i + k * arr [ i ] ] and arr [ i ] ; update the overall maximum sum ; print the answer ; 
def maxSum(arr, N, K):
    dp = [0 for i in range(N + 2)]
    maxval = 0
    i = N - 1
    while (i >= 0):
        if ((i + K * arr[i]) >= N):
            dp[i] = arr[i]
        else:
            dp[i] = dp[i + K * arr[i]] + arr[i]
        maxval = max(maxval, dp[i])
        i -= 1
    print(maxval)


if __name__ == '__main__':
    arr = [2, 1, 3, 1, 2]
    N = len(arr)
    K = 3
    maxSum(arr, N, K)

# Expected output:
3
# maximum sum submatrix | function to find maximum sum submatrix ; stores the number of rows and columns in the matrix ; stores maximum submatrix sum ; take each row as starting row ; take each column as the starting column ; take each row as the ending row ; take each column as the ending column ; stores the sum of submatrix having topleft index ( i , j ) and bottom right index ( k , l ) ; iterate the submatrix row - wise and calculate its sum ; update the maximum sum ; prthe answer ; 
def maxSubmatrixSum(matrix):
    r = len(matrix)
    c = len(matrix[0])
    maxSubmatrix = 0
    for i in range(r):
        for j in range(c):
            for k in range(i, r):
                for l in range(j, c):
                    sumSubmatrix = 0
                    for m in range(i, k + 1):
                        for n in range(j, l + 1):
                            sumSubmatrix += matrix[m][n]
                    maxSubmatrix = max(maxSubmatrix, sumSubmatrix)
    print(maxSubmatrix)


if __name__ == '__main__':
    matrix = [[0, -2, -7, 0], [9, 2, -6, 2], [-4, 1, -4, 1], [-1, 8, 0, -2]]
    maxSubmatrixSum(matrix)

# Expected output:
15
# maximize sum of subsets from two arrays having no consecutive values | function to calculate maximum subset sum ; initialize array to store dp states ; base cases ; pre initializing for dp [ 0 ] & dp [ 1 ] ; calculating dp [ index ] based on above formula ; prmaximum subset sum ; 
def maximumSubsetSum(arr1, arr2, length):
    dp = [0] * (length + 1)
    if (length == 1):
        print(max(arr1[0], arr2[0]))
        return
    if (length == 2):
        print(max(max(arr1[1], arr2[1]), max(arr1[0], arr2[0])))
        return
    else:
        dp[0] = max(arr1[0], arr2[0])
        dp[1] = max(max(arr1[1], arr2[1]), max(arr1[0], arr2[0]))
        index = 2
        while (index < length):
            dp[index] = max(max(arr1[index], arr2[index]), max(
                max(arr1[index] + dp[index - 2], arr2[index] + dp[index - 2]), dp[index - 1]))
            index += 1
        print(dp[length - 1])


arr1 = [-1, -2, 4, -4, 5]
arr2 = [-1, -2, -3, 4, 10]
length = 5
maximumSubsetSum(arr1, arr2, length)

# Expected output:
14
# modify array by replacing every array element with minimum possible value of arr [ j ] + | j | function to find minimum value of arr [ j ] + | j - i | for every array index ; stores minimum of a [ j ] + | i - j | upto position i ; stores minimum of a [ j ] + | i - j | upto position i from the end ; traversing and storing minimum of a [ j ] + | i - j | upto i ; traversing and storing minimum of a [ j ] + | i - j | upto i from the end ; traversing from [ 0 , n ] and storing minimum of a [ j ] + | i - j | from starting and end ; print the required array ; 
def minAtEachIndex(n, arr):
    dp1 = [0] * n
    dp2 = [0] * n
    i = 0
    dp1[0] = arr[0]
    for i in range(1, n):
        dp1[i] = min(arr[i], dp1[i - 1] + 1)
    dp2[n - 1] = arr[n - 1]
    for i in range(n - 2, -1, -1):
        dp2[i] = min(arr[i], dp2[i + 1] + 1)
    v = []
    for i in range(0, n):
        v .append(min(dp1[i], dp2[i]))
    for x in v:
        print(x, end=" ")


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

# Expected output:
1 2 2 3 3
# count all n | function to prthe count of arrays satisfying given condition ; first element of array is set as 1 ; since the first element of arr is 1 , the second element can 't be 1 ; traverse the remaining indices ; if arr [ i ] = 1 ; if arr [ i ] a 1 ; since last element needs to be 1 ; 
def totalArrays(N, M):
    end_with_one = [0] * (N + 1)
    end_not_with_one = [0] * (N + 1)
    end_with_one[0] = 1
    end_not_with_one[0] = 0
    end_with_one[1] = 0
    end_not_with_one[1] = M - 1
    for i in range(2, N):
        end_with_one[i] = end_not_with_one[i - 1]
        end_not_with_one[i] = end_with_one[i - 1] * \
            (M - 1) + end_not_with_one[i - 1] * (M - 2)
    return end_with_one[N - 1]


if __name__ == '__main__':
    N = 3
    M = 3
    temp = totalArrays(N, M)
    ans = M * temp
    print(ans)

# Expected output:
6
# count numbers from a given range whose product of digits is k | function to find the product of digits of a number ; stores product of digits of n ; update res ; update n ; function to count numbers in the range [ 0 , x ] whose product of digit is k ; stores count of numbers in the range [ l , r ] whose product of digit is k ; iterate over the range [ l , r ] ; if product of digits of i equal to k ; update cnt ; 
def prodOfDigit(N):
    res = 1
    while (N):
        res = res * (N % 10)
        N //= 10
    return res


def cntNumRange(L, R, K):
    cnt = 0
    for i in range(L, R + 1):
        if (prodOfDigit(i) == K):
            cnt += 1
    return cnt


if __name__ == '__main__':
    L, R, K = 20, 10000, 14
    print(cntNumRange(L, R, K))

# Expected output:
20
# median of bitwise xor of all submatrices starting from the top left corner | function to find the median of bitwise xor of all the submatrix whose topmost leftmost corner is ( 0 , 0 ) ; dp [ i ] [ j ] : stores the bitwise xor of submatrix having top left corner at ( 0 , 0 ) and bottom right corner at ( i , j ) ; stores count of submatrix ; base case ; base case ; fill dp using tabulation ; fill dp [ i ] [ j ] ; 
def findMedXOR(mat, N, M):
    dp = [[0 for i in range(M)]for j in range(N)]
    med = [0] * (N * M)
    dp[0][0] = mat[0][0]
    med[0] = dp[0][0]
    len = 1
    for i in range(1, N):
        dp[i][0] = dp[i - 1][0] ^ mat[i][0]
        med[len] = dp[i][0]
        len += 1
    for i in range(1, M):
        dp[0][i] = dp[0][i - 1] ^ mat[0][i]
        med[len] = dp[0][i]
        len += 1
    for i in range(1, N):
        for j in range(1, M):
            dp[i][j] = dp[i - 1][j] ^ dp[i][j - 1] ^ dp[i - 1][j - 1] ^ mat[i][j]
            med[len] = dp[i][j]
            len += 1
    med .sort()
    if (len % 2 == 0):
        return (med[(len // 2)] + med[(len // 2) - 1]) / 2.0
    return med[len // 2]


if __name__ == '__main__':
    mat = [[1, 2], [2, 3]]
    N = len(mat[0])
    M = 2
    print(findMedXOR(mat, N, M))

# Expected output:
2.5
# count ways to tile an n | function to count the ways to tile n * 1 board using 1 * 1 and 2 * 1 tiles ; dp [ i ] : stores count of ways to tile i * 1 board using given tiles ; base case ; iterate over the range [ 2 , n ] ; fill dp [ i ] using the recurrence relation ; 
def countWaysToTileBoard(N):
    dp = [0] * (N + 1)
    dp[0] = 1
    dp[1] = 2
    for i in range(2, N + 1):
        dp[i] = (2 * dp[i - 1] + dp[i - 2])
    print(dp[N])


if __name__ == "__main__":
    N = 2
    countWaysToTileBoard(N)

# Expected output:
5
# subsequences generated by including characters or ascii value of characters of given string | function to print subsequences containing ascii value of the characters or the the characters of the given string ; base case ; if length of the subsequence exceeds 0 ; print the subsequence ; stores character present at i - th index of str ; if the i - th character is not included in the subsequence ; including the i - th character in the subsequence ; include the ascii value of the ith character in the subsequence ; 
def FindSub(string, res, i):
    if (i == len(string)):
        if (len(res) > 0):
            print(res, end=" ")
        return
    ch = string[i]
    FindSub(string, res, i + 1)
    FindSub(string, res + ch, i + 1)
    FindSub(string, res + str(ord(ch)), i + 1)


if __name__ == "__main__":
    string = "ab"
    res = ""
    N = len(string)
    FindSub(string, res, 0)

# Expected output:
b 98 a ab a98 97 97b 9798
# minimize given flips required to reduce n to 0 | function to find the minimum count of operations required to reduce n to 0 ; stores count of bits in n ; recurrence relation ; 

import math


def MinOp(N):
    if (N <= 1):
        return N
    bit = (int)(math .log(N) / math .log(2)) + 1
    return ((1 << bit) - 1) - MinOp(N - (1 << (bit - 1)))


if __name__ == '__main__':
    N = 4
    print(MinOp(N))

# Expected output:
7
# maximum non | function to find the maximum product from the top left and bottom right cell of the given matrix grid [ ] [ ] ; store dimension of grid ; stores maximum product path ; stores minimum product path ; traverse the grid and update maxpath and minpath array ; initialize to inf and - inf ; base case ; calculate for row : ; update the maximum ; update the minimum ; calculate for column : ; update the maximum ; update the minimum ; update maxpath and minpath ; if negative product ; otherwise ; 
def maxProductPath(grid):
    n, m = len(grid), len(grid[0])
    maxPath = [[0 for i in range(m)]for j in range(n)]
    minPath = [[0 for i in range(m)]for j in range(n)]
    for i in range(n):
        for j in range(m):
            mn = float("inf")
            mx = float("-inf")
            if (i == 0 and j == 0):
                mx = grid[i][j]
                mn = grid[i][j]
            if i > 0:
                tempmx = max((maxPath[i - 1][j] * grid[i][j]),
                             (minPath[i - 1][j] * grid[i][j]))
                mx = max(mx, tempmx)
                tempmn = min((maxPath[i - 1][j] * grid[i][j]),
                             (minPath[i - 1][j] * grid[i][j]))
                mn = min(mn, tempmn)
            if (j > 0):
                tempmx = max((maxPath[i][j - 1] * grid[i][j]),
                             (minPath[i][j - 1] * grid[i][j]))
                mx = max(mx, tempmx)
                tempmn = min((maxPath[i][j - 1] * grid[i][j]),
                             (minPath[i][j - 1] * grid[i][j]))
                mn = min(mn, tempmn)
            maxPath[i][j] = mx
            minPath[i][j] = mn
    if (maxPath[n - 1][m - 1] < 0):
        return -1
    else:
        return (maxPath[n - 1][m - 1])


mat = [[1, -2, 1], [1, -2, 1], [3, -4, 1]]
print(maxProductPath(mat))

# Expected output:
8
# smallest submatrix required to be removed such that sum of the remaining matrix is divisible by k | python3 program to implement the above approach ; function to find the length of the smallest subarray to be removed such that sum of elements is equal to s % k ; remainder when total_sum is divided by k ; stores curr_remainder and the most recent index at which curr_remainder has occured ; stores required answer ; add current element to curr_sum and take mod ; update current remainder index ; if mod already exists in map the subarray exists ; update res ; if not possible ; return the result ; function to find the smallest submatrix rqured to be deleted to make the sum of the matrix divisible by k ; stores the sum of element of the matrix ; traverse the matrix mat [ ] [ ] ; update s ; stores smallest area need to be deleted to get sum divisible by k ; stores leftmost column of each matrix ; stores rightmost column of each matrix ; stores number of coulmns deleted of a matrix ; store area of the deleted matrix ; prefixrowsum [ i ] : store sum of sub matrix whose topmost left and bottommost right position is ( 0 , left ) ( i , right ) ; iterate over all possible values of ( left , right ) ; initialize all possible values of prefixrowsum [ ] to 0 ; traverse each row from left to right column ; update row_sum [ i ] ; update width ; if no submatrix of the length ( right - left + 1 ) found to get the required output ; update area ; if area is less than min_area ; update min_area ; 

import sys


def removeSmallestSubarray(arr, S, n, k):
    target_remainder = S % k
    map1 = {}
    map1[0] = -1
    curr_remainder = 0
    res = sys .maxsize
    for i in range(n):
        curr_remainder = (curr_remainder + arr[i] + k) % k
        map1[curr_remainder] = i
        mod = (curr_remainder - target_remainder + k) % k
        if (mod in map1):
            res = min(res, i - map1[mod])
    if (res == sys .maxsize or res == n):
        res = -1
    return res


def smstSubmatDeleted(mat, N, M, K):
    S = 0
    for i in range(N):
        for j in range(M):
            S += mat[i][j]
    min_area = N * M
    left = 0
    right = 0
    width = 0
    area = 0
    prefixRowSm = [0] * N
    for left in range(M):
        prefixRowSum = [0] * N
        for right in range(left, M):
            for i in range(N):
                prefixRowSum[i] += mat[i][right]
            width = removeSmallestSubarray(prefixRowSum, S, N, K)
            if (width != -1):
                area = (right - left + 1) * (width)
                if (area < min_area):
                    min_area = area
    return min_area


if __name__ == '__main__':
    mat = [[6, 2, 6], [3, 2, 8], [2, 5, 3]]
    K = 3
    N = len(mat)
    M = len(mat[0])
    print(smstSubmatDeleted(mat, N, M, K))

# Expected output:
2
# Count N | to keep the string in lexicographically sorted order use start index to add the vowels starting the from that index ; base case : if string length is 0 add to the count ; if last character in string is ' e ' add vowels starting from ' e ' i . e ' e ' , ' i ' , ' o ' , 'u ; decrease the length of string ; char arr [ 5 ] = { ' a ' , ' e ' , ' i ' , ' o ' , ' u ' } ; starting from index 0 add the vowels to strings
def countstrings(n, start):
    if n == 0:
        return 1
    cnt = 0
    for i in range(start, 5):
        cnt += countstrings(n - 1, i)
    return cnt


def countVowelStrings(n):
    return countstrings(n, 0)


n = 2
print(countVowelStrings(n))

# Expected output:
15
# count n | function to count n - length strings consisting of vowels only sorted lexicographically ; 
def findNumberOfStrings(n):
    return int((n + 1) * (n + 2) * (n + 3) * (n + 4) / 24)


if __name__ == '__main__':
    N = 2
    print(findNumberOfStrings(N))

# Expected output:
15
# count unique paths with given sum in an n | python3 program for the above approach ; function for counting total no of paths possible with the sum is equal to x ; if the path of the sum from the root to current node is stored in sum ; if already computed ; count different no of paths using all possible ways ; return total no of paths ; 

mod = int(1e9 + 7)


def findTotalPath(X, n, dp):
    if (X == 0):
        return 1
    ans = 0
    if (dp[X] != -1):
        return dp[X]
    for i in range(1, min(X, n) + 1):
        ans = ans + findTotalPath(X - i, n, dp) % mod
        ans %= mod
    dp[X] = ans
    return ans


if __name__ == '__main__':
    n = 3
    X = 2
    dp = [-1] * (X + 1)
    print(findTotalPath(X, n, dp))

# Expected output:
2