giulio98/xlcost-single-prompt huggingface dataset:
# find sum of f ( s ) for all the chosen sets from the given array | python3 implementation of the approach ; to store the factorial and the factorial mod inverse of a number ; function to find factorial of all the numbers ; function to find the factorial mod inverse of all the numbers ; function to return ncr ; function to find sum of f ( s ) for all the chosen sets from the given array ; sort the given array ; calculate the factorial and modinverse of all elements ; for all the possible sets calculate max ( s ) and min ( s ) ; 

N = 100005
mod = (10 ** 9 + 7)
factorial = [0] * N
modinverse = [0] * N


def factorialfun():
    factorial[0] = 1
    for i in range(1, N):
        factorial[i] = (factorial[i - 1] * i) % mod


def modinversefun():
    modinverse[N - 1] = pow(factorial[N - 1], mod - 2, mod) % mod
    for i in range(N - 2, -1, -1):
        modinverse[i] = (modinverse[i + 1] * (i + 1)) % mod


def binomial(n, r):
    if (r > n):
        return 0
    a = (factorial[n] * modinverse[n - r]) % mod
    a = (a * modinverse[r]) % mod
    return a


def max_min(a, n, k):
    a = sorted(a)
    factorialfun()
    modinversefun()
    ans = 0
    k -= 1
    for i in range(n):
        x = n - i - 1
        if (x >= k):
            ans -= (binomial(x, k) * a[i]) % mod
        y = i
        if (y >= k):
            ans += (binomial(y, k) * a[i]) % mod
        ans = (ans + mod) % mod
    return ans


a = [1, 1, 3, 4]
k = 2
n = len(a)
print(max_min(a, n, k))

# Expected output:
11
# length of smallest subarray in range 1 to n with sum greater than a given value | function to return the count of minimum elements such that the sum of those elements is > s . ; initialize currentsum = 0 ; loop from n to 1 to add the numbers and check the condition . ; 
def countNumber(N, S):
    countElements = 0
    currSum = 0
    while (currSum <= S):
        currSum += N
        N = N - 1
        countElements = countElements + 1
    return countElements


N = 5
S = 11
count = countNumber(N, S)
print(count)

# Expected output:
3
# next number with distinct digits | python3 program to find next consecutive number with all distinct digits ; function to count distinct digits in a number ; to count the occurrence of digits in number from 0 to 9 ; iterate over the digits of the number flag those digits as found in the array ; traverse the array arr and count the distinct digits in the array ; function to return the total number of digits in the number ; iterate over the digits of the number ; function to return the next number with distinct digits ; count the distinct digits in n + 1 ; count the total number of digits in n + 1 ; return the next consecutive number ; increment number by 1 ; 

import sys
INT_MAX = sys .maxsize


def countDistinct(n):
    arr = [0] * 10
    count = 0
    while (n != 0):
        r = int(n % 10)
        arr[r] = 1
        n //= 10
    for i in range(10):
        if (arr[i] != 0):
            count += 1
    return count


def countDigit(n):
    c = 0
    while (n != 0):
        r = n % 10
        c += 1
        n //= 10
    return c


def nextNumberDistinctDigit(n):
    while (n < INT_MAX):
        distinct_digits = countDistinct(n + 1)
        total_digits = countDigit(n + 1)
        if (distinct_digits == total_digits):
            return n + 1
        else:
            n += 1
    return -1


if __name__ == '__main__':
    n = 2019
    print(nextNumberDistinctDigit(n))

# Expected output:
2031
# minimum possible sum of array b such that aibi = ajbj for all 1 â ‰¤ i < j â ‰¤ n | python3 implementation of the approach ; to store least prime factors of all the numbers ; function to find the least prime factor of all the numbers ; function to return the sum of elements of array b ; find the prime factors of all the numbers ; to store each prime count in lcm ; current number ; map to store the prime count of a single number ; basic way to calculate all prime factors ; if it is the first number in the array ; take the maximum count of prime in a number ; calculate lcm of given array ; calculate sum of elements of array b ; 

mod = 10 ** 9 + 7
N = 1000005
lpf = [0 for i in range(N)]


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


def sum_of_elements(a, n):
    least_prime_factor()
    prime_factor = dict()
    for i in range(n):
        temp = a[i]
        single_number = dict()
        while (temp > 1):
            x = lpf[temp]
            single_number[x] = single_number .get(x, 0) + 1
            temp //= x
        if (i == 0):
            prime_factor = single_number
        else:
            for x in single_number:
                if x in prime_factor:
                    prime_factor[x] = max(prime_factor[x], single_number[x])
                else:
                    prime_factor[x] = single_number[x]
    ans, lcm = 0, 1
    for x in prime_factor:
        lcm = (lcm * pow(x, prime_factor[x], mod)) % mod
    for i in range(n):
        ans = (ans + (lcm * pow(a[i], mod - 2, mod)) % mod) % mod
    return ans


if __name__ == '__main__':
    a = [2, 3, 4]
    n = len(a)
    print(sum_of_elements(a, n))

# Expected output:
13
# find number of even cells in a zero matrix after q queries | function to find the number of even cell in a 2d matrix ; maintain two arrays , one for rows operation and one for column operation ; increment operation on row [ i ] ; increment operation on col [ i ] ; count odd and even values in both arrays and multiply them ; count of rows having even numbers ; count of rows having odd numbers ; count of columns having even numbers ; count of columns having odd numbers ; 
def findNumberOfEvenCells(n, q, size):
    row = [0] * n
    col = [0] * n
    for i in range(size):
        x = q[i][0]
        y = q[i][1]
        row[x - 1] += 1
        col[y - 1] += 1
    r1 = 0
    r2 = 0
    c1 = 0
    c2 = 0
    for i in range(n):
        if (row[i] % 2 == 0):
            r1 += 1
        if (row[i] % 2 == 1):
            r2 += 1
        if (col[i] % 2 == 0):
            c1 += 1
        if (col[i] % 2 == 1):
            c2 += 1
    count = r1 * c1 + r2 * c2
    return count


if __name__ == "__main__":
    n = 2
    q = [[1, 1], [1, 2], [2, 1]]
    size = len(q)
    print(findNumberOfEvenCells(n, q, size))

# Expected output:
2
# find maximum unreachable height using two ladders | function to return the maximum height which can 't be reached ; 
def maxHeight(h1, h2):
    return ((h1 * h2) - h1 - h2)


h1 = 7
h2 = 5
print(max(0, maxHeight(h1, h2)))

# Expected output:
23
# fermat 's factorization method | python 3 implementation of fermat 's factorization ; this function finds the value of a and b and returns a + b and a - b ; since fermat 's factorization applicable  for odd positive integers only ; check if n is a even number ; if n is a perfect root , then both its square roots are its factors ; 

from math import ceil, sqrt


def FermatFactors(n):
    if (n <= 0):
        return [n]
    if (n & 1) == 0:
        return [n / 2, 2]
    a = ceil(sqrt(n))
    if (a * a == n):
        return [a, a]
    while (True):
        b1 = a * a - n
        b = int(sqrt(b1))
        if (b * b == b1):
            break
        else:
            a += 1
    return [a - b, a + b]


print(FermatFactors(6557))

# Expected output:
[79, 83]
# append two elements to make the array satisfy the given condition | function to find the required numbers ; find the sum and xor ; print the required elements ; 
def findNums(arr, n):
    S = 0
    X = 0
    for i in range(n):
        S += arr[i]
        X ^= arr[i]
    print(X, X + S)


if __name__ == "__main__":
    arr = [1, 7]
    n = len(arr)
    findNums(arr, n)

# Expected output:
6 14
# satisfy the parabola when point ( a , b ) and the equation is given | function to find the required values ; 
def solve(A, B):
    p = B / 2
    M = int(4 * p)
    N = 1
    O = -2 * A
    Q = int(A * A + 4 * p * p)
    return [M, N, O, Q]


a = 1
b = 1
print(*solve(a, b))

# Expected output:
2 1 -2 2
# largest number dividing maximum number of elements in the array | python3 implementation of the approach ; function to return the largest number that divides the maximum elements from the given array ; finding gcd of all the numbers in the array ; 

from math import gcd as __gcd


def findLargest(arr, n):
    gcd = 0
    for i in range(n):
        gcd = __gcd(arr[i], gcd)
    return gcd


if __name__ == '__main__':
    arr = [3, 6, 9]
    n = len(arr)
    print(findLargest(arr, n))

# Expected output:
3
# check if the sum of digits of n is palindrome | function to return the sum of digits of n ; function that returns true if n is palindrome ; find the appropriate divisor to extract the leading digit ; if first and last digit not same return false ; removing the leading and trailing digit from number ; reducing divisor by a factor of 2 as 2 digits are dropped ; function that returns true if the digit sum of n is palindrome ; sum of the digits of n ; if the digit sum is palindrome ; 
def digitSum(n):
    sum = 0
    while (n > 0):
        sum += (n % 10)
        n //= 10
    return sum


def isPalindrome(n):
    divisor = 1
    while (n // divisor >= 10):
        divisor *= 10
    while (n != 0):
        leading = n // divisor
        trailing = n % 10
        if (leading != trailing):
            return False
        n = (n % divisor) // 10
        divisor = divisor // 100
    return True


def isDigitSumPalindrome(n):
    sum = digitSum(n)
    if (isPalindrome(sum)):
        return True
    return False


if __name__ == "__main__":
    n = 56
    if (isDigitSumPalindrome(n)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# find the value of n xor 'ed to itself k times | function to return n ^ n ^ ... k times ; if k is odd the answer is the number itself ; else the answer is 0 ; 
def xorK(n, k):
    if (k % 2 == 1):
        return n
    return 0


n = 123
k = 3
print(xorK(n, k))

# Expected output:
123
# find the sum of the costs of all possible arrangements of the cells | python3 implementation of the approach ; to store the factorials and factorial mod inverse of the numbers ; function to return ( a ^ m1 ) % mod ; function to find the factorials of all the numbers ; function to find factorial mod inverse of all the numbers ; function to return ncr ; function to return the sum of the costs of all the possible arrangements of the cells ; for all possible x 's ; for all possible y 's ; 

N = 100005
mod = (int)(1e9 + 7)
factorial = [0] * N
modinverse = [0] * N


def power(a, m1):
    if (m1 == 0):
        return 1
    elif (m1 == 1):
        return a
    elif (m1 == 2):
        return (a * a) % mod
    elif (m1 & 1):
        return (a * power(power(a, m1 // 2), 2)) % mod
    else:
        return power(power(a, m1 // 2), 2) % mod


def factorialfun():
    factorial[0] = 1
    for i in range(1, N):
        factorial[i] = (factorial[i - 1] * i) % mod


def modinversefun():
    modinverse[N - 1] = power(factorial[N - 1], mod - 2) % mod
    for i in range(N - 2, -1, -1):
        modinverse[i] = (modinverse[i + 1] * (i + 1)) % mod


def binomial(n, r):
    if (r > n):
        return 0
    a = (factorial[n] * modinverse[n - r]) % mod
    a = (a * modinverse[r]) % mod
    return a


def arrange(n, m, k):
    factorialfun()
    modinversefun()
    ans = 0
    for i in range(1, n):
        ans += (i * (n - i) * m * m) % mod
    for i in range(1, m):
        ans += (i * (m - i) * n * n) % mod
    ans = (ans * binomial(n * m - 2, k - 2)) % mod
    return int(ans)


if __name__ == "__main__":
    n = 2
    m = 2
    k = 2
    print(arrange(n, m, k))

# Expected output:
8
# find the nth digit in the proper fraction of two numbers | function to print the nth digit in the fraction ( p / q ) ; while n > 0 compute the nth digit by dividing p and q and store the result into variable res and go to next digit ; 
def findNthDigit(p, q, N):
    while (N > 0):
        N -= 1
        p *= 10
        res = p // q
        p %= q
    return res


if __name__ == "__main__":
    p = 1
    q = 2
    N = 1
    print(findNthDigit(p, q, N))

# Expected output:
5
# sum of the updated array after performing the given operation | utility function to return the sum of the array ; function to return the sum of the modified array ; subtract the subarray sum ; sum of subarray arr [ i ... n - 1 ] ; return the sum of the modified array ; 
def sumArr(arr, n):
    sum = 0
    for i in range(n):
        sum += arr[i]
    return sum


def sumModArr(arr, n):
    subSum = arr[n - 1]
    for i in range(n - 2, -1, -1):
        curr = arr[i]
        arr[i] -= subSum
        subSum += curr
    return sumArr(arr, n)


arr = [40, 25, 12, 10]
n = len(arr)
print(sumModArr(arr, n))

# Expected output:
8
# find closest integer with the same weight | python3 implementation of the approach ; function to return the number closest to x which has equal number of set bits as x ; loop for each bit in x and compare with the next bit ; 

NumUnsignBits = 64


def findNum(x):
    for i in range(NumUnsignBits - 1):
        if (((x >> i) & 1) != ((x >> (i + 1)) & 1)):
            x ^= (1 << i) | (1 << (i + 1))
            return x


if __name__ == "__main__":
    n = 92
    print(findNum(n))

# Expected output:
90
# cake distribution problem | function to return the remaining count of cakes ; sum for 1 cycle ; no . of full cycle and remainder ; 
def cntCakes(n, m):
    sum = (n * (n + 1)) // 2
    quo, rem = m // sum, m % sum
    ans = m - quo * sum
    x = int((-1 + (8 * rem + 1)**0.5) / 2)
    ans = ans - x * (x + 1) // 2
    return ans


def main():
    n = 4
    m = 11
    ans = cntCakes(n, m)
    print(ans)


main()

# Expected output:
0
# find the number of squares inside the given square grid | function to return the number of squares inside an n * n grid ; 
def cntSquares(n):
    return int(n * (n + 1) * (2 * n + 1) / 6)


if __name__ == "__main__":
    print(cntSquares(4))

# Expected output:
30
# find all palindrome numbers of given digits | function to return the reverse of num ; function that returns true if num is palindrome ; if the number is equal to the reverse of it then it is a palindrome ; function to prall the d - digit palindrome numbers ; smallest and the largest d - digit numbers ; starting from the smallest d - digit number till the largest ; if the current number is palindrome ; 
def reverse(num):
    rev = 0
    while (num > 0):
        rev = rev * 10 + num % 10
        num = num // 10
    return rev


def isPalindrome(num):
    if (num == reverse(num)):
        return True
    return False


def printPalindromes(d):
    if (d <= 0):
        return
    smallest = pow(10, d - 1)
    largest = pow(10, d) - 1
    for i in range(smallest, largest + 1):
        if (isPalindrome(i)):
            print(i, end=" ")


d = 2
printPalindromes(d)

# Expected output:
11 22 33 44 55 66 77 88 99
# minimum number of moves to reach n starting from ( 1 , 1 ) | python3 implementation of the approach ; function to return the minimum number of moves required to reach the cell containing n starting from ( 1 , 1 ) ; to store the required answer ; for all possible values of divisors ; if i is a divisor of n ; get the moves to reach n ; return the required answer ; 

import sys
from math import sqrt


def min_moves(n):
    ans = sys .maxsize
    for i in range(1, int(sqrt(n)) + 1):
        if (n % i == 0):
            ans = min(ans, i + n // i - 2)
    return ans


if __name__ == "__main__":
    n = 10
    print(min_moves(n))

# Expected output:
5
# minimum possible value of ( i * j ) % 2019 | python3 implementation of the approach ; function to return the minimum possible value of ( i * j ) % 2019 ; if we can get a number divisible by 2019 ; find the minimum value by running nested loops ; 

MOD = 2019


def min_modulo(l, r):
    if (r - l >= MOD):
        return 0
    else:
        ans = MOD - 1
        for i in range(l, r + 1):
            for j in range(i + 1, r + 1):
                ans = min(ans, (i * j) % MOD)
        return ans


if __name__ == "__main__":
    l = 2020
    r = 2040
    print(min_modulo(l, r))

# Expected output:
2
# represent ( 2 / n ) as the sum of three distinct positive integers of the form ( 1 / m ) | function to find the required fractions ; base condition ; for n > 1 ; 
def find_numbers(N):
    if (N == 1):
        print(-1, end="")
    else:
        print(N, N + 1, N * (N + 1))


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

# Expected output:
5 6 30
# count the pairs in an array such that the difference between them and their indices is equal | function to return the count of all valid pairs ; to store the frequencies of ( arr [ i ] - i ) ; to store the required count ; if cnt is the number of elements whose difference with their index is same then ( ( cnt * ( cnt - 1 ) ) / 2 ) such pairs are possible ; 
def countPairs(arr, n):
    map = dict()
    for i in range(n):
        map[arr[i] - i] = map .get(arr[i] - i, 0) + 1
    res = 0
    for x in map:
        cnt = map[x]
        res += ((cnt * (cnt - 1)) // 2)
    return res


arr = [1, 5, 6, 7, 9]
n = len(arr)
print(countPairs(arr, n))

# Expected output:
3
# minimum possible number with the given operation | function to return the minimum possible integer that can be obtained from the given integer after performing the given operations ; for every digit ; digits less than 5 need not to be changed as changing them will lead to a larger number ; the resulting integer cannot have leading zero ; 
def minInt(str1):
    for i in range(len(str1)):
        if (str1[i] >= 5):
            str1[i] = (9 - str1[i])
    if (str1[0] == 0):
        str1[0] = 9
    temp = ""
    for i in str1:
        temp += str(i)
    return temp


str1 = "589"
str1 = [int(i)for i in str1]
print(minInt(str1))

# Expected output:
410
# reduce n to 1 with minimum number of given operations | function to return the minimum number of given operations required to reduce n to 1 ; to store the count of operations ; to store the digit ; if n is already then no operation is required ; extract all the digits except the first digit ; store the maximum of that digits ; for each digit ; first digit ; add the value to count ; 
def minOperations(n):
    count = 0
    d = 0
    if (n == 1):
        return 0
    while (n > 9):
        d = max(n % 10, d)
        n //= 10
        count += 10
    d = max(d, n - 1)
    count += abs(d)
    return count - 1


if __name__ == '__main__':
    n = 240
    print(minOperations(n))

# Expected output:
23
# find the largest number that can be formed by changing at most k digits | function to return the maximum number that can be formed by changing at most k digits in str ; for every digit of the number ; if no more digits can be replaced ; if current digit is not already 9 ; replace it with 9 ; one digit has been used ; 
def findMaximumNum(st, n, k):
    for i in range(n):
        if (k < 1):
            break
        if (st[i] != '9'):
            st = st[0:i] + '9' + st[i + 1:]
            k -= 1
    return st


st = "569431"
n = len(st)
k = 3
print(findMaximumNum(st, n, k))

# Expected output:
999931
# check if two integer are anagrams of each other | function that returns true if a and b are anagarams of each other ; converting numbers to strings ; checking if the sorting values of two strings are equal ; 
def areAnagrams(a, b):
    a = str(a)
    b = str(b)
    if (sorted(a) == sorted(b)):
        return True
    else:
        return False


a = 240
b = 204
if (areAnagrams(a, b)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# number of occurrences of a given angle formed using 3 vertices of a n | function that calculates occurrences of given angle that can be created using any 3 sides ; maximum angle in a regular n - gon is equal to the interior angle if the given angle is greater than the interior angle then the given angle cannot be created ; the given angle times n should be divisible by 180 else it cannot be created ; initialise answer ; calculate the frequency of given angle for each vertex ; multiply answer by frequency . ; multiply answer by the number of vertices . ; 
def solve(ang, n):
    if ((ang * n) > (180 * (n - 2))):
        return 0
    elif ((ang * n) % 180 != 0):
        return 0
    ans = 1
    freq = (ang * n) // 180
    ans = ans * (n - 1 - freq)
    ans = ans * n
    return ans


ang = 90
n = 4
print(solve(ang, n))

# Expected output:
4
# find third number such that sum of all three number becomes prime | function that will check whether number is prime or not ; function to print the 3 rd number ; if the summ is odd ; if summ is not prime ; 
def prime(n):
    for i in range(2, n + 1):
        if i * i > n + 1:
            break
        if (n % i == 0):
            return False
    return True


def thirdNumber(a, b):
    summ = 0
    temp = 0
    summ = a + b
    temp = 1
    if (summ & 1):
        temp = 2
    while (prime(summ + temp) == False):
        temp += 2
    print(temp)


a = 3
b = 5
thirdNumber(a, b)

# Expected output:
3
# total ways of selecting a group of x men from n men with or without including a particular man | function to return the value of ncr ; initialize the answer ; divide simultaneously by i to avoid overflow ; function to return the count of ways ; 
def nCr(n, r):
    ans = 1
    for i in range(1, r + 1):
        ans *= (n - r + i)
        ans //= i
    return ans


def total_ways(N, X):
    return (nCr(N - 1, X - 1) + nCr(N - 1, X))


if __name__ == "__main__":
    N = 5
    X = 3
    print(total_ways(N, X))

# Expected output:
10
# compute the maximum power with a given condition | function to return the largest power ; if n is greater than given m ; if n == m ; checking for the next power ; 
def calculate(n, k, m, power):
    if n > m:
        if power == 0:
            return 0
        else:
            return power - 1
    elif n == m:
        return power
    else:
        return calculate(n * k, k, m, power + 1)


if __name__ == "__main__":
    N = 1
    K = 2
    M = 5
    print(calculate(N, K, M, 0))

# Expected output:
2
# program to find the number from given holes | function that will find out the number ; if number of holes equal 0 then return 1 ; if number of holes equal 0 then return 0 ; if number of holes is more than 0 or 1. ; if number of holes is odd ; 
def printNumber(holes):
    if (holes == 0):
        print("1")
    elif (holes == 1):
        print("0", end="")
    else:
        rem = 0
        quo = 0
        rem = holes % 2
        quo = holes // 2
        if (rem == 1):
            print("4", end="")
        for i in range(quo):
            print("8", end="")


holes = 3
printNumber(holes)

# Expected output:
48
# minimum cost to make all array elements equal | function to return the minimum cost to make each array element equal ; to store the count of even numbers present in the array ; to store the count of odd numbers present in the array ; iterate through the array and find the count of even numbers and odd numbers ; 
def minCost(arr, n):
    count_even = 0
    count_odd = 0
    for i in range(n):
        if (arr[i] % 2 == 0):
            count_even += 1
        else:
            count_odd += 1
    return min(count_even, count_odd)


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

# Expected output:
2