giulio98/xlcost-single-prompt huggingface dataset:
# find the subarray of size k with minimum xor | function to find the minimum xor of the subarray of size k ; k must be smaller than or equal to n ; initialize beginning index of result ; compute xor sum of first subarray of size k ; initialize minimum xor sum as current xor ; traverse from ( k + 1 ) ' th ▁ ▁ element ▁ to ▁ n ' th element ; xor with current item and first item of previous subarray ; update result if needed ; 
def findMinXORSubarray(arr, n, k):
    if (n < k):
        return
    res_index = 0
    curr_xor = 0
    for i in range(0, k):
        curr_xor = curr_xor ^ arr[i]
    min_xor = curr_xor
    for i in range(k, n):
        curr_xor ^= (arr[i] ^ arr[i - k])
        if (curr_xor < min_xor):
            min_xor = curr_xor
            res_index = (i - k + 1)
    print(min_xor, end='')


arr = [3, 7, 90, 20, 10, 50, 40]
k = 3
n = len(arr)
findMinXORSubarray(arr, n, k)

# Expected output:
16
# greatest number that can be formed from a pair in a given array | python3 implementation to find the greatest number from the given pairs of the array ; function to find the greatest number formed from the pairs ; first append y at the end of x ; then append x at the end of y ; now see which of the two formed numbers is greater than other ; function to find pairs from array ; iterate through all pairs ; 

import sys


def getNumber(a, b):
    X = str(a)
    Y = str(b)
    XY = X + Y
    YX = Y + X
    if (XY > YX):
        return XY
    else:
        return YX


def printMaxPair(arr, n):
    largest = -sys .maxsize - 1
    for i in range(0, n):
        for j in range(i + 1, n):
            number = int(getNumber(arr[i], arr[j]))
            largest = max(largest, number)
    print(largest)


a = [23, 14, 16, 25, 3, 9]
n = len(a)
printMaxPair(a, n)

# Expected output:
2523
# check whether two numbers are in golden ratio | function to check that two numbers are in golden ratio ; swapping the numbers such that a contains the maximum number between these numbers ; first ratio ; second ratio ; condition to check that two numbers are in golden ratio ; 
def checkGoldenRatio(a, b):
    a, b = max(a, b), min(a, b)
    ratio1 = round(a / b, 3)
    ratio2 = round((a + b) / a, 3)
    if ratio1 == ratio2 and ratio1 == 1.618:
        print("Yes")
        return True
    else:
        print("No")
        return False


if __name__ == "__main__":
    a = 0.618
    b = 1
    checkGoldenRatio(a, b)

# Expected output:
Yes
# find the minimum number to be added to n to make it a power of k | python3 program to find the minimum number to be added to n to make it a power of k ; function to return the minimum number to be added to n to make it a power of k . ; computing the difference between then next greater power of k and n ; 

import math


def minNum(n, k):
    x = int((math .log(n) // math .log(k))) + 1
    mn = pow(k, x) - n
    return mn


if __name__ == '__main__':
    n = 20
    k = 5
    print(minNum(n, k))

# Expected output:
5
# find the count of even odd pairs in a given array | function to count the pairs in array of the form ( even , odd ) ; variable to store count of such pairs ; iterate through all pairs ; increment count if condition is satisfied ; 
def findCount(arr, n):
    res = 0
    for i in range(0, n - 1):
        for j in range(i + 1, n):
            if ((arr[i] % 2 == 0) and (arr[j] % 2 == 1)):
                res = res + 1
    return res


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

# Expected output:
3
# find the count of even odd pairs in a given array | function to count the pairs in array of the form ( even , odd ) ; check if number is even or not ; 
def findCount(arr, n):
    count = 0
    ans = 0
    for i in range(0, n):
        if (arr[i] % 2 == 0):
            count = count + 1
        else:
            ans = ans + count
    return ans


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

# Expected output:
3
# product of all non repeating subarrays of an array | function to find the product of all non - repeating subarrays of an array ; finding the occurrence of every element ; iterating through the array and finding the product ; we are taking the power of each element in array with the occurrence and then taking product of those . ; 
def product(arr):
    occurrence = pow(2, len(arr) - 1)
    product = 1
    for i in range(0, len(arr)):
        product *= pow(arr[i], occurrence)
    return product


arr = [10, 3, 7]
print(product(arr))

# Expected output:
1944810000
# sum of all n | python 3 program for the above approach ; function to find a ^ b efficiently ; base case ; to store the value of a ^ b ; multiply temp by a until b is not 0 ; return the final ans a ^ b ; function to find sum of all n - digit palindromic number divisible by 9 ; base case ; if n is even , decrease ways by 1 ; find the total number of ways ; iterate over [ 1 , n ] and find the sum at each index ; print the final sum ; 

MOD = 1000000007


def power(a, b):
    if (b == 0):
        return 1
    temp = power(a, b // 2)
    temp = (temp * temp) % MOD
    if (b % 2 != 0):
        temp = (temp * a) % MOD
    return temp


def palindromicSum(N):
    sum = 0
    if (N == 1):
        print("9")
        return
    if (N == 2):
        print("99")
        return
    ways = N // 2
    if (N % 2 == 0):
        ways -= 1
    res = power(9, ways - 1)
    for i in range(N):
        sum = sum * 10 + 45 * res
        sum %= MOD
    print(sum)


if __name__ == "__main__":
    N = 3
    palindromicSum(N)

# Expected output:
4995
# maximize array sum by concatenating corresponding elements of given two arrays | function to join the two numbers ; loop to reverse the digits of the one number ; loop to join two numbers ; function to find the maximum array sum ; loop to iterate over the two elements of the array ; find the array sum ; return the array sum ; 
def joinNumbers(numA, numB):
    revB = 0
    while (numB > 0):
        revB = revB * 10 + (numB % 10)
        numB = numB // 10
    while (revB > 0):
        numA = numA * 10 + (revB % 10)
        revB = revB // 10
    return numA


def findMaxSum(A, B, n):
    maxArr = [0 for i in range(n)]
    for i in range(n):
        X = joinNumbers(A[i], B[i])
        Y = joinNumbers(B[i], A[i])
        mx = max(X, Y)
        maxArr[i] = mx
    maxAns = 0
    for i in range(n):
        maxAns += maxArr[i]
    return maxAns


if __name__ == '__main__':
    N = 5
    A = [11, 23, 38, 43, 59]
    B = [36, 24, 17, 40, 56]
    print(findMaxSum(A, B, N))

# Expected output:
19850
# split n natural numbers into two sets having gcd of their sums greater than 1 | function to create and print the two sets ; for n <= 2 such sets can never be formed ; check if n is even or odd and store the element which divides the sum of n natural numbers accordingly ; first set ; print elements of second set ; 
def createSets(n):
    if (n <= 2):
        print("-1")
        return
    else:
        x = (n // 2)if (n % 2 == 0)else ((n + 1) // 2)
        print(x, " ")
        for i in range(1, n + 1):
            if (i == x):
                continue
            print(i, end=" ")
    return


if __name__ == '__main__':
    N = 7
    createSets(N)

# Expected output:
4  
1 2 3 5 6 7
# count the factors of k present in the given array | function to find the count of factors of k present in array ; loop to consider every element of array ; 
def calcCount(arr, n, k):
    count = 0
    for i in range(0, n):
        if (k % arr[i] == 0):
            count = count + 1
    return count


arr = [1, 2, 4, 5, 6]
n = len(arr)
k = 6
print(calcCount(arr, n, k))

# Expected output:
3
# multiply n complex numbers given as strings | function which returns the in digit format ; a : real b : imaginary ; sa : sign of a sb : sign of b ; extract the real number ; extract the imaginary part ; if size == 1 means we reached at result ; extract the first two elements ; remove them ; calculate and store the real part ; calculate and store the imaginary part ; append the real part ; append the imaginary part ; insert into vector ; 
def findnum(s1):
    v = []
    a = 0
    b = 0
    sa = 0
    sb = 0
    i = 0
    if (s1[0] == '-'):
        sa = 1
        i = 1
    while (s1[i].isdigit()):
        a = a * 10 + (int(s1[i]))
        i += 1
    if (s1[i] == '+'):
        sb = 0
        i += 1
    if (s1[i] == '-'):
        sb = 1
        i += 1
    while (i < len(s1) and s1[i].isdigit()):
        b = b * 10 + (int(s1[i]))
        i += 1
    if (sa):
        a *= -1
    if (sb):
        b *= -1
    v .append(a)
    v .append(b)
    return v


def complexNumberMultiply(v):
    while (len(v) != 1):
        v1 = findnum(v[0])
        v2 = findnum(v[1])
        del v[0]
        del v[0]
        r = (v1[0] * v2[0] - v1[1] * v2[1])
        img = v1[0] * v2[1] + v1[1] * v2[0]
        res = ""
        res += str(r)
        res += '+'
        res += str(img) + 'i'
        v .insert(0, res)
    return v[0]


if __name__ == '__main__':
    n = 3
    v = ["3+1i", "2+1i", "-5+-7i"]
    print(complexNumberMultiply(v))

# Expected output:
10+-60i
# length of largest subarray whose all elements are perfect number | function that returns true if n is perfect ; to store sum of divisors ; find all divisors and add them ; check if the sum of divisors is equal to n , then n is a perfect number ; function to return the length of the largest sub - array of an array every element of whose is a perfect number ; check if arr [ i ] is a perfect number ; 
def isPerfect(n):
    sum = 1
    i = 2
    while i * i <= n:
        if n % i == 0:
            sum = sum + i + n / i
        i += 1
    return (True if sum == n and n != 1 else False)


def contiguousPerfectNumber(arr, n):
    current_length = 0
    max_length = 0
    for i in range(0, n, 1):
        if (isPerfect(arr[i])):
            current_length += 1
        else:
            current_length = 0
        max_length = max(max_length, current_length)
    return max_length


if __name__ == '__main__':
    arr = [1, 7, 36, 4, 6, 28, 4]
    n = len(arr)
    print(contiguousPerfectNumber(arr, n))

# Expected output:
2
# length of largest subarray whose all elements powerful number | python 3 program to find the length of the largest sub - array of an array every element of whose is a powerful number ; function to check if the number is powerful ; first divide the number repeatedly by 2 ; if only 2 ^ 1 divides n ( not higher powers ) , then return false ; if n is not a power of 2 then this loop will execute repeat above process ; find highest power of " factor " that divides n ; if only factor ^ 1 divides n ( not higher powers ) , then return false ; n must be 1 now if it is not a prime numenr . since prime numbers are not powerful , we return false if n is not 1. ; function to return the length of the largest sub - array of an array every element of whose is a powerful number ; if arr [ i ] is a powerful number ; 

import math


def isPowerful(n):
    while (n % 2 == 0):
        power = 0
        while (n % 2 == 0):
            n = n // 2
            power = power + 1
        if (power == 1):
            return False
    for factor in range(3, int(math .sqrt(n)) + 1, 2):
        power = 0
        while (n % factor == 0):
            n = n // factor
            power = power + 1
        if (power == 1):
            return false
    return (n == 1)


def contiguousPowerfulNumber(arr, n):
    current_length = 0
    max_length = 0
    for i in range(0, n, 1):
        if (isPowerful(arr[i])):
            current_length += 1
        else:
            current_length = 0
        max_length = max(max_length, current_length)
    return max_length


if __name__ == '__main__':
    arr = [1, 7, 36, 4, 6, 28, 4]
    n = len(arr)
    print(contiguousPowerfulNumber(arr, n))

# Expected output:
2
# maximize sum of k corner elements in array | function to return maximum sum ; base case ; pick the start index ; pick the end index ; recursive function call ; return the final answer ; function to find the maximized sum ; 
def maxSum(arr, K, start, end, max_sum):
    if (K == 0):
        return max_sum
    max_sum_start = max_sum + arr[start]
    max_sum_end = max_sum + arr[end]
    ans = max(
        maxSum(
            arr,
            K - 1,
            start + 1,
            end,
            max_sum_start),
        maxSum(
            arr,
            K - 1,
            start,
            end - 1,
            max_sum_end))
    return ans


def maximizeSum(arr, K, n):
    max_sum = 0
    start = 0
    end = n - 1
    print(maxSum(arr, K, start, end, max_sum))


if __name__ == '__main__':
    arr = [8, 4, 4, 8, 12, 3, 2, 9]
    K = 3
    n = len(arr)
    maximizeSum(arr, K, n)

# Expected output:
21
# maximize sum of k corner elements in array | function to return maximum sum ; initialize variables ; iterate over first k elements of array and update the value for curr_points ; update value for max_points ; j points to the end of the array ; return the final result ; 
def maxPointCount(arr, K, size):
    curr_points = 0
    max_points = 0
    for i in range(K):
        curr_points += arr[i]
    max_points = curr_points
    j = size - 1
    for i in range(K - 1, -1, -1):
        curr_points = (curr_points + arr[j] - arr[i])
        max_points = max(curr_points, max_points)
        j -= 1
    return max_points


if __name__ == "__main__":
    arr = [8, 4, 4, 8, 12, 3, 2, 9]
    K = 3
    n = len(arr)
    print(maxPointCount(arr, K, n))

# Expected output:
21
# sum of all n digit palindromic numbers divisible by 9 formed using digits 1 to 9 | function for finding count of n digits palindrome which are divisible by 9 ; if n is odd ; if n is even ; function for finding sum of n digits palindrome which are divisible by 9 ; count the possible number of palindrome ; 
def countPalindrome(n):
    count = 0
    if (n % 2 == 1):
        count = pow(9, (n - 1) // 2)
    else:
        count = pow(9, (n - 2) // 2)
    return count


def sumPalindrome(n):
    count = countPalindrome(n)
    res = 0
    if (n == 1):
        return 9
    if (n == 2):
        return 99
    for i in range(n):
        res = res * 10 + count * 5
    return res


n = 3
print(sumPalindrome(n))

# Expected output:
4995
# find the total count of numbers up to n digits in a given base b | python3 implementation to find the count of natural numbers up to n digits ; function to return the count of natural numbers upto n digits ; loop to iterate from 1 to n and calculating number of natural numbers for every ' i ' th digit . ; 

from math import pow


def count(N, B):
    sum = 0
    for i in range(1, N + 1):
        sum += (B - 1) * pow(B, i - 1)
    return sum


if __name__ == '__main__':
    N = 2
    B = 10
    print(int(count(N, B)))

# Expected output:
99
# find smallest perfect square number a such that n + a is also a perfect square number | python3 code to find out the smallest perfect square x which when added to n yields another perfect square number . ; x is the smallest perfect square number ; loop from 1 to square root of n ; condition to check whether i is factor of n or not ; condition to check whether factors satisfies the equation or not ; stores minimum value ; return if x * x if x is not equal to 1e9 else return - 1 ; 

import math


def SmallestPerfectSquare(N):
    X = 1e9
    for i in range(1, int(math .sqrt(N)) + 1):
        if N % i == 0:
            a = i
            b = N // i
            if b - a != 0 and (b - a) % 2 == 0:
                X = min(X, (b - a) // 2)
    return (X * X if X != 1e9 else -1)


if __name__ == "__main__":
    N = 3
    print(SmallestPerfectSquare(N))

# Expected output:
1
# minimum cost path to visit all nodes situated at the circumference of circular road | function to find the minimum cost ; sort the given array ; initialise a new array of double size ; fill the array elements ; find the minimum path cost ; return the final result ; 
def minCost(arr, n, circumference):
    arr .sort()
    arr2 = [0] * (2 * n)
    for i in range(n):
        arr2[i] = arr[i]
        arr2[i + n] = arr[i] + circumference
    res = 9999999999999999999
    for i in range(n):
        res = min(res, arr2[i + (n - 1)] - arr2[i])
    return res


arr = [19, 3, 6]
n = len(arr)
circumference = 20
print(minCost(arr, n, circumference))

# Expected output:
7
# unique element in an array where all elements occur k times except one | set 2 | function to find single occurrence element ; by shifting 1 to left ith time and taking and with 1 will give us that ith bit of a [ j ] is 1 or 0 ; taking modulo of p with k ; generate result ; loop for negative numbers ; check if the calculated value res is present in array , then mark c = 1 and if c = 1 return res else res must be - ve ; 
def findunique(a, k):
    res = 0
    for i in range(32):
        p = 0
        for j in range(len(a)):
            if (abs(a[j]) & (1 << i)) != 0:
                p += 1
        p %= k
        res += pow(2, i) * p
    c = 0
    for x in a:
        if (x == res):
            c = 1
            break
    if c == 1:
        return res
    else:
        return -res


a = [12, 12, 2, 2, 3]
k = 2
print(findunique(a, k))

# Expected output:
3
# find minimum gcd of all pairs in an array | python3 program to find the minimum gcd of any pair in the array ; function returns the minimum gcd of any pair ; finding gcd of all the elements in the array . ; 

from math import gcd


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


if __name__ == '__main__':
    arr = [2, 4, 6, 8, 3]
    N = len(arr)
    print(MinimumGCD(arr, N))

# Expected output:
1
# minimum steps to reach the nth stair in jumps of perfect power of 2 | function to count the number of jumps required to reach nth stairs . ; till n becomes 0 ; removes the set bits from the right to left ; 
def stepRequired(N):
    cnt = 0
    while (N > 0):
        N = N & (N - 1)
        cnt += 1
    return cnt


if __name__ == '__main__':
    N = 23
    print(stepRequired(N))

# Expected output:
4
# count of total subarrays whose sum is a fibonacci numbers | python3 program for the above approach ; function to check whether a number is perfect square or not ; function to check whether a number is fibonacci number or not ; if 5 * n * n + 4 or 5 * n * n - 5 is a perfect square , then the number is fibonacci ; function to count the subarray with sum fibonacci number ; traverse the array arr [ ] to find the sum of each subarray ; to store the sum ; check whether sum of subarray between [ i , j ] is fibonacci or not ; 

import math


def isPerfectSquare(x):
    s = int(math .sqrt(x))
    if s * s == x:
        return True
    return False


def isFibonacci(n):
    return (isPerfectSquare(5 * n * n + 4) or isPerfectSquare(5 * n * n - 4))


def fibonacciSubarrays(arr, n):
    count = 0
    for i in range(n):
        sum = 0
        for j in range(i, n):
            sum += arr[j]
            if (isFibonacci(sum)):
                count += 1
    print(count)


arr = [6, 7, 8, 9]
n = len(arr)
fibonacciSubarrays(arr, n)

# Expected output:
3
# count the number of ways to fill k boxes with n distinct items | python3 program to calculate the above formula ; to store the factorials of all numbers ; function to calculate factorial of all numbers ; calculate x to the power y in o ( log n ) time ; function to find inverse mod of a number x ; calculate ( n c r ) ; loop to compute the formula evaluated ; add even power terms ; subtract odd power terms ; choose the k boxes which were used ; 

mod = 1000000007
factorial = [0 for i in range(100005)]


def StoreFactorials(n):
    factorial[0] = 1
    for i in range(1, n + 1, 1):
        factorial[i] = (i * factorial[i - 1]) % mod


def Power(x, y):
    ans = 1
    while (y > 0):
        if (y % 2 == 1):
            ans = (ans * x) % mod
        x = (x * x) % mod
        y //= 2
    return ans


def invmod(x):
    return Power(x, mod - 2)


def nCr(n, r):
    return (
        (factorial[n] * invmod((factorial[r] * factorial[n - r]) % mod)) % mod)


def CountWays(n, k):
    StoreFactorials(n)
    ans = 0
    i = k
    while (i >= 0):
        if (i % 2 == k % 2):
            ans = ((ans + (Power(i, n) * nCr(k, i)) % mod) % mod)
        else:
            ans = ((ans + mod - (Power(i, n) * nCr(k, i)) % mod) % mod)
        i -= 1
    ans = (ans * nCr(n, k)) % mod
    return ans


if __name__ == '__main__':
    N = 5
    K = 5
    print(CountWays(N, K))

# Expected output:
120
# program to print numbers from n to 1 in reverse order | recursive function to print from n to 1 ; if n is less than 1 then return void function ; recursive call of the function ; 
def PrintReverseOrder(N):
    if (N <= 0):
        return
    else:
        print(N, end=" ")
        PrintReverseOrder(N - 1)


N = 5
PrintReverseOrder(N)

# Expected output:
5 4 3 2 1
# find the xor of first half and second half elements of an array | function to find the xor of the first half elements and second half elements of an array ; xor of elements in firsthalfxor ; xor of elements in secondhalfxor ; 
def XOROfElements(arr, n):
    FirstHalfXOR = 0
    SecondHalfXOR = 0
    for i in range(n):
        if (i < n // 2):
            FirstHalfXOR ^= arr[i]
        else:
            SecondHalfXOR ^= arr[i]
    print(FirstHalfXOR, ",", SecondHalfXOR)


arr = [20, 30, 50, 10, 55, 15, 42]
N = len(arr)
XOROfElements(arr, N)

# Expected output:
56 , 24
# minimum volume of cone that can be circumscribed about a sphere of radius r | python3 program to find the minimum volume of the cone that can be circumscribed about a sphere of radius r ; function to find the volume of the cone ; r = radius of cone h = height of cone volume of cone = ( 1 / 3 ) * ( 3.14 ) * ( r * * 2 ) * ( h ) we get radius of cone from the derivation is root ( 2 ) times multiple of r we get height of cone from the derivation is 4 times multiple of r ; 

import math


def Volume_of_cone(R):
    V = (1 / 3) * (3.14) * (2 * (R ** 2)) * (4 * R)
    return V


if __name__ == "__main__":
    R = 10
    print(Volume_of_cone(R))

# Expected output:
8373.333333333332
# program to balance the given chemical equation | function to calculate gcd ; function to calculate b1 , b2 and b3 ; variable declaration ; temp variable to store gcd ; computing gcd ; 
def gcd(a, b):
    if (b == 0):
        return a
    return gcd(b, a % b)


def balance(x, y, p, q):
    if (p % x == 0 and q % y == 0):
        b1 = p // x
        b2 = q // y
        b3 = 1
    else:
        p = p * y
        q = q * x
        b3 = x * y
        temp = gcd(p, gcd(q, b3))
        b1 = p // temp
        b2 = q // temp
        b3 = b3 // temp
    print(b1, b2, b3)


if __name__ == '__main__':
    x = 2
    y = 3
    p = 4
    q = 5
    balance(x, y, p, q)

# Expected output:
6 5 3
# find the maximum alternate subsequence sum from a 
def maxAlternatingSum(arr, n):
    max_sum = 0
    i = 0
    while i < n:
        current_max = arr[i]
        k = i
        while k < n and (
            (arr[i] > 0 and arr[k] > 0) or (
                arr[i] < 0 and arr[k] < 0)):
            current_max = max(current_max, arr[k])
            k += 1
        max_sum += current_max
        i = k
    return max_sum


arr = [1, 2, 3, 4, -1, -2]
n = len(arr)
print(maxAlternatingSum(arr, n))

# Expected output:
3