giulio98/xlcost-single-prompt huggingface dataset:
# count pairs from an array with even product of count of distinct prime factors | python 3 implementation of the above approach ; function to calculate count of distinct prime factors of a number ; sieve of eratosthenes ; function to count pairs with even product of distinct prime factors ; stores count of distinct prime factors ; stores the count of numbers with even prime factors in b [ ] ; stores the count of numbers with odd prime factors in b [ ] ; even product pairs ; traverse the array b [ ] ; since , product has to be positive i . e > 0 ; if count of prime factors is odd ; increment oddcount by 1 ; increment evencount by 1 ; since , product has to be positive i . e > 0 ; if count of prime factors is odd ; odd * even = even ; if count of prime factors is even ; even * odd = even even * even = even ; 

MAX = 1000000


def countOfPrimefactors(CountDistinct):
    global MAX
    prime = [0 for i in range(MAX + 1)]
    for i in range(MAX + 1):
        CountDistinct[i] = 0
        prime[i] = True
    for i in range(2, MAX + 1, 1):
        if (prime[i]):
            CountDistinct[i] = 1
            for j in range(i * 2, MAX + 1, i):
                CountDistinct[j] += 1
                prime[j] = False


def CountEvenPair(A, B, N, M):
    global MAX
    countDistinct = [0 for i in range(MAX + 1)]
    countOfPrimefactors(countDistinct)
    evenCount = 0
    oddCount = 0
    evenPairs = 0
    for i in range(M):
        if (countDistinct[B[i]] == 0):
            continue
        if (countDistinct[B[i]] & 1):
            oddCount += 1
        else:
            evenCount += 1
    for i in range(N):
        if (countDistinct[A[i]] == 0):
            continue
        if (countDistinct[A[i]] & 1):
            evenPairs += (evenCount)
        else:
            evenPairs += evenCount + oddCount
    return evenPairs


if __name__ == '__main__':
    A = [1, 2, 3]
    B = [4, 5, 6]
    N = len(A)
    M = len(B)
    print(CountEvenPair(A, B, N, M))

# Expected output:
2
# minimum array elements required to be subtracted from either end to reduce k to 0 | function to find the length of longest subarray having sum k ; stores the index of the prefix sum ; traverse the given array ; update sum ; if the subarray starts from index 0 ; add the current prefix sum with index if it is not present in the map um ; check if sum - k is present in map um or not ; update the maxlength ; return the required maximum length ; function to find the minimum removal of array elements required to reduce k to 0 ; stores the sum of the array ; traverse the array arr [ ] ; update sum of the array ; find maxlen ; if the subarray with sum doesn 't exist ; otherwise , print the required maximum length ; 
def longestSubarray(arr, N, K):
    um = {}
    sum, maxLen = 0, 0
    for i in range(N):
        sum += arr[i]
        if (sum == K):
            maxLen = i + 1
        if (sum not in um):
            um[sum] = i
        if ((sum - K) in um):
            if (maxLen < (i - um[sum - K])):
                maxLen = i - um[sum - K]
    return maxLen


def minRequiredOperation(arr, N, K):
    TotalSum = 0
    for i in range(N):
        TotalSum += arr[i]
    maxLen = longestSubarray(arr, N, TotalSum - K)
    if (maxLen == -1):
        print(-1, end="")
    else:
        print(N - maxLen, end="")


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

# Expected output:
2
# minimum number of digits required to be removed to make a number divisible by 4 | function to count the minimum number of digits required to be removed to make a given number divisible by 4 ; store the size of the string ; stores the required result ; check for every pair of digits if the number formed by them is divisible by 4 or not ; store s [ i ] in a variable ; if it is divisible by 2 ; store the number formed by s [ j ] and s [ i ] ; check if it is divisible by 4 ; store the number of digits required to be deleted ; update ans ; if value of ans is unchanged , then check if any s [ i ] is divisible by 4 ; if true , update ans to n - 1 ; prthe result ; 
def minimumDeletions(s):
    n = len(s)
    ans = n
    for i in range(n - 1, -1, -1):
        t = ord(s[i]) - ord('0')
        if (t % 2 == 0):
            for j in range(i - 1, -1, -1):
                num = (ord(s[j]) - ord('0')) * 10 + t
                if (num % 4 == 0):
                    k1 = i - j - 1
                    k2 = n - i - 1
                    ans = min(ans, k1 + k2)
    if (ans == n):
        for i in range(n):
            num = ord(s[i]) - ord('0')
            if (num % 4 == 0):
                ans = n - 1
    print(ans)


if __name__ == '__main__':
    str = "12367"
    minimumDeletions(str)

# Expected output:
1
# generate a sequence such that float division of array elements is maximized | function to place the parenthesis such that the result is maximized ; store the required string ; add the first integer to string ; if the size of array is 1 ; if the size of array is 2 , print the 1 st integer followed by / operator followed by the next integer ; if size of array is exceeds two , pr1st integer concatenated with operators ' / ' , ' ( ' and next integers with the operator '/ ; add parenthesis at the end ; prthe final expression ; 
def generateSequence(arr, n):
    ans = ""
    ans = str(arr[0])
    if (n == 1):
        print(ans)
    elif (n == 2):
        print(ans + "/"+str(arr[1]))
    else:
        ans += "/(" + str(arr[1])

        for i in range(2, n):
            ans += "/" + str(arr[i])
        ans += ")"
        print(ans)


if __name__ == '__main__':
    arr = [1000, 100, 10, 2]
    N = len(arr)
    generateSequence(arr, N)

# Expected output:
1000/(100/10/2)
# count of pairs having even and odd lcm from an array | function to find count of distinct pairs having even lcm and odd lcm ; store the total number of pairs ; stores the count of odd numbers in the array ; traverse the array arr [ ] ; update the count of pairs with odd lcm ; print the count of required pairs ; 
def LCMPairs(arr, N):
    total_pairs = (N * (N - 1)) / 2
    odd = 0
    for i in range(N):
        if (arr[i] & 1):
            odd += 1
    odd = (odd * (odd - 1)) // 2
    print("Even =", int(total_pairs - odd), ",", " Odd =", odd)


if __name__ == '__main__':
    arr = [3, 6, 5, 4]
    N = len(arr)
    LCMPairs(arr, N)

# Expected output:
Even = 5 ,  Odd = 1
# value required to be added to n to obtain the sum of first m multiples of k | function to print the value to be added to n to obtain sum of first m multiples of k ; store the sum of the first m multiples of k ; store the value to be added to obtain n ; 
def printNumber(N, K, M):
    sum = K * (M * (M + 1) / 2)
    return sum - N


N = 17
K = 3
M = 4
print(int(printNumber(N, K, M)))

# Expected output:
13
# count even and odd bitwise xors of consecutive numbers in a range [ l , r ] starting from l | prcount of even and odd numbers of xor value from l to r ; store the number of elements between l and r ; count of even xor values ; if l is odd and range % 4 = 3 ; increment even by 1 ; if l is even and range % 4 != 0 ; increment even by 1 ; prthe answer ; 
def countEvenOdd(L, R):
    range = R - L + 1
    even = (range // 4) * 2
    if ((L & 1) != 0 and (range % 4 == 3)):
        even += 1
    elif ((L & 1) == 0 and (range % 4 != 0)):
        even += 1
    print("Even = ", even, ", Odd = ", (range - even))


if __name__ == '__main__':
    L = 2
    R = 7
    countEvenOdd(L, R)

# Expected output:
Even =  3 , Odd =  3
# minimize swaps of same | function to count the number of swaps required to make the sum of ascii values of the characters of both strings odd ; initialize alphabets with value ; initialize values for each alphabet ; size of the string ; sum of s ; sum of t ; stores whether there is any index i such that s [ i ] and t [ i ] have different parities ; traverse the strings ; update sum1 and sum2 ; if ord ( s [ i ] ) anord ( ' a ) rd ( t [ i ] ) ▁ haord ( ' a ) different parities ; if sum1 and sum2 are both odd ; if sum1 and sum2 are both even ; if exists pr1 ; otherwise ; if sum1 and sum2 are of different parities ; 
def countSwaps(S, T):
    value = [0] * 26
    for i in range(26):
        value[i] = i + 1
    N = len(S)
    sum1 = 0
    sum2 = 0
    flag = False
    for i in range(N):
        sum1 += value[ord(S[i]) - ord('a')]
        sum2 += value[ord(T[i]) - ord('a')]
        if (value[ord(S[i]) - ord('a')] %
            2 == 0 and value[ord(T[i]) - ord('a')] %
            2 == 1 or value[ord(S[i]) - ord('a')] %
            2 == 1 and value[ord(T[i]) - ord('a')] %
                2 == 0):
            flag = False
    if (sum1 % 2 == 1 and sum2 % 2 == 1):
        print("0")
    elif (sum1 % 2 == 0 and sum2 % 2 == 0):
        if (flag):
            print("1")
        else:
            print("-1")
    else:
        print("-1")


if __name__ == '__main__':
    S = "acd"
    T = "dbf"
    countSwaps(S, T)

# Expected output:
-1
# count distinct prime triplets up to n such that sum of two primes is equal to the third prime | python3 program for the above approach ; function to check if a number is a prime or not ; function to count the number of valid prime triplets ; stores the count of prime triplets ; iterate from 2 to n and check for each p , whether p & ( p - 2 ) are prime or not ; prthe count obtained ; 

import math


def isPrime(N):
    if (N <= 1):
        return False
    for i in range(2, int(math .sqrt(N) + 1)):
        if (N % i == 0):
            return False
    return True


def countPrimeTuples(N):
    count = 0
    for i in range(2, N + 1):
        if (isPrime(i) and isPrime(i - 2)):
            count += 1
    print(count)


N = 6
countPrimeTuples(N)

# Expected output:
1
# count pairs from an array whose bitwise or is greater than bitwise and | function to count the number of pairs ( i , j ) their bitwise or is greater than bitwise and ; store the required answer ; check for all possible pairs ; if the condition satisfy then increment count by 1 ; prthe answer ; 
def countPairs(A, n):
    count = 0
    for i in range(n):
        for j in range(i + 1, n):
            if ((A[i] | A[j]) > (A[i] & A[j])):
                count += 1
    print(count)


if __name__ == '__main__':
    A = [1, 4, 7]
    N = len(A)
    countPairs(A, N)

# Expected output:
3
# count maximum number of consumable candies | function to find the count of maximum consumable candies ; store the count of total candies ; stores the count of maximum consumable candies ; checks if it is safe to counsume all candies ; traverse the array arr ; if a [ i ] + m is greater than b [ i ] ; mark all_safe as false ; update ans ; update ans ; increment total by a [ i ] ; if all_safe is true ; otherwise , ; 
def maximumCandy(candies, safety, N, M):
    total = 0
    ans = 10 ** 8
    all_safe = True
    for i in range(N):
        if (candies[i] + M > safety[i]):
            all_safe = False
            ans = min(ans, safety[i])
        else:
            ans = min(ans, candies[i] + M)
        total += candies[i]
    if (all_safe):
        return total
    else:
        return ans


if __name__ == '__main__':
    A = [4, 5, 2, 3]
    B = [8, 13, 6, 4]
    M = 5
    N = len(A)
    print(maximumCandy(A, B, N, M))

# Expected output:
4
# check if two arrays can be made equal by swapping pairs of one of the arrays | function to check if two arrays can be made equal or not by swapping pairs of only one of the arrays ; stores elements required to be replaced ; to check if the arrays can be made equal or not ; traverse the array ; if array elements are not equal ; increment count by 1 ; decrement count by 1 ; if flag is true and count is 0 , pr " yes " . otherwise " no " ; 
def checkArrays(arr1, arr2, N):
    count = 0
    flag = True
    for i in range(N):
        if (arr1[i] != arr2[i]):
            if (arr1[i] == 0):
                count += 1
            else:
                count -= 1
                if (count < 0):
                    flag = 0
                    break
    if (flag and count == 0):
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    arr1 = [0, 0, 1, 1]
    arr2 = [1, 1, 0, 0]
    N = len(arr1)
    checkArrays(arr1, arr2, N)

# Expected output:
Yes
# generate an n | function to construct an array with sum of each subarray divisible by k ; traverse a loop from 1 to n ; pri - th multiple of k ; 
def construct_Array(N, K):
    for i in range(1, N + 1):
        print(K * i, end=" ")


N = 3
K = 3
construct_Array(N, K)

# Expected output:
3 6 9
# generate an n | fun dtion to print the required sequence ; stores count of even and odd elements ; stores sum of even and odd elements ; print n / 2 even elements ; print n / 2 - 1 odd elements ; print final odd element ; 
def Print(N):
    if ((N / 2) % 2 or (N % 2)):
        print(-1)
        return
    CurEven = 2
    CurOdd = 1
    SumOdd = 0
    SumEven = 0
    for i in range(N // 2):
        print(CurEven, end=" ")
        SumEven += CurEven
        CurEven += 2
    for i in range((N // 2) - 1):
        print(CurOdd, end=" ")
        SumOdd += CurOdd
        CurOdd += 2
    CurOdd = SumEven - SumOdd
    print(CurOdd)


N = 12
Print(N)

# Expected output:
2 4 6 8 10 12 1 3 5 7 9 17
# find the last remaining element after repeated removal of an element from pairs of increasing adjacent array elements | function to print the last remaining array element after after performing given operations ; if size of the array is 1 ; check for the condition ; if condition is not satisfied ; 
def canArrayBeReduced(arr, N):
    if (N == 1):
        print(arr[0])
        return
    if (arr[0] < arr[N - 1]):
        print(arr[N - 1])
    else:
        print("Not Possible")


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

# Expected output:
7
# minimum prefix increments required to make all elements of an array multiples of another array | python3 program for the above approach ; function to find minimum count of operations required to make a [ i ] multiple of b [ i ] by incrementing prefix subarray ; stores minimum count of operations required to make a [ i ] multiple of b [ i ] by incrementing prefix subarray ; stores the carry ; stores minimum difference of correspoinding element in prefix subarray ; traverse the array ; stores the closest greater or equal number to a [ i ] which is a multiple of b [ i ] ; stores minimum difference ; update totaloperations ; update carry ; 

from math import ceil, floor


def MinimumMoves(A, B, N):
    totalOperations = 0
    carry = 0
    K = 0
    for i in range(N - 1, -1, -1):
        nearestMultiple = ceil((A[i] + carry) / B[i]) * B[i]
        K = nearestMultiple - (A[i] + carry)
        totalOperations += K
        carry += K
    return totalOperations


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

# Expected output:
22
# minimize deviation of an array by given operations | function to find the minimum deviation of the array a [ ] ; store all array elements in sorted order ; odd number are transformed using 2 nd operation ; ( maximum - minimum ) ; check if the size of set is > 0 and the maximum element is divisible by 2 ; maximum element of the set ; erase the maximum element ; using operation 1 ; ( maximum - minimum ) ; print the minimum deviation obtained ; 
def minimumDeviation(A, N):
    s = set([])
    for i in range(N):
        if (A[i] % 2 == 0):
            s .add(A[i])
        else:
            s .add(2 * A[i])
    s = list(s)
    diff = s[-1] - s[0]
    while (len(s) and s[-1] % 2 == 0):
        maxEl = s[-1]
        s .remove(maxEl)
        s .append(maxEl // 2)
        diff = min(diff, s[-1] - s[0])
    print(diff)


if __name__ == "__main__":
    A = [4, 1, 5, 20, 3]
    N = len(A)
    minimumDeviation(A, N)

# Expected output:
3
# maximize given integer by swapping pairs of unequal bits | function to return the maximum possible value that can be obtained from the given integer by performing given operations ; convert to equivalent binary representation ; stores binary representation of the maximized value ; store the count of 0 ' s ▁ and ▁ 1' s ; stores the total number of bits ; if current bit is set ; increment count1 ; increment count0 ; shift all set bits to left to maximize the value ; shift all unset bits to right to maximize the value ; return the maximized value ; 
def findMaxNum(num):
    binaryNumber = bin(num)[2:]
    maxBinaryNumber = ""
    count0, count1 = 0, 0
    N = len(binaryNumber)
    for i in range(N):
        if (binaryNumber[i] == '1'):
            count1 += 1
        else:
            count0 += 1
    for i in range(count1):
        maxBinaryNumber += '1'
    for i in range(count0):
        maxBinaryNumber += '0'
    return int(maxBinaryNumber, 2)


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

# Expected output:
14
# minimum deletions to convert given integer to an odd number whose sum of digits is even | set 2 | function to find minimum count of digits required to be remove to make n odd and the sum of digits of n even ; stores count of even digits ; stores count of odd digits ; iterate over the digits of n ; if current digit is even ; update even ; otherwise ; update odd ; base conditions ; stores count of digits required to be removed to make n odd and the sum of digits of n even ; iterate over the digits of n ; if current digit is even ; update ans ; otherwise , ; update ans ; if count of odd digits is odd ; update ans ; finally print the ans ; 
def minOperations(N):
    even = 0
    odd = 0
    for it in N:
        if (int(ord(it) - ord('0')) % 2 == 0):
            even += 1
        else:
            odd += 1
    if (odd == 0 or odd == 1):
        print("Not Possible" + "")
    else:
        ans = 0
        for it in N:
            if (int(ord(it) - ord('0')) % 2 == 0):
                ans += 1
            else:
                ans = 0
        if (odd % 2 != 0):
            ans += 1
        print(ans, end=" ")


if __name__ == '__main__':
    N = "12345"
    minOperations(N)

# Expected output:
1
# find the player with least 0 s after emptying a binary string by removing non | function to find the player who wins the game ; stores total count of 0 s in the string ; stores count of consecutive 1 s ; stores nim - sum on count of consecutive 1 s ; stores length of the string ; traverse the string ; if the current character is 1 ; update cntconone ; update nimsum ; update cntconone ; update cntzero ; update nimsum ; if countzero is an even number ; nimsum is not 0 ; if nimsum is zero ; 
def FindwinnerOfGame(S):
    cntZero = 0
    cntConOne = 0
    nimSum = 0
    N = len(S)
    for i in range(N):
        if (S[i] == '1'):
            cntConOne += 1
        else:
            nimSum ^= cntConOne
            cntConOne = 0
            cntZero += 1
    nimSum ^= cntConOne
    if (cntZero % 2 == 0):
        print("Tie")
    elif (nimSum):
        print("player 1")
    else:
        print("player 2")


if __name__ == '__main__':
    S = "0110011"
    FindwinnerOfGame(S)

# Expected output:
player 2
# place first n natural numbers at indices not equal to their values in an array | function to place first n natural numbers in an array such that none of the values are equal to its indices ; stores the required array ; place n at the first position ; iterate the range [ 1 , n ) ; append elements to the sequence ; print the sequence ; 
def generatepermutation(N):
    answer = []
    answer .append(N)
    for i in range(1, N):
        answer .append(i)
    print(*answer)


N = 4
generatepermutation(N)

# Expected output:
4 1 2 3
# maximize element at index k in an array with at most sum m and difference between adjacent elements at most 1 | function to calculate the maximum possible value at index k ; stores the sum of elements in the left and right of index k ; stores the maximum possible value at index k ; prthe answer ; 
def maxValueAtIndexK(N, K, M):
    S1 = 0
    S2 = 0
    S1 = K * (K + 1) // 2
    S2 = (N - K - 1) * (N - K) // 2
    X = (M + S1 + S2) // N
    print(X)


if __name__ == '__main__':
    N = 3
    K = 1
    M = 7
    maxValueAtIndexK(N, K, M)

# Expected output:
3
# farthest cell from a given cell in a matrix | function to find the farthest cell distance from the given cell ; from cell ( n , m ) ; from cell ( 1 , 1 ) ; from cell ( n , 1 ) ; from cell ( 1 , m ) ; finding out maximum ; prthe answer ; 
def farthestCellDistance(N, M, R, C):
    d1 = N + M - R - C
    d2 = R + C - 2
    d3 = N - R + C - 1
    d4 = M - C + R - 1
    maxDistance = max(d1, max(d2, max(d3, d4)))
    print(maxDistance)


if __name__ == '__main__':
    N = 15
    M = 12
    R = 1
    C = 6
    farthestCellDistance(N, M, R, C)

# Expected output:
20
# maximize every array element by repeatedly adding all valid i + a [ i ] th array element | function to maximize value at every array index by performing given operations ; traverse the array in reverse ; if the current index is a valid index ; prthe array ; 
def maxSum(arr, N):
    ans = 0
    for i in range(N - 1, -1, -1):
        t = i
        if (t + arr[i] < N):
            arr[i] += arr[t + arr[i]]
    for i in range(N):
        print(arr[i], end=" ")


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

# Expected output:
12 11 7 9 8
# path from the root node to a given node in an n | python3 program for the above approach ; function to find the path from root to n ; stores the number of nodes at ( i + 1 ) - th level ; stores the number of nodes ; stores if the current level is even or odd ; if level is odd ; if level is even ; if level with node n is reached ; push into vector ; compute prefix sums of count of nodes in each level ; stores the level in which node n s present ; store path ; insert temp into path ; print path ; 

from bisect import bisect_left


def PrintPathNodes(N):
    arr = []
    arr .append(1)
    k = 1
    flag = True
    while (k < N):
        if (flag):
            k *= 2
            flag = False
        else:
            k *= 4
            flag = True
        if (k > N):
            break
        arr .append(k)
    lenn = len(arr)
    prefix = [0] * (lenn)
    prefix[0] = 1
    for i in range(1, lenn):
        prefix[i] = arr[i] + prefix[i - 1]
    it = bisect_left(prefix, N)
    ind = it
    temp = N
    path = []
    path .append(N)
    while (ind > 1):
        val = temp - prefix[ind - 1]
        if (ind % 2 != 0):
            temp = prefix[ind - 2] + (val + 1) // 2
        else:
            temp = prefix[ind - 2] + (val + 3) // 4
        ind -= 1
        path .append(temp)
    if (N != 1):
        path .append(1)
    for i in range(len(path) - 1, -1, -1):
        print(path[i], end=" ")


if __name__ == '__main__':
    N = 14
    PrintPathNodes(N)

# Expected output:
1 2 5 14
# generate an n | python3 program for the above approach ; function to construct an array with given conditions ; traverse the array arr [ ] ; stores closest power of 2 less than or equal to arr [ i ] ; stores r into brr [ i ] ; prarray elements ; 

from math import log


def constructArray(arr, N):
    brr = [0] * N
    for i in range(N):
        K = int(log(arr[i]) / log(2))
        R = pow(2, K)
        brr[i] = R
    for i in range(N):
        print(brr[i], end=" ")


if __name__ == '__main__':
    arr = [11, 5, 7, 3, 2]
    N = len(arr)
    constructArray(arr, N)

# Expected output:
8 4 4 2 2
# find the element at specified index in a spiral matrix | function to the find element at ( i , j ) index ; 
def findInGrid(i, j):
    if (i == j):
        return (i * i - (i - 1))
    elif (i > j):
        if (i % 2 == 0):
            return i * i - (j - 1)
        else:
            return ((i - 1) * (i - 1) + 1 + (j - 1))
    else:
        if (j % 2 == 0):
            return ((j - 1) * (j - 1) + 1 + (i - 1))
        else:
            return j * j - (i - 1)


i = 3
j = 4
print(findInGrid(i, j))

# Expected output:
12