giulio98/xlcost-single-prompt huggingface dataset:
# maximize minimum array element possible by exactly k decrements | function to find the maximized minimum element of the array after performing given operation exactly k times ; stores the minimum element ; traverse the given array ; update the minimum element ; stores the required operations to make all elements equal to the minimum element ; update required operations ; if reqoperations < k ; decrement the value of k by reqoperations ; update minelement ; return minimum element ; 
def minimumElement(arr, N, K):
    minElement = arr[0]
    for i in range(N):
        minElement = min(minElement, arr[i])
    reqOperations = 0
    for i in range(N):
        reqOperations += arr[i] - minElement
    if (reqOperations < K):
        K -= reqOperations
        minElement -= (K + N - 1) // N
    return minElement


arr = [10, 10, 10, 10]
K = 7
N = len(arr)
print(minimumElement(arr, N, K))

# Expected output:
8
# split the fraction into sum of multiple fractions having numerator as 1 | function to split the fraction into distinct unit fraction ; to store answer ; while numerator is positive ; finding x = ceil ( d / n ) ; add 1 / x to list of ans ; update fraction ; 
def FractionSplit(n, d):
    UnitFactions = []
    while (n > 0):
        x = (d + n - 1) // n
        s = "1/" + str(x)
        UnitFactions .append(s)
        n = n * x - d
        d = d * x
    return UnitFactions


n = 13
d = 18
res = FractionSplit(n, d)
for s in res:
    print(s + ", ", end=" ")

# Expected output:
1/2,  1/5,  1/45,
# find permutation of [ 1 , n ] such that ( arr [ i ] != i + 1 ) and sum of absolute difference between arr [ i ] and ( i + 1 ) is minimum | function to generate the permutation of the first n natural numbers having sum of absolute difference between element and indices as minimum ; initialize array arr [ ] from 1 to n ; swap alternate positions ; check n is greater than 1 and n is odd ; swapping last two positions ; print the permutation ; 
def findPermutation(N):
    arr = [i + 1 for i in range(N)]
    for i in range(1, N, 2):
        arr[i], arr[i - 1] = arr[i - 1], arr[i]
    if N % 2 and N > 1:
        arr[-1], arr[-2] = arr[-2], arr[-1]
    for i in arr:
        print(i, end=" ")


if __name__ == '__main__':
    N = 7
    findPermutation(N)

# Expected output:
2 1 4 3 6 7 5
# find array obtained after adding terms of ap for q queries | function to find array after performing the given query to the array elements ; traverse the given query ; traverse the given array ; update the value of a [ i ] ; update the value of curr ; print the array elements ; 
def addAP(A, Q, operations):
    for L, R, a, d in operations:
        curr = a
        for i in range(L - 1, R):
            A[i] += curr
            curr += d
    for i in A:
        print(i, end=' ')


A = [5, 4, 2, 8]
Q = 2
Query = [(1, 2, 1, 3), (1, 4, 4, 1)]
addAP(A, Q, Query)

# Expected output:
10 13 8 15
# maximize the number n by inserting given digit at any position | function to find the maximum value of n after inserting the digit k ; convert it into n to string ; stores the maximum value of n after inserting k ; iterate till all digits that are not less than k ; add the current digit to the string result ; add digit ' k ' to result ; iterate through all remaining characters ; add current digit to result ; print the maximum number formed ; 
def maximizeNumber(N, K):
    s = str(N)
    L = len(s)
    result = ""
    i = 0
    while ((i < L) and (K <= (ord(s[i]) - ord('0')))):
        result += (s[i])
        i += 1
    result += (chr(K + ord('0')))
    while (i < L):
        result += (s[i])
        i += 1
    print(result)


if __name__ == "__main__":
    N = 6673
    K = 6
    maximizeNumber(N, K)

# Expected output:
66763
# count of n | function to find the value of x to the power y ; stores the value of x ^ y ; if y is odd , multiply x with result ; update the value of y and x ; return the result ; function to count number of arrays having element over the range [ 0 , 2 ^ k - 1 ] with bitwise and value 0 having maximum possible sum ; print the value of n ^ k ; 
def power(x, y):
    res = 1
    while (y > 0):
        if (y & 1):
            res = res * x
        y = y >> 1
        x = x * x
    return res


def countArrays(N, K):
    print(power(N, K))


N = 5
K = 6
countArrays(N, K)

# Expected output:
15625
# minimum absolute value of ( k – arr [ i ] ) for all possible values of k over the range [ 0 , n – 1 ] | function to find the minimum absolute value of ( k - arr [ i ] ) for each possible value of k over the range [ 0 , n - 1 ] | ; traverse the given array arr [ ] ; stores the mininimum distance to any array element arr [ i ] ; check if there is no safe position smaller than i ; check if the current position is between two safe positions ; take the minimum of two distances ; check if the current index is a safe position ; check if there is no safe position greater than i ; print the minimum distance ; 
def minimumDistance(arr, N):
    ind = 0
    prev = arr[ind]
    s = len(arr)
    for i in range(N):
        distance = 10 ** 9
        if (i < arr[0]):
            distance = arr[0] - i
        elif (i >= prev and ind + 1 < s and i <= arr[ind + 1]):
            distance = min(i - prev, arr[ind + 1] - i)
            if (i == arr[ind + 1]):
                distance = 0
                prev = arr[ind + 1]
                ind += 1
        else:
            distance = i - prev
        print(distance, end=" ")


N = 5
arr = [0, 4]
minimumDistance(arr, N)

# Expected output:
0 1 2 1 0
# count of pairs in array such that bitwise and of xor of pair and x is 0 | function to find the number of pairs that satisfy the given criteria i . e . , i < j and ( arr [ i ] ^ arr [ j ] ) & x is 0 ; stores the resultant count of pairs ; iterate over the range [ 0 , n ) ; iterate over the range ; check for the given condition ; return the resultant count ; 
def countOfPairs(arr, N, X):
    count = 0
    for i in range(N - 1):
        for j in range(i + 1, N):
            if (((arr[i] ^ arr[j]) & X) == 0):
                count += 1
    return count


if __name__ == '__main__':
    arr = [3, 2, 5, 4, 6, 7]
    X = 6
    N = len(arr)
    print(countOfPairs(arr, N, X))

# Expected output:
3
# count of pairs in array such that bitwise and of xor of pair and x is 0 | function to find the number of pairs that satisfy the given criteria i . e . , i < j and ( arr [ i ] ^ arr [ j ] ) & x is 0 ; stores the resultant count of pairs ; initialize the dictionary m ; populate the map ; count number of pairs for every element in map using mathematical concept of combination ; as nc2 = n * ( n - 1 ) / 2 ; return the resultant count ; 
def countOfPairs(arr, N, X):
    count = 0
    M = dict()
    for i in range(0, N):
        k = arr[i] & X
        if k in M:
            M[k] += 1
        else:
            M[k] = 1
    for m in M .keys():
        p = M[m]
        count += p * (p - 1) // 2
    return count


if __name__ == '__main__':
    arr = [3, 2, 5, 4, 6, 7]
    X = 6
    N = len(arr)
    print(countOfPairs(arr, N, X))

# Expected output:
3
# count of square submatrices with average at least k | function to count submatrixes with average greater than or equals to k ; stores count of submatrices ; stores the prefix sum of matrix ; iterate over the range [ 1 , n ] ; iterate over the range [ 1 , m ] ; update the prefix sum ; iterate over the range [ 1 , n ] ; iterate over the range [ 1 , m ] ; iterate until l and r are greater than 0 ; update count ; stores sum of submatrix with bottom right corner as ( i , j ) and top left corner as ( l , r ) ; if sum1 is less than or equal to sum2 ; increment cnt by 1 ; return cnt as the answer ; 
def cntMatrices(arr, N, M, K):
    cnt = 0
    pre = [[0 for i in range(M + 1)]for i in range(N + 1)]
    for i in range(1, N + 1):
        for j in range(1, M + 1):
            pre[i][j] = (arr[i - 1][j - 1] + pre[i - 1][j] +
                         pre[i][j - 1] - pre[i - 1][j - 1])
    for i in range(1, N + 1):
        for j in range(1, M + 1):
            l, r = i, j
            while l > 0 and r > 0:
                sum1 = (K * (i - l + 1) * (i - r + 1))
                sum2 = (pre[i][j] - pre[l - 1][r] -
                        pre[l][r - 1] + pre[l - 1][r - 1])
                if (sum1 <= sum2):
                    cnt += 1
                l -= 1
                r -= 1
    return cnt


if __name__ == '__main__':
    arr = [[2, 2, 3], [3, 4, 5], [4, 5, 5]]
    K = 4
    N = len(arr)
    M = len(arr[0])
    print(cntMatrices(arr, N, M, K))

# Expected output:
7
# concatenate the array of elements into a single element | python3 program for the above approach ; function to find the integer value obtained by joining array elements together ; stores the resulting integer value ; traverse the array arr [ ] ; stores the count of digits of arr [ i ] ; update ans ; increment ans by arr [ i ] ; return the ans ; 

import math


def ConcatenateArr(arr, N):
    ans = arr[0]
    for i in range(1, N):
        l = math .floor(math .log10(arr[i]) + 1)
        ans = ans * math .pow(10, l)
        ans += arr[i]
    return int(ans)


if __name__ == "__main__":
    arr = [1, 23, 456]
    N = len(arr)
    print(ConcatenateArr(arr, N))

# Expected output:
123456
# count of integers k in range [ 0 , n ] such that ( k xor k + 1 ) equals ( k + 2 xor k + 3 ) | function to count all the integers less than n satisfying the given condition ; store the count of even numbers less than n + 1 ; return the count ; 
def countXor(N):
    cnt = N // 2 + 1
    return cnt


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

# Expected output:
3
# maximize absolute displacement from origin by moving on x | recursive function to find the maximum absolute displacement from origin by performing the 
def DistRecursion(S, i, dist):
    if i == len(S):
        return abs(dist)
    if S[i] == 'L':
        return DistRecursion(S, i + 1, dist-1)
    if S[i] == 'R':
        return DistRecursion(S, i + 1, dist + 1)
    return max(DistRecursion(S, i + 1, dist-1),
               DistRecursion(S, i + 1, dist + 1))


def maxDistance(S):
    return DistRecursion(S, 0, 0)


S = "?RRR?"
print(maxDistance(S))

# Expected output:
5
# count of pairs in range [ p , q ] with numbers as multiple of r and their product lie in range [ p * q / 4 , p * q ] | function to find the number of pairs such that both the elements are in the range [ p , q ] and the numbers should be multiple of r , and the product of numbers should lie in the range [ p * q / 4 , p * q ] ; store multiple of r in range of [ p , q ] ; itearte in the range [ p , q ] ; vector to store pair of answer ; iterate through the vector v ; iterate in the range [ i + 1 , v . size ( ) - 1 ] ; if pair follow this condition insert the pair in vector ans ; if no pair satisfy the conditions , pr - 1 ; print the pairs which satisfy the given condition ; 
def findPairs(p, q, r):
    v = []
    for i in range(p, q + 1):
        if (i % r == 0):
            v .append(i)
    ans = []
    for i in range(len(v)):
        for j in range(i + 1, len(v)):
            if (v[i] * v[j] >= p * q // 4 and v[i] * v[j] <= p * q):
                ans .append([v[i], v[j]])
    if (len(ans) == 0):
        print(-1)
    else:
        for i in range(len(ans)):
            print(ans[i][0], ans[i][1])


if __name__ == '__main__':
    p = 14
    q = 30
    r = 5
    findPairs(p, q, r)

# Expected output:
15 20
15 25
# print rectangular pattern with given center | function to print the matrix filled with rectangle pattern having center coordinates are c1 , c2 ; iterate in the range [ 0 , n - 1 ] ; iterate in the range [ 0 , n - 1 ] ; 
def printRectPattern(c1, c2, n):
    for i in range(n):
        for j in range(n):
            print(max(abs(c1 - i), abs(c2 - j)), end=" ")
        print("")


c1 = 2
c2 = 2
n = 5
printRectPattern(c1, c2, n)

# Expected output:
2 2 2 2 2 
2 1 1 1 2 
2 1 0 1 2 
2 1 1 1 2 
2 2 2 2 2
# count of numbers in range [ l , r ] with lsb as 0 in their binary representation | function to return the count of required numbers ; if rightmost bit is 0 ; return the required count ; 
def countNumbers(l, r):
    count = 0
    for i in range(l, r + 1):
        if ((i & 1) == 0):
            count += 1
    return count


l = 10
r = 20
print(countNumbers(l, r))

# Expected output:
6
# count of numbers in range [ l , r ] with lsb as 0 in their binary representation | function to return the count of required numbers ; count of numbers in range which are divisible by 2 ; 
def countNumbers(l, r):
    return ((r // 2) - (l - 1) // 2)


l = 10
r = 20
print(countNumbers(l, r))

# Expected output:
6
# length of longest fibonacci subarray | python3 program for the above approach ; a utility function that returns true if x is perfect square ; returns true if n is a fibonacci number , else false ; here n is fibonacci if one of 5 * n * n + 4 or 5 * n * n - 4 or both is a perfect square ; function to find the length of the largest sub - array of an array every element of whose is a fibonacci number ; traverse the array arr ; check if arr [ i ] is a fibonacci number ; stores the maximum length of the fibonacci number subarray ; finally , return the maximum length ; 

import math


def isPerfectSquare(x):
    s = int(math .sqrt(x))
    if s * s == x:
        return True
    else:
        return False


def isFibonacci(n):
    return (isPerfectSquare(5 * n * n + 4) or isPerfectSquare(5 * n * n - 4))


def contiguousFibonacciNumber(arr, n):
    current_length = 0
    max_length = 0
    for i in range(0, n):
        if isFibonacci(arr[i]):
            current_length += 1
        else:
            current_length = 0
        max_length = max(max_length, current_length)
    return max_length


if __name__ == '__main__':
    arr = [11, 8, 21, 5, 3, 28, 4]
    n = len(arr)
    print(contiguousFibonacciNumber(arr, n))

# Expected output:
4
# minimum number of flips required such that a binary matrix doesn 't contain any path from the top left to the bottom right consisting only of 0s | the four direction coordinates changes from the current cell ; function that returns true if there exists any path from the top - left to the bottom - right cell of 0 s ; if the bottom - right cell is reached ; update the cell to 1 ; traverse in all four directions ; find the new coordinates ; if the new cell is valid ; recursively call dfs ; if path exists , then return true ; return false , if there doesn 't  exists any such path ; function to flip the minimum number of cells such that there doesn 't exists any such path from (0, 0) to (n - 1, m - 1) cell consisting of 0s ; case 1 : if no such path exists already ; case 2 : if there exists only one path ; case 3 : if there exists two - path ; 

direction = [[-1, 0], [0, 1], [0, -1], [1, 0]]


def dfs(i, j, N, M):
    global matrix
    if (i == N - 1 and j == M - 1):
        return True
    matrix[i][j] = 1
    for k in range(4):
        newX = i + direction[k][0]
        newY = j + direction[k][1]
        if (newX >= 0 and newX < N and newY >=
                0 and newY < M and matrix[newX][newY] == 0):
            if (dfs(newX, newY, N, M)):
                return True
    return False


def solve():
    global matrix
    N = len(matrix)
    M = len(matrix[0])
    if (not dfs(0, 0, N, M)):
        return 0
    if (not dfs(0, 0, N, M)):
        return 1
    return 2


if __name__ == '__main__':
    matrix = [[0, 1, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0]]
    print(solve())

# Expected output:
1
# maximize product of subarray sum with its maximum element | function to find the maximum product of the sum of the subarray with its maximum element ; traverse the array arr [ ] ; increment currsum by a [ i ] ; maximize the value of currmax ; maximize the value of largestsum ; if currsum goes less than 0 then update currsum = 0 ; return the resultant value ; function to maximize the product of the sum of the subarray with its maximum element ; find the largest sum of the subarray ; multiply each array element with - 1 ; find the largest sum of the subarray with negation of all array element ; return the resultant maximum value ; 
def Kadane(arr, n):
    largestSum = 0
    currMax = 0
    currSum = 0
    for i in range(n):
        currSum += arr[i]
        currMax = max(currMax, arr[i])
        largestSum = max(largestSum, currMax * currSum)
        if (currSum < 0):
            currMax = 0
            currSum = 0
    return largestSum


def maximumWeight(arr, n):
    largestSum = Kadane(arr, n)
    for i in range(n):
        arr[i] = -arr[i]
    largestSum = max(largestSum, Kadane(arr, n))
    return largestSum


if __name__ == '__main__':
    arr = [2, -3, 8, -2, 5]
    N = len(arr)
    print(maximumWeight(arr, N))

# Expected output:
88
# partition an array into two subsets with equal count of unique elements | function to partition the array into two subsets such that count of unique elements in both subsets is the same ; stores the subset number for each array elements ; stores the count of unique array elements ; stores the frequency of elements ; traverse the array ; count of elements having a frequency of 1 ; check if there exists any element with frequency > 2 ; count of elements needed to have frequency exactly 1 in each subset ; initialize all values in the array ans [ ] as 1 ; traverse the array ans [ ] ; this array element is a part of first subset ; half array elements with frequency 1 are part of the second subset ; if count of elements is exactly 1 are odd and has no element with frequency > 2 ; if count of elements that occurs exactly once are even ; print the result ; if the count of elements has exactly 1 frequency are odd and there is an element with frequency greater than 2 ; print the result ; 
def arrayPartition(a, n):
    ans = [0] * n
    cnt = 0
    ind, flag = 0, 0
    mp = {}
    for i in a:
        mp[i] = mp .get(i, 0) + 1
    for i in range(n):
        if ((a[i] in mp) and mp[a[i]] == 1):
            cnt += 1
        if (mp[a[i]] > 2 and flag == 0):
            flag = 1
            ind = i
    p = (cnt + 1) // 2
    ans1 = 0
    for i in range(n):
        ans[i] = 1
    for i in range(n):
        if ((a[i] in mp) and mp[a[i]] == 1 and ans1 < p):
            ans[i] = 1
            ans1 += 1
        elif ((a[i] in mp) and mp[a[i]] == 1):
            ans[i] = 2
    if (cnt % 2 == 1 and flag == 0):
        print(-1)
        return
    if (cnt % 2 == 0):
        print(*ans)
    else:
        for i in range(n):
            if (ind == i):
                print(2, end=" ")
            else:
                print(ans[i], end=" ")


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

# Expected output:
1 1 1 2 1 1
# modify a given array by replacing each element with the sum or product of their digits based on a given condition | function to modify the given array as per the given conditions ; traverse the given array arr [ ] ; initialize the count of even and odd digits ; initialize temp with the current array element ; for count the number of even digits ; increment the odd count ; otherwise ; divide temp by 10 ; performe addition ; performe multiplication ; otherwise ; 
def evenOdd(arr, N):
    for i in range(N):
        even_digits = 0
        odd_digits = 0
        temp = arr[i]
        while (temp):
            if ((temp % 10) & 1):
                odd_digits += 1
            else:
                even_digits += 1
            temp = temp // 10
        if (even_digits > odd_digits):
            res = 0
            while (arr[i]):
                res += arr[i] % 10
                arr[i] = arr[i] // 10
            print(res, end=" ")
        elif (odd_digits > even_digits):
            res = 1
            while (arr[i]):
                res *= arr[i] % 10
                arr[i] = arr[i] // 10
            print(res, end=" ")
        else:
            print(arr[i], end=" ")


arr = [113, 141, 214, 3186]
N = len(arr)
evenOdd(arr, N)

# Expected output:
3 4 7 3186
# find prime factors of z such that z is product of all even numbers till n that are product of two distinct prime numbers | function to print the prime factorization of the product of all numbers <= n that are even and can be expressed as a product of two distinct prime numbers ; sieve of eratosthenese ; store prime numbers in the range [ 3 , n / 2 ] ; print the coefficient of 2 in the prime factorization ; print the coefficients of other primes ; 
def primeFactorization(N):
    sieve = [0 for i in range(N // 2 + 1)]
    for i in range(2, N // 2 + 1, 1):
        if (sieve[i] == 0):
            for j in range(i * i, N // 2 + 1, i):
                sieve[j] = 1
    prime = []
    for i in range(3, N // 2 + 1, 1):
        if (sieve[i] == 0):
            prime .append(i)
    x = len(prime)
    print("2->", x)
    for i in prime:
        print(i, "->1")


if __name__ == '__main__':
    N = 18
    primeFactorization(N)

# Expected output:
2-> 3
3 ->1
5 ->1
7 ->1
# sum of the first m elements of array formed by infinitely concatenating given array | function to find the sum of first m numbers formed by the infinite concatenation of the array a [ ] ; stores the resultant sum ; iterate over the range [ 0 , m - 1 ] ; add the value a [ i % n ] to sum ; return the resultant sum ; 
def sumOfFirstM(A, N, M):
    sum = 0
    for i in range(M):
        sum = sum + A[i % N]
    return sum


if __name__ == '__main__':
    arr = [1, 2, 3]
    M = 5
    N = len(arr)
    print(sumOfFirstM(arr, N, M))

# Expected output:
9
# check if it is possible to reach m from 0 by given paths | function to check if it is possible to reach m from 0 ; stores the farther point that can reach from 1 point ; stores the farthest point it can go for each index i ; initialize rightmost [ i ] with 0 ; traverse the array ; update the rightmost position reached from a1 ; find the farthest point it can reach from i ; if point < can be reached ; 
def canReach0toM(a, n, m):
    rightMost = [0 for i in range(m + 1)]
    dp = [0 for i in range(m + 1)]
    for i in range(m + 1):
        rightMost[i] = 0
    for i in range(n):
        a1 = a[i][0]
        b1 = a[i][1]
        rightMost[a1] = max(rightMost[a1], b1)
    i = m
    while (i >= 0):
        dp[i] = i
        j = min(m, rightMost[i])
        while (j > i):
            dp[i] = max(dp[i], dp[j])
            j -= 1
        i -= 1
    if (dp[0] >= m):
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    arr = [[0, 2], [2, 2], [2, 5], [4, 5]]
    M = 5
    N = len(arr)
    canReach0toM(arr, N, M)

# Expected output:
Yes
# count of non co | recursive program to return gcd of two numbers ; function to count the number of non co - prime pairs for each query ; traverse the array arr [ ] ; stores the count of non co - prime pairs ; iterate over the range [ 1 , x ] ; iterate over the range [ x , y ] ; if gcd of current pair is greater than 1 ; 
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


def countPairs(arr, N):
    for i in range(0, N):
        count = 0
        for x in range(1, arr[i] + 1):
            for y in range(x, arr[i] + 1):
                if gcd(x, y) > 1:
                    count += 1
        print(count, end=" ")


if __name__ == '__main__':
    arr = [5, 10, 20]
    N = len(arr)
    countPairs(arr, N)

# Expected output:
5 23 82
# maximum number of multiplication by 3 or division by 2 operations possible on an array | function to count maximum number of multiplication by 3 or division by 2 operations that can be performed ; stores the maximum number of operations possible ; traverse the array arr [ ] ; iterate until arr [ i ] is even ; increment count by 1 ; update arr [ i ] ; return the value of count as the answer ; 
def maximumTurns(arr, N):
    Count = 0
    for i in range(0, N):
        while (arr[i] % 2 == 0):
            Count += 1
            arr[i] = arr[i] // 2
    return Count


arr = [5, 2, 4]
M = 3
K = 2
N = len(arr)
print(maximumTurns(arr, N))

# Expected output:
3