giulio98/xlcost-single-prompt huggingface dataset:
# queries to calculate sum of array elements consisting of odd number of divisors | python3 program for the above approach ; function to find the sum of elements having odd number of divisors in index range [ l , r ] for q queries ; initialize the dp [ ] array ; traverse the array , arr [ ] ; if a [ i ] is a perfect square , then update value of dp [ i ] to a [ i ] ; find the prefix sum of dp [ ] array ; iterate through all the queries ; find the sum for each query ; 

from math import sqrt


def OddDivisorsSum(n, q, a, Query):
    DP = [0] * n
    for i in range(n):
        x = sqrt(a[i])
        if (x * x == a[i]):
            DP[i] = a[i]
    for i in range(1, n):
        DP[i] = DP[i - 1] + DP[i]
    for i in range(q):
        l = Query[i][0]
        r = Query[i][1]
        if (l == 0):
            print(DP[r], end=" ")
        else:
            print(DP[r] - DP[l - 1], end=" ")


if __name__ == '__main__':
    arr = [2, 4, 5, 6, 9]
    N = len(arr)
    Q = 3
    Query = [[0, 2], [1, 3], [1, 4]]
    OddDivisorsSum(N, Q, arr, Query)

# Expected output:
4 4 13
# smallest subset of maximum sum possible by splitting array into two subsets | python 3 program for the above approach ; function to split array elements into two subsets having sum of the smaller subset maximized ; stores the size of the array ; stores the frequency of array elements ; stores the total sum of the array ; stores the sum of the resultant set ; stores if it is possible to split the array that satisfies the conditions ; stores the elements of the first subseta ; traverse the array arr [ ] ; increment total sum ; increment count of arr [ i ] ; sort the array arr [ ] ; stores the index of the last element of the array ; traverse the array arr [ ] ; stores the frequency of arr [ i ] ; if frq + ans . size ( ) is at most remaining size ; append arr [ i ] to ans ; decrement totsum by arr [ i ] ; increment s by arr [ i ] ; otherwise , decrement i by frq ; if s is greater than totsum ; mark flag 1 ; if flag is equal to 1 ; print the arrlist ans ; otherwise , print " - 1" ; 

from collections import defaultdict


def findSubset(arr):
    N = len(arr)
    mp = defaultdict(int)
    totSum = 0
    s = 0
    flag = 0
    ans = []
    for i in range(len(arr)):
        totSum += arr[i]
        mp[arr[i]] = mp[arr[i]] + 1
    arr .sort()
    i = N - 1
    while (i >= 0):
        frq = mp[arr[i]]
        if ((frq + len(ans)) < (N - (frq + len(ans)))):
            for k in range(frq):
                ans .append(arr[i])
                totSum -= arr[i]
                s += arr[i]
                i -= 1
        else:
            i -= frq
        if (s > totSum):
            flag = 1
            break
    if (flag == 1):
        for i in range(len(ans) - 1, -1, -1):
            print(ans[i], end=" ")
    else:
        print(-1)


if __name__ == "__main__":
    arr = [5, 3, 2, 4, 1, 2]
    findSubset(arr)

# Expected output:
4 5
# minimize bitwise xor of array elements with 1 required to make sum of array at least k | function to find minimum number of bitwise xor of array elements with 1 required to make sum of the array at least k ; stores the count of even array elements ; stores sum of the array ; traverse the array arr [ ] ; increment sum ; if array element is even ; increase count of even ; if s is at least k ; if s + e is less than k ; otherwise , moves = k - s ; 
def minStepK(arr, N, K):
    E = 0
    S = 0
    for i in range(N):
        S += arr[i]
        if (arr[i] % 2 == 0):
            E += 1
    if (S >= K):
        return 0
    elif (S + E < K):
        return -1
    else:
        return K - S


if __name__ == "__main__":
    arr = [0, 1, 1, 0, 1]
    N = len(arr)
    K = 4
    print(minStepK(arr, N, K))

# Expected output:
1
# maximize difference between odd and even | function to find maximum and minimum value of a number that can be obtained by rotating bits ; stores the value of n ; stores the maximum value ; stores the minimum value ; if temp is odd ; update the maximum and the minimum value ; if flag is 1 , then return the maximum value ; otherwise , return the maximum value ; function to find the maximum difference between the sum of odd and even - indexed array elements possible by rotating bits ; stores the maximum difference ; stores the sum of elements present at odd indices ; stores the sum of elements present at even indices ; traverse the given array ; if the index is even ; update the caseone ; stores the maximum difference ; stores the sum of elements placed at odd positions ; stores the sum of elements placed at even positions ; traverse the array ; if the index is even ; update the casetwo ; return the maximum of caseone and casetwo ; 
def Rotate(n, f):
    temp = n
    maxi = n
    mini = n
    for idx in range(7):
        if temp & 1:
            temp >>= 1
            temp += 2 ** 7
        else:
            temp >>= 1
        mini = min(mini, temp)
        maxi = max(maxi, temp)
    if (f):
        return (maxi)
    else:
        return (mini)


def calcMinDiff(arr):
    caseOne = 0
    sumOfodd = 0
    sumOfeven = 0
    for i in range(len(arr)):
        if i % 2:
            sumOfodd += Rotate(arr[i], 0)
        else:
            sumOfeven += Rotate(arr[i], 1)
    caseOne = abs(sumOfodd - sumOfeven)
    caseTwo = 0
    sumOfodd = 0
    sumOfeven = 0
    for i in range(len(arr)):
        if i % 2:
            sumOfodd += Rotate(arr[i], 1)
        else:
            sumOfeven += Rotate(arr[i], 0)
    caseTwo = abs(sumOfodd - sumOfeven)
    return max(caseOne, caseTwo)


arr = [123, 86, 234, 189]
print(calcMinDiff(arr))

# Expected output:
326
# array element with minimum sum of absolute differences | set 2 | function to find the element with minimum sum of differences between any elements in the array ; stores the required x and sum of absolute differences ; calculate sum of array elements ; the sum of absolute differences can 't be greater than sum ; update res that gives the minimum sum ; if the current difference is less than the previous difference ; update min_diff and res ; print the resultant value ; 
def minimumDiff(arr, N):
    res = arr[0]
    sum1 = 0
    for i in range(N):
        sum1 += arr[i]
    min_diff = sum1
    for i in range(N):
        if (abs(sum1 - (arr[i] * N)) < min_diff):
            min_diff = abs(sum1 - (arr[i] * N))
            res = arr[i]
    print(res)


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

# Expected output:
3
# sum of array elements possible by appending arr [ i ] / k to the end of the array k times for array elements divisible by k | function to calculate sum of array elements after adding arr [ i ] / k to the end of the array if arr [ i ] is divisible by k ; stores the sum of the array ; stores the array elements ; traverse the array ; stores if the operation should be formed or not ; traverse the vector v ; if flag is false and if v [ i ] is divisible by k ; otherwise , set flag as true ; increment the sum by v [ i % n ] ; return the resultant sum ; 
def Sum(arr, N, K):
    sum = 0
    v = []
    for i in range(N):
        v .append(arr[i])
    flag = False
    i = 0
    lenn = len(v)
    while (i < lenn):
        if (flag == False and (v[i] % K == 0)):
            v .append(v[i] // K)
        else:
            flag = True
        sum += v[i % N]
        i += 1
        lenn = len(v)
    return sum


arr = [4, 6, 8, 2]
K = 2
N = len(arr)
print(Sum(arr, N, K))

# Expected output:
44
# count array elements whose count of divisors is a prime number | function to count the array elements whose count of divisors is prime ; stores the maximum element ; find the maximum element ; store if i - th element is prime ( 0 ) or non - prime ( 1 ) ; base case ; if i is a prime number ; mark all multiples of i as non - prime ; stores the count of divisors ; base case ; iterate to count factors ; stores the count of array elements whose count of divisors is a prime number ; traverse the array arr [ ] ; if count of divisors is prime ; return the resultant count ; 
def primeDivisors(arr, N):
    K = arr[0]
    for i in range(1, N):
        K = max(K, arr[i])
    prime = [0] * (K + 1)
    prime[0] = 1
    prime[1] = 1
    for i in range(2, K + 1):
        if (not prime[i]):
            for j in range(2 * i, K + 1, i):
                prime[j] = 1
    factor = [0] * (K + 1)
    factor[0] = 0
    factor[1] = 1
    for i in range(2, K + 1):
        factor[i] += 1
        for j in range(i, K + 1, i):
            factor[j] += 1
    count = 0
    for i in range(N):
        if (prime[factor[arr[i]]] == 0):
            count += 1
    return count


if __name__ == '__main__':
    arr = [10, 13, 17, 25]
    N = len(arr)
    print(primeDivisors(arr, N))

# Expected output:
3
# count prime numbers up to n that can be represented as a sum of two prime numbers | function to store all prime numbers up to n using sieve of eratosthenes ; set 0 and 1 as non - prime ; if p is prime ; set all multiples of p as non - prime ; function to count prime numbers up to n that can be represented as the sum of two prime numbers ; stores all the prime numbers ; update the prime array ; create a dp array of size n + 1 ; update dp [ 1 ] = 0 ; iterate over the range [ 2 , n ] ; add the previous count value ; increment dp [ i ] by 1 if i and ( i - 2 ) are both prime ; print the result ; 
def SieveOfEratosthenes(n, prime):
    prime[0] = 0
    prime[1] = 0
    p = 2
    while p * p <= n:
        if (prime[p]):
            for i in range(p * p, n + 1, p):
                prime[i] = False
        p += 1


def countPrime(n):
    prime = [True] * (n + 1)
    SieveOfEratosthenes(n, prime)
    dp = [0] * (n + 1)
    dp[1] = 0
    for i in range(2, n + 1):
        dp[i] += dp[i - 1]
        if (prime[i] == 1 and prime[i - 2] == 1):
            dp[i] += 1
    print(dp[n])


if __name__ == "__main__":
    N = 6
    countPrime(N)

# Expected output:
1
# minimize sum of an array having bitwise and of all its pairs present in a given matrix | function to find the minimum sum of the array such that bitwise and of arr [ i ] ana arr [ j ] is mat [ i ] [ j ] ; stores the minimum possible sum ; traverse the range [ 0 , n - 1 ] ; stores the bitwise or of all the element of a row ; traverse the range [ 0 , n - 1 ] ; if i not equal to j ; update the value of res ; increment the sum by res ; return minimum possible sum ; 
def findMinSum(mat, N):
    sum1 = 0
    for i in range(N):
        res = 0
        for j in range(N):
            if (i != j):
                res |= mat[i][j]
        sum1 += res
    return sum1


if __name__ == '__main__':
    mat = [[-1, 2, 3], [9, -1, 7], [4, 5, -1]]
    N = 3
    print(findMinSum(mat, N))

# Expected output:
23
# lcm of unique elements present in an array | python3 program for the above approach ; function to find gcd of two numbers ; base case ; recursively find the gcd ; function to find lcm of two numbers ; function to find lcm of unique elements present in the array ; stores the frequency of each number of the array ; store the frequency of each element of the array ; store the required result ; traverse the map freq ; if the frequency of the current element is 1 , then update ans ; if there is no unique element , set lcm to - 1 ; print the result ; 

from collections import defaultdict


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


def findLCM(a, b):
    return (a * b) // findGCD(a, b)


def uniqueElementsLCM(arr, N):
    freq = defaultdict(int)
    for i in range(N):
        freq[arr[i]] += 1
    lcm = 1
    for i in freq:
        if (freq[i] == 1):
            lcm = findLCM(lcm, i)
    if (lcm == 1):
        lcm = -1
    print(lcm)


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

# Expected output:
4
# minimize maximum difference between adjacent elements possible by removing a single array element | python3 program to implement the above approach ; function to find maximum difference between adjacent array elements ; store the maximum difference ; traverse the array ; update maximum difference ; function to calculate the minimum of maximum difference between adjacent array elements possible by removing a single array element ; stores the required minimum ; stores the updated array ; skip the i - th element ; update minvalue ; return minvalue ; 

import sys


def maxAdjacentDifference(A):
    diff = 0
    for i in range(1, len(A), 1):
        diff = max(diff, A[i] - A[i - 1])
    return diff


def MinimumValue(arr, N):
    MinValue = sys .maxsize
    for i in range(N):
        new_arr = []
        for j in range(N):
            if (i == j):
                continue
            new_arr .append(arr[j])
        MinValue = min(MinValue, maxAdjacentDifference(new_arr))
    return MinValue


if __name__ == '__main__':
    arr = [1, 3, 7, 8]
    N = len(arr)
    print(MinimumValue(arr, N))

# Expected output:
4
# number of antisymmetric relations on a set of n elements | python3 program for the above approach ; function to calculate the value of x ^ y % mod in o ( log y ) ; stores the result of x ^ y ; update x if it exceeds 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 antisymmetric relations in a set consisting of n elements ; print the count of antisymmetric relations ; 

mod = 1000000007


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


def antisymmetricRelation(N):
    return (power(2, N) * power(3, (N * N - N) // 2)) % mod


if __name__ == "__main__":
    N = 2
    print(antisymmetricRelation(N))

# Expected output:
12
# number of asymmetric relations on a set of n elements | python3 program for the above approach ; function to calculate x ^ y modulo ( 10 ^ 9 + 7 ) ; 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 final value of x ^ y ; function to count the number of asymmetric relations in a set consisting of n elements ; return the resultant count ; 

mod = 1000000007


def power(x, y):
    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 asymmetricRelation(N):
    return power(3, (N * N - N) // 2)


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

# Expected output:
3
# check if two vectors are collinear or not | function to calculate cross product of two vectors ; update cross_p [ 0 ] ; update cross_p [ 1 ] ; update cross_p [ 2 ] ; function to check if two given vectors are collinear or not ; store the first and second vectors ; store their cross product ; calculate their cross product ; check if their cross product is a null vector or not ; 
def crossProduct(vect_A, vect_B, cross_P):
    cross_P[0] = (vect_A[1] * vect_B[2] - vect_A[2] * vect_B[1])
    cross_P[1] = (vect_A[2] * vect_B[0] - vect_A[0] * vect_B[2])
    cross_P[2] = (vect_A[0] * vect_B[1] - vect_A[1] * vect_B[0])


def checkCollinearity(x1, y1, z1, x2, y2, z2):
    A = [x1, y1, z1]
    B = [x2, y2, z2]
    cross_P = [0 for i in range(3)]
    crossProduct(A, B, cross_P)
    if (cross_P[0] == 0 and cross_P[1] == 0 and cross_P[2] == 0):
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    x1 = 4
    y1 = 8
    z1 = 12
    x2 = 8
    y2 = 16
    z2 = 24
    checkCollinearity(x1, y1, z1, x2, y2, z2)

# Expected output:
Yes
# count pairs with odd bitwise xor that can be removed and replaced by their bitwise or | function to count the number of pairs required to be removed from the array and replaced by their bitwise or values ; stores the count of even array elements ; traverse the given array ; increment the count of even array elements ; if the array contains at least one odd array element ; otherwise , print 0 ; 
def countPairs(arr, N):
    even = 0
    for i in range(N):
        if (arr[i] % 2 == 0):
            even += 1
    if (N - even >= 1):
        print(even)
        return
    print(0)


if __name__ == "__main__":
    arr = [5, 4, 7, 2]
    N = len(arr)
    countPairs(arr, N)

# Expected output:
2
# distribute m objects starting from sth person such that every ith person gets arr [ i ] objects | function to find distribution of m objects among all array elements ; stores the distribution of m objects ; stores the indices of distribution ; stores the remaining objects ; iterate until rem is positive ; if the number of remaining objects exceeds required the number of objects ; increase the number of objects for the index ptr by arr [ ptr ] ; decrease remaining objects by arr [ ptr ] ; increase the number of objects for the index ptr by rem ; decrease remaining objects to 0 ; increase ptr by 1 ; print the final distribution ; 
def distribute(N, K, M, arr):
    distribution = [0] * N
    ptr = K - 1
    rem = M
    while (rem > 0):
        if (rem >= arr[ptr]):
            distribution[ptr] += arr[ptr]
            rem -= arr[ptr]
        else:
            distribution[ptr] += rem
            rem = 0
        ptr = (ptr + 1) % N
    for i in range(N):
        print(distribution[i], end=" ")


arr = [2, 3, 2, 1, 4]
M = 11
S = 2
N = len(arr)
distribute(N, S, M, arr)

# Expected output:
1 3 2 1 4
# sum of squares of differences between all pairs of an array | function to calculate sum of squares of differences of all possible pairs ; stores the final sum ; stores temporary values ; traverse the array ; final sum ; prthe answer ; 
def sumOfSquaredDifferences(arr, N):
    ans = 0
    sumA, sumB = 0, 0
    for i in range(N):
        sumA += (arr[i] * arr[i])
        sumB += arr[i]
    sumA = N * sumA
    sumB = (sumB * sumB)
    ans = sumA - sumB
    print(ans)


if __name__ == '__main__':
    arr = [2, 8, 4]
    N = len(arr)
    sumOfSquaredDifferences(arr, N)

# Expected output:
56
# count ways to remove objects such that exactly m equidistant objects remain | function to count the number of ways of removing objects such that after removal , exactly m equidistant objects remain ; store the resultant number of arrangements ; base case : when only 1 object is left ; print the result and return ; iterate until len <= n and increment the distance in each iteration ; total length if adjacent objects are d distance apart ; if length > n ; update the number of ways ; print the result ; 
def waysToRemove(n, m):
    ans = 0
    if (m == 1):
        print(n)
        return
    d = 0
    while d >= 0:
        length = m + (m - 1) * d
        if (length > n):
            break
        ans += (n - length) + 1
        d += 1
    print(ans)


if __name__ == "__main__":
    N = 5
    M = 3
    waysToRemove(N, M)

# Expected output:
4
# count unique stairs that can be reached by moving given number of steps forward or backward | python3 program for the above approach ; function to count the number of unique stairs visited ; checks whether the current stair is visited or not ; store the possible moves from the current position ; initialize a queue ; push the starting position ; mark the starting position s as visited ; iterate until queue is not empty ; store the current stair number ; check for all possible moves from the current stair ; store the new stair number ; if it is valid and unvisited ; push it into queue ; mark the stair as visited ; store the result ; count the all visited stairs ; print the result ; 

from collections import deque


def countStairs(n, x, a, b):
    vis = [0] * (n + 1)
    moves = [+a, -a, +b, -b]
    q = deque()
    q .append(x)
    vis[x] = 1
    while (len(q) > 0):
        currentStair = q .popleft()
        for j in range(4):
            newStair = currentStair + moves[j]
            if (newStair > 0 and newStair <= n and (not vis[newStair])):
                q .append(newStair)
                vis[newStair] = 1
    cnt = 0
    for i in range(1, n + 1):
        if (vis[i] == 1):
            cnt += 1
    print(cnt)


if __name__ == '__main__':
    N, S, A, B = 10, 2, 5, 7
    countStairs(N, S, A, B)

# Expected output:
4
# count smaller elements present in the array for each array element | function to count for each array element , the number of elements that are smaller than that element ; traverse the array ; stores the count ; traverse the array ; increment count ; print the count of smaller elements for the current element ; 
def smallerNumbers(arr, N):
    for i in range(N):
        count = 0
        for j in range(N):
            if (arr[j] < arr[i]):
                count += 1
        print(count, end=" ")


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

# Expected output:
3 4 0 0 2
# minimize divisions by 2 , 3 , or 5 required to make two given integers equal | function to calculate gcd of two numbers ; base case ; calculate gcd recursively ; function to count the minimum number of divisions required to make x and y equal ; calculate gcd of x and y ; divide x and y by their gcd ; stores the number of divisions ; iterate until x != y ; maintain the order x <= y ; if x is divisible by 2 , then divide x by 2 ; if x is divisible by 3 , then divide x by 3 ; if x is divisible by 5 , then divide x by 5 ; if x is not divisible by 2 , 3 , or 5 , then pr - 1 ; increment count by 1 ; print the value of count as the minimum number of operations ; 
def gcd(a, b):
    if (b == 0):
        return a
    return gcd(b, a % b)


def minimumOperations(X, Y):
    GCD = gcd(X, Y)
    X = X // GCD
    Y = Y // GCD
    count = 0
    while (X != Y):
        if (Y > X):
            X, Y = Y, X
        if (X % 2 == 0):
            X = X // 2
        elif (X % 3 == 0):
            X = X // 3
        elif (X % 5 == 0):
            X = X // 5
        else:
            print("-1")
            return
        count += 1
    print(count)


if __name__ == '__main__':
    X, Y = 15, 20
    minimumOperations(X, Y)

# Expected output:
3
# minimum increments or decrements required to convert a sorted array into a power sequence | function to find the minimum number of increments or decrements required to convert array into a power sequence ; initialize the count to f ( x ) for x = 1 ; calculate the value of f ( x ) x ^ ( n - 1 ) <= f ( 1 ) + a [ n - 1 ] ; calculate f ( x ) ; check if x ^ ( n - 1 ) > f ( 1 ) + a [ n - 1 ] ; update ans to store the minimum of ans and f ( x ) ; return the minimum number of operations required ; 
def minOperations(a, n):
    ans = 0
    for i in range(n):
        ans += a[i]
    ans -= n
    x = 1
    while (1):
        curPow = 1
        curCost = 0
        for i in range(n):
            curCost += abs(a[i] - curPow)
            curPow *= x
        if (curPow / x > ans + a[n - 1]):
            break
        ans = min(ans, curCost)
        x += 1
    return ans


if __name__ == '__main__':
    arr = [1, 5, 7]
    N = len(arr)
    print(minOperations(arr, N))

# Expected output:
4