giulio98/xlcost-single-prompt huggingface dataset:
# sum of bitwise xor of each array element with all other array elements | function to calculate for each array element , sum of its bitwise xor with all other array elements ; declare an array of size 64 to store count of each bit ; traversing the array ; check if bit is present of not ; increase the bit position ; reduce the number to half ; traverse the array ; stores the bit position ; stores the sum of bitwise xor ; check if bit is present of not ; reduce the number to its half ; print the sum for a [ i ] ; 
def XOR_for_every_i(A, N):
    frequency_of_bits = [0] * 32
    for i in range(N):
        bit_position = 0
        M = A[i]
        while (M):
            if (M & 1 != 0):
                frequency_of_bits[bit_position] += 1
            bit_position += 1
            M >>= 1
    for i in range(N):
        M = A[i]
        value_at_that_bit = 1
        XOR_sum = 0
        for bit_position in range(32):
            if (M & 1 != 0):
                XOR_sum += ((N -
                             frequency_of_bits[bit_position]) *
                            value_at_that_bit)
            else:
                XOR_sum += ((frequency_of_bits[bit_position])
                            * value_at_that_bit)
            M >>= 1
            value_at_that_bit <<= 1
        print(XOR_sum, end=" ")
    return


A = [1, 2, 3]
N = len(A)
XOR_for_every_i(A, N)

# Expected output:
5 4 3
# minimize the maximum difference of any pair by doubling odd elements and reducing even elements by half | function to minimize the maximum difference between any pair of elements of the array by the given operations ; traverse the array ; if current element is even ; insert it into even ; otherwise ; make it even by multiplying by 2 and insert it into set ; calculate difference between first and the last element of the set ; iterate until difference is minimized ; erase the current element ; reduce current element by half and insert it into the set ; update difference ; return the resultant difference ; 
def minimumMaxDiff(nums):
    s = {}
    for i in range(len(nums)):
        if (nums[i] % 2 == 0):
            s[nums[i]] = 1
        else:
            s[nums[i] * 2] = 1
    sr = list(s .keys())
    res = sr[-1] - sr[0]
    while (list(s .keys())[-1] % 2 == 0):
        r = list(s .keys())
        x = r[-1]
        del s[x]
        s[x // 2] = 1
        rr = list(s .keys())
        res = min(res, rr[-1] - r[0])
    return res


if __name__ == '__main__':
    arr = [1, 2, 5, 9]
    print(minimumMaxDiff(arr))

# Expected output:
7
# xor of all even numbers from a given range | function to calculate xor of numbers in the range [ 1 , n ] ; if n is divisible by 4 ; if n mod 4 is equal to 1 ; if n mod 4 is equal to 2 ; function to find xor of even numbers in the range [ l , r ] ; update xor_r ; update xor_l ; 
def bitwiseXorRange(n):
    if (n % 4 == 0):
        return n
    if (n % 4 == 1):
        return 1
    if (n % 4 == 2):
        return n + 1
    return 0


def evenXorRange(l, r):
    xor_r = 2 * bitwiseXorRange(r // 2)
    xor_l = 2 * bitwiseXorRange((l - 1) // 2)
    return xor_l ^ xor_r


if __name__ == '__main__':
    l = 10
    r = 20
    print(evenXorRange(l, r))

# Expected output:
30
# program to calculate variance of first n natural numbers | function to calculate variance of first n natural numbers ; 
def find_Variance(n):
    numerator = n * n - 1
    ans = (numerator * 1.0) / 12
    return ans


if __name__ == '__main__':
    N = 5
    a = find_Variance(N)
    print("{0:.6f}".format(a))

# Expected output:
2.000000
# count digits present in each element of a given matrix | python3 program for the above approach ; function to count the number of digits in each element of the given matrix ; traverse each row of arr [ ] [ ] ; traverse each column of arr [ ] [ ] ; store the current matrix element ; count the number of digits ; print the result ; 

from math import floor, log10
M = 3
N = 3


def countDigit(arr):
    for i in range(M):
        for j in range(N):
            X = arr[i][j]
            d = floor(log10(X) * 1.0) + 1
            print(d, end=" ")
        print()


if __name__ == '__main__':
    arr = [[27, 173, 5], [21, 6, 624], [5, 321, 49]]
    countDigit(arr)

# Expected output:
2 3 1 
2 1 3 
1 3 2
# difference between sum of odd and even frequent elements in an array | function to find the sum of all even and odd frequent elements in an array ; stores the frequency of array elements ; traverse the array ; update frequency of current element ; stores sum of odd and even frequent elements ; traverse the map ; if frequency is odd ; add sum of all occurrences of current element to sum_odd ; if frequency is even ; add sum of all occurrences of current element to sum_even ; calculate difference between their sum ; return diff ; 
def findSum(arr, N):
    mp = {}
    for i in range(0, N):
        if arr[i] in mp:
            mp[arr[i]] += 1
        else:
            mp[arr[i]] = 1
    sum_odd, sum_even = 0, 0
    for itr in mp:
        if (mp[itr] % 2 != 0):
            sum_odd += (itr) * (mp[itr])
        if (mp[itr] % 2 == 0):
            sum_even += (itr) * (mp[itr])
    diff = sum_even - sum_odd
    return diff


arr = [1, 5, 5, 2, 4, 3, 3]
N = len(arr)
print(findSum(arr, N))

# Expected output:
9
# count n | function to find the number of arrays following the given condition ; initialize answer ; calculate npm ; print ans ; 
def noOfArraysPossible(N, M):
    ans = 1
    for i in range(N):
        ans = ans * (M - i)
    print(ans)


if __name__ == "__main__":
    N = 2
    M = 3
    noOfArraysPossible(N, M)

# Expected output:
6
# permutations of an array having sum of bitwise and of adjacent elements at least k | python3 program for the above approach ; function to print all permutations of arr [ ] such that the sum of bitwise and of all adjacent element is at least k ; to check if any permutation exists or not ; sort the given array ; find all possible permutations ; stores the sum of bitwise and of adjacent elements of the current permutation ; traverse the current permutation of arr [ ] ; update the sum ; if the sum is at least k , then print the current permutation ; set the flag variable ; print the current permutation ; if flag is unset , then print - 1 ; 
def next_permutation(a):
    for i in reversed(range(len(a) - 1)):
        if a[i] < a[i + 1]:
            break
    else:
        return False
    j = next(j for j in reversed(range(i + 1, len(a)))if a[i] < a[j])
    a[i], a[j] = a[j], a[i]
    a[i + 1:] = reversed(a[i + 1:])
    return True


def printPermutation(arr, n, k):
    flag = False
    arr .sort()
    while True:
        sum = 0
        for i in range(n - 1):
            sum += arr[i] & arr[i + 1]
        if (sum >= k):
            flag = True
            for i in range(n):
                print(arr[i], end=" ")
            print()
        if (next_permutation(arr) == False):
            break
    if (flag == False):
        print("-1")


arr = [1, 2, 3, 4, 5]
K = 8
N = len(arr)
printPermutation(arr, N, K)

# Expected output:
2 3 1 5 4 
4 5 1 3 2
# positive integers up to n that are not present in given array | function to find positive integers from 1 to n that are not present in the array ; declare bitset ; iterate from 0 to m - 1 ; iterate from 0 to n - 1 ; iterate from bset . _find_first ( ) to bset . size ( ) - 1 ; 
def findMissingNumbers(arr, n):
    M = 15
    bset = [0] * M
    for i in range(M):
        bset[i] = i
    for i in range(n):
        bset[arr[i] - 1] = 0
    bset = [i for i in bset if i != 0]
    for i in range(len(bset)):
        if (bset[i] + 1 > n):
            break
        print(bset[i] + 1)


if __name__ == "__main__":
    arr = [1, 2, 4, 6, 8, 9]
    n = len(arr)
    findMissingNumbers(arr, n)

# Expected output:
3
5
# sum of the first n terms of xor fibonacci series | function to calculate the sum of the first n terms of xor fibonacci series ; base case ; stores the sum of the first n terms ; iterate from [ 0 , n - 3 ] ; store xor of last 2 elements ; update sum ; update the first element ; update the second element ; print the final sum ; 
def findSum(a, b, N):
    if N == 1:
        print(a)
        return
    s = a + b
    for i in range(0, N - 2):
        x = a ^ b
        s += x
        a = b
        b = x
    print(s)
    return


if __name__ == '__main__':
    a = 2
    b = 5
    N = 8
    findSum(a, b, N)

# Expected output:
35
# sum of the first n terms of xor fibonacci series | function to calculate sum of the first n terms of xor fibonacci series ; store the sum of first n terms ; store xor of a and b ; case 1 : if n is divisible by 3 ; case 2 : if n % 3 leaves remainder 1 ; case 3 : if n % 3 leaves remainder 2 on division by 3 ; print the final sum ; 
def findSum(a, b, N):
    sum = 0
    x = a ^ b
    if N % 3 == 0:
        sum = (N // 3) * (a + b + x)
    elif N % 3 == 1:
        sum = (N // 3) * (a + b + x) + a
    else:
        sum = (N // 3) * (a + b + x) + a + b
    print(sum)
    return


if __name__ == '__main__':
    a = 2
    b = 5
    N = 8
    findSum(a, b, N)

# Expected output:
35
# length of the longest subarray whose bitwise xor is k | function to find the length of the longest subarray whose bitwise xor is equal to k ; stores prefix xor of the array ; stores length of longest subarray having bitwise xor equal to k ; stores index of prefix xor of the array ; insert 0 into the map ; traverse the array ; update prefixxor ; if ( prefixxor ^ k ) present in the map ; update maxlen ; if prefixxor not present in the map ; insert prefixxor into the map ; 
def LongestLenXORK(arr, N, K):
    prefixXOR = 0
    maxLen = 0
    mp = {}
    mp[0] = -1
    for i in range(N):
        prefixXOR ^= arr[i]
        if (prefixXOR ^ K) in mp:
            maxLen = max(maxLen, (i - mp[prefixXOR ^ K]))
        else:
            mp[prefixXOR] = i
    return maxLen


if __name__ == "__main__":
    arr = [1, 2, 4, 7, 2]
    N = len(arr)
    K = 1
    print(LongestLenXORK(arr, N, K))

# Expected output:
3
# two dimensional difference array | python3 program to implement the above approach ; function to initialize the difference array ; function to add k to the specified submatrix ( r1 , c1 ) to ( r2 , c2 ) ; function to print the modified array ; function to perform the given queries ; difference array ; function to initialize the difference array ; count of queries ; perform queries ; 

N = 3
M = 3


def intializeDiff(D, A):
    for i in range(N):
        D[i][0] = A[i][0]
        D[i][M] = 0
    for i in range(N):
        for j in range(1, M):
            D[i][j] = A[i][j] - A[i][j - 1]


def update(D, k, r1, c1, r2, c2):
    for i in range(r1, r2 + 1):
        D[i][c1] += k
        D[i][c2 + 1] -= k


def printArray(A, D):
    for i in range(N):
        for j in range(M):
            if (j == 0):
                A[i][j] = D[i][j]
            else:
                A[i][j] = D[i][j] + A[i][j - 1]
            print(A[i][j], end=' ')
        print()


def performQueries(A, Queries):
    D = [[0 for j in range(M + 1)]for i in range(N)]
    intializeDiff(D, A)
    Q = len(Queries)
    for i in range(Q):
        update(
            D,
            Queries[i][0],
            Queries[i][1],
            Queries[i][2],
            Queries[i][3],
            Queries[i][4])
    printArray(A, D)


if __name__ == '__main__':
    A = [[1, 2, 3], [1, 1, 0], [4, -2, 2]]
    Queries = [[2, 0, 0, 1, 1], [-1, 1, 0, 2, 2]]
    performQueries(A, Queries)

# Expected output:
3 4 3 
2 2 -1 
3 -3 1
# minimum value to be added to maximize bitwise xor of the given array | python program for the above approach ; function to find complement of an integer ; count the number of bits of maxelement ; return 1 's complement ; function to find the value required to be added to maximize xor of the given array ; stores the required value to be added to the array ; stores the maximum array element ; traverse the array ; update xor of the array ; find maximum element in array ; calculate 1 s ' complement ; return the answer ; 

import math


def onesComplement(n, maxElement):
    bits = math .floor(math .log2(maxElement)) + 1
    return ((1 << bits) - 1) ^ n


def findNumber(arr, n):
    res = 0
    maxElement = 0
    for i in range(n):
        res = res ^ arr[i]
        if (maxElement < arr[i]):
            maxElement = arr[i]
    res = onesComplement(res, maxElement)
    return (res)


arr = [1, 2, 3, 4, 5]
N = len(arr)
print(findNumber(arr, N))

# Expected output:
6
# count ways to select k array elements lying in a given range | function to calculate factorial of all the numbers up to n ; factorial of 0 is 1 ; calculate factorial of all the numbers upto n ; calculate factorial of i ; function to find count of ways to select at least k elements whose values in range [ l , r ] ; stores count of ways to select at leas k elements whose values in range [ l , r ] ; stores count of numbers having value lies in the range [ l , r ] ; traverse the array ; check if the array elements lie in the given range ; update cntnum ; stores factorial of numbers upto n ; calculate total ways to select at least k elements whose values lies in [ l , r ] ; update cntways ; 
def calculateFactorial(N):
    fact = [0] * (N + 1)
    fact[0] = 1
    for i in range(1, N + 1):
        fact[i] = fact[i - 1] * i
    return fact


def cntWaysSelection(arr, N, K, L, R):
    cntWays = 0
    cntNum = 0
    for i in range(0, N):
        if (arr[i] >= L and arr[i] <= R):
            cntNum += 1
    fact = list(calculateFactorial(cntNum))
    for i in range(K, cntNum + 1):
        cntWays += fact[cntNum] // (fact[i] * fact[cntNum - i])
    return cntWays


if __name__ == "__main__":
    arr = [12, 4, 6, 13, 5, 10]
    N = len(arr)
    K = 3
    L = 4
    R = 10
    print(cntWaysSelection(arr, N, K, L, R))

# Expected output:
5
# bitwise and of all unordered pairs from a given array | function to calculate bitwise and of all pairs from the given array ; stores bitwise and of all possible pairs ; generate all possible pairs ; calculate bitwise and of each pair ; 
def TotalAndPair(arr, N):
    totalAND = (1 << 30) - 1
    for i in range(N):
        for j in range(i + 1, N):
            totalAND &= (arr[i] & arr[j])
    return totalAND


if __name__ == '__main__':
    arr = [4, 5, 12, 15]
    N = len(arr)
    print(TotalAndPair(arr, N))

# Expected output:
4
# modify n by adding its smallest positive divisor exactly k times | python 3 program to implement the above approach ; function to find the smallest divisor of n greater than 1 ; if i is a divisor of n ; if n is a prime number ; function to find the value of n by performing the operations k times ; iterate over the range [ 1 , k ] ; update n ; 

import math


def smallestDivisorGr1(N):
    for i in range(2, int(math .sqrt(N)) + 1):
        if (N % i == 0):
            return i
    return N


def findValOfNWithOperat(N, K):
    for i in range(1, K + 1):
        N += smallestDivisorGr1(N)
    return N


if __name__ == "__main__":
    N = 6
    K = 4
    print(findValOfNWithOperat(N, K))

# Expected output:
14
# sum of all possible triplet products from given ranges | python3 program to implement the above approach ; function to find the sum of all possible triplet products ( i * j * k ) ; stores sum required sum ; iterate over all possible values of i ; iterate over all possible values of j ; iterate over all possible values of k ; stores the product of ( i * j * k ) ; update sum ; 

M = 1000000007


def findTripleSum(A, B, C):
    sum = 0
    for i in range(1, A + 1):
        for j in range(1, B + 1):
            for k in range(1, C + 1):
                prod = (((i % M) * (j % M)) % M * (k % M)) % M
                sum = (sum + prod) % M
    return sum


if __name__ == '__main__':
    A = 10
    B = 100
    C = 1000
    print(findTripleSum(A, B, C))

# Expected output:
13874027
# sum of all possible triplet products from given ranges | python3 implementation to implement the above approach ; function to find the value of power ( x , n ) % m ; stores the value of ( x ^ n ) % m ; calculate the value of power ( x , n ) % m ; if n is odd ; update res ; update x ; update n ; function to find modulo multiplicative inverse of x under modulo m ; function to find the sum of all possible triplet products ( i * j * k ) ; stores modulo multiplicative inverse of 8 ; stores the sum of all possible values of ( i * j * k ) ; update res ; 

M = 1000000007


def power(x, N):
    global M
    res = 1
    while (N > 0):
        if (N & 1):
            res = (res * x) % M
        x = (x * x) % M
        N = N >> 1
    return res


def modinv(X):
    return power(X, M - 2)


def findTripleSum(A, B, C):
    global M
    MMI = modinv(8)
    res = 0
    res = ((((A % M * (A + 1) % M) % M * (B % M * (B + 1) % M) % M) %
           M * (C % M * (C + 1) % M) % M) % M * MMI) % M
    return res


if __name__ == '__main__':
    A = 10
    B = 100
    C = 1000
    print(findTripleSum(A, B, C))

# Expected output:
13874027
# maximize minimum of array generated by maximums of same indexed elements of two rows of a given matrix | python3 program for the above approach ; function to find the maximum of minimum of array constructed from any two rows of the given matrix ; initialize global max as int_min ; iterate through the rows ; iterate through remaining rows ; initialize row_min as int_max ; iterate through the column values of two rows ; find max of two elements ; update the row_min ; update the global_max ; print the global max ; 

import sys


def getMaximum(N, M, mat):
    global_max = -1 * (sys .maxsize)
    for i in range(0, N):
        for j in range(i + 1, N):
            row_min = sys .maxsize
            for k in range(0, M):
                m = max(mat[i][k], mat[j][k])
                row_min = min(row_min, m)
            global_max = max(global_max, row_min)
    return global_max


if __name__ == '__main__':
    mat = [[5, 0, 3, 1, 2], [1, 8, 9, 1, 3], [1, 2, 3, 4, 5],
           [9, 1, 0, 3, 7], [2, 3, 0, 6, 3], [6, 4, 1, 7, 0]]
    N = 6
    M = 5
    print(getMaximum(N, M, mat))

# Expected output:
3
# minimum value exceeding x whose count of divisors has different parity with count of divisors of x | function to count divisors of n ; function to find the minimum value exceeding x whose count of divisors has different parity with count of divisors of x ; divisor count of x ; iterate from x + 1 and check for each element ; 
def divisorCount(n):
    x = 0
    for i in range(1, n):
        if (n % i == 0):
            if (i == n // i):
                x += 1
            else:
                x += 2
        if (i * i > n):
            break
    return x


def minvalue_y(x):
    a = divisorCount(x)
    y = x + 1
    while ((a & 1) == (divisorCount(y) & 1)):
        y += 1
    return y


if __name__ == '__main__':
    x = 5
    print(minvalue_y(x))

# Expected output:
9
# minimum value exceeding x whose count of divisors has different parity with count of divisors of x | function to find the minimum value exceeding x whose count of divisors has different parity with count of divisors of x ; check if x is perfect square ; 
def minvalue_y(x):
    n = int(pow(x, 1 / 2))
    if (n * n == x):
        return x + 1
    return (pow(n + 1, 2))


if __name__ == '__main__':
    x = 5
    print(minvalue_y(x))

# Expected output:
9
# sum of first n natural numbers with alternate signs | function to find the sum of first n natural numbers with alternate signs ; stores sum of alternate sign of first n natural numbers ; if n is an even number ; update alternatesum ; if n is an odd number ; update alternatesum ; 
def alternatingSumOfFirst_N(N):
    alternateSum = 0
    if (N % 2 == 0):
        alternateSum = (-N) // 2
    else:
        alternateSum = (N + 1) // 2
    return alternateSum


if __name__ == '__main__':
    N = 6
    print(alternatingSumOfFirst_N(N))

# Expected output:
-3
# minimum value to be added to the prefix sums at each array indices to make them positive | function to find minimum startvalue for positive prefix sum at each index ; store the minimum prefix sum ; stores prefix sum at each index ; traverse over the array ; update the prefix sum ; update the minvalue ; return the positive start value ; 
def minStartValue(nums):
    minValue = 0
    sum = 0
    for i in range(len(nums)):
        sum += nums[i]
        minValue = min(minValue, sum)
    startValue = 1 - minValue
    return startValue


if __name__ == '__main__':
    nums = [-3, 2, -3, 4, 2]
    print(minStartValue(nums))

# Expected output:
5
# count ways to split array into two equal sum subarrays by replacing each array element to 0 once | function to find number of ways to split array into 2 subarrays having equal sum by changing element to 0 once ; stores the count of elements in prefix and suffix of array elements ; stores the sum of array ; traverse the array ; increase the frequency of current element in suffix ; stores prefix sum upto index i ; stores sum of suffix of index i ; stores the desired result ; traverse the array ; modify prefix sum ; add arr [ i ] to prefix map ; calculate suffix sum by subtracting prefix sum from total sum of elements ; remove arr [ i ] from suffix map ; store the difference between the subarrays ; count number of ways to split the array at index i such that subarray sums are equal ; update the final result ; return the result ; 
def countSubArrayRemove(arr, N):
    prefix_element_count = {}
    suffix_element_count = {}
    total_sum_of_elements = 0
    i = N - 1
    while (i >= 0):
        total_sum_of_elements += arr[i]
        suffix_element_count[arr[i]] = suffix_element_count .get(arr[i], 0) + 1
        i -= 1
    prefix_sum = 0
    suffix_sum = 0
    count_subarray_equal_sum = 0
    for i in range(N):
        prefix_sum += arr[i]
        prefix_element_count[arr[i]] = prefix_element_count .get(arr[i], 0) + 1
        suffix_sum = total_sum_of_elements - prefix_sum
        suffix_element_count[arr[i]] = suffix_element_count .get(arr[i], 0) - 1
        difference = prefix_sum - suffix_sum
        number_of_subarray_at_i_split = (prefix_element_count .get(
            difference, 0) + suffix_element_count .get(-difference, 0))
        count_subarray_equal_sum += number_of_subarray_at_i_split
    return count_subarray_equal_sum


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

# Expected output:
6
# count set bits in bitwise xor of all adjacent elements upto n | function to count of set bits in bitwise xor of adjacent elements up to n ; stores count of set bits by bitwise xor on adjacent elements of [ 0 , n ] ; stores all possible values on right most set bit over [ 0 , n ] ; iterate over the range [ 0 , n ] ; update n ; update bit_position ; 
def countXORSetBitsAdjElemRange1_N(N):
    total_set_bits = 0
    bit_Position = 1
    while (N):
        total_set_bits += ((N + 1) // 2 * bit_Position)
        N -= (N + 1) // 2
        bit_Position += 1
    return total_set_bits


if __name__ == '__main__':
    N = 4
    print(countXORSetBitsAdjElemRange1_N(N))

# Expected output:
7
# check if a right | python3 program to implement the above approach ; function to check if n is a perfect square number or not ; if n is a non positive integer ; stores square root of n ; check for perfect square ; if n is not a perfect square number ; function to check if given two sides of a triangle forms a right - angled triangle ; if the value of ( a * a + b * b ) is a perfect square number ; update checktriangle ; if the value of ( a * a - b * b ) is a perfect square number ; update checktriangle ; if the value of ( b * b - a * a ) is a perfect square number ; update checktriangle ; 

from math import sqrt, floor, ceil


def checkPerfectSquare(N):
    if (N <= 0):
        return 0
    sq = sqrt(N)
    if (floor(sq) == ceil(sq)):
        return 1
    return 0


def checktwoSidesareRighTriangle(A, B):
    checkTriangle = False
    if (checkPerfectSquare(A * A + B * B)):
        checkTriangle = True
    if (checkPerfectSquare(A * A - B * B)):
        checkTriangle = True
    if (checkPerfectSquare(B * B - A * A)):
        checkTriangle = True
    return checkTriangle


if __name__ == '__main__':
    A = 3
    B = 4
    if (checktwoSidesareRighTriangle(A, B)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes