giulio98/xlcost-single-prompt huggingface dataset:
# number of array elements derivable from d after performing certain operations | function to return gcd of a and b ; function to return the number of elements of arr [ ] which can be derived from d by performing ( + a , - a , + b , - b ) ; find the gcd of a and b ; counter stores the number of array elements which can be derived from d ; arr [ i ] can be derived from d only if | arr [ i ] - d | is divisible by gcd of a and b ; 
def gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)


def findPossibleDerivables(arr, n, D, A, B):
    gcdAB = gcd(A, B)
    counter = 0
    for i in range(n):
        if ((abs(arr[i] - D) % gcdAB) == 0):
            counter += 1
    return counter


if __name__ == "__main__":
    arr = [1, 2, 3, 4, 7, 13]
    n = len(arr)
    D, A, B = 5, 4, 2
    print(findPossibleDerivables(arr, n, D, A, B))
    a = [1, 2, 3]
    n = len(a)
    D, A, B = 6, 3, 2
    print(findPossibleDerivables(a, n, D, A, B))

# Expected output:
4
3
# find the sum of first n terms of the series 2 * 3 * 5 , 3 * 5 * 7 , 4 * 7 * 9 , ... | function to return the sum of the first n terms of the given series ; as described in the approach ; 
def calSum(n):
    return (n * (2 * n * n * n + 12 * n * n + 25 * n + 21)) / 2


n = 3
print(calSum(n))

# Expected output:
387.0
# find elements of array using xor of consecutive elements | function to find the array elements using xor of consecutive elements ; array to store the original elements ; first elements a i . e elements [ 0 ] = a ; to get the next elements we have to calculate xor of previous elements with given xor of 2 consecutive elements . e . g . if a ^ b = k1 so to get b xor a both side . b = k1 ^ a ; printing the original array elements ; 
def getElements(a, arr, n):
    elements = [1 for i in range(n + 1)]
    elements[0] = a
    for i in range(n):
        elements[i + 1] = arr[i] ^ elements[i]
    for i in range(n + 1):
        print(elements[i], end=" ")


arr = [13, 2, 6, 1]
n = len(arr)
a = 5
getElements(a, arr, n)

# Expected output:
5 8 10 12 13
# maximum gcd of n integers with given product | python3 implementation of above approach ; function to find maximum gcd of n integers with product p ; map to store prime factors of p ; prime factorization of p ; traverse all prime factors and multiply its 1 / n power to the result ; 

from math import sqrt


def maxGCD(N, P):
    ans = 1
    prime_factors = {}
    for i in range(2, int(sqrt(P) + 1)):
        while (P % i == 0):
            if i not in prime_factors:
                prime_factors[i] = 0
            prime_factors[i] += 1
            P //= i
    if (P != 1):
        prime_factors[P] += 1
    for key, value in prime_factors .items():
        ans *= pow(key, value // N)
    return ans


if __name__ == "__main__":
    N, P = 3, 24
    print(maxGCD(N, P))

# Expected output:
2
# check if the sum of distinct digits of two integers are equal | function to return the sum of distinct digits of a number ; take last digit ; if digit has not been used before ; set digit as used ; remove last digit ; function to check whether the sum of distinct digits of two numbers are equal ; 
def distinctDigitSum(n):
    used = [False] * 10
    sum = 0
    while (n > 0):
        digit = n % 10
        if (not used[digit]):
            used[digit] = True
            sum += digit
        n = n // 10
    return sum


def checkSum(m, n):
    sumM = distinctDigitSum(m)
    sumN = distinctDigitSum(n)
    if (sumM == sumN):
        return "YES"
    return "NO"


if __name__ == "__main__":
    m = 2452
    n = 9222
    print(checkSum(m, n))

# Expected output:
YES
# all pairs whose xor gives unique prime | function that returns true if n is prime ; corner cases ; this is checked so that we can skip middle five numbers in below loop ; function to return the count of valid pairs ; if xor ( a [ i ] , a [ j ] ) is prime and unique ; 
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 += 6
    return True


def countPairs(a, n):
    count = 0
    m = dict()
    for i in range(n - 1):
        for j in range(i + 1, n):
            if isPrime(a[i] ^ a[j]) and m .get(a[i] ^ a[j], 0) == 0:
                m[(a[i] ^ a[j])] = 1
                count += 1
    return count


a = [10, 12, 23, 45, 5, 6]
n = len(a)
print(countPairs(a, n))

# Expected output:
4
# sum of element whose prime factors are present in array | python program to find the sum of the elements of an array whose prime factors are present in the same array ; stores smallest prime factor for every number ; function to calculate spf ( smallest prime factor ) for every number till maxn ; marking smallest prime factor for every number to be itself . ; separately marking spf for every even number as 2 ; if i is prime ; marking spf for all numbers divisible by i ; marking spf [ j ] if it is not previously marked ; function to return the sum of the elements of an array whose prime factors are present in the same array ; function call to calculate smallest prime factors of all the numbers upto maxn ; create map for each element ; if smallest prime factor of num is present in array ; each factor of arr [ i ] is present in the array ; 

from collections import defaultdict
MAXN = 1000001
MAXN_sqrt = int(MAXN ** (0.5))
spf = [None] * (MAXN)


def sieve():
    spf[1] = 1
    for i in range(2, MAXN):
        spf[i] = i
    for i in range(4, MAXN, 2):
        spf[i] = 2
    for i in range(3, MAXN_sqrt):
        if spf[i] == i:
            for j in range(i * i, MAXN, i):
                if spf[j] == j:
                    spf[j] = i


def sumFactors(arr, n):
    sieve()
    Map = defaultdict(lambda: 0)
    for i in range(0, n):
        Map[arr[i]] = 1
    Sum = 0
    for i in range(0, n):
        num = arr[i]
        while num != 1 and Map[spf[num]] == 1:
            num = num // spf[num]
        if num == 1:
            Sum += arr[i]
    return Sum


if __name__ == "__main__":
    arr = [5, 11, 55, 25, 100]
    n = len(arr)
    print(sumFactors(arr, n))

# Expected output:
96
# find nth hermite number | python 3 program to find nth hermite number ; utility function to calculate double factorial of a number ; function to return nth hermite number ; if n is even then return 0 ; if n is odd ; calculate double factorial of ( n - 1 ) and multiply it with 2 ^ ( n / 2 ) ; if n / 2 is odd then nth hermite number will be negative ; return nth hermite number ; 

from math import pow


def doubleFactorial(n):
    fact = 1
    for i in range(1, n + 1, 2):
        fact = fact * i
    return fact


def hermiteNumber(n):
    if (n % 2 == 1):
        return 0
    else:
        number = ((pow(2, n / 2)) * doubleFactorial(n - 1))
        if ((n / 2) % 2 == 1):
            number = number * -1
        return number


if __name__ == '__main__':
    n = 6
    print(int(hermiteNumber(n)))

# Expected output:
-120
# count numbers in a range having gcd of powers of prime factors equal to 1 | python3 implementation of the approach ; vector to store powers greater than 3 ; set to store perfect squares ; set to store powers other than perfect squares ; pushing squares ; if the values is already a perfect square means present in the set ; run loop until some power of current number doesn 't exceed max ; pushing only odd powers as even power of a number can always be expressed as a perfect square which is already present in set squares ; inserting those sorted values of set into a vector ; precompute the powers ; calculate perfect squares in range using sqrtl function ; calculate upper value of r in vector using binary search ; calculate lower value of l in vector using binary search ; calculate perfect powers ; compute final answer ; 

from bisect import bisect as upper_bound
from bisect import bisect_left as lower_bound
from math import floor
N = 1000005
MAX = 10 ** 18
powers = []
squares = dict()
s = dict()


def powersPrecomputation():
    for i in range(2, N):
        squares[i * i] = 1
        if (i not in squares .keys()):
            continue
        temp = i
        while (i * i <= (MAX // temp)):
            temp *= (i * i)
            s[temp] = 1
    for x in s:
        powers .append(x)


def calculateAnswer(L, R):
    powersPrecomputation()
    perfectSquares = floor((R)**(.5)) - floor((L - 1)**(.5))
    high = upper_bound(powers, R)
    low = lower_bound(powers, L)
    perfectPowers = perfectSquares + (high - low)
    ans = (R - L + 1) - perfectPowers
    return ans


L = 13
R = 20
print(calculateAnswer(L, R))

# Expected output:
7
# sum of integers upto n with given unit digit | function to return the required sum ; if the unit digit is d ; 
def getSum(n, d):
    sum = 0
    for i in range(n + 1):
        if (i % 10 == d):
            sum += i
    return sum


if __name__ == "__main__":
    n, d = 30, 3
    print(getSum(n, d))

# Expected output:
39
# split a number into 3 parts such that none of the parts is divisible by 3 | python3 program to split a number into three parts such than none of them is divisible by 3. ; print x = 1 , y = 1 and z = n - 2 ; otherwise , print x = 1 , y = 2 and z = n - 3 ; 
def printThreeParts(N):
    if (N % 3 == 0):
        print(" x = 1, y = 1, z = ", N - 2)
    else:
        print(" x = 1, y = 2, z = ", N - 3)


if __name__ == "__main__":
    N = 10
    printThreeParts(N)

# Expected output:
 x = 1, y = 2, z =  7
# minimum absolute difference of a number and its closest prime | python 3 program to find the minimum absolute difference between a number and its closest prime ; function to check if a number is prime or not ; function to find the minimum absolute difference between a number and its closest prime ; variables to store first prime above and below n ; finding first prime number greater than n ; finding first prime number less than n ; variables to store the differences ; 

from math import sqrt


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


def getDifference(N):
    if (N == 0):
        return 2
    elif (N == 1):
        return 1
    elif (isPrime(N)):
        return 0
    aboveN = -1
    belowN = -1
    n1 = N + 1
    while (True):
        if (isPrime(n1)):
            aboveN = n1
            break
        else:
            n1 += 1
    n1 = N - 1
    while (True):
        if (isPrime(n1)):
            belowN = n1
            break
        else:
            n1 -= 1
    diff1 = aboveN - N
    diff2 = N - belowN
    return min(diff1, diff2)


if __name__ == '__main__':
    N = 25
    print(getDifference(N))

# Expected output:
2
# check if the sum of perfect squares in an array is divisible by x | python3 implementation of the approach ; function that returns true if the the sum of all the perfect squares of the given array is divisible by x ; if a [ i ] is a perfect square ; 

import math


def check(a, y):
    sum = 0
    for i in range(len(a)):
        x = math .sqrt(a[i])
        if (math .floor(x) == math .ceil(x)):
            sum = sum + a[i]
    if (sum % y == 0):
        return True
    else:
        return False


a = [2, 3, 4, 9, 10]
x = 13
if check(a, x):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# largest divisor of a number not divisible by a perfect square | python3 program to find the largest divisor not divisible by any perfect square greater than 1 ; function to find the largest divisor not divisible by any perfect square greater than 1 ; set to store divisors in descending order ; if the number is divisible by i , then insert it ; vector to store perfect squares ; check for each divisor , if it is not divisible by any perfect square , simply return it as the answer . ; 

MAX = 10 ** 5


def findLargestDivisor(n):
    m = n
    s = set()
    s .add(1)
    s .add(n)
    for i in range(2, int(n ** (0.5)) + 1):
        if n % i == 0:
            s .add(n // i)
            s .add(i)
            while m % i == 0:
                m //= i
    if m > 1:
        s .add(m)
    vec = [i ** 2 for i in range(2, MAX + 1)]
    for d in sorted(s, reverse=True):
        divi, j = 0, 0
        while j < len(vec) and vec[j] <= d:
            if d % vec[j] == 0:
                divi = 1
                break
            j += 1
        if not divi:
            return d


if __name__ == "__main__":
    n = 12
    print(findLargestDivisor(n))
    n = 97
    print(findLargestDivisor(n))

# Expected output:
6
97
# find the number of maximum product quadruples | python3 program to find the number of quadruples having maximum product ; returns the number of ways to select r objects out of available n choices ; ncr = ( n * ( n - 1 ) * ( n - 2 ) * ... . . ... ( n - r + 1 ) ) / ( r * ( r - 1 ) * ... * 1 ) ; returns the number of quadruples having maximum product ; stores the frequency of each element ; remaining_choices denotes the remaining elements to select inorder to form quadruple ; traverse the elements of the map in reverse order ; if frequency of element < remaining choices , select all of these elements , else select only the number of elements required ; decrement remaining_choices acc to the number of the current elements selected ; if the quadruple is formed stop the algorithm ; 

from collections import defaultdict


def NCR(n, r):
    numerator = 1
    denominator = 1
    while (r > 0):
        numerator *= n
        denominator *= r
        n -= 1
        r -= 1
    return (numerator // denominator)


def findWays(arr, n):
    count = defaultdict(int)
    if (n < 4):
        return 0
    for i in range(n):
        count[arr[i]] += 1
    remaining_choices = 4
    ans = 1
    for it in reversed(sorted(count .keys())):
        number = it
        frequency = count[it]
        toSelect = min(remaining_choices, frequency)
        ans = ans * NCR(frequency, toSelect)
        remaining_choices -= toSelect
        if (not remaining_choices):
            break
    return ans


if __name__ == "__main__":
    arr = [1, 2, 3, 3, 3, 5]
    n = len(arr)
    maxQuadrupleWays = findWays(arr, n)
    print(maxQuadrupleWays)

# Expected output:
1
# minimum and maximum number of pairs in m teams of n people | python3 program to find minimum and maximum no . of pairs ; 

from math import ceil


def MinimumMaximumPairs(n, m):
    max_pairs = ((n - m + 1) * (n - m)) // 2
    min_pairs = (m * (((n - m) // m + 1) * ((n - m) // m)) //
                 2 + ceil((n - m) / (m)) * ((n - m) % m))
    print("Minimum no. of pairs = ", min_pairs)
    print("Maximum no. of pairs = ", max_pairs)


if __name__ == "__main__":
    n, m = 5, 2
    MinimumMaximumPairs(n, m)

# Expected output:
Minimum no. of pairs =  4
Maximum no. of pairs =  6
# larger of a ^ b or b ^ a ( a raised to power b or b raised to power a ) | python 3 code for finding greater between the a ^ b and b ^ a ; function to find the greater value ; 

import math


def findGreater(a, b):
    x = a * (math .log(b))
    y = b * (math .log(a))
    if (y > x):
        print("a^b is greater")
    elif (y < x):
        print("b^a is greater")
    else:
        print("Both are equal")


a = 3
b = 5
c = 2
d = 4
findGreater(a, b)
findGreater(c, d)

# Expected output:
a^b is greater
Both are equal
# expressing a fraction as a natural number under modulo ' m ' | python3 implementation of the approach ; function to return the gcd of given numbers ; recursive function to return ( x ^ n ) % m ; function to return the fraction modulo mod ; ( b ^ m - 2 ) % m ; final answer ; 

m = 1000000007


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


def modexp(x, n):
    if (n == 0):
        return 1
    elif (n % 2 == 0):
        return modexp((x * x) % m, n // 2)
    else:
        return (x * modexp((x * x) % m, (n - 1) / 2) % m)


def getFractionModulo(a, b):
    c = gcd(a, b)
    a = a // c
    b = b // c
    d = modexp(b, m - 2)
    ans = ((a % m) * (d % m)) % m
    return ans


if __name__ == "__main__":
    a = 2
    b = 6
    print(getFractionModulo(a, b))

# Expected output:
333333336
# find sum of a number and its maximum prime factor | python 3 program to find sum of n and it 's largest prime factor ; function to return the sum of n and it 's largest prime factor ; initialise maxprime to - 1. ; n must be odd at this point , thus skip the even numbers and iterate only odd numbers ; this condition is to handle the case when n is a prime number greater than 2 ; finally return the sum . ; 

from math import sqrt


def maxPrimeFactors(n):
    num = n
    maxPrime = -1
    while (n % 2 == 0):
        maxPrime = 2
        n = n / 2
    p = int(sqrt(n) + 1)
    for i in range(3, p, 2):
        while (n % i == 0):
            maxPrime = i
            n = n / i
    if (n > 2):
        maxPrime = n
    sum = maxPrime + num
    return sum


if __name__ == '__main__':
    n = 19
    print(maxPrimeFactors(n))

# Expected output:
38
# largest number less than n with digit sum greater than the digit sum of n | function to return the sum of the digits of n ; loop for each digit of the number ; function to return the greatest number less than n such that the sum of its digits is greater than the sum of the digits of n ; starting from n - 1 ; check until 1 ; if i satisfies the given condition ; if the condition is not satisfied ; 
def sumOfDigits(n):
    res = 0
    while (n > 0):
        res += n % 10
        n /= 10
    return res


def findNumber(n):
    i = n - 1
    while (i > 0):
        if (sumOfDigits(i) > sumOfDigits(n)):
            return i
        i -= 1
    return -1


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

# Expected output:
819
# find the nth term of the series 14 , 28 , 20 , 40 , ... . . | function to find the n - th term ; initializing the 1 st number ; loop from 2 nd term to nth term ; if i is even , double the previous number ; if i is odd , subtract 8 from previous number ; 
def findNth(N):
    b = 14
    for i in range(2, N + 1):
        if (i % 2 == 0):
            b = b * 2
        else:
            b = b - 8
    return b


N = 6
print(findNth(N))

# Expected output:
64
# problem of 8 neighbours of an element in a 2 | dimension of array ; count of 1 s ; counting all neighbouring 1 s ; comparing the number of neighbouring 1 s with given ranges ; copying changes to the main matrix ; 

N = 4


def predictMatrix(arr, range1a, range1b, range0a, range0b, K, b):
    c = 0
    while (K):
        for i in range(N):
            for j in range(N):
                c = 0
                if (i > 0 and arr[i - 1][j] == 1):
                    c += 1
                if (j > 0 and arr[i][j - 1] == 1):
                    c += 1
                if (i > 0 and j > 0 and arr[i - 1][j - 1] == 1):
                    c += 1
                if (i < N - 1 and arr[i + 1][j] == 1):
                    c += 1
                if (j < N - 1 and arr[i][j + 1] == 1):
                    c += 1
                if (i < N - 1 and j < N - 1 and arr[i + 1][j + 1] == 1):
                    c += 1
                if (i < N - 1 and j > 0 and arr[i + 1][j - 1] == 1):
                    c += 1
                if (i > 0 and j < N - 1 and arr[i - 1][j + 1] == 1):
                    c += 1
                if (arr[i][j] == 1):
                    if (c >= range1a and c <= range1b):
                        b[i][j] = 1
                    else:
                        b[i][j] = 0
                if (arr[i][j] == 0):
                    if (c >= range0a and c <= range0b):
                        b[i][j] = 1
                    else:
                        b[i][j] = 0
        K -= 1
        for k in range(N):
            for m in range(N):
                arr[k][m] = b[k][m]


if __name__ == "__main__":
    arr = [[0, 0, 0, 0], [0, 1, 1, 0], [0, 0, 1, 0], [0, 1, 0, 1]]
    range1a = 2
    range1b = 2
    range0a = 2
    range0b = 3
    K = 3
    b = [[0 for x in range(N)]for y in range(N)]
    predictMatrix(arr, range1a, range1b, range0a, range0b, K, b)
    for i in range(N):
        print()
        for j in range(N):
            print(b[i][j], end=" ")

# Expected output:

0 1 0 0 
0 1 0 0 
1 1 1 0 
0 0 1 0
# number of moves required to guess a permutation . | function that returns the required moves ; final move ; 
def countMoves(n):
    ct = 0
    for i in range(1, n + 1):
        ct += i * (n - i)
    ct += n
    return ct


if __name__ == "__main__":
    n = 3
    print(countMoves(n))

# Expected output:
7
# count number of triplets ( a , b , c ) such that a ^ 2 + b ^ 2 = c ^ 2 and 1 <= a <= b <= c <= n | python3 implementation of the approach ; function to return an arraylist containing all the perfect squares upto n ; while current perfect square is less than or equal to n ; function to return the count of triplet ( a , b , c ) pairs such that a ^ 2 + b ^ 2 = c ^ 2 and 1 <= a <= b <= c <= n ; list of perfect squares upto n ^ 2 ; since , a ^ 2 + b ^ 2 = c ^ 2 ; if c < a or bsquare is not a perfect square ; if triplet pair ( a , b , c ) satisfy the given condition ; 

import math


def getPerfectSquares(n):
    perfectSquares = []
    current = 1
    i = 1
    while (current <= n):
        perfectSquares .append(current)
        i += 1
        current = i ** 2
    return perfectSquares


def countTriplets(n):
    perfectSquares = getPerfectSquares(n ** 2)
    count = 0
    for a in range(1, n + 1):
        aSquare = a ** 2
        for i in range(len(perfectSquares)):
            cSquare = perfectSquares[i]
            bSquare = abs(cSquare - aSquare)
            b = math .sqrt(bSquare)
            b = int(b)
            c = math .sqrt(cSquare)
            c = int(c)
            if (c < a or (bSquare not in perfectSquares)):
                continue
            if ((b >= a) and (b <= c) and (aSquare + bSquare == cSquare)):
                count += 1
    return count


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

# Expected output:
2
# count numbers with n digits which consists of even number of 0 ’ s | function to count numbers with n digits which consists of odd number of 0 's ; 
def countNumber(n):
    return (pow(10, n) - 1) - (pow(10, n) - pow(8, n)) // 2


n = 2
print(countNumber(n))

# Expected output:
81
# find determinant of matrix generated by array rotation | function to calculate determinant ; 
def calcDeterminant(arr, n):
    determinant = 0
    for i in range(n):
        determinant += pow(arr[i], 3)
    determinant -= 3 * arr[0] * arr[1] * arr[2]
    return determinant


arr = [4, 5, 3]
n = len(arr)
print(calcDeterminant(arr, n))

# Expected output:
36
# minimum elements to be added in a range so that count of elements is divisible by k | python 3 implementation of the approach ; total elements in the range ; if total elements are already divisible by k ; value that must be added to count in order to make it divisible by k ; 
def minimumMoves(k, l, r):
    count = r - l + 1
    if (count % k == 0):
        return 0
    return (k - (count % k))


if __name__ == '__main__':
    k = 3
    l = 10
    r = 10
    print(minimumMoves(k, l, r))

# Expected output:
2
# sum of all even numbers in range l and r | function to return the sum of all natural numbers ; function to return sum of even numbers in range l and r ; 
def sumNatural(n):
    sum = (n * (n + 1))
    return int(sum)


def sumEven(l, r):
    return (sumNatural(int(r / 2)) - sumNatural(int((l - 1) / 2)))


l, r = 2, 5
print("Sum of Natural numbers", "from L to R is", sumEven(l, r))

# Expected output:
Sum of Natural numbers from L to R is 6
# check if n is divisible by a number which is composed of the digits from the set { a , b } | function to check whether n is divisible by a number whose digits are either a or b ; base condition ; recursive call ; check for all numbers beginning with ' a ' or 'b ; 
def isDivisibleRec(x, a, b, n):
    if (x > n):
        return False

    if (n % x == 0):
        return True
    return (isDivisibleRec(x * 10 + a, a, b, n) or
            isDivisibleRec(x * 10 + b, a, b, n))


def isDivisible(a, b, n):
    return (isDivisibleRec(a, a, b, n) or
            isDivisibleRec(b, a, b, n))


a = 3
b = 5
n = 53

if (isDivisible(a, b, n)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes