giulio98/xlcost-single-prompt huggingface dataset:
# find the absolute difference between the nearest powers of two given integers for every array element | python3 program for the above approach ; function to print the array ; traverse the array ; function to modify array elements by absolute difference of the nearest perfect power of a and b ; traverse the array arr [ ] ; find the log a of arr [ i ] ; find the power of a less than and greater than a ; find the log b of arr [ i ] ; find the power of b less than and greater than b ; update arr [ i ] with absolute difference of log_a & log _b ; print the modified array ; 

import math


def printArray(arr, N):
    for i in range(N):
        print(arr[i], end=" ")


def nearestPowerDiff(arr, N, a, b):
    for i in range(N):
        log_a = int(math .log(arr[i]) / math .log(a))
        A = int(pow(a, log_a))
        B = int(pow(a, log_a + 1))
        if ((arr[i] - A) < (B - arr[i])):
            log_a = A
        else:
            log_a = B
        log_b = int(math .log(arr[i]) / math .log(b))
        A = int(pow(b, log_b))
        B = int(pow(b, log_b + 1))
        if ((arr[i] - A) < (B - arr[i])):
            log_b = A
        else:
            log_b = B
        arr[i] = abs(log_a - log_b)
    printArray(arr, N)


arr = [5, 12, 25]
A = 2
B = 3
N = len(arr)
nearestPowerDiff(arr, N, A, B)

# Expected output:
1 7 5
# count subarrays having even bitwise or | function to count the number of subarrays having even bitwise or ; store number of subarrays having even bitwise or ; store the length of the current subarray having even numbers ; traverse the array ; if the element is even ; increment size of the current continuous sequence of even array elements ; if arr [ i ] is odd ; if length is non zero ; adding contribution of subarrays consisting only of even numbers ; make length of subarray as 0 ; add contribution of previous subarray ; return total count of subarrays ; 
def bitOr(arr, N):
    count = 0
    length = 0
    for i in range(N):
        if (arr[i] % 2 == 0):
            length += 1
        else:
            if (length != 0):
                count += ((length) * (length + 1)) // 2
            length = 0
    count += ((length) * (length + 1)) // 2
    return count


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

# Expected output:
6
# generate an array having sum of bitwise or of same | function to print the resultant array ; stores the sum of the array ; calculate sum of the array ; if sum > k ; not possible to construct the array ; update k ; stores the resultant array ; traverse the array ; stores the current element ; if jth bit is not set and value of 2 ^ j is less than k ; update curr ; update k ; push curr into b ; if k is greater than 0 ; print the vector b ; otherwise ; 
def constructArr(A, N, K):
    sum = 0
    for i in range(N):
        sum += A[i]
    if (sum > K):
        print(-1)
        return
    K -= sum
    B = []
    for i in range(N):
        curr = A[i]
        j = 32
        while (j >= 0 and K > 0):
            if ((curr & (1 << j)) == 0 and (1 << j) <= K):
                curr |= (1 << j)
                K -= (1 << j)
            j -= 1
        B .append(curr)
    if (K == 0):
        for it in B:
            print(it, end=" ")
        print("", end="")
    else:
        print("-1")


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

# Expected output:
23 2 3 4
# find all pairs raised to power k differs by exactly n | function to prpairs whose difference raised to the power k is x ; stores the count of valid pairs ; iterate over the range [ - 1000 , 1000 ] ; iterate over the range [ - 1000 , 1000 ] ; if the current pair satisfies the given condition ; increment the count by 1 ; if no such pair exists ; 
def ValidPairs(X, K):
    count = 0
    for A in range(-1000, 1001, 1):
        for B in range(-1000, 1001, 1):
            if (pow(A, K) - pow(B, K) == X):
                count += 1
                print(A, B)
    if (count == 0):
        cout << "-1"


X = 33
K = 5
ValidPairs(X, K)

# Expected output:
1 -2
2 -1
# connect a graph by m edges such that the graph does not contain any cycle and bitwise and of connected vertices is maximum | function to find the maximum bitwise and of connected components possible by connecting a graph using m edges ; stores total number of ways to connect the graph ; stores the maximum bitwise and ; iterate over the range [ 0 , 2 ^ n ] ; store the bitwise and of the connected vertices ; store the count of the connected vertices ; check for all the bits ; if i - th bit is set ; if the first vertex is added ; set andans equal to arr [ i ] ; calculate bitwise and of arr [ i ] with andans ; increase the count of connected vertices ; if number of connected vertices is ( m + 1 ) , no cycle is formed ; find the maximum bitwise and value possible ; return the maximum bitwise and possible ; 
def maximumAND(arr, n, m):
    tot = 1 << n
    mx = 0
    for bm in range(tot):
        andans = 0
        count = 0
        for i in range(n):
            if ((bm >> i) & 1):
                if (count == 0):
                    andans = arr[i]
                else:
                    andans = andans & arr[i]
                count += 1
        if (count == (m + 1)):
            mx = max(mx, andans)
    return mx


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

# Expected output:
0
# calculate sum of the array generated by given operations | function to find the sum of the array formed by performing given set of operations while traversing the array ops [ ] ; if the size of array is 0 ; stores the required sum ; traverse the array ops [ ] ; if the character is c , remove the top element from the stack ; if the character is d , then push 2 * top element into stack ; if the character is + , add sum of top two elements from the stack ; otherwise , push x and add it to ans ; print the resultant sum ; 
def findTotalSum(ops):
    if (len(ops) == 0):
        print(0)
        return
    pts = []
    ans = 0
    for i in range(len(ops)):
        if (ops[i] == "C"):
            ans -= pts[-1]
            pts .pop()
        elif (ops[i] == "D"):
            pts .append(pts[-1] * 2)
            ans += pts[-1]
        elif (ops[i] == "+"):
            a = pts[-1]
            pts .pop()
            b = pts[-1]
            pts .append(a)
            ans += (a + b)
            pts .append(a + b)
        else:
            n = int(ops[i])
            ans += n
            pts .append(n)
    print(ans)


if __name__ == "__main__":
    arr = ["5", "-2", "C", "D", "+"]
    findTotalSum(arr)

# Expected output:
30
# xor of major diagonal elements of a 3d matrix | function to calculate bitwise xor of major diagonal elements of 3d matrix ; stores the bitwise xor of the major diagonal elements ; if element is part of major diagonal ; print the resultant bitwise xor ; 
def findXOR(mat, N):
    XOR = 0
    for i in range(N):
        for j in range(N):
            for k in range(N):
                if ((i == j and j == k)):
                    XOR ^= mat[i][j][k]
                    XOR ^= mat[i][j][N - k - 1]
    print(XOR)


mat = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
N = len(mat)
findXOR(mat, N)

# Expected output:
12
# xor of major diagonal elements of a 3d matrix | function to find the bitwise xor of both diagonal elements of 3d matrix ; stores the bitwise xor of the major diagonal elements ; print the resultant bitwise xor ; 
def findXOR(mat, N):
    XOR = 0
    for i in range(N):
        XOR ^= mat[i][i][i]
        XOR ^= mat[i][i][N - i - 1]
    print(XOR)


mat = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
N = len(mat)
findXOR(mat, N)

# Expected output:
12
# count of subtrees possible from an n | python3 program of the above approach ; adjacency list to represent the graph ; stores the count of subtrees possible from given n - ary tree ; utility function to count the number of subtrees possible from given n - ary tree ; stores the count of subtrees when cur node is the root ; traverse the adjacency list ; iterate over every ancestor ; calculate product of the number of subtrees for each child node ; update the value of ans ; return the resultant count ; function to count the number of subtrees in the given tree ; initialize an adjacency matrix ; add the edges ; function call to count the number of subtrees possible ; prcount of subtrees ; 

MAX = 300004
graph = [[]for i in range(MAX)]
mod = 10 ** 9 + 7
ans = 0


def countSubtreesUtil(cur, par):
    global mod, ans
    res = 1
    for i in range(len(graph[cur])):
        v = graph[cur][i]
        if (v == par):
            continue
        res = (res * (countSubtreesUtil(v, cur) + 1)) % mod
    ans = (ans + res) % mod
    return res


def countSubtrees(N, adj):
    for i in range(N - 1):
        a = adj[i][0]
        b = adj[i][1]
        graph[a].append(b)
        graph[b].append(a)
    countSubtreesUtil(1, 1)
    print(ans + 1)


if __name__ == '__main__':
    N = 3
    adj = [[0, 1], [1, 2]]
    countSubtrees(N, adj)

# Expected output:
7
# find the player who wins the game of placing alternate + and | function to check which player wins the game ; stores the difference between + ve and - ve array elements ; traverse the array ; update diff ; checks if diff is even ; 
def checkWinner(arr, N):
    diff = 0
    for i in range(N):
        diff -= arr[i]
    if (diff % 2 == 0):
        print("A")
    else:
        print("B")


if __name__ == "__main__":
    arr = [1, 2]
    N = len(arr)
    checkWinner(arr, N)

# Expected output:
B
# generate an array having sum of euler totient function of all elements equal to n | python3 program for the above approach ; function to construct the array such the sum of values of euler totient functions of all array elements is n ; stores the resultant array ; find divisors in sqrt ( n ) ; if n is divisible by i ; push the current divisor ; if n is not a perfect square ; push the second divisor ; print the resultant array ; 

from math import sqrt


def constructArray(N):
    ans = []
    for i in range(1, int(sqrt(N)) + 1, 1):
        if (N % i == 0):
            ans .append(i)
            if (N != (i * i)):
                ans .append(N / i)
    for it in ans:
        print(int(it), end=" ")


if __name__ == '__main__':
    N = 12
    constructArray(N)

# Expected output:
1 12 2 6 3 4
# place n boys and m girls in different rows such that count of persons placed in each row is maximized | function to calculate gcd of two numbers ; function to count maximum persons that can be placed in a row ; 
def gcd(a, b):
    if (b == 0):
        return a
    return gcd(b, a % b)


def maximumRowValue(n, m):
    return gcd(n, m)


if __name__ == '__main__':
    N = 4
    M = 2
    print(maximumRowValue(N, M))

# Expected output:
2
# program to generate an array having convolution of two given arrays | python3 program for the above approach ; function to generate a convolution array of two given arrays ; stores the size of arrays ; stores the final array ; traverse the two given arrays ; update the convolution array ; print the convolution array c [ ] ; 

MOD = 998244353


def findConvolution(a, b):
    global MOD
    n, m = len(a), len(b)
    c = [0] * (n + m - 1)
    for i in range(n):
        for j in range(m):
            c[i + j] += (a[i] * b[j]) % MOD
    for k in range(len(c)):
        c[k] %= MOD
        print(c[k], end=" ")


if __name__ == '__main__':
    A = [1, 2, 3, 4]
    B = [5, 6, 7, 8, 9]
    findConvolution(A, B)

# Expected output:
5 16 34 60 70 70 59 36
# minimum number of 1 s present in a submatrix of given dimensions in a binary matrix | python3 program for the above approach ; function to count number of 1 s present in a sub matrix from ( start_i , start_j ) to ( end_i , end_j ) ; stores the number of 1 s present in current submatrix ; traverse the submatrix ; if mat [ x ] [ y ] is equal to 1 ; increase count by 1 ; return the total count of 1 s ; function to find the minimum number of 1 s present in a sub - matrix of size a * b or b * a ; stores the minimum count of 1 s ; iterate i from 0 to n ; iterate j from 0 to m ; if a valid sub matrix of size a * b from ( i , j ) is possible ; count the number of 1 s present in the sub matrix of size a * b from ( i , j ) ; update minimum if count is less than the current minimum ; if a valid sub matrix of size b * a from ( i , j ) is possible ; count the number of 1 s in the sub matrix of size b * a from ( i , j ) ; update minimum if count is less than the current minimum ; return minimum as the final result ; 

P = 51


def count1s(start_i, start_j, end_i, end_j, mat):
    count = 0
    for x in range(start_i, end_i):
        for y in range(start_j, end_j):
            if (mat[x][y] == 1):
                count += 1
    return count


def findMinimumCount(N, M, A, B, mat):
    minimum = 1e9
    for i in range(N):
        for j in range(M):
            if (i + A <= N and j + B <= M):
                count = count1s(i, j, i + A, j + B, mat)
                minimum = min(count, minimum)
            if (i + B <= N and j + A <= M):
                count = count1s(i, j, i + B, j + A, mat)
                minimum = min(count, minimum)
    return minimum


if __name__ == "__main__":
    A = 2
    B = 2
    N = 3
    M = 4
    mat = [[1, 0, 1, 0], [0, 1, 0, 1], [1, 0, 1, 0]]
    print(findMinimumCount(N, M, A, B, mat))

# Expected output:
2
# program to check if a number can be expressed as an even power of 2 or not | function to check if n can be expressed as an even power of 2 ; iterate till x is n ; if x is even then return true ; increment x ; if n is not a power of 2 ; 
def checkEvenPower(n):
    x = 0
    while (x < n):
        value = pow(2, x)
        if (value == n):
            if (x % 2 == 0):
                return True
            else:
                return False
        x += 1
    return False


if __name__ == '__main__':
    N = 4
    if checkEvenPower(N):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# program to check if a number can be expressed as an even power of 2 or not | function to check if n can be expressed as an even power of 2 or not ; iterate until low > high ; calculate mid ; if 2 ^ mid is equal to n ; if mid is odd ; update the value of low ; update the value of high ; otherwise ; 
def checkEvenPower(n):
    low, high = 0, n
    while (low <= high):
        mid = low + (high - low) / 2
        value = pow(2, mid)
        if (value == n):
            if (mid % 2 == 1):
                return "No"
            else:
                return "Yes"
        elif (value < n):
            low = mid + 1
        else:
            high = mid - 1
    return "No"


N = 4
print(checkEvenPower(N))

# Expected output:
Yes
# program to check if a number can be expressed as an even power of 2 or not | function to check if n can be expressed as an even power of 2 or not ; if n is not a power of 2 ; bitwise and operation ; 
def checkEvenPower(N):
    if ((N & (N - 1)) != 0):
        return false
    N = N & 0x55555555
    return (N > 0)


N = 4
print(1 if checkEvenPower(N)else 0)

# Expected output:
1
# sum of quotients of division of n by powers of k not exceeding n | function to calculate sum of quotients obtained by dividing n by powers of k <= n ; store the required sum ; iterate until i exceeds n ; update sum ; multiply i by k to obtain next power of k ; prthe result ; 
def findSum(N, K):
    ans = 0
    i = 1
    while (i <= N):
        ans += N // i
        i = i * K
    print(ans)


if __name__ == '__main__':
    N, K = 10, 2
    findSum(N, K)

# Expected output:
18
# count arithmetic progressions having sum s and common difference equal to d | function to count the number of aps with sum s and common difference d ; multiply s by 2 ; stores the count of aps ; iterate over the factors of 2 * s ; check if i is the factor or not ; conditions to check if ap can be formed using factor f ; return the total count of aps ; 
def countAPs(S, D):
    S = S * 2
    answer = 0
    for i in range(1, S):
        if i * i > S:
            break
        if (S % i == 0):
            if (((S // i) - D * i + D) % 2 == 0):
                answer += 1
            if ((D * i - (S // i) + D) % 2 == 0):
                answer += 1
    return answer


if __name__ == '__main__':
    S, D = 12, 1
    print(countAPs(S, D))

# Expected output:
4
# minimize insertions to make sum of every pair of consecutive array elements at most k | function to count minimum number of insertions required to make sum of every pair of adjacent elements at most k ; stores if it is possible to make sum of each pair of adjacent elements at most k ; stores the count of insertions ; stores the previous value to index i ; traverse the array ; if arr [ i ] is greater than or equal to k ; mark possible 0 ; if last + arr [ i ] is greater than k ; increment res by 1 ; assign arr [ i ] to last ; if possible to make the sum of pairs of adjacent elements at most k ; otherwise print " - 1" ; 
def minimumInsertions(arr, N, K):
    possible = 1
    res = 0
    last = 0
    for i in range(N):
        if (arr[i] >= K):
            possible = 0
            break
        if (last + arr[i] > K):
            res += 1
        last = arr[i]
    if (possible):
        print(res)
    else:
        print("-1")


if __name__ == "__main__":
    arr = [1, 2, 3, 4, 5]
    K = 6
    N = len(arr)
    minimumInsertions(arr, N, K)

# Expected output:
2
# maximum sum of k | function to count the number of distinct elements present in the array ; insert array elements into set ; return the st size ; function to calculate maximum sum of k - length subarray having same unique elements as arr [ ] ; not possible to find an subarray of length k from an n - sized array , if k > n ; traverse the array ; update the mp ; if i >= k , then decrement arr [ i - k ] element 's one  occurence ; if frequency of any element is 0 then remove the element ; if mp size is same as the count of distinct elements of array arr [ ] then update maximum sum ; function that finds the maximum sum of k - length subarray having same number of distinct elements as the original array ; size of array ; stores count of distinct elements ; print maximum subarray sum ; 
def distinct(arr, N):
    st = set()
    for i in range(N):
        st .add(arr[i])
    return len(st)


def maxSubarraySumUtil(arr, N, K, totalDistinct):
    if (K > N):
        return 0
    mx = 0
    sum = 0
    mp = {}
    for i in range(N):
        if (arr[i] in mp):
            mp[arr[i]] += 1
        else:
            mp[arr[i]] = 1
        sum += arr[i]
        if (i >= K):
            if (arr[i - K] in mp):
                mp[arr[i - K]] -= 1
                sum -= arr[i - K]
            if (arr[i - K] in mp and mp[arr[i - K]] == 0):
                mp .remove(arr[i - K])
        if (len(mp) == totalDistinct):
            mx = max(mx, sum)
    return mx


def maxSubarraySum(arr, K):
    N = len(arr)
    totalDistinct = distinct(arr, N)
    print(maxSubarraySumUtil(arr, N, K, totalDistinct))


if __name__ == '__main__':
    arr = [7, 7, 2, 4, 2, 7, 4, 6, 6, 6]
    K = 6
    maxSubarraySum(arr, K)

# Expected output:
31
# number of irreflexive relations on a set | python3 program for the above approach ; function to calculate x ^ y modulo 1000000007 in o ( log y ) ; stores the result of x ^ y ; update x if it exceeds mod ; if x is divisible by mod ; if y is odd , then multiply x with result ; divide y by 2 ; update the value of x ; return the resultant value of x ^ y ; function to count the number of irreflixive relations in a set consisting of n elements ; return the resultant count ; 

mod = 1000000007


def power(x, y):
    global mod
    res = 1
    x = x % mod
    if (x == 0):
        return 0
    while (y > 0):
        if (y & 1):
            res = (res * x) % mod
        y = y >> 1
        x = (x * x) % mod
    return res


def irreflexiveRelation(N):
    return power(2, N * N - N)


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

# Expected output:
4
# count arithmetic progressions having sum n and common difference equal to 1 | function to count all possible ap series with common difference 1 and sum of elements equal to n ; stores the count of ap series ; traverse through all factors of 2 * n ; check for the given conditions ; increment count ; prcount - 1 ; 
def countAPs(N):
    count = 0
    i = 1
    while (i * i <= 2 * N):
        res = 2 * N
        if (res % i == 0):
            op = res / i - i + 1
            if (op % 2 == 0):
                count += 1
            if (i * i != res and (i - res / i + 1) % 2 == 0):
                count += 1
        i += 1
    print(count - 1)


N = 963761198400
countAPs(N)

# Expected output:
1919
# check if a given number n has at least one odd divisor not exceeding n | function to check whether n has at least one odd divisor not exceeding n - 1 or not ; stores the value of n ; reduce the given number n by dividing it by 2 ; if n is divisible by an odd divisor i ; check if n is an odd divisor after reducing n by dividing it by 2 ; otherwise ; 
def oddDivisor(N):
    X = N
    while (N % 2 == 0):
        N //= 2
    i = 3
    while (i * i <= X):
        if (N % i == 0):
            return "Yes"
        i += 2
    if (N != X):
        return "Yes"
    return "No"


N = 10
print(oddDivisor(N))

# Expected output:
Yes
# minimize steps required to make two values equal by repeated division by any of their prime factor which is less than m | stores the prime factor of numbers ; function to find gcd of a and b ; base case ; otherwise , calculate gcd ; function to precompute the prime numbers till 1000000 ; initialize all the positions with their respective values ; iterate over the range [ 2 , sqrt ( 10 ^ 6 ) ] ; if i is prime number ; mark it as the factor ; utility function to count the number of steps to make x and y equal ; initialise steps ; iterate x is at most 1 ; divide with the smallest prime factor ; if x and y can 't be  made equal ; return steps ; function to find the minimum number of steps required to make x and y equal ; generate all the prime factors ; calculate gcd of x and y ; divide the numbers by their gcd ; if not possible , then return - 1 ; return the resultant number of steps ; 

primes = [0] * (1000006)


def gcd(a, b):
    if (b == 0):
        return a
    else:
        return gcd(b, a % b)


def preprocess():
    for i in range(1, 1000001):
        primes[i] = i
    i = 2
    while (i * i <= 1000000):
        if (primes[i] == i):
            for j in range(2 * i, 1000001, i):
                if (primes[j] == j):
                    primes[j] = i
        i += 1


def Steps(x, m):
    steps = 0
    flag = False
    while (x > 1):
        if (primes[x] > m):
            flag = True
            break
        x //= primes[x]
        steps += 1
    if (flag != 0):
        return -1
    return steps


def minimumSteps(x, y, m):
    preprocess()
    g = gcd(x, y)
    x = x // g
    y = y // g
    x_steps = Steps(x, m)
    y_steps = Steps(y, m)
    if (x_steps == -1 or y_steps == -1):
        return -1
    return x_steps + y_steps


X = 160
Y = 180
M = 10
print(minimumSteps(X, Y, M))

# Expected output:
5