giulio98/xlcost-single-prompt huggingface dataset:
# number of subarrays with positive product | function to return the count of subarrays with negative product ; replace current element with 1 if it is positive else replace it with - 1 instead ; take product with previous element to form the prefix product ; count positive and negative elements in the prefix product array ; return the required count of subarrays ; function to return the count of subarrays with positive product ; total subarrays possible ; count to subarrays with negative product ; return the count of subarrays with positive product ; 
def negProdSubArr(arr, n):
    positive = 1
    negative = 0
    for i in range(n):
        if (arr[i] > 0):
            arr[i] = 1
        else:
            arr[i] = -1
        if (i > 0):
            arr[i] *= arr[i - 1]
        if (arr[i] == 1):
            positive += 1
        else:
            negative += 1
    return (positive * negative)


def posProdSubArr(arr, n):
    total = (n * (n + 1)) / 2
    cntNeg = negProdSubArr(arr, n)
    return (total - cntNeg)


arr = [5, -4, -3, 2, -5]
n = len(arr)
print(posProdSubArr(arr, n))

# Expected output:
7.0
# sum of all natural numbers from l to r ( for large values of l and r ) | python3 implementation of the approach ; value of inverse modulo 2 with 10 ^ 9 + 7 ; function to return num % 1000000007 where num is a large number ; initialize result ; one by one process all the digits of string 'num ; function to return the sum of the integers from the given range modulo 1000000007 ; a stores the value of l modulo 10 ^ 9 + 7 ; b stores the value of r modulo 10 ^ 9 + 7 ; l stores the sum of natural numbers from 1 to ( a - 1 ) ; r stores the sum of natural numbers from 1 to b ; if the result is negative ; 

mod = 1000000007
inv2 = 500000004


def modulo(num):
    res = 0
    for i in range(len(num)):
        res = (res * 10 + int(num[i]) - 0) % mod
    return res


def findSum(L, R):
    a = modulo(L)
    b = modulo(R)
    l = ((a * (a - 1)) % mod * inv2) % mod
    r = ((b * (b + 1)) % mod * inv2) % mod
    ret = (r % mod - l % mod)
    if (ret < 0):
        ret = ret + mod
    else:
        ret = ret % mod
    return ret

if __name__ == "__main__":
    L = "88949273204"
    R = "98429729474298592"
    print((findSum(L, R)))

# Expected output:
252666158
# maximum subsequence sum such that all elements are k distance apart | function to return the maximum subarray sum for the array { a [ i ] , a [ i + k ] , a [ i + 2 k ] , ... } ; function to return the sum of the maximum required subsequence ; to store the result ; run a loop from 0 to k ; find the maximum subarray sum for the array { a [ i ] , a [ i + k ] , a [ i + 2 k ] , ... } ; return the maximum value ; 

import sys


def maxSubArraySum(a, n, k, i):
    max_so_far = -sys .maxsize
    max_ending_here = 0
    while (i < n):
        max_ending_here = max_ending_here + a[i]
        if (max_so_far < max_ending_here):
            max_so_far = max_ending_here
        if (max_ending_here < 0):
            max_ending_here = 0
        i += k
    return max_so_far


def find(arr, n, k):
    maxSum = 0
    for i in range(0, min(n, k) + 1):
        sum = 0
        maxSum = max(maxSum, maxSubArraySum(arr, n, k, i))
    return maxSum


if __name__ == '__main__':
    arr = [2, -3, -1, -1, 2]
    n = len(arr)
    k = 2
    print(find(arr, n, k))

# Expected output:
3
# probability that a n digit number is palindrome | find the probability that a n digit number is palindrome ; denominator ; assign 10 ^ ( floor ( n / 2 ) ) to denominator ; display the answer ; 
def solve(n):
    n_2 = n // 2
    den = "1"
    while (n_2):
        den += '0'
        n_2 -= 1
    print(str(1) + "/" + str(den))


if __name__ == "__main__":
    N = 5
    solve(N)

# Expected output:
1/100
# ways to choose balls such that at least one ball is chosen | python3 implementation of the approach ; function to return the count of ways to choose the balls ; return ( ( 2 ^ n ) - 1 ) % mod ; 

MOD = 1000000007


def countWays(n):
    return (((2 ** n) - 1) % MOD)


n = 3
print(countWays(n))

# Expected output:
7
# minimize the sum of the array according the given condition | function to return the minimum sum ; sort the array to find the minimum element ; finding the number to divide ; checking to what instance the sum has decreased ; getting the max difference ; 
def findMin(arr, n):
    sum = 0
    for i in range(0, n):
        sum = sum + arr[i]
    arr .sort()
    min = arr[0]
    max = 0
    for i in range(n - 1, 0, -1):
        num = arr[i]
        total = num + min
        for j in range(2, num + 1):
            if (num % j == 0):
                d = j
                now = (num // d) + (min * d)
                reduce = total - now
                if (reduce > max):
                    max = reduce
    print(sum - max)


arr = [1, 2, 3, 4, 5]
n = len(arr)
findMin(arr, n)

# Expected output:
14
# print all the permutation of length l using the elements of an array | iterative | convert the number to lth base and print the sequence ; sequence is of length l ; print the ith element of sequence ; print all the permuataions ; there can be ( len ) ^ l permutations ; convert i to len th base ; 
def convert_To_Len_th_base(n, arr, Len, L):
    for i in range(L):
        print(arr[n % Len], end="")
        n //= Len
    print()


def printf(arr, Len, L):
    for i in range(pow(Len, L)):
        convert_To_Len_th_base(i, arr, Len, L)


arr = [1, 2, 3]
Len = len(arr)
L = 2
printf(arr, Len, L)

# Expected output:
11
21
31
12
22
32
13
23
33
# number of possible permutations when absolute difference between number of elements to the right and left are given | function to find the number of permutations possible of the original array to satisfy the given absolute differences ; to store the count of each a [ i ] in a map ; if n is odd ; check the count of each whether it satisfy the given criteria or not ; there is only 1 way for middle element . ; for others there are 2 ways . ; now find total ways ; when n is even . ; there will be no middle element so for each a [ i ] there will be 2 ways ; 
def totalways(arr, n):
    cnt = dict()
    for i in range(n):
        cnt[arr[i]] = cnt .get(arr[i], 0) + 1
    if (n % 2 == 1):
        start, endd = 0, n - 1
        for i in range(start, endd + 1, 2):
            if (i == 0):
                if (cnt[i] != 1):
                    return 0
            else:
                if (cnt[i] != 2):
                    return 0
        ways = 1
        start = 2
        endd = n - 1
        for i in range(start, endd + 1, 2):
            ways = ways * 2
        return ways
    elif (n % 2 == 0):
        start = 1
        endd = n - 1
        for i in range(1, endd + 1, 2):
            if (cnt[i] != 2):
                return 0
        ways = 1
        for i in range(start, endd + 1, 2):
            ways = ways * 2
        return ways


N = 5
arr = [2, 4, 4, 0, 2]
print(totalways(arr, N))

# Expected output:
4
# proizvolov 's identity | function to implement proizvolov 's identity ; according to proizvolov 's identity ; 
def proizvolov(a, b, n):
    return n * n


a = [1, 5, 6, 8, 10]
b = [9, 7, 4, 3, 2]
n = len(a)
print(proizvolov(a, b, n,))

# Expected output:
25
# find the sum of elements of the matrix generated by the given rules | function to return the required ssum ; to store the ssum ; for every row ; update the ssum as a appears i number of times in the current row ; update a for the next row ; return the ssum ; 
def Sum(A, B, R):
    ssum = 0
    for i in range(1, R + 1):
        ssum = ssum + (i * A)
        A = A + B
    return ssum


A, B, R = 5, 3, 3
print(Sum(A, B, R))

# Expected output:
54
# euclidâ €“ mullin sequence | function to return the smallest prime factor of n ; 
def smallestPrimeFactor(n):
    i = 2
    while (i * i) <= n:
        if n % i == 0:
            return i
        i += 1
    return n


def solve(n):
    product = 1
    i = 0
    while i < n:
        num = smallestPrimeFactor(product + 1)
        print(num, end=' ')
        product = product * num
        i += 1


b = 14
solve(b)

# Expected output:
2 3 7 43 13 53 5 6221671 38709183810571 139 2801 11 17 5471
# count total set bits in all numbers from 1 to n | set 2 | function to return the sum of the count of set bits in the integers from 1 to n ; ignore 0 as all the bits are unset ; to store the powers of 2 ; to store the result , it is initialized with n / 2 because the count of set least significant bits in the integers from 1 to n is n / 2 ; loop for every bit required to represent n ; total count of pairs of 0 s and 1 s ; totalpairs / 2 gives the complete count of the pairs of 1 s multiplying it with the current power of 2 will give the count of 1 s in the current bit ; if the count of pairs was odd then add the remaining 1 s which could not be groupped together ; next power of 2 ; return the result ; 
def countSetBits(n):
    n += 1
    powerOf2 = 2
    cnt = n // 2
    while (powerOf2 <= n):
        totalPairs = n // powerOf2
        cnt += (totalPairs // 2) * powerOf2
        if (totalPairs & 1):
            cnt += (n % powerOf2)
        else:
            cnt += 0
        powerOf2 <<= 1
    return cnt


if __name__ == "__main__":
    n = 14
    print(countSetBits(n))

# Expected output:
28
# find the height of a right | function to return the height of the right - angled triangle whose area is x times its base ; 
def getHeight(X):
    return (2 * X)


if __name__ == '__main__':
    X = 35
    print(getHeight(X))

# Expected output:
70
# find sum of inverse of the divisors when sum of divisors and the number is given | function to return the sum of inverse of divisors ; calculating the answer ; return the answer ; 
def SumofInverseDivisors(N, Sum):
    ans = float(Sum) * 1.0 / float(N)
    return round(ans, 2)


N = 9
Sum = 13
print(SumofInverseDivisors(N, Sum))

# Expected output:
1.44
# number of triplets such that each value is less than n and each pair sum is a multiple of k | function to return the number of triplets ; initializing the count array ; storing the frequency of each modulo class ; if k is odd ; if k is even ; 
def NoofTriplets(N, K):
    cnt = [0] * K
    for i in range(1, N + 1):
        cnt[i % K] += 1
    if (K & 1):
        rslt = cnt[0] * cnt[0] * cnt[0]
        return rslt
    else:
        rslt = (cnt[0] * cnt[0] * cnt[0] + cnt[K // 2]
                * cnt[K // 2] * cnt[K // 2])
        return rslt


if __name__ == "__main__":
    N = 3
    K = 2
    print(NoofTriplets(N, K))

# Expected output:
9
# find a number containing n | function to compute number using our deduced formula ; initialize num to n - 1 ; 
def findNumber(n):
    num = n - 1
    num = 2 * (4 ** num)
    num = num // 3
    return num


if __name__ == "__main__":
    n = 5
    print(findNumber(n))

# Expected output:
170
# find xor of numbers from the range [ l , r ] | python3 implementation of the approach ; function to return the xor of elements from the range [ 1 , n ] ; if n is a multiple of 4 ; if n % 4 gives remainder 1 ; if n % 4 gives remainder 2 ; if n % 4 gives remainder 3 ; function to return the xor of elements from the range [ l , r ] ; 

from operator import xor


def findXOR(n):
    mod = n % 4
    if (mod == 0):
        return n
    elif (mod == 1):
        return 1
    elif (mod == 2):
        return n + 1
    elif (mod == 3):
        return 0


def findXORFun(l, r):
    return (xor(findXOR(l - 1), findXOR(r)))


l = 4
r = 8
print(findXORFun(l, r))

# Expected output:
8
# number of elements from the array which are reachable after performing given operations on d | function to return the gcd of a and b ; function to return the count of reachable integers from the given array ; gcd of a and b ; to store the count of reachable integers ; if current element can be reached ; return the count ; 
def GCD(a, b):
    if (b == 0):
        return a
    return GCD(b, a % b)


def findReachable(arr, D, A, B, n):
    gcd_AB = GCD(A, B)
    count = 0
    for i in range(n):
        if ((arr[i] - D) % gcd_AB == 0):
            count += 1
    return count


arr = [4, 5, 6, 7, 8, 9]
n = len(arr)
D = 4
A = 4
B = 6
print(findReachable(arr, D, A, B, n))

# Expected output:
3
# number of trees whose sum of degrees of all the vertices is l | iterative function to calculate ( x ^ y ) in o ( log y ) ; initialize result ; if y is odd , multiply x with result ; y must be even now y = y / 2 ; function to return the count of required trees ; number of nodes ; return the result ; 
def power(x, y):
    res = 1
    while (y > 0):
        if (y % 2 == 1):
            res = (res * x)
        y = int(y) >> 1
        x = (x * x)
    return res


def solve(L):
    n = L / 2 + 1
    ans = power(n, n - 2)
    return int(ans)


L = 6
print(solve(L))

# Expected output:
16
# change one element in the given array to make it an arithmetic progression | python program to change one element of an array such that the resulting array is in arithmetic progression . ; finds the initial term and common difference and prints the resulting array . ; check if the first three elements are in arithmetic progression ; check if the first element is not in arithmetic progression ; the first and fourth element are in arithmetic progression ; print the arithmetic progression ; 
def makeAP(arr, n):
    initial_term, common_difference = 0, 0
    if (n == 3):
        common_difference = arr[2] - arr[1]
        initial_term = arr[1] - common_difference
    elif ((arr[1] - arr[0]) == arr[2] - arr[1]):
        initial_term = arr[0]
        common_difference = arr[1] - arr[0]
    elif ((arr[2] - arr[1]) == (arr[3] - arr[2])):
        common_difference = arr[2] - arr[1]
        initial_term = arr[1] - common_difference
    else:
        common_difference = (arr[3] - arr[0]) / 3
        initial_term = arr[0]
    for i in range(n):
        print(int(initial_term + (i * common_difference)), end=" ")
    print()


arr = [1, 3, 7]
n = len(arr)
makeAP(arr, n)

# Expected output:
-1 3 7
# find if ncr is divisible by the given prime | function to return the highest power of p that divides n ! implementing legendre formula ; return the highest power of p which divides n ! ; function that returns true if ncr is divisible by p ; find the highest powers of p that divide n ! , r ! and ( n - r ) ! ; if ncr is divisible by p ; 
def getfactor(n, p):
    pw = 0
    while (n):
        n //= p
        pw += n
    return pw


def isDivisible(n, r, p):
    x1 = getfactor(n, p)
    x2 = getfactor(r, p)
    x3 = getfactor(n - r, p)
    if (x1 > x2 + x3):
        return True
    return False


if __name__ == "__main__":
    n = 7
    r = 2
    p = 7
    if (isDivisible(n, r, p)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# check if the number is even or odd whose digits and base ( radix ) is given | function that returns true if the number represented by arr [ ] is even in base r ; if the base is even , then the last digit is checked ; if base is odd , then the number of odd digits are checked ; to store the count of odd digits ; number is odd ; 
def isEven(arr, n, r):
    if (r % 2 == 0):
        if (arr[n - 1] % 2 == 0):
            return True
    else:
        oddCount = 0
        for i in range(n):
            if (arr[i] % 2 != 0):
                oddCount += 1
        if (oddCount % 2 == 0):
            return True
    return False


if __name__ == '__main__':
    arr = [1, 0]
    n = len(arr)
    r = 2
    if (isEven(arr, n, r)):
        print("Even")
    else:
        print("Odd")

# Expected output:
Even
# bitwise and of sub | python implementation of the approach ; function to return the minimum possible value of | k - x | where x is the bitwise and of the elements of some sub - array ; check all possible sub - arrays ; find the overall minimum ; no need to perform more and operations as | k - x | will increase ; 

import sys


def closetAND(arr, n, k):
    ans = sys .maxsize
    for i in range(n):
        X = arr[i]
        for j in range(i, n):
            X &= arr[j]
            ans = min(ans, abs(k - X))
            if (X <= k):
                break
    return ans


arr = [4, 7, 10]
n = len(arr)
k = 2
print(closetAND(arr, n, k))

# Expected output:
0
# count of quadruplets from range [ l , r ] having gcd equal to k | function to return the gcd of a and b ; function to return the count of quadruplets having gcd = k ; count the frequency of every possible gcd value in the range ; to store the required count ; calculate the answer using frequency values ; return the required count ; 
def gcd(a, b):
    if (b == 0):
        return a
    return gcd(b, a % b)


def countQuadruplets(l, r, k):
    frequency = [0] * (r + 1)
    for i in range(l, r + 1):
        for j in range(l, r + 1):
            frequency[gcd(i, j)] += 1
    answer = 0
    for i in range(l, r + 1):
        for j in range(l, r + 1):
            if (gcd(i, j) == k):
                answer += (frequency[i] * frequency[j])
    return answer


if __name__ == '__main__':
    l, r, k = 1, 10, 2
    print(countQuadruplets(l, r, k))

# Expected output:
607
# rearrange the array to maximize the number of primes in prefix sum of the array | function to print the re - arranged array ; count the number of ones and twos in a [ ] ; if the array element is 1 ; array element is 2 ; if it has at least one 2 fill up first 2 ; decrease the cnt of ones if even ; fill up with odd count of ones ; fill up with remaining twos ; if even ones , then fill last position ; print the rearranged array ; 
def solve(a, n):
    ones, twos = 0, 0
    for i in range(n):
        if (a[i] == 1):
            ones += 1
        else:
            twos += 1
    ind = 0
    if (twos):
        a[ind] = 2
        ind += 1
    if ones % 2 == 0:
        evenOnes = True
    else:
        evenOnes = False
    if (evenOnes):
        ones -= 1
    for i in range(ones):
        a[ind] = 1
        ind += 1
    for i in range(twos - 1):
        a[ind] = 2
        ind += 1
    if (evenOnes):
        a[ind] = 1
        ind += 1
    for i in range(n):
        print(a[i], end=" ")


a = [1, 2, 1, 2, 1]
n = len(a)
solve(a, n)

# Expected output:
2 1 1 1 2
# generate an array in which count of even and odd sum sub | function to generate and print the required array ; find the number of odd prefix sums ; if no odd prefix sum found ; calculating the number of even prefix sums ; stores the current prefix sum ; if current prefix sum is even ; print 0 until e = evenpresums - 1 ; print 1 when e = evenpresums ; print 0 for rest of the values ; 
def CreateArray(N, even, odd):
    temp = -1
    for i in range(N + 2):
        if (i * ((N + 1) - i) == odd):
            temp = 0
            OddPreSums = i
            break
    if (temp == -1):
        print(temp)
    else:
        EvenPreSums = (N + 1) - OddPreSums
        e = 1
        o = 0
        CurrSum = 0
        for i in range(N):
            if (CurrSum % 2 == 0):
                if (e < EvenPreSums):
                    e += 1
                    print("0 ", end="")
                else:
                    o += 1
                    print("1 ", end="")
                    CurrSum += 1
            else:
                if (e < EvenPreSums):
                    e += 1
                    print("1 ")
                    CurrSum += 1
                else:
                    o += 1
                    print("0 ", end="")
        print("",  end="")


if __name__ == '__main__':
    N = 15
    even = 60
    odd = 60
    CreateArray(N, even, odd)

# Expected output:
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
# minimum operations required to change the array such that | arr [ i ] | python3 implementation of the approach ; function to return the minimum number of operations required ; minimum and maximum elements from the array ; to store the minimum number of operations required ; to store the number of operations required to change every element to either ( num - 1 ) , num or ( num + 1 ) ; if current element is not already num ; add the count of operations required to change arr [ i ] ; update the minimum operations so far ; 

import math
import sys


def changeTheArray(arr, n):
    minEle = min(arr)
    maxEle = max(arr)
    minOperations = sys .maxsize
    for num in range(minEle, maxEle + 1):
        operations = 0
        for i in range(n):
            if arr[i] != num:
                operations += (abs(num - arr[i]) - 1)
        minOperations = min(minOperations, operations)
    return minOperations


if __name__ == '__main__':
    arr = [10, 1, 4]
    n = len(arr)
    print(changeTheArray(arr, n))

# Expected output:
7
# choose x such that ( a xor x ) + ( b xor x ) is minimized | function to return the integer x such that ( a xor x ) + ( b ^ x ) is minimized ; while either a or b is non - zero ; position at which both a and b have a set bit ; inserting a set bit in x ; right shifting both numbers to traverse all the bits ; 
def findX(A, B):
    j = 0
    x = 0
    while (A or B):
        if ((A & 1) and (B & 1)):
            x += (1 << j)
        A >>= 1
        B >>= 1
        j += 1
    return x


if __name__ == '__main__':
    A = 2
    B = 3
    X = findX(A, B)
    print("X =", X, ", Sum =", (A ^ X) + (B ^ X))

# Expected output:
X = 2 , Sum = 1
# compare sum of first n | function that returns true if sum of first n - 1 elements of the array is equal to the last element ; find the sum of first n - 1 elements of the array ; if sum equals to the last element ; 
def isSumEqual(ar, n):
    sum = 0
    for i in range(n - 1):
        sum += ar[i]
    if (sum == ar[n - 1]):
        return True
    return False


if __name__ == '__main__':
    arr = [1, 2, 3, 4, 10]
    n = len(arr)
    if (isSumEqual(arr, n)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# count number of 1 s in the array after n moves | python3 implementation of the above approach ; function to count number of perfect squares ; counting number of perfect squares between a and b ; function to count number of 1 s in array after n moves ; 

from math import sqrt, ceil, floor


def perfectSquares(a, b):
    return (floor(sqrt(b)) - ceil(sqrt(a)) + 1)


def countOnes(arr, n):
    return perfectSquares(1, n)


if __name__ == "__main__":
    N = 10
    arr = [0] * 10
    print(countOnes(arr, N))

# Expected output:
3
# find the position of box which occupies the given ball | python3 implementation of the approach ; function to print the position of each boxes where a ball has to be placed ; find the cumulative sum of array a [ ] ; find the position of box for each ball ; row number ; column ( position of box in particular row ) ; row + 1 denotes row if indexing of array start from 1 ; 

import bisect


def printPosition(A, B, sizeOfA, sizeOfB):
    for i in range(1, sizeOfA):
        A[i] += A[i - 1]
    for i in range(sizeOfB):
        row = bisect .bisect_left(A, B[i])
        if row >= 1:
            boxNumber = B[i] - A[row - 1]
        else:
            boxNumber = B[i]
        print(row + 1, ",", boxNumber)


A = [2, 2, 2, 2]
B = [1, 2, 3, 4]
sizeOfA = len(A)
sizeOfB = len(B)
printPosition(A, B, sizeOfA, sizeOfB)

# Expected output:
1 , 1
1 , 2
2 , 1
2 , 2