giulio98/xlcost-single-prompt huggingface dataset:
# minimum increments required to make given matrix palindromic | function to evaluate minimum number of operation required to convert the matrix to a palindrome matrix ; variable to store number of operations required ; iterate over the first quadrant of the matrix ; store positions of all four values from four quadrants ; store the values having unique indexes ; largest value in the values vector ; evaluate minimum increments required to make all vector elements equal ; print the answer ; 
def palindromeMatrix(N, M, arr):
    ans = 0
    for i in range((N + 1) // 2):
        for j in range((M + 1) // 2):
            s = {}
            s[(i, j)] = 1
            s[(i, M - j - 1)] = 1
            s[(N - i - 1, j)] = 1
            s[(N - i - 1, M - j - 1)] = 1
            values = []
            for p, q in s:
                values .append(arr[p][q])
            maxm = max(values)
            for k in range(len(values)):
                ans += maxm - values[k]
    print(ans)


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

# Expected output:
2
# minimum division by 10 and multiplication by 2 required to reduce given number to 1 | function to find the minimum number operations required to reduce n to 1 ; stores count of powers of 2 and 5 ; calculating the primefactors 2 ; calculating the primefactors 5 ; if n is 1 and cnt2 <= cnt5 ; return the minimum operations ; otherwise , n can 't be reduced ; 
def minimumMoves(n):
    cnt2 = 0
    cnt5 = 0
    while (n % 2 == 0):
        n //= 2
        cnt2 += 1
    while (n % 5 == 0):
        n //= 5
        cnt5 += 1
    if (n == 1 and cnt2 <= cnt5):
        return 2 * cnt5 - cnt2
    else:
        return -1


if __name__ == '__main__':
    N = 25
    print(minimumMoves(N))

# Expected output:
4
# check if permutation of first n natural numbers exists having bitwise and of adjacent elements non | function to check if a permutation of first n natural numbers exist with bitwise and of adjacent elements not equal to 0 ; if n is a power of 2 ; 
def check(n):
    if ((n & n - 1) != 0):
        print("YES")
    else:
        print("NO")


if __name__ == '__main__':
    n = 5
    check(n)

# Expected output:
YES
# minimize adding odd and subtracting even numbers to make all array elements equal to k | function to find the minimum operations required to make array elements equal to k ; stores minimum count of operations ; traverse the given array ; if k is greater than arr [ i ] ; if ( k - arr [ i ] ) is even ; update cntope ; update cntope ; if k is less than arr [ i ] ; if ( arr [ i ] - k ) is even ; update cntope ; update cntope ; 
def MinOperation(arr, N, K):
    cntOpe = 0
    for i in range(N):
        if (K > arr[i]):
            if ((K - arr[i]) % 2 == 0):
                cntOpe += 2
            else:
                cntOpe += 1
        elif (K < arr[i]):
            if ((K - arr[i]) % 2 == 0):
                cntOpe += 1
            else:
                cntOpe += 2
    return cntOpe


arr = [8, 7, 2, 1, 3]
K = 5
N = len(arr)
print(MinOperation(arr, N, K))

# Expected output:
8
# rearrange array to make bitwise xor of similar indexed elements of two arrays is same | function to rearrange the array b [ ] such that a [ i ] ^ b [ i ] is same for each element ; store frequency of elements ; stores xor value ; taking xor of all the values of both arrays ; store frequency of b [ ] ; find the array b [ ] after rearrangement ; if the updated value is present then decrement its frequency ; otherwise return empty vector ; utility function to rearrange the array b [ ] such that a [ i ] ^ b [ i ] is same for each element ; store rearranged array b ; if rearrangement possible ; otherwise return - 1 ; 
def rearrangeArray(A, B, N):
    m = {}
    xor_value = 0

    for i in range(0, N):
        xor_value ^= A[i]
        xor_value ^= B[i]
        if B[i] in m:
            m[B[i]] = m[B[i]] + 1
        else:
            m[B[i]] = 1

    for i in range(0, N):
        B[i] = A[i] ^ xor_value
        if B[i] in m:
            m[B[i]] = m[B[i]] - 1
        else:
            X = []
            return X

    return B


def rearrangeArrayUtil(A, B, N):
    ans = rearrangeArray(A, B, N)
    if (len(ans) > 0):
        for x in ans:
            print(x, end=' ')
    else:
        print("-1")


if __name__ == '__main__':
    A = [13, 21, 33, 49, 53]
    B = [54, 50, 34, 22, 14]
    N = len(A)
    rearrangeArrayUtil(A, B, N)

# Expected output:
14 22 34 50 54
# count sequences of given length having non | function to find the binomial coefficient c ( n , r ) ; stores the value c ( n , r ) ; update c ( n , r ) = c ( n , n - r ) ; find c ( n , r ) iteratively ; return the final value ; function to find number of sequence whose prefix sum at each index is always non - negative ; find n ; value of c ( 2 n , n ) ; catalan number ; print the answer ; 
def binCoff(n, r):
    val = 1
    if (r > (n - r)):
        r = (n - r)
    for i in range(0, r):
        val *= (n - i)
        val //= (i + 1)
    return val


def findWays(M):
    n = M // 2
    a = binCoff(2 * n, n)
    b = a // (n + 1)
    print(b)


if __name__ == '__main__':
    M = 4
    X = 5
    findWays(M)

# Expected output:
2
# xor of array elements whose modular inverse with a given number exists | function to return the gcd of a & b ; base case ; recursively calculate gcd ; function to print the bitwise xor of elements of arr [ ] if gcd ( arr [ i ] , m ) is 1 ; initialize xor ; traversing the array ; gcd of m and arr [ i ] ; if gcd is 1 , update xor ; print xor ; 
def gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)


def countInverse(arr, N, M):
    XOR = 0
    for i in range(0, N):
        gcdOfMandelement = gcd(M, arr[i])
        if (gcdOfMandelement == 1):
            XOR = XOR ^ arr[i]
    print(XOR)


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

# Expected output:
2
# count positions in a chessboard that can be visited by the queen which are not visited by the king | function to print the number of cells only visited by the queen ; find all the moves ; find all moves for x + 1 , y + 1 ; find all moves for x - 1 , y - 1 ; find all moves for x - 1 , y + 1 ; find all moves for x + 1 , y - 1 ; find all squares visited by king x + 1 , in same row ; x - 1 , in same row ; y + 1 , in same column ; y - 1 , in same column ; return answer ; 
def Moves_Calculator(x, y, row, col):
    total_moves = 0
    if (row - x) > 0 and (col - y) > 0:
        total_moves += min((row - x), (col - y))
    if (y - 1) > 0 and (x - 1) > 0:
        total_moves += min((y - 1), (x - 1))
    if (x - 1) > 0 and (col - y) > 0:
        total_moves += min((x - 1), (col - y))
    if (row - x) > 0 and (y - 1) > 0:
        total_moves += min((row - x), (y - 1))
    total_moves += (row - 1) + (col - 1)
    king_moves = 0
    if x + 1 <= m:
        king_moves += 1
    if x - 1 > 0:
        king_moves += 1
    if y + 1 <= n:
        king_moves += 1
    if y - 1 > 0:
        king_moves += 1
    if x + 1 <= m and y + 1 <= n:
        king_moves += 1
    if x + 1 <= m and y - 1 > 0:
        king_moves += 1
    if x - 1 > 0 and y - 1 > 0:
        king_moves += 1
    if x - 1 > 0 and y + 1 <= n:
        king_moves += 1
    return total_moves - king_moves


if __name__ == '__main__':
    n, m = 8, 8
    x, y = 1, 1
    print(Moves_Calculator(x, y, m, n))

# Expected output:
18
# nearest smaller number to n having multiplicative inverse under modulo n equal to that number | function to find the nearest smaller number satisfying the condition ; 
def clstNum(N):
    return (N - 1)


if __name__ == '__main__':
    N = 11
    print(clstNum(N))

# Expected output:
10
# count array elements having modular inverse under given prime number p equal to itself | function to get the count of elements that satisfy the given condition . ; stores count of elements that satisfy the condition ; traverse the given array . ; if square of current element is equal to 1 ; 
def equvInverse(arr, N, P):
    cntElem = 0
    for i in range(0, N):
        if ((arr[i] * arr[i]) % P == 1):
            cntElem = cntElem + 1
    return cntElem


if __name__ == "__main__":
    arr = [1, 6, 4, 5]
    N = len(arr)
    P = 7
    print(equvInverse(arr, N, P))

# Expected output:
2
# count ways to split array into k non | function to get the value of pow ( k , m ) ; stores value of pow ( k , m ) ; calculate value of pow ( k , n ) ; if n is odd , update res ; update m to m / 2 ; update k ; function to print total ways to split the array that satisfies the given condition ; stores total ways that satisfies the given condition ; stores count of distinct elements in the given arr ; store distinct elements of the given array ; traverse the given array ; insert current element into set st . ; update m ; update cntways ; 
def power(K, M):
    res = 1
    while (M > 0):
        if ((M & 1) == 1):
            res = (res * K)
        M = M >> 1
        K = (K * K)
    return res


def cntWays(arr, N, K):
    cntways = 0
    M = 0
    st = set()
    for i in range(N):
        st .add(arr[i])
    M = len(st)
    cntways = power(K, M)
    return cntways


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

# Expected output:
4
# minimize product of first n | python3 program for the above approach ; function to find the minimum product of 1 to n - 1 after performing the given operations ; initialize ans with 1 ; multiply ans with n - 2 ( ( n - 4 ) / 2 ) times ; multiply ans with n - 1 and n - 2 once ; print ans ; 

mod = 1e9 + 7


def minProduct(n):
    ans = 1
    for i in range(1, (n - 4) // 2 + 1):
        ans = (ans * (n - 2)) % mod
    ans = (ans * (n - 2) * (n - 1)) % mod
    print(int(ans))


if __name__ == '__main__':
    N = 8
    minProduct(N)

# Expected output:
1512
# bitwise xor of all unordered pairs from a given array | function to get bitwise xor of all possible pairs of the given array ; stores bitwise xor of all possible pairs ; generate all possible pairs and calculate bitwise xor of all possible pairs ; calculate bitwise xor of each pair ; 
def TotalXorPair(arr, N):
    totalXOR = 0
    for i in range(0, N):
        for j in range(i + 1, N):
            totalXOR ^= arr[i] ^ arr[j]
    return totalXOR


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

# Expected output:
4
# count permutations of all integers upto n that can form an acyclic graph based on given conditions | find the count of possible graphs ; 
def possibleAcyclicGraph(N):
    print(pow(2, N - 1))
    return


if __name__ == '__main__':
    N = 4
    possibleAcyclicGraph(N)

# Expected output:
8
# minimize positive product of two given numbers by at most n decrements | function to minimize the product of two numbers ; reducing x , n times , minimizes the product ; reduce x to 1 and reduce remaining n from y ; reducing y , n times , minimizes the product ; reduce y to 1 and reduce remaining n from x ; 
def minProd(X, Y, N):
    if (X <= Y):
        if (N < X):
            return (X - N) * Y
        else:
            return max(Y - (N - X + 1), 1)
    if (Y >= N):
        return (Y - N) * X
    return max(X - (N - Y + 1), 1)


if __name__ == "__main__":
    X = 47
    Y = 42
    N = 167
    print(minProd(X, Y, N))

# Expected output:
1
# count pairs from an array having product of their sum and difference equal to 1 | function to count the desired number of pairs ; initialize onecount ; initialize the desiredpair ; traverse the given array ; if 1 is encountered ; if 0 is encountered ; update count of pairs ; return the final count ; 
def countPairs(arr, n):
    oneCount = 0
    desiredPair = 0
    for i in range(n):
        if (arr[i] == 1):
            oneCount += 1
        if (arr[i] == 0):
            desiredPair += oneCount
    return desiredPair


if __name__ == '__main__':
    arr = [3, 1, 1, 0]
    N = len(arr)
    print(countPairs(arr, N))

# Expected output:
2
# smallest missing non | function to prthe smallest missing non - negative integer up to every array indices ; stores the smallest missing non - negative integers between start index to current index ; store the boolean value to check smnonneg present between start index to each index of the array ; traverse the array ; since output always lies in the range [ 0 , n - 1 ] ; check if smnonneg is present between start index and current index or not ; print smallest missing non - negative integer ; 
def smlstNonNeg(arr, N):
    smNonNeg = 0
    hash = [0] * (N + 1)
    for i in range(N):
        if (arr[i] >= 0 and arr[i] < N):
            hash[arr[i]] = True
        while (hash[smNonNeg]):
            smNonNeg += 1
        print(smNonNeg, end=" ")


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

# Expected output:
1 2 3 4 4
# split array into two subsequences having minimum count of pairs with sum equal to x | function to split the array into two subsequences ; stores the two subsequences ; flag to set / reset to split arrays elements alternately into two arrays ; traverse the given array ; if 2 * arr [ i ] is less than x ; push element into the first array ; if 2 * arr [ i ] is greater than x ; push element into the second array ; if 2 * arr [ i ] is equal to x ; alternatively place the elements into the two arrays ; print both the arrays ; 
def solve(arr, N, X):
    A = []
    B = []
    c = 0
    for i in range(N):
        if ((2 * arr[i]) < X):
            A .append(arr[i])
        elif ((2 * arr[i]) > X):
            B .append(arr[i])
        else:
            if (c % 2 == 0):
                A .append(arr[i])
            else:
                B .append(arr[i])
            c += 1
    print("The First Array is - ", end=" ")
    for i in range(len(A)):
        print(A[i], end=" ")
    print()
    print("The Second Array is - ", end=" ")
    for i in range(len(B)):
        print(B[i], end=" ")


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

# Expected output:
The First Array is -  1 3 2 3 
The Second Array is -  5 4 6 4
# split array into minimum number of subsets having maximum pair sum at most k | function to get the minimum count of subsets that satisfy the given condition ; store the minimum count of subsets that satisfy the given condition ; stores start index of the sorted array . ; stores end index of the sorted array ; sort the given array ; traverse the array ; if only two elements of sorted array left ; if only one elements left in the array ; 
def cntMinSub(arr, N, K):
    res = 0
    start = 0
    end = N - 1
    arr = sorted(arr)
    while (end - start > 1):
        if (arr[start] + arr[end] <= K):
            start += 1
        else:
            res += 1
            end -= 1
    if (end - start == 1):
        if (arr[start] + arr[end] <= K):
            res += 1
            start += 1
            end -= 1
        else:
            res += 1
            end -= 1
    if (start == end):
        res += 1
    return res


if __name__ == '__main__':
    arr = [2, 6, 8, 10, 20, 25]
    N = len(arr)
    K = 26
    print(cntMinSub(arr, N, K))

# Expected output:
3
# count possible values of k such that x | function to count integers k satisfying given equation ; calculate the absoluter difference between a and b ; iterate till sqrt of the difference ; return the count ; 
def condition(a, b):
    d = abs(a - b)
    count = 0
    for i in range(1, d + 1):
        if i * i > d:
            break
        if (d % i == 0):
            if (d // i == i):
                count += 1
            else:
                count += 2
    return count


if __name__ == '__main__':
    x = 2
    y = 6
    print(condition(x, y))

# Expected output:
3
# count pairs in an array whose product is composite number | function to check if a number is prime or not ; check if n is multiple of i or not . ; if n is multiple of i . ; function to get the count of pairs whose product is a composite number . ; stores the count of pairs whose product is a composite number ; generate all possible pairs ; stores the product of element of current pair ; if prod is a composite number ; 
def isComposite(N):
    for i in range(2, N + 1):
        if i * i > N:
            break
        if (N % i == 0):
            return True
    return False


def compositePair(arr, N):
    res = 0
    for i in range(N):
        for j in range(i + 1, N):
            prod = arr[i] * arr[j]
            if (isComposite(prod)):
                res += 1
    return res


if __name__ == '__main__':
    arr = [1, 1, 2, 2, 8]
    N = len(arr)
    print(compositePair(arr, N))

# Expected output:
5
# count n | python3 program for the above approach ; function for calculate ( x ^ y ) % mod in o ( log y ) ; base condition ; transition state of power function ; function for counting total numbers that can be formed such that digits x , y are present in each number ; calculate the given expression ; return the final answer ; 

mod = 1e9 + 7


def power(x, y):
    if (y == 0):
        return 1
    p = power(x, y // 2) % mod
    p = (p * p) % mod
    if (y & 1):
        p = (x * p) % mod
    return p


def TotalNumber(N):
    ans = (power(10, N) - 2 * power(9, N) + power(8, N) + 2 * mod) % mod
    return ans


if __name__ == '__main__':
    N = 10
    X = 3
    Y = 4
    print(TotalNumber(N))

# Expected output:
100172994.0
# array obtained by repeatedly reversing array after every insertion from given array | python3 program of the above approach ; function to generate the array by inserting array elements one by one followed by reversing the array ; doubly ended queue ; iterate over the array ; push array elements alternately to the front and back ; if size of list is odd ; reverse the list ; print the elements of the array ; 

from collections import deque


def generateArray(arr, n):
    ans = deque()
    for i in range(n):
        if (i & 1 != 0):
            ans .appendleft(arr[i])
        else:
            ans .append(arr[i])
    if (n & 1 != 0):
        ans .reverse()
    for x in ans:
        print(x, end=" ")
    print()


n = 4
arr = [1, 2, 3, 4]
generateArray(arr, n)

# Expected output:
4 2 1 3
# find the greater number closest to n having at most one non | function to get closest greater number with at most non zero digit ; stores the closest greater number with at most one non - zero digit ; stores length of str ; append 10 to the end of resultant string ; append n - 1 times '0' to the end of resultant string ; 
def closestgtNum(str):
    res = ""
    n = len(str)
    if (str[0] < '9'):
        res += (chr)(ord(str[0]) + 1)
    else:
        res += (chr)('1')
        res += (chr)('0')
    for i in range(n - 1):
        res += ('0')
    return res


if __name__ == '__main__':
    str = "120"
    print(closestgtNum(str))

# Expected output:
200
# count maximum non | function to count maximum number of non - overlapping subarrays with sum equals to the target ; stores the final count ; next subarray should start from index >= availidx ; tracks the prefix sum ; map to store the prefix sum for respective indices ; check if cur_sum - target is present in the array or not ; update the index of current prefix sum ; return the count of subarrays ; 
def maximumSubarrays(arr, N, target):
    ans = 0
    availIdx = -1
    cur_sum = 0
    mp = {}
    mp[0] = -1
    for i in range(N):
        cur_sum += arr[i]
        if ((cur_sum - target) in mp and mp[cur_sum - target] >= availIdx):
            ans += 1
            availIdx = i
        mp[cur_sum] = i
    return ans


if __name__ == '__main__':
    arr = [2, -1, 4, 3, 6, 4, 5, 1]
    N = len(arr)
    target = 6
    print(maximumSubarrays(arr, N, target))

# Expected output:
3
# maximize array sum by replacing equal adjacent pairs by their sum and x respectively | function to calculate x ^ y ; base case ; find the value in temp ; if y is even ; function that find the maximum possible sum of the array ; print the result using the formula ; 
def power(x, y):
    if (y == 0):
        return 1
    temp = power(x, y // 2)
    if (y % 2 == 0):
        return temp * temp
    else:
        return x * temp * temp


def maximumPossibleSum(N, X):
    print(X * (power(2, N) - 1))


N = 3
X = 5
maximumPossibleSum(N, X)

# Expected output:
35
# count ways to generate pairs having bitwise xor and bitwise and equal to x and y respectively | function to return the count of possible pairs of a and b whose bitwise xor is x and y respectively ; stores the count of pairs ; iterate till any bit are set ; extract i - th bit of x and y ; divide x and y by 2 ; if xi = 1 and yi = 2 , multiply counter by 2 ; increase required count ; if xi = 1 and yi = 1 ; no answer exists ; return the final count ; 
def countOfPairs(x, y):
    counter = 1
    while (x or y):
        bit1 = x % 2
        bit2 = y % 2
        x >>= 1
        y >>= 1
        if (bit1 == 1 and bit2 == 0):
            counter *= 2
            continue
        if (bit1 & bit2):
            counter = 0
            break
    return counter


X = 2
Y = 5
print(countOfPairs(X, Y))

# Expected output:
2
# make all array elements equal by repeated subtraction of absolute difference of pairs from their maximum | function to return gcd of a and b ; base case ; recursive call ; function to find gcd of array ; initialise the result ; traverse the array arr [ ] ; update result as gcd of the result and arr [ i ] ; return the resultant gcd ; 
def gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)


def findGCD(arr, N):
    result = 0
    for element in arr:
        result = gcd(result, element)
        if (result == 1):
            return 1
    return result


arr = [2, 3, 4]
N = len(arr)
print(findGCD(arr, N))

# Expected output:
1
# check if any permutation of array contains sum of every adjacent pair not divisible by 3 | function to checks if any permutation of the array exists whose sum of adjacent pairs is not divisible by 3 ; count remainder 0 ; count remainder 1 ; count remainder 2 ; condition for valid arrangements ; 
def factorsOf3(arr, N):
    a = 0
    b = 0
    c = 0
    for i in range(N):
        if (arr[i] % 3 == 0):
            a += 1
        elif (arr[i] % 3 == 1):
            b += 1
        elif (arr[i] % 3 == 2):
            c += 1
    if (a >= 1 and a <= b + c + 1):
        print("Yes")
    elif (a == 0 and b == 0 and c > 0):
        print("Yes")
    elif (a == 0 and c == 0 and b > 0):
        print("Yes")
    else:
        print("No")


arr = [1, 2, 3, 3]
N = len(arr)
factorsOf3(arr, N)

# Expected output:
Yes
# check if a number is a perfect square having all its digits as a perfect square | python3 program for the above approach ; function to check if digits of n is a perfect square or not ; iterate over the digits ; extract the digit ; check if digit is a perfect square or not ; divide n by 10 ; return true ; function to check if n is a perfect square or not ; if floor and ceil of n is not same ; function to check if n satisfies the required conditions or not ; if both the conditions are satisfied ; 

import math


def check_digits(N):
    while (N > 0):
        n = N % 10
        if ((n != 0) and (n != 1) and (n != 4) and (n != 9)):
            return 0
        N = N // 10
    return 1


def is_perfect(N):
    n = math .sqrt(N)
    if (math .floor(n) != math .ceil(n)):
        return 0
    return 1


def isFullSquare(N):
    if (is_perfect(N) and check_digits(N)):
        print("Yes")
    else:
        print("No")


N = 144
isFullSquare(N)

# Expected output:
Yes