giulio98/xlcost-single-prompt huggingface dataset:
# find two numbers whose sum is n and does not contain any digit as k | function to find two numbers whose sum is n and do not contain any digit as k ; check every number i and ( n - i ) ; check if i and n - i doesn 't  contain k in them print i and n-i ; check if flag is 0 then print - 1 ; 
def findAandB(n, k):
    flag = 0
    for i in range(1, n):
        if str(i).count(chr(k + 48)) == 0 and str(n -
                                                  i).count(chr(k + 48)) == 0:
            print(i, n - i)
            flag = 1
            break
    if (flag == 0):
        print(-1)


if __name__ == '__main__':
    N = 100
    K = 0
    findAandB(N, K)

# Expected output:
1 99
# find the value of p and modular inverse of q modulo 998244353 | function to find the value of p * q ^ - 1 mod 998244353 ; loop to find the value until the expo is not zero ; multiply p with q if expo is odd ; reduce the value of expo by 2 ; 
def calculate(p, q):
    mod = 998244353
    expo = 0
    expo = mod - 2
    while (expo):
        if (expo & 1):
            p = (p * q) % mod
        q = (q * q) % mod
        expo >>= 1
    return p


if __name__ == '__main__':
    p = 1
    q = 4
    print(calculate(p, q))

# Expected output:
748683265
# find two numbers with given sum and maximum possible lcm | function that print two numbers with the sum x and maximum possible lcm ; if x is odd ; if x is even ; if floor ( x / 2 ) is even ; if floor ( x / 2 ) is odd ; print the result ; 
def maxLCMWithGivenSum(X):
    if X % 2 != 0:
        A = X / 2
        B = X / 2 + 1
    else:
        if (X / 2) % 2 == 0:
            A = X / 2 - 1
            B = X / 2 + 1
        else:
            A = X / 2 - 2
            B = X / 2 + 2
    print(int(A), int(B), end=" ")


if __name__ == '__main__':
    X = 30
    maxLCMWithGivenSum(X)

# Expected output:
13 17
# length of longest subarray whose sum is not divisible by integer k | function to find the longest subarray with sum is not divisible by k ; left is the index of the leftmost element that is not divisible by k ; sum of the array ; find the element that is not multiple of k ; left = - 1 means we are finding the leftmost element that is not divisible by k ; updating the rightmost element ; update the sum of the array up to the index i ; check if the sum of the array is not divisible by k , then return the size of array ; all elements of array are divisible by k , then no such subarray possible so return - 1 ; length of prefix elements that can be removed ; length of suffix elements that can be removed ; return the length of subarray after removing the elements which have lesser number of elements ; 
def MaxSubarrayLength(arr, n, k):
    left = -1
    sum = 0
    for i in range(n):
        if ((arr[i] % k) != 0):
            if (left == -1):
                left = i
            right = i
        sum += arr[i]
    if ((sum % k) != 0):
        return n
    elif (left == -1):
        return -1
    else:
        prefix_length = left + 1
        suffix_length = n - right
        return n - min(prefix_length, suffix_length)


if __name__ == "__main__":
    arr = [6, 3, 12, 15]
    n = len(arr)
    K = 3
    print(MaxSubarrayLength(arr, n, K))

# Expected output:
-1
# minimum steps to convert x to y by repeated division and multiplication | python3 implementation to find minimum steps to convert x to y by repeated division and multiplication ; check if x is greater than y then swap the elements ; check if x equals y ; 
def solve(X, Y):
    if (X > Y):
        temp = X
        X = Y
        Y = temp
    if (X == Y):
        print(0)
    elif (Y % X == 0):
        print(1)
    else:
        print(2)


X = 8
Y = 13
solve(X, Y)

# Expected output:
2
# count quadruplets ( a , b , c , d ) till n such that sum of square of a and b is equal to that of c and d | python3 program for the above approach ; function to count the quadruples ; counter variable ; map to store the sum of pair ( a ^ 2 + b ^ 2 ) ; iterate till n ; calculate a ^ 2 + b ^ 2 ; increment the value in map ; check if this sum was also in a ^ 2 + b ^ 2 ; return the count ; 

from collections import defaultdict


def countQuadraples(N):
    cnt = 0
    m = defaultdict(int)
    for a in range(1, N + 1):
        for b in range(1, N + 1):
            x = a * a + b * b
            m[x] += 1
    for c in range(1, N + 1):
        for d in range(1, N + 1):
            x = c * c + d * d
            if x in m:
                cnt += m[x]
    return cnt


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

# Expected output:
6
# count of distinct index pair ( i , j ) such that element sum of first array is greater | python3 program of the above approach ; function to find the number of pairs . ; array c [ ] where c [ i ] = a [ i ] - b [ i ] ; sort the array c ; initialise answer as 0 ; iterate from index 0 to n - 1 ; if c [ i ] <= 0 then in the sorted array c [ i ] + c [ pos ] can never greater than 0 where pos < i ; find the minimum index such that c [ i ] + c [ j ] > 0 which is equivalent to c [ j ] >= - c [ i ] + 1 ; add ( i - pos ) to answer ; return the answer ; 

from bisect import bisect_left


def numberOfPairs(a, b, n):
    c = [0 for i in range(n)]
    for i in range(n):
        c[i] = a[i] - b[i]
    c = sorted(c)
    answer = 0
    for i in range(1, n):
        if (c[i] <= 0):
            continue
        pos = bisect_left(c, -c[i] + 1)
        answer += (i - pos)
    return answer


if __name__ == '__main__':
    n = 5
    a = [1, 2, 3, 4, 5]
    b = [2, 5, 6, 1, 9]
    print(numberOfPairs(a, b, n))

# Expected output:
1
# find k for every array element such that at least k prefixes are â ‰¥ k | function to find the k - value for every index in the array ; multiset to store the array in the form of red - black tree ; iterating over the array ; inserting the current value in the multiset ; condition to check if the smallest value in the set is less than it 's size ; erase the smallest value ; h - index value will be the size of the multiset ; 
def print_h_index(arr, N):
    ms = []
    for i in range(N):
        ms .append(arr[i])
        ms .sort()
        if (ms[0] < len(ms)):
            ms .pop(0)
        print(len(ms), end=' ')


if __name__ == '__main__':
    arr = [9, 10, 7, 5, 0, 10, 2, 0]
    N = len(arr)
    print_h_index(arr, N)

# Expected output:
1 2 3 4 4 5 5 5
# non | function to find count of prime ; find maximum value in the array ; find and store all prime numbers up to max_val using sieve create a boolean array " prime [ 0 . . n ] " . a value in prime [ i ] will finally be false if i is not a prime , else true . ; remaining part of sieve ; if prime [ p ] is not changed , then it is a prime ; update all multiples of p ; function to print non - repeating primes ; precompute primes using sieve ; create hashmap to store frequency of prime numbers ; traverse through array elements and count frequencies of all primes ; traverse through map and print non repeating primes ; 
def findPrimes(arr, n):
    max_val = max(arr)
    prime = [True for i in range(max_val + 1)]
    prime[0] = False
    prime[1] = False
    p = 2
    while (p * p <= max_val):
        if (prime[p]):
            for i in range(p * 2, max_val + 1, p):
                prime[i] = False
        p += 1
    return prime


def nonRepeatingPrimes(arr, n):
    prime = findPrimes(arr, n)
    mp = dict()
    for i in range(n):
        if (prime[arr[i]]):
            if (arr[i] in mp):
                mp[arr[i]] += 1
            else:
                mp[arr[i]] = 1
    for entry in mp .keys():
        if (mp[entry] == 1):
            print(entry)


if __name__ == '__main__':
    arr = [2, 3, 4, 6, 7, 9, 7, 23, 21, 3]
    n = len(arr)
    nonRepeatingPrimes(arr, n)

# Expected output:
2
23
# prefix product array | function to generate prefix product array ; update the array with the product of prefixes ; print the array ; 
def prefixProduct(a, n):
    for i in range(1, n):
        a[i] = a[i] * a[i - 1]
    for j in range(0, n):
        print(a[j], end=", ")
    return 0


arr = [2, 4, 6, 5, 10]
N = len(arr)
prefixProduct(arr, N)

# Expected output:
2, 8, 48, 240, 2400,
# count of ways to distribute n items among 3 people with one person receiving maximum | function to find the number of ways to distribute n items among 3 people ; no distribution possible ; total number of ways to distribute n items among 3 people ; store the number of distributions which are not possible ; count possibilities of two persons receiving the maximum ; if n is divisible by 3 ; return the final count of ways to distribute ; 
def countWays(N):
    if (N < 4):
        return 0
    ans = ((N - 1) * (N - 2)) // 2
    s = 0
    for i in range(2, N - 2, 1):
        for j in range(1, i, 1):
            if (N == 2 * i + j):
                s += 1
    if (N % 3 == 0):
        s = 3 * s + 1
    else:
        s = 3 * s
    return ans - s


N = 10
print(countWays(N))

# Expected output:
33
# magnanimous numbers | function to check if n is prime ; corner cases ; this is checked so that we can skip middle five numbers in below loop ; function to check if the number is magnanimous or not ; converting the number to string ; finding length of string ; number should not be of single digit ; loop to find all left and right part of the string ; 
def isPrime(n):
    if (n <= 1):
        return False
    if (n <= 3):
        return True
    if (n % 2 == 0) or (n % 3 == 0):
        return False
    i = 5
    while (i * i <= n):
        if (n % i == 0 or n % (i + 2) == 0):
            return False
        i = i + 6
    return True


def isMagnanimous(N):
    s = str(N)
    l = len(s)
    if (l < 2):
        return False
    for i in range(l - 1):
        left = s[0:i + 1]
        right = s[i + 1:]
        x = int(left)
        y = int(right)
        if (not isPrime(x + y)):
            return False
    return True


N = 12
if isMagnanimous(N):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# sum of sum | function to find the sum ; calculate sum - series for every natural number and add them ; 
def sumOfSumSeries(N):
    _sum = 0
    for i in range(N + 1):
        _sum = _sum + (i * (i + 1)) // 2
    return _sum


N = 5
print(sumOfSumSeries(N))

# Expected output:
35
# sum of sum | function to find the sum ; 
def sumOfSumSeries(n):
    return (n * (n + 1) * (n + 2)) // 6


N = 5
print(sumOfSumSeries(N))

# Expected output:
35
# tetradic primes | function to check if the number n having all digits lies in the set ( 0 , 1 , 8 ) ; function to check if the number n is palindrome ; function to check if a number n is tetradic ; function to generate all primes and checking whether number is tetradic or not ; create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . a value in prime [ i ] will finally be false if i is not a prime , else true . ; if prime [ p ] is not changed , then it is a prime ; update all multiples of p ; print all tetradic prime numbers ; checking whether the given number is prime tetradic or not ; 
def isContaindigit(n):
    temp = str(n)
    for i in temp:
        if i not in ['0', '1', '8']:
            return False
    return True


def ispalindrome(n):
    temp = str(n)
    if temp == temp[::-1]:
        return True
    return False


def isTetradic(n):
    if ispalindrome(n):
        if isContaindigit(n):
            return True
    return False


def printTetradicPrimesLessThanN(n):
    prime = [True] * (n + 1)
    p = 2
    while (p * p <= n):
        if (prime[p]):
            for i in range(p * 2, n + 1, p):
                prime[i] = False
        p += 1
    for p in range(2, n + 1):
        if (prime[p] and isTetradic(p)):
            print(p, end=" ")


n = 1000
printTetradicPrimesLessThanN(n)

# Expected output:
11 101 181
# astonishing numbers | function to concatenate two integers into one ; convert both the integers to string ; concatenate both strings ; convert the concatenated string to integer ; return the formed integer ; function to check if n is a astonishing number ; loop to find sum of all integers from i till the sum becomes >= n ; variable to store sum of all integers from i to j and check if sum and concatenation equals n or not ; finding concatenation of i and j ; condition for astonishing number ; 
def concat(a, b):
    s1 = str(a)
    s2 = str(b)
    s = s1 + s2
    c = int(s)
    return c


def isAstonishing(n):
    for i in range(n):
        sum = 0
        for j in range(i, n):
            sum += j
            if (sum == n):
                concatenation = concat(i, j)
                if (concatenation == n):
                    return True
    return False


n = 429
if (isAstonishing(n)):
    print('Yes')
else:
    print('No')

# Expected output:
Yes
# digitally balanced numbers | function to check if the digits in the number is the same number of digits ; loop to iterate over the digits of the number n ; loop to iterate over the map ; 
def checkSame(n, b):
    m = {}
    while (n != 0):
        r = n % b
        n = n // b
        if r in m:
            m[r] += 1
        else:
            m[r] = 1
    last = -1
    for i in m:
        if last != -1 and m[i] != last:
            return False
        else:
            last = m[i]
    return True


n = 9
base = 2
if (checkSame(n, base)):
    print("Yes")
else:
    print("NO")

# Expected output:
Yes
# sum of series formed by difference between product and sum of n natural numbers | function to calculate the sum upto nth term ; stores the sum of the series ; stores the product of natural numbers upto the current term ; stores the sum of natural numbers upto the upto current term ; generate the remaining terms and calculate sum ; update the sum ; return the sum ; 
def seriesSum(n):
    sum1 = 0
    currProd = 1
    currSum = 1
    for i in range(2, n + 1):
        currProd *= i
        currSum += i
        sum1 += currProd - currSum
    return sum1


N = 5
print(seriesSum(N), end=" ")

# Expected output:
118
# count of elements not divisible by any other elements of array | function to count the number of elements of array which are not divisible by any other element in the array arr [ ] ; iterate over the array ; check if the element is itself or not ; check for divisibility ; return the final result ; 
def count(a, n):
    countElements = 0
    for i in range(n):
        flag = True
        for j in range(n):
            if (i == j):
                continue
            if (a[i] % a[j] == 0):
                flag = False
                break
        if (flag):
            countElements += 1
    return countElements


if __name__ == "__main__":
    arr = [86, 45, 18, 4, 8, 28, 19, 33, 2]
    n = len(arr)
    print(count(arr, n))

# Expected output:
4
# smallest n digit number divisible by n | python3 program for the above approach ; function to find the smallest n - digit number divisible by n ; return the smallest n - digit number calculated using above formula ; 

import math


def smallestNumber(N):
    return N * math .ceil(pow(10, (N - 1)) // N)


N = 2
print(smallestNumber(N))

# Expected output:
10
# count pairs in an array containing at least one even value | function to count the pairs in the array such as there is at least one even element in each pair ; generate all possible pairs and increment then count if the condition is satisfied ; 
def CountPairs(arr, n):
    count = 0
    for i in range(n):
        for j in range(i + 1, n):
            if (arr[i] % 2 == 0 or arr[j] % 2 == 0):
                count += 1
    return count


arr = [8, 2, 3, 1, 4, 2]
n = len(arr)
print(CountPairs(arr, n))

# Expected output:
14
# count pairs in an array containing at least one even value | function to count the pairs in the array such as there is at least one even element in each pair ; store count of even and odd elements ; check element is even or odd ; 
def CountPairs(arr, n):
    even = 0
    odd = 0
    for i in range(n):
        if (arr[i] % 2 == 0):
            even += 1
        else:
            odd += 1
    return ((even * (even - 1)) // 2 + (even * odd))


arr = [8, 2, 3, 1, 4, 2]
n = len(arr)
print(CountPairs(arr, n))

# Expected output:
14
# giuga numbers | python program for the above approach ; function to check if n is a composite number ; corner cases ; this is checked to skip middle 5 numbers ; function to check if n is a giuga number ; n should be composite to be a giuga number ; print the number of 2 s that divide n ; n must be odd at this point . so we can skip one element ; while i divides n , print i and divide n ; this condition is to handle the case when n is a prime number > 2 ; 

import math


def isComposite(n):
    if (n <= 1):
        return False
    if (n <= 3):
        return False
    if (n % 2 == 0 or n % 3 == 0):
        return True
    i = 5
    while (i * i <= n):
        if (n % i == 0 or n % (i + 2) == 0):
            return True
        i += 6
    return False


def isGiugaNum(n):
    if (not (isComposite(n))):
        return False
    N = n
    while (n % 2 == 0):
        if ((int(N / 2) - 1) % 2 != 0):
            return False
        n = int(n / 2)
    for i in range(3, int(math .sqrt(n)) + 1, 2):
        while (n % i == 0):
            if ((int(N / i) - 1) % i != 0):
                return False
            n = int(n / i)
    if (n > 2):
        if ((int(N / n) - 1) % n != 0):
            return False
    return True


n = 30
if (isGiugaNum(n)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# droll numbers | python3 program for the above approach ; function to check droll numbers ; to store sum of even prime factors ; to store sum of odd prime factors ; add the number of 2 s that divide n in sum_even ; n must be odd at this point . so we can skip one element ( note i = i + 2 ) ; while i divides n , print i and divide n ; this condition is to handle the case when n is a prime number greater than 2 ; condition to check droll number ; 

import math


def isDroll(n):
    if (n == 1):
        return False
    sum_even = 0
    sum_odd = 0
    while (n % 2 == 0):
        sum_even += 2
        n = n // 2
    for i in range(3, int(math .sqrt(n)) + 1, 2):
        while (n % i == 0):
            sum_odd += i
            n = n // i
    if (n > 2):
        sum_odd += n
    return sum_even == sum_odd


N = 72
if (isDroll(N)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# count all pairs of divisors of a number n whose sum is coprime with n | python3 program to count all pairs of divisors such that their sum is coprime with n ; function to count all valid pairs ; initialize count ; check if sum of pair and n are coprime ; return the result ; 

import math as m


def CountPairs(n):
    cnt = 0
    i = 1
    while i * i <= n:
        if (n % i == 0):
            div1 = i
            div2 = n // i
            sum = div1 + div2
            if (m .gcd(sum, n) == 1):
                cnt += 1
        i += 1
    return cnt


n = 24
print(CountPairs(n))

# Expected output:
2
# check if a can be converted to b by reducing with a prime number | function to find if it is possible to make a equal to b ; 
def isPossible(A, B):
    return (A - B > 1)


A = 10
B = 4
if (isPossible(A, B)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# maximize sum of minimum difference of divisors of nodes in n | python3 program to maximize the sum of minimum difference of divisors of nodes in an n - ary tree ; array to store the result at each node ; function to get minimum difference between the divisors of a number ; iterate from square root of n to n ; return absolute difference ; dfs function to calculate the maximum sum ; store the min difference ; add the maximum of all children to sub [ u ] ; return maximum sum of node ' u ' to its parent ; 

import math
sub = [0 for i in range(100005)]


def minDivisorDifference(n):
    num1 = 0
    num2 = 0
    for i in range(int(math .sqrt(n)), n + 1):
        if (n % i == 0):
            num1 = i
            num2 = n // i
            break
    return abs(num1 - num2)


def dfs(g, u, par):
    sub[u] = minDivisorDifference(u)
    mx = 0
    for c in g[u]:
        if (c != par):
            ans = dfs(g, c, u)
            mx = max(mx, ans)
    sub[u] += mx
    return sub[u]


if __name__ == '__main__':
    g = [[]for i in range(100005)]
    edges = 6
    g[18].append(7)
    g[7].append(18)
    g[18].append(15)
    g[15].append(18)
    g[15].append(2)
    g[2].append(15)
    g[7].append(4)
    g[4].append(7)
    g[7].append(12)
    g[12].append(7)
    g[12].append(9)
    g[9].append(12)
    root = 18
    print(dfs(g, root, -1))

# Expected output:
10
# program to check if n is a centered cubic number | function to check if n is a centered cubic number ; iterating from 1 ; infinite loop ; finding ith_term ; checking if the number n is a centered cube number ; if ith_term > n then n is not a centered cube number ; incrementing i ; 
def isCenteredcube(N):
    i = 1
    while (True):
        ith_term = ((2 * i + 1) * (i * i + i + 1))
        if (ith_term == N):
            return True
        if (ith_term > N):
            return False
        i += 1


N = 9
if (isCenteredcube(N)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# product of n terms of a given geometric series | function to calculate product of geometric series ; initialise final product with 1 ; multiply product with each term stored in a ; return the final product ; 
def productOfGP(a, r, n):
    product = 1
    for i in range(0, n):
        product = product * a
        a = a * r
    return product


a = 1
r = 2
N = 4
print(productOfGP(a, r, N))

# Expected output:
64
# sum of given n fractions in reduced form | function to find gcd of a & b using euclid lemma ; base case ; function to find the lcm of all elements in arr [ ] ; initialize result ; iterate arr [ ] to find lcm ; return the final lcm ; function to find the sum of n fraction in reduced form ; to store the sum of all final numerators ; find the lcm of all denominator ; find the sum of all n numerators & denominators ; add each fraction one by one ; find gcd of final numerator and denominator ; convert into reduced form by dividing from gcd ; print the final fraction ; given n ; given numerator ; 
def gcd(a, b):
    if (b == 0):
        return a
    return gcd(b, a % b)


def findlcm(arr, n):
    ans = arr[0]
    for i in range(1, n):
        ans = (((arr[i] * ans)) // (gcd(arr[i], ans)))
    return ans


def addReduce(n, num, den):
    final_numerator = 0
    final_denominator = findlcm(den, n)
    for i in range(n):
        final_numerator = (final_numerator +
                           (num[i]) * (final_denominator // den[i]))
    GCD = gcd(final_numerator, final_denominator)
    final_numerator //= GCD
    final_denominator //= GCD
    print(final_numerator, "/", final_denominator)


N = 3
arr1 = [1, 2, 5]
arr2 = [2, 1, 6]
addReduce(N, arr1, arr2)

# Expected output:
10 / 3
# minimum lcm of all pairs in a given array | python3 program to find minimum possible lcm from any pair ; function to compute gcd of two numbers ; function that return minimum possible lcm from any pair ; fix the ith element and iterate over all the array to find minimum lcm ; 

import sys


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


def minLCM(arr, n):
    ans = 1000000000
    for i in range(n):
        for j in range(i + 1, n):
            g = gcd(arr[i], arr[j])
            lcm = arr[i] / g * arr[j]
            ans = min(ans, lcm)
    return ans


arr = [2, 4, 3, 6, 5]
print(minLCM(arr, 5))

# Expected output:
4.0
# find two numbers whose difference of fourth power is equal to n | python3 implementation to find the values of x and y for the given equation with integer n ; function which find required x & y ; upper limit of x & y , if such x & y exists ; num1 stores x ^ 4 ; num2 stores y ^ 4 ; if condition is satisfied the print and return ; if no such pair exists ; 

from math import pow, ceil


def solve(n):
    upper_limit = ceil(pow(n, 1.0 / 4))
    for x in range(upper_limit + 1):
        for y in range(upper_limit + 1):
            num1 = x * x * x * x
            num2 = y * y * y * y
            if (num1 - num2 == n):
                print("x =", x, ", y =", y)
                return
    print(-1)


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

# Expected output:
x = 2 , y = 1