giulio98/xlcost-single-prompt huggingface dataset:
# minimum value to be assigned to the elements so that sum becomes greater than initial sum | function to return the minimum required value ; find the sum of the array elements ; return the required value ; 
def findMinValue(arr, n):
    sum = 0
    for i in range(n):
        sum += arr[i]
    return (sum // n) + 1


arr = [4, 2, 1, 10, 6]
n = len(arr)
print(findMinValue(arr, n))

# Expected output:
5
# color all boxes in line such that every m consecutive boxes are unique | python3 implementation of the approach ; function to return ( m ! % mod ) ; 

MOD = 1000000007


def modFact(n, m):
    result = 1
    for i in range(1, m + 1):
        result = (result * i) % MOD
    return result


n = 3
m = 2
print(modFact(n, m))

# Expected output:
2
# sum of squares of all subsets of given array | python3 implementation of the approach ; function to return ( 2 ^ p % mod ) ; function to return the sum of squares of subsets ; squaring the elements and adding it to ans ; 

mod = 10 ** 9 + 7


def power(p):
    res = 1
    for i in range(1, p + 1):
        res *= 2
        res %= mod
    return res % mod


def subset_square_sum(A):
    n = len(A)
    ans = 0
    for i in A:
        ans += i * i % mod
        ans %= mod
    return ans * power(n - 1) % mod


A = [3, 7]
print(subset_square_sum(A))

# Expected output:
116
# find the number of pairs such that their gcd is equals to 1 | python3 program to find the number of pairs such that gcd equals to 1 ; function to calculate least prime factor of each number ; if it is a prime number ; for all multiples which are not visited yet . ; function to find the value of mobius function for all the numbers from 1 to n ; if number is one ; if number has a squared prime factor ; multiply - 1 with the previous number ; function to find the number of pairs such that gcd equals to 1 ; to store maximum number ; to store frequency of each number ; find frequency and maximum number ; to store number of pairs with gcd equals to 1 ; traverse through the all possible elements ; return the number of pairs ; 

N = 100050
lpf = [0 for i in range(N)]
mobius = [0 for i in range(N)]


def least_prime_factor():
    for i in range(2, N):
        if (lpf[i] == 0):
            for j in range(i, N, i):
                if (lpf[j] == 0):
                    lpf[j] = i


def Mobius():
    for i in range(1, N):
        if (i == 1):
            mobius[i] = 1
        else:
            if (lpf[(i // lpf[i])] == lpf[i]):
                mobius[i] = 0
            else:
                mobius[i] = -1 * mobius[i // lpf[i]]


def gcd_pairs(a, n):
    maxi = 0
    fre = [0 for i in range(N)]
    for i in range(n):
        fre[a[i]] += 1
        maxi = max(a[i], maxi)
    least_prime_factor()
    Mobius()
    ans = 0
    for i in range(1, maxi + 1):
        if (mobius[i] == 0):
            continue
        temp = 0
        for j in range(i, maxi + 1, i):
            temp += fre[j]
        ans += temp * (temp - 1) // 2 * mobius[i]
    return ans


a = [1, 2, 3, 4, 5, 6]
n = len(a)
print(gcd_pairs(a, n))

# Expected output:
11
# comparing x ^ y and y ^ x for very large values of x and y | python3 implementation of the approach ; function to compare x ^ y and y ^ x ; storing values of x ^ y and y ^ x ; comparing values ; 

from math import log


def compareVal(x, y):
    a = y * log(x)
    b = x * log(y)
    if (a > b):
        print(x, "^", y, ">", y, "^", x)
    elif (a < b):
        print(x, "^", y, "<", y, "^", x)
    elif (a == b):
        print(x, "^", y, "=", y, "^", x)


if __name__ == "__main__":
    x = 4
    y = 5
    compareVal(x, y)

# Expected output:
4 ^ 5 > 5 ^ 4
# euler zigzag numbers ( alternating permutation ) | function to prfirst n zigzag numbers ; to store factorial and n 'th zig zag number ; initialize factorial upto n ; set first two zig zag numbers ; print first two zig zag number ; print the rest zig zag numbers ; binomial ( n , k ) * a ( k ) * a ( n - k ) ; store the value ; print the number ; 
def ZigZag(n):
    fact = [0 for i in range(n + 1)]
    zig = [0 for i in range(n + 1)]
    fact[0] = 1
    for i in range(1, n + 1):
        fact[i] = fact[i - 1] * i
    zig[0] = 1
    zig[1] = 1
    print("zig zag numbers: ", end=" ")
    print(zig[0], zig[1], end=" ")
    for i in range(2, n):
        sum = 0
        for k in range(0, i):
            sum += ((fact[i - 1] // (fact[i - 1 - k] * fact[k]))
                    * zig[k] * zig[i - 1 - k])
        zig[i] = sum // 2
        print(sum // 2, end=" ")


n = 10
ZigZag(n)

# Expected output:
zig zag numbers:  1 1 1 2 5 16 61 272 1385 7936
# gijswijt 's sequence | if the sequence is a ( 1 ) a ( 2 ) a ( 3 ) . . a ( n - 1 ) check if the sequence can be represented as x * ( y ^ k ) find the largest value of k ; count ; pattern of elements of size i from the end of sequence ; count ; extract the pattern in a reverse order ; check how many times the pattern is repeated ; if the element dosent match ; if the end of pattern is reached set value of k = 0 and increase the count ; return the max count ; print first n terms of gijswijt 's sequence ; set the count ; stoes the element ; print the first n terms of the sequence ; push the element ; find the count for next number ; 
def find_count(ele):
    count = 0
    for i in range(len(ele)):
        p = []
        c = 0
        j = len(ele) - 1
        while j >= (len(ele) - 1 - i) and j >= 0:
            p .append(ele[j])
            j -= 1
        j = len(ele) - 1
        k = 0
        while j >= 0:
            if ele[j] != p[k]:
                break
            j -= 1
            k += 1
            if k == len(p):
                c += 1
                k = 0
        count = max(count, c)
    return count


def solve(n):
    count = 1
    ele = []
    for i in range(n):
        print(count, end=" ")
        ele .append(count)
        count = find_count(ele)


if __name__ == "__main__":
    n = 10
    solve(n)

# Expected output:
1 1 2 1 1 2 2 2 3 1
# wedderburn – etherington number | stores the wedderburn etherington numbers ; function to return the nth wedderburn etherington numbers ; base case ; if n is even n = 2 x ; get x ; a ( 2 x ) = a ( 1 ) a ( 2 x - 1 ) + a ( 2 ) a ( 2 x - 2 ) + ... + a ( x - 1 ) a ( x + 1 ) ; a ( x ) ( a ( x ) + 1 ) / 2 ; store the ans ; return the required answer ; if n is odd ; a ( 2 x - 1 ) = a ( 1 ) a ( 2 x - 2 ) + a ( 2 ) a ( 2 x - 3 ) + ... + a ( x - 1 ) a ( x ) , ; store the ans ; return the required answer ; function to prfirst n wedderburn etherington numbers ; store first 3 numbers ; prn terms ; 

store = dict()


def Wedderburn(n):
    if (n <= 2):
        return store[n]
    elif (n % 2 == 0):
        x = n // 2
        ans = 0
        for i in range(1, x):
            ans += store[i] * store[n - i]
        ans += (store[x] * (store[x] + 1)) // 2
        store[n] = ans
        return ans
    else:
        x = (n + 1) // 2
        ans = 0
        for i in range(1, x):
            ans += store[i] * store[n - i]
        store[n] = ans
        return ans


def Wedderburn_Etherington(n):
    store[0] = 0
    store[1] = 1
    store[2] = 1
    for i in range(n):
        print(Wedderburn(i), end="")
        if (i != n - 1):
            print(end=", ")


n = 10
Wedderburn_Etherington(n)

# Expected output:
0, 1, 1, 1, 2, 3, 6, 11, 23, 46
# maximum value after merging all elements in the array | function to maximum value after merging all elements in the array ; to check if positive and negative elements present or not ; check for positive integer ; check for negative integer ; if both positive and negative elements are present ; to store maximum value possible ; to find minimum value ; remove minimum element ; replace with absolute values ; to find minimum value ; remove minimum element ; return the required sum ; 
def Max_sum(a, n):
    pos = 0
    neg = 0
    for i in range(n):
        if (a[i] > 0):
            pos = 1
        elif (a[i] < 0):
            neg = 1
        if (pos == 1 and neg == 1):
            break
    sum = 0
    if (pos == 1 and neg == 1):
        for i in range(n):
            sum += abs(a[i])
    elif (pos == 1):
        mini = a[0]
        sum = a[0]
        for i in range(1, n, 1):
            mini = min(mini, a[i])
            sum += a[i]
        sum -= 2 * mini
    elif (neg == 1):
        for i in range(n):
            a[i] = abs(a[i])
        mini = a[0]
        sum = a[0]
        for i in range(1, n):
            mini = min(mini, a[i])
            sum += a[i]
        sum -= 2 * mini
    return sum


if __name__ == '__main__':
    a = [1, 3, 5, -2, -6]
    n = len(a)
    print(Max_sum(a, n))

# Expected output:
17
# decimal to binary using recursion and without using power operator | recursive function to convert n to its binary equivalent ; base case ; recursive call ; 
def decimalToBinary(n):
    if (n == 0):
        print("0", end="")
        return
    decimalToBinary(n // 2)
    print(n % 2, end="")


if __name__ == "__main__":
    n = 13
    decimalToBinary(n)

# Expected output:
01101
# find minimum possible values of a , b and c when two of the ( a + b ) , ( a + c ) and ( b + c ) are given | function to find a , b and c ; keep minimum number in x ; find the numbers ; 
def MinimumValue(x, y):
    if (x > y):
        x, y = y, x
    a = 1
    b = x - 1
    c = y - b
    print(a, b, c)


x = 123
y = 13
MinimumValue(x, y)

# Expected output:
1 12 111
# check whether it is possible to convert a into b | function that returns true if a can be converted to b with the given operations ; if the current number ends with 1 ; if the current number is divisible by 2 ; if the above two conditions fail ; if it is possible to convert a to b ; 
def canConvert(a, b):
    while (b > a):
        if (b % 10 == 1):
            b //= 10
            continue
        if (b % 2 == 0):
            b /= 2
            continue
        return false
    if (b == a):
        return True
    return False


if __name__ == "__main__":
    A = 2
    B = 82
    if (canConvert(A, B)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# count distinct rectangles in n * n chessboard | function to return the count of distinct rectangles ; 
def count(N):
    a = 0
    a = (N * (N + 1)) / 2
    return int(a)


N = 4
print(count(N))

# Expected output:
10
# total number of days taken to complete the task if after certain days one person leaves | function to return the number of days required ; 
def numberOfDays(a, b, n):
    Days = b * (n + a) // (a + b)
    return Days


if __name__ == "__main__":
    a = 10
    b = 20
    n = 5
    print(numberOfDays(a, b, n))

# Expected output:
10
# find average of two numbers using bit operation | function to return the average of x and y using bit operations ; calculate the average floor value of ( x + y ) / 2 ; 
def getAverage(x, y):
    avg = (x & y) + ((x ^ y) >> 1)
    return avg


x = 10
y = 9
print(getAverage(x, y))

# Expected output:
9
# smallest index such that there are no 0 or 1 to its right | function to find the smallest index such that there are no 0 or 1 to its right ; initially ; traverse in the array ; check if array element is 1 ; a [ i ] = 0 ; return minimum of both ; 
def smallestIndex(a, n):
    right1 = 0
    right0 = 0
    for i in range(n):
        if (a[i] == 1):
            right1 = i
        else:
            right0 = i
    return min(right1, right0)


if __name__ == '__main__':
    a = [1, 1, 1, 0, 0, 1, 0, 1, 1]
    n = len(a)
    print(smallestIndex(a, n))

# Expected output:
6
# total position where king can reach on a chessboard in exactly m moves | set 2 | function to return the count of squares that can be visited by king in m moves ; to store the count of squares ; check all squares of the chessboard ; check if square ( i , j ) is at a distance <= m units from king 's current position ; return count of squares ; 
def countSquares(r, c, m):
    squares = 0
    for i in range(1, 9):
        for j in range(1, 9):
            if (max(abs(i - r), abs(j - c)) <= m):
                squares = squares + 1
    return squares


r = 4
c = 4
m = 1
print(countSquares(r, c, m))

# Expected output:
9
# number of quadruples where the first three terms are in ap and last three terms are in gp | function to return the count of quadruples ; hash table to count the number of occurrences ; traverse and increment the count ; run two nested loop for second and third element ; if they are same ; initially decrease the count ; find the first element using common difference ; find the fourth element using gp y ^ 2 = x * z property ; if it is an integer ; if not equal ; same elements ; later increase the value for future calculations ; 
def countQuadruples(a, n):
    mpp = dict .fromkeys(a, 0)
    for i in range(n):
        mpp[a[i]] += 1
    count = 0
    for j in range(n):
        for k in range(n):
            if (j == k):
                continue
            mpp[a[j]] -= 1
            mpp[a[k]] -= 1
            first = a[j] - (a[k] - a[j])
            if first not in mpp:
                mpp[first] = 0
            fourth = (a[k] * a[k]) // a[j]
            if fourth not in mpp:
                mpp[fourth] = 0
            if ((a[k] * a[k]) % a[j] == 0):
                if (a[j] != a[k]):
                    count += mpp[first] * mpp[fourth]
                else:
                    count += (mpp[first] * (mpp[fourth] - 1))
            mpp[a[j]] += 1
            mpp[a[k]] += 1
    return count


if __name__ == "__main__":
    a = [2, 6, 4, 9, 2]
    n = len(a)
    print(countQuadruples(a, n))

# Expected output:
2
# numbers in a range with given digital root | function to return the count of required numbers ; count of numbers present in given range ; number of groups of 9 elements starting from l ; left over elements not covered in factor 9 ; one number in each group of 9 ; to check if any number in rem satisfy the property ; 
def countNumbers(L, R, K):
    if (K == 9):
        K = 0
    totalnumbers = R - L + 1
    factor9 = totalnumbers // 9
    rem = totalnumbers % 9
    ans = factor9
    for i in range(R, R - rem, -1):
        rem1 = i % 9
        if (rem1 == K):
            ans += 1
    return ans


L = 10
R = 22
K = 3
print(countNumbers(L, R, K))

# Expected output:
2
# sum of even values and update queries on an array | function to return the sum of even elements after updating value at given index ; add given value to a [ index ] ; to store the sum of even elements ; if current element is even ; function to print result for every query ; resultant vector that stores the result for every query ; get sum of even elements after updating value at given index ; store sum for each query ; print the result for every query ; 
def EvenSum(A, index, value):
    A[index] = A[index] + value
    sum = 0
    for i in A:
        if (i % 2 == 0):
            sum = sum + i
    return sum


def BalanceArray(A, Q):
    ANS = []
    i, sum = 0, 0
    for i in range(len(Q)):
        index = Q[i][0]
        value = Q[i][1]
        sum = EvenSum(A, index, value)
        ANS .append(sum)
    for i in ANS:
        print(i, end=" ")


A = [1, 2, 3, 4]
Q = [[0, 1], [1, -3], [0, -4], [3, 2]]
BalanceArray(A, Q)

# Expected output:
8 6 2 4
# sum of even values and update queries on an array | function to print the result for every query ; if current element is even ; if element is even then remove it from sum ; if the value becomes even after updating ; store sum for each query ; print the result for every query ; 
def BalanceArray(A, Q):
    ANS = []
    sum = 0
    for i in range(len(A)):
        if (A[i] % 2 == 0):
            sum += A[i]
    for i in range(len(Q)):
        index = Q[i][0]
        value = Q[i][1]
        if (A[index] % 2 == 0):
            sum -= A[index]
        A[index] += value
        if (A[index] % 2 == 0):
            sum += A[index]
        ANS .append(sum)
    for i in range(len(ANS)):
        print(ANS[i], end=" ")


if __name__ == "__main__":
    A = [1, 2, 3, 4]
    Q = [[0, 1], [1, -3], [0, -4], [3, 2]]
    BalanceArray(A, Q)

# Expected output:
8 6 2 4
# number of hamiltonian cycle | python3 program for implementation of the above program ; function that calculates number of hamiltonian cycle ; calculating factorial ; 

import math as mt


def Cycles(N):
    fact = 1
    result = N - 1
    i = result
    while (i > 0):
        fact = fact * i
        i -= 1
    return fact // 2


N = 5
Number = Cycles(N)
print("Hamiltonian cycles = ", Number)

# Expected output:
Hamiltonian cycles =  12
# smallest integer greater than n such that it consists of digit m exactly k times | function that returns true if n contains digit m exactly k times ; function to return the smallest integer > n with digit m occurring exactly k times ; 
def digitWell(n, m, k):
    cnt = 0
    while (n > 0):
        if (n % 10 == m):
            cnt = cnt + 1
        n = (int)(n / 10)
    return cnt == k


def findInt(n, m, k):
    i = n + 1
    while (True):
        if (digitWell(i, m, k)):
            return i
        i = i + 1


n = 111
m = 2
k = 2
print(findInt(n, m, k))

# Expected output:
122
# composite xor and coprime and | function to return the count of odd numbers in the array ; variable to count odd numbers ; odd number ; function to return the count of valid pairs ; 
def countOdd(arr, n):
    odd = 0
    for i in range(0, n):
        if (arr[i] % 2 == 1):
            odd = odd + 1
    return odd


def countValidPairs(arr, n):
    odd = countOdd(arr, n)
    return (odd * (odd - 1)) / 2


arr = [1, 2, 3, 4, 5]
n = len(arr)
print(int(countValidPairs(arr, n)))

# Expected output:
3
# smallest perfect cube divisible by all elements of an array | function to return the gcd of two numbers ; function to return the lcm of all the elements of the array ; to calculate lcm of two numbers multiply them and divide the result by gcd of both the numbers ; return the lcm of the array elements ; function to return the smallest perfect cube divisible by all the elements of arr [ ] ; lcm of all the elements of arr [ ] ; if 2 divides lcm cnt number of times ; check all the numbers that divide lcm ; return the answer ; 
def gcd(a, b):
    if (b == 0):
        return a
    else:
        return gcd(b, a % b)


def lcmOfArray(arr, n):
    if (n < 1):
        return 0
    lcm = arr[0]
    for i in range(n):
        lcm = (lcm * arr[i]) // gcd(lcm, arr[i])
    return lcm


def minPerfectCube(arr, n):
    lcm = lcmOfArray(arr, n)
    minPerfectCube = lcm
    cnt = 0
    while (lcm > 1 and lcm % 2 == 0):
        cnt += 1
        lcm //= 2
    if (cnt % 3 == 2):
        minPerfectCube *= 2
    elif (cnt % 3 == 1):
        minPerfectCube *= 4
    i = 3
    while (lcm > 1):
        cnt = 0
        while (lcm % i == 0):
            cnt += 1
            lcm //= i
        if (cnt % 3 == 1):
            minPerfectCube *= i * i
        elif (cnt % 3 == 2):
            minPerfectCube *= i
        i += 2
    return minPerfectCube


if __name__ == "__main__":
    arr = [10, 125, 14, 42, 100]
    n = len(arr)
    print(minPerfectCube(arr, n))

# Expected output:
9261000
# check if n is strong prime | python 3 program to check if given number is strong prime ; utility function to check if a number is prime or not ; corner cases ; this is checked so that we can skip middle five numbers in below loop ; function that returns true if n is a strong prime ; if n is not a prime number or n is the first prime then return false ; initialize previous_prime to n - 1 and next_prime to n + 1 ; find next prime number ; find previous prime number ; arithmetic mean ; if n is a strong prime ; 

from math import sqrt


def isPrime(n):
    if (n <= 1):
        return False
    if (n <= 3):
        return True
    if (n % 2 == 0 or n % 3 == 0):
        return False
    k = int(sqrt(n)) + 1
    for i in range(5, k, 6):
        if (n % i == 0 or n % (i + 2) == 0):
            return False
    return True


def isStrongPrime(n):
    if (isPrime(n) == False or n == 2):
        return False
    previous_prime = n - 1
    next_prime = n + 1
    while (isPrime(next_prime) == False):
        next_prime += 1
    while (isPrime(previous_prime) == False):
        previous_prime -= 1
    mean = (previous_prime + next_prime) / 2
    if (n > mean):
        return True
    else:
        return False


if __name__ == '__main__':
    n = 11
    if (isStrongPrime(n)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# minimum removals in a number to be divisible by 10 power raised to k | function to return the required number of digits to be removed ; converting the given number into string ; variable to store number of digits to be removed ; variable to denote if atleast one zero has been found ; zero found ; return size - 1 if k is not zero and atleast one zero is present , otherwise result ; 
def countDigitsToBeRemoved(N, K):
    s = str(N)
    res = 0
    f_zero = 0
    for i in range(len(s) - 1, -1, -1):
        if (K == 0):
            return res
        if (s[i] == '0'):
            f_zero = 1
            K -= 1
        else:
            res += 1
    if (K == 0):
        return res
    elif (f_zero > 0):
        return len(s) - 1
    return -1


N = 10904025
K = 2
print(countDigitsToBeRemoved(N, K))
N = 1000
K = 5
print(countDigitsToBeRemoved(N, K))
N = 23985
K = 2
print(countDigitsToBeRemoved(N, K))

# Expected output:
3
3
-1
# program to find the sum of the series ( 1 / a + 2 / a ^ 2 + 3 / a ^ 3 + ... + n / a ^ n ) | python 3 program to find the sum of the given series ; function to return the sum of the series ; variable to store the answer ; math . pow ( x , y ) returns x ^ y ; 

import math


def getSum(a, n):
    sum = 0
    for i in range(1, n + 1):
        sum += (i / math .pow(a, i))
    return sum


a = 3
n = 3
print(getSum(a, n))

# Expected output:
0.6666666666666667
# check if a number is an unusual number or not | python program to check unusual number ; utility function to find largest prime factor of a number ; initialize the maximum prime factor variable with the lowest one ; print the number of 2 s that divide n ; n must be odd at this point , thus skip the even numbers and iterate only for odd integers ; this condition is to handle the case when n is a prime number greater than 2 ; function to check unusual number ; get the largest prime factor of the number ; check if largest prime factor is greater than sqrt ( n ) ; 

from math import sqrt


def largestPrimeFactor(n):
    max = -1
    while n % 2 == 0:
        max = 2
        n >>= 1
    for i in range(3, int(sqrt(n))+1, 2):
        while n % i == 0:
            max = i
            n = n / i
    if n > 2:
        max = n

    return max


def checkUnusual(n):
    factor = largestPrimeFactor(n)
    if factor > sqrt(n):
        return True
    else:
        return False


if __name__ == '__main__':

    n = 14

    if checkUnusual(n):
        print("YES")
    else:
        print("NO")

# Expected output:
YES
# check if at least half array is reducible to zero by performing some operations | function to print the desired result after computation ; 
def isHalfReducible(arr, n, m):
    frequencyHash = [0] * (m + 1)
    i = 0
    while (i < n):
        frequencyHash[(arr[i] % (m + 1))] += 1
        i += 1
    i = 0
    while (i <= m):
        if (frequencyHash[i] >= (n / 2)):
            break
        i += 1
    if (i <= m):
        print("Yes")
    else:
        print("No")


arr = [8, 16, 32, 3, 12]
n = len(arr)
m = 7
isHalfReducible(arr, n, m)

# Expected output:
Yes