giulio98/xlcost-single-prompt huggingface dataset:
# minimize increments required to make count of even and odd array elements equal | function to find min operations to make even and odd count equal ; odd size will never make odd and even counts equal ; stores the count of even numbers in the array arr [ ] ; stores count of odd numbers in the array arr [ ] ; traverse the array arr [ ] ; if arr [ i ] is an even number ; update cnteven ; odd numbers in arr [ ] ; return absolute difference divided by 2 ; 
def minimumIncrement(arr, N):
    if (N % 2 != 0):
        print("-1")
        return
    cntEven = 0
    cntOdd = 0
    for i in range(N):
        if (arr[i] % 2 == 0):
            cntEven += 1
    cntOdd = N - cntEven
    return abs(cntEven - cntOdd) // 2


if __name__ == '__main__':
    arr = [1, 3, 4, 9]
    N = len(arr)
    print(minimumIncrement(arr, N))

# Expected output:
1
# count ways to construct array with even product from given array such that absolute difference of same indexed elements is at most 1 | function to find count the ways to construct an array , b [ ] such that abs ( a [ i ] - b [ i ] ) <= 1 and product of elements of b [ ] is even ; stores count of arrays b [ ] such that abs ( a [ i ] - b [ i ] ) <= 1 ; stores count of arrays b [ ] whose product of elements is not even ; traverse the array ; update total ; if a [ i ] is an even number ; update oddarray ; print 3 ^ n - 2 ^ x ; 
def cntWaysConsArray(A, N):
    total = 1
    oddArray = 1
    for i in range(N):
        total = total * 3
        if (A[i] % 2 == 0):
            oddArray *= 2
    print(total - oddArray)


if __name__ == "__main__":
    A = [2, 4]
    N = len(A)
    cntWaysConsArray(A, N)

# Expected output:
5
# count numbers up to n whose rightmost set bit is k | function to count the numbers in the range [ 1 , n ] whose rightmost set bit is k ; stores the number whose rightmost set bit is k ; numbers whose rightmost set bit is i ; subtracting the number whose rightmost set bit is i , from n ; since i = k , then the number whose rightmost set bit is k is stored ; 
def countNumberHavingKthBitSet(N, K):
    numbers_rightmost_setbit_K = 0
    for i in range(1, K + 1):
        numbers_rightmost_bit_i = (N + 1) // 2
        N -= numbers_rightmost_bit_i
        if (i == K):
            numbers_rightmost_setbit_K = numbers_rightmost_bit_i
    print(numbers_rightmost_setbit_K)


if __name__ == '__main__':
    N = 15
    K = 2
    countNumberHavingKthBitSet(N, K)

# Expected output:
4
# count odd and even binomial coefficients of n | function to count set bits in binary representation of number n ; count set bits in n ; return the final count ; 
def countSetBits(N: int) -> int:
    count = 0
    while (N):
        N = N & (N - 1)
        count += 1
    return count


if __name__ == "__main__":
    N = 4
    bits = countSetBits(N)
    print("Odd : {}".format(pow(2, bits)))
    print("Even : {}".format(N + 1 - pow(2, bits)))

# Expected output:
Odd : 2
Even : 3
# make all array elements even by replacing any pair of array elements with their sum | function to find the minimum number of replacements required to make all array elements even ; stores the count of odd elements ; traverse the array ; increase count of odd elements ; store number of replacements required ; two extra moves will be required to make the last odd element even ; prthe minimum replacements ; 
def minMoves(arr, N):
    odd_element_cnt = 0
    for i in range(N):
        if (arr[i] % 2 != 0):
            odd_element_cnt += 1
    moves = (odd_element_cnt) // 2
    if (odd_element_cnt % 2 != 0):
        moves += 2
    print(moves)


if __name__ == '__main__':
    arr = [5, 6, 3, 7, 20]
    N = len(arr)
    minMoves(arr, N)

# Expected output:
3
# split squares of first n natural numbers into two sets with minimum absolute difference of their sums | function to partition squares of n natural number in two subset ; store the count of blocks of size 8 ; partition of block of 8 element ; store the minimum subset difference ; partition of n elements to minimize their subset sum difference ; store elements of subset a and b ; if element is of type a ; if the element is of type b ; print the minimum subset difference ; print the first subset ; print the second subset ; 
def minimumSubsetDifference(N):
    blockOfSize8 = N // 8
    str = "ABBABAAB"
    subsetDifference = 0
    partition = ""
    while blockOfSize8 != 0:
        partition = partition + str
        blockOfSize8 = blockOfSize8 - 1
    A = []
    B = []
    for i in range(N):
        if partition[i] == 'A':
            A .append((i + 1) * (i + 1))
        else:
            B .append((i + 1) * (i + 1))
    print(subsetDifference)
    for i in A:
        print(i, end=" ")
    print()
    for i in B:
        print(i, end=" ")


N = 8
minimumSubsetDifference(N)

# Expected output:
0
1 16 36 49 
4 9 25 64
# largest divisor of a number not divisible by another given number | python3 program to implement the above approach ; function to find the largest number x such that it divides p but is not divisible by q ; stores the frequency count of of all prime factors ; increment the frequency of the current prime factor ; if q is a prime factor ; stores the desired result ; iterate through all divisors of q ; stores the frequency count of current prime divisor on dividing p ; count the frequency of the current prime factor ; if cur is less than frequency then p is the final result ; iterate to get temporary answer ; update current answer ; print the desired result ; 

from collections import defaultdict


def findTheGreatestX(P, Q):
    divisiors = defaultdict(int)
    i = 2
    while i * i <= Q:
        while (Q % i == 0 and Q > 1):
            Q //= i
            divisiors[i] += 1
        i += 1
    if (Q > 1):
        divisiors[Q] += 1
    ans = 0
    for i in divisiors:
        frequency = divisiors[i]
        temp = P
        cur = 0
        while (temp % i == 0):
            temp //= i
            cur += 1
        if (cur < frequency):
            ans = P
            break
        temp = P
        for j in range(cur, frequency - 1, -1):
            temp //= i
        ans = max(temp, ans)
    print(ans)


if __name__ == "__main__":
    P = 10
    Q = 4
    findTheGreatestX(P, Q)

# Expected output:
10
# check if rows of a matrix can be rearranged to make bitwise xor of first column non | function to check if there is any row where number of unique elements are greater than 1 ; iterate over the matrix ; function to check if it is possible to rearrange mat [ ] [ ] such that xor of its first column is non - zero ; find bitwise xor of the first column of mat [ ] [ ] ; if bitwise xor of the first column of mat [ ] [ ] is non - zero ; otherwise check rearrangements ; 
def checkRearrangements(mat, N, M):
    for i in range(N):
        for j in range(1, M):
            if (mat[i][0] != mat[i][j]):
                return "Yes"
    return "No"


def nonZeroXor(mat, N, M):
    res = 0
    for i in range(N):
        res = res ^ mat[i][0]
    if (res != 0):
        return "Yes"
    else:
        return checkRearrangements(mat, N, M)


if __name__ == "__main__":
    mat = [[1, 1, 2], [2, 2, 2], [3, 3, 3]]
    N = len(mat)
    M = len(mat[0])
    print(nonZeroXor(mat, N, M))

# Expected output:
Yes
# maximize bitwise and of first element with complement of remaining elements for any permutation of given array | function to maximize the value for the given function and the array elements ; vector array to maintain which bit is set for which integer in the given array by saving index of that integer ; check if j - th bit is set for i - th integer ; push the index of that integer in setbit [ j ] ; find the element having highest significant set bit unset in other elements ; place that integer at 0 - th index ; store the maximum and value ; return the answer ; 
def functionMax(arr, n):
    setBit = [[]for i in range(32)]
    for i in range(n):
        for j in range(32):
            if (arr[i] & (1 << j)):
                setBit[j].append(i)
    i = 31
    while (i >= 0):
        if (len(setBit[i]) == 1):
            temp = arr[0]
            arr[0] = arr[setBit[i][0]]
            arr[setBit[i][0]] = temp
            break
        i -= 1
    maxAnd = arr[0]
    for i in range(1, n, 1):
        maxAnd = (maxAnd & (~arr[i]))
    return maxAnd


if __name__ == '__main__':
    arr = [1, 2, 4, 8, 16]
    n = len(arr)
    print(functionMax(arr, n))

# Expected output:
16
# sum of all products of the binomial coefficients of two numbers up to k | function returns ncr i . e . binomial coefficient ; initialize res with 1 ; since c ( n , r ) = c ( n , n - r ) ; evaluating expression ; function to calculate and return the sum of the products ; initialize sum to 0 ; traverse from 0 to k ; 
def nCr(n, r):
    res = 1
    if r > n - r:
        r = n - r
    for i in range(r):
        res *= (n - i)
        res /= (i + 1)
    return res


def solve(n, m, k):
    sum = 0
    for i in range(k + 1):
        sum += nCr(n, i) * nCr(m, k - i)
    return int(sum)


if __name__ == '__main__':
    n = 3
    m = 2
    k = 2
    print(solve(n, m, k))

# Expected output:
10
# fast exponention using bit manipulation | function to return a ^ n ; stores final answer ; check if current lsb is set ; right shift ; 
def powerOptimised(a, n):
    ans = 1
    while (n > 0):
        last_bit = (n & 1)
        if (last_bit):
            ans = ans * a
        a = a * a
        n = n >> 1
    return ans


if __name__ == '__main__':
    a = 3
    n = 5
    print(powerOptimised(a, n))

# Expected output:
243
# find m such that gcd of m and given number n is maximum | function to find the integer m such that gcd ( n , m ) is maximum ; initialize variables ; find all the divisors of n and return the maximum divisor ; check if i is divisible by n ; update max_gcd ; return the maximum value ; 
def findMaximumGcd(n):
    max_gcd = 1
    i = 1
    while (i * i <= n):
        if n % i == 0:
            if (i > max_gcd):
                max_gcd = i
            if ((n / i != i) and (n / i != n) and ((n / i) > max_gcd)):
                max_gcd = n / i
        i += 1
    return (int(max_gcd))


if __name__ == '__main__':
    n = 10
    print(findMaximumGcd(n))

# Expected output:
5
# product of complex numbers using three multiplication operation | function to multiply complex numbers with just three multiplications ; find value of prod1 , prod2 and prod3 ; real part ; imaginary part ; print the result ; 
def print_product(a, b, c, d):
    prod1 = a * c
    prod2 = b * d
    prod3 = (a + b) * (c + d)
    real = prod1 - prod2
    imag = prod3 - (prod1 + prod2)
    print(real, " + ", imag, "i")


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

# Expected output:
-7  +  22 i
# insolite numbers | function to check if a number is an insolite numbers ; to store sum of squares of digits ; to store product of squares of digits ; extracting digit ; 
def isInsolite(n):
    N = n
    sum = 0
    product = 1
    while (n != 0):
        r = n % 10
        sum = sum + r * r
        product = product * r * r
        n = n // 10
    return ((N % sum == 0) and (N % product == 0))


if __name__ == '__main__':
    N = 111
    if (isInsolite(N)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# d numbers | python3 implementation for the above approach ; function to find the n - th icosikaipentagon number ; number should be greater than 3 ; check every k in range 2 to n - 1 ; condition for d - number ; 

import math


def isDNum(n):
    if n < 4:
        return False
    for k in range(2, n):
        numerator = pow(k, n - 2) - k
        hcf = math .gcd(n, k)
        if (hcf == 1 and (numerator % n) != 0):
            return False
    return True


n = 15
if isDNum(n):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# ruth | function to find prime divisors of all numbers from 1 to n ; if the number is prime ; add this prime to all it 's multiples ; function to check ruth - aaron number ; 
def Sum(N):
    SumOfPrimeDivisors = [0] * (N + 1)
    for i in range(2, N + 1):
        if (SumOfPrimeDivisors[i] == 0):
            for j in range(i, N + 1, i):
                SumOfPrimeDivisors[j] += i
    return SumOfPrimeDivisors[N]


def RuthAaronNumber(n):
    if (Sum(n) == Sum(n + 1)):
        return True
    else:
        return False


N = 714
if (RuthAaronNumber(N)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# maximize sum of absolute difference between adjacent elements in array with sum k | function for maximising the sum ; difference is 0 when only one element is present in array ; difference is k when two elements are present in array ; otherwise ; 
def maxAdjacentDifference(N, K):
    if (N == 1):
        return 0
    if (N == 2):
        return K
    return 2 * K


N = 6
K = 11
print(maxAdjacentDifference(N, K))

# Expected output:
22
# sum of all divisors from 1 to n | set 2 | python3 program for the above approach ; functions returns sum of numbers from 1 to n ; functions returns sum of numbers from a + 1 to b ; function returns total sum of divisors ; stores total sum ; finding numbers and its occurence ; sum of product of each number and its occurence ; 

mod = 1000000007


def linearSum(n):
    return n * (n + 1) // 2 % mod


def rangeSum(b, a):
    return (linearSum(b) - (linearSum(a))) % mod


def totalSum(n):
    result = 0
    i = 1
    while True:
        result += rangeSum(n // i, n // (i + 1)) * (i % mod) % mod
        result %= mod
        if i == n:
            break
        i = n // (n // (i + 1))
    return result


N = 4
print(totalSum(N))
N = 12
print(totalSum(N))

# Expected output:
15
127
# nontrivial undulant numbers | function to check if a string is double string or not ; a and b should not be equal ; condition to check if length is odd make length even ; first half of s ; second half of s ; double string if first and last half are equal ; function to check if n is an nontrivial undulant number ; 
def isDouble(num):
    s = str(num)
    l = len(s)
    if (s[0] == s[1]):
        return False
    if (l % 2 == 1):
        s = s + s[1]
        l += 1
    s1 = s[:l // 2]
    s2 = s[l // 2:]
    return s1 == s2


def isNontrivialUndulant(N):
    return N > 100 and isDouble(N)


n = 121
if (isNontrivialUndulant(n)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# megagon number | function to find the nth megagon number ; 
def MegagonNum(n):
    return (999998 * n * n - 999996 * n) // 2


n = 3
print(MegagonNum(n))

# Expected output:
2999997
# product of all the pairs from the given array | python3 implementation to find the product of all the pairs from the given array ; function to return the product of the elements of all possible pairs from the array ; to store the required product ; nested loop to calculate all possible pairs ; multiply the product of the elements of the current pair ; return the final result ; 

mod = 1000000007


def productPairs(arr, n):
    product = 1
    for i in range(n):
        for j in range(n):
            product *= (arr[i] % mod * arr[j] % mod) % mod
            product = product % mod
    return product % mod


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

# Expected output:
46656
# product of all the pairs from the given array | python3 implementation to find the product of all the pairs from the given array ; function to calculate ( x ^ y ) % 1000000007 ; initialize result ; update x if it is more than or equal to p ; if y is odd , multiply x with result ; return the final result ; function to return the product of the elements of all possible pairs from the array ; to store the required product ; iterate for every element of the array ; each element appears ( 2 * n ) times ; 

mod = 1000000007


def power(x, y):
    p = 1000000007
    res = 1
    x = x % p
    while (y > 0):
        if ((y & 1) != 0):
            res = (res * x) % p
        y = y >> 1
        x = (x * x) % p
    return res


def productPairs(arr, n):
    product = 1
    for i in range(n):
        product = (product % mod * (int)(power(arr[i], (2 * n))) % mod) % mod
    return (product % mod)


arr = [1, 2, 3]
n = len(arr)
print(productPairs(arr, n))

# Expected output:
46656
# construct an array such that cube sum of all element is a perfect square | function to create and print the array ; initialise the array of size n ; print the array ; 
def constructArray(N):
    arr = [0] * N
    for i in range(1, N + 1):
        arr[i - 1] = i
    for i in range(N):
        print(arr[i], end=", ")


N = 6
constructArray(N)

# Expected output:
1, 2, 3, 4, 5, 6,
# count of all subsequence whose product is a composite number | function to check whether a number is prime or not ; function to find number of subsequences whose product is a composite number ; find total non empty subsequence ; find count of prime number and ones ; calculate the non empty one subsequence ; find count of composite subsequence ; 
def isPrime(n):
    if (n <= 1):
        return False
    for i in range(2, n):
        if (n % i == 0):
            return False
    return True


def countSubsequences(arr, n):
    totalSubsequence = (int)(pow(2, n) - 1)
    countPrime = 0
    countOnes = 0
    for i in range(n):
        if (arr[i] == 1):
            countOnes += 1
        elif (isPrime(arr[i])):
            countPrime += 1
    compositeSubsequence = 0
    onesSequence = (int)(pow(2, countOnes) - 1)
    compositeSubsequence = (
        totalSubsequence -
        countPrime -
        onesSequence -
        onesSequence *
        countPrime)
    return compositeSubsequence


if __name__ == '__main__':
    arr = [2, 1, 2]
    n = len(arr)
    print(countSubsequences(arr, n))

# Expected output:
2
# find k consecutive integers such that their sum is n | function to check if a number can be expressed as the sum of k consecutive ; finding the first term of ap ; checking if first term is an integer ; loop to print the k consecutive integers ; 
def checksum(n, k):
    first_term = ((2 * n) / k + (1 - k)) / 2.0
    if (first_term - int(first_term) == 0):
        for i in range(int(first_term), int(first_term) + k):
            print(i, end=' ')
    else:
        print('-1')


if __name__ == '__main__':
    (n, k) = (33, 6)
    checksum(n, k)

# Expected output:
3 4 5 6 7 8
# represent n as sum of k even numbers | function to print the representation ; n must be greater than equal to 2 * k and must be even ; 
def sumEvenNumbers(N, K):
    check = N - 2 * (K - 1)
    if (check > 0 and check % 2 == 0):
        for i in range(K - 1):
            print("2 ", end="")
        print(check)
    else:
        print("-1")


N = 8
K = 2
sumEvenNumbers(N, K)

# Expected output:
2 6
# smallest number greater than or equal to x whose sum of digits is divisible by y | python3 program to find the smallest number greater than or equal to x and divisible by y ; function that returns the sum of digits of a number ; initialize variable to store the sum ; add the last digit of the number ; remove the last digit from the number ; function that returns the smallest number greater than or equal to x and divisible by y ; initialize result variable ; loop through numbers greater than equal to x ; calculate sum of digits ; check if sum of digits is divisible by y ; 

MAXN = 10000000


def sumOfDigits(n):
    sum = 0
    while (n > 0):
        sum += n % 10
        n //= 10
    return sum


def smallestNum(X, Y):
    res = -1
    for i in range(X, MAXN):
        sum_of_digit = sumOfDigits(i)
        if sum_of_digit % Y == 0:
            res = i
            break
    return res


if __name__ == '__main__':
    (X, Y) = (5923, 13)
    print(smallestNum(X, Y))

# Expected output:
5939
# count the numbers which can convert n to 1 using given operation | function to count the numbers which can convert n to 1 using the given operation ; store all the divisors of n ; if i is a divisor ; if i is not equal to n / i ; iterate through all the divisors of n - 1 and count them in answer ; check if n - 1 is a divisor or not ; iterate through all divisors and check for n mod d = 1 or ( n - 1 ) mod d = 0 ; 
def countValues(N):
    div = []
    i = 2
    while ((i * i) <= N):
        if (N % i == 0):
            div .append(i)
            if (N != i * i):
                div .append(N // i)
        i += 1
    answer = 0
    i = 1
    while ((i * i) <= N - 1):
        if ((N - 1) % i == 0):
            if (i * i == N - 1):
                answer += 1
            else:
                answer += 2
        i += 1
    for d in div:
        K = N
        while (K % d == 0):
            K //= d
        if ((K - 1) % d == 0):
            answer += 1
    return answer


if __name__ == "__main__":
    N = 6
    print(countValues(N))

# Expected output:
3
# maximum possible prime divisors that can exist in numbers having exactly n divisors | function to find the maximum possible prime divisors of a number can have with n divisors ; number of time number divided by 2 ; divide by other prime numbers ; if the last number of also prime then also include it ; 
def findMaxPrimeDivisor(n):
    max_possible_prime = 0
    while (n % 2 == 0):
        max_possible_prime += 1
        n = n // 2
    i = 3
    while (i * i <= n):
        while (n % i == 0):
            max_possible_prime += 1
            n = n // i
        i = i + 2
    if (n > 2):
        max_possible_prime += 1
    print(max_possible_prime)


n = 4
findMaxPrimeDivisor(n)

# Expected output:
2
# count ways to express a number as sum of exactly two numbers | function returns the count of ways express a number as sum of two numbers . ; 
def CountWays(n):
    ans = (n - 1) // 2
    return ans


N = 8
print(CountWays(N))

# Expected output:
3
# divide array in two maximum equal length arrays of similar and dissimilar elements | function to find the max - size to which an array can be divided into 2 equal parts ; vector to find the frequency of each element of list ; find the maximum frequency element present in list arr ; find total unique elements present in list arr ; find the max - size to which an array arr [ ] can be splitted ; find the first array containing same elements ; find the second array containing unique elements ; 
def Solve(arr, size, n):
    v = [0] * (n + 1)
    for i in range(size):
        v[arr[i]] += 1
    max1 = max(set(arr), key=v .count)
    diff1 = n + 1 - v .count(0)
    max_size = max(min(v[max1] - 1, diff1), min(v[max1], diff1 - 1))
    print("Maximum size is :", max_size)
    print("The First Array Is : ")
    for i in range(max_size):
        print(max1, end=" ")
        v[max1] -= 1
    print()
    print("The Second Array Is : ")
    for i in range(n + 1):
        if (v[i] > 0):
            print(i, end=" ")
            max_size -= 1
        if (max_size < 1):
            break
    print()


if __name__ == "__main__":
    n = 7
    arr = [1, 2, 1, 5, 1, 6, 7, 2]
    size = len(arr)
    Solve(arr, size, n)

# Expected output:
Maximum size is : 3
The First Array Is : 
1 1 1 
The Second Array Is : 
2 5 6