giulio98/xlcost-single-prompt huggingface dataset:
# number of ways to erase exactly one element in the binary array to make xor zero | function to find the number of ways ; calculate the number of 1 ' s ▁ and ▁ 0' s ; considering the 4 cases ; 
def no_of_ways(a, n):
    count_0 = 0
    count_1 = 0
    for i in range(0, n):
        if (a[i] == 0):
            count_0 += 1
        else:
            count_1 += 1
    if (count_1 % 2 == 0):
        return count_0
    else:
        return count_1


if __name__ == '__main__':
    n = 4
    a1 = [1, 1, 0, 0]
    print(no_of_ways(a1, n))
    n = 5
    a2 = [1, 1, 1, 0, 0]
    print(no_of_ways(a2, n))
    n = 5
    a3 = [1, 1, 0, 0, 0]
    print(no_of_ways(a3, n))
    n = 6
    a4 = [1, 1, 1, 0, 0, 0]
    print(no_of_ways(a4, n))

# Expected output:
2
3
3
3
# minimum absolute difference between n and any power of 2 | python3 implementation of the approach ; function to return the highest power of 2 less than or equal to n ; function to return the smallest power of 2 greater than or equal to n ; function that returns the minimum absolute difference between n and any power of 2 ; 

from math import log


def prevPowerof2(n):
    p = int(log(n))
    return pow(2, p)


def nextPowerOf2(n):
    p = 1
    if (n and (n & (n - 1)) == 0):
        return n
    while (p < n):
        p <<= 1
    return p


def minDiff(n):
    low = prevPowerof2(n)
    high = nextPowerOf2(n)
    return min(n - low, high - n)


n = 6
print(minDiff(n))

# Expected output:
2
# maximum possible number with the given operation | function to return the maximum possible integer that can be obtained from the given integer after performing the given operations ; for every digit ; digits greater than or equal to 5 need not to be changed as changing them will lead to a smaller number ; the resulting integer cannot have leading zero ; 
def maxInt(string):
    string2 = ""
    for i in range(0, len(string)):
        if (string[i] < '5'):
            string2 += str((ord('9') - ord(string[i])))
        else:
            string2 += str(string[i])
    if (string2[0] == '0'):
        string2[0] = '9'
    return string2


if __name__ == '__main__':
    string = "42"
    print(maxInt(string))

# Expected output:
57
# find the ratio of number of elements in two arrays from their individual and combined average | python3 program to find the ratio of number of elements in two arrays from their individual and combined average ; function to find the ratio of number of array elements ; calculating gcd of them ; make neumarator and denominator coprime ; 

from math import gcd


def FindRatio(a, b, c):
    up = abs(b - c)
    down = abs(c - a)
    g = gcd(up, down)
    up //= g
    down //= g
    print(up, ":", down)


a = 4
b = 10
c = 6
FindRatio(a, b, c)

# Expected output:
2 : 1
# maximum distance between two 1 's in binary representation of n | python3 program to find the maximum distance between two 1 's in binary representation of n ; compute the binary representation ; if n is a power of 2 then return - 1 ; else find the distance between the first position of 1 and last position of 1 ; 
def longest_gap(N):
    distance = 0
    count = 0
    first_1 = -1
    last_1 = -1
    while (N > 0):
        count += 1
        r = N & 1
        if (r == 1):
            if first_1 == -1:
                first_1 = count
            else:
                first_1 = first_1
            last_1 = count
        N = N // 2
    if (last_1 <= first_1):
        return -1
    else:
        distance = last_1 - first_1 - 1
        return distance


N = 131
print(longest_gap(N))
N = 8
print(longest_gap(N))
N = 17
print(longest_gap(N))
N = 33
print(longest_gap(N))

# Expected output:
6
-1
3
4
# check if it is possible to move from ( 0 , 0 ) to ( x , y ) in exactly k steps | function that returns true if it is possible to move from ( 0 , 0 ) to ( x , y ) in exactly k moves ; minimum moves required ; if possible ; 
def isPossible(x, y, k):
    minMoves = abs(x) + abs(y)
    if (k >= minMoves and (k - minMoves) % 2 == 0):
        return True
    return False


x = 5
y = 8
k = 20
if (isPossible(x, y, k)):
    print("Yes")
else:
    print("No")

# Expected output:
No
# check whether n is a factorion or not | python3 implementation of the approach ; function that returns true if n is a factorion ; fact [ i ] will store i ! ; a copy of the given integer ; to store the sum of factorials of the digits of n ; get the last digit ; add the factorial of the current digit to the sum ; remove the last digit ; 

MAX = 10


def isFactorion(n):
    fact = [0] * MAX
    fact[0] = 1
    for i in range(1, MAX):
        fact[i] = i * fact[i - 1]
    org = n
    sum = 0
    while (n > 0):
        d = n % 10
        sum += fact[d]
        n = n // 10
    if (sum == org):
        return True
    return False


n = 40585
if (isFactorion(n)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# find the possible permutation of the bits of n | python3 implementation of the approach ; function that returns true if it is possible to arrange the bits of n in alternate fashion ; to store the count of 1 s in the binary representation of n ; if the number set bits and the number of unset bits is equal ; 

TOTAL_BITS = 32


def isPossible(n):
    cnt = bin(n).count('1')
    if (cnt == TOTAL_BITS // 2):
        return True
    return False


if __name__ == "__main__":
    n = 524280
    if (isPossible(n)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# check if two integer are anagrams of each other | python3 implementation of the approach ; function to update the frequency array such that freq [ i ] stores the frequency of digit i in n ; while there are digits left to process ; update the frequency of the current digit ; remove the last digit ; function that returns true if a and b are anagarams of each other ; to store the frequencies of the digits in a and b ; update the frequency of the digits in a ; update the frequency of the digits in b ; match the frequencies of the common digits ; if frequency differs for any digit then the numbers are not anagrams of each other ; 

TEN = 10


def updateFreq(n, freq):
    while (n):
        digit = n % TEN
        freq[digit] += 1
        n //= TEN


def areAnagrams(a, b):
    freqA = [0] * TEN
    freqB = [0] * TEN
    updateFreq(a, freqA)
    updateFreq(b, freqB)
    for i in range(TEN):
        if (freqA[i] != freqB[i]):
            return False
    return True


a = 240
b = 204
if (areAnagrams(a, b)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# find the permutation of first n natural numbers such that sum of i % pi is maximum possible | function to find the permutation of the first n natural numbers such that the sum of ( i % pi ) is maximum possible and return the maximum sum ; 
def Max_Sum(n):
    return (n * (n - 1)) // 2


if __name__ == "__main__":
    n = 8
    print(Max_Sum(n))

# Expected output:
28
# check if the number formed by the last digits of n numbers is divisible by 10 or not | function that returns true if the number formed by the last digits of all the elements is divisible by 10 ; last digit of the last element ; number formed will be divisible by 10 ; 
def isDivisible(arr, n):
    lastDigit = arr[n - 1] % 10
    if (lastDigit == 0):
        return True
    return False


if __name__ == "__main__":
    arr = [12, 65, 46, 37, 99]
    n = len(arr)
    if (isDivisible(arr, n)):
        print("Yes")
    else:
        print("No")

# Expected output:
No
# count of multiples of a , b or c less than or equal to n | function to return the gcd of a and b ; function to return the count of integers from the range [ 1 , num ] which are divisible by either a , b or c ; calculate the number of terms divisible by a , b and c then remove the terms which are divisible by both ( a , b ) or ( b , c ) or ( c , a ) and then add the numbers which are divisible by a , b and c ; 
def gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)


def lcm(x, y):
    return (x * y) // gcd(x, y)


def divTermCount(a, b, c, num):
    return (num // a + num // b + num // c - num // lcm(a, b) - num //
            lcm(c, b) - num // lcm(a, c) + num // (lcm(lcm(a, b), c)))


if __name__ == "__main__":
    a = 7
    b = 3
    c = 5
    n = 100
    print(divTermCount(a, b, c, n))

# Expected output:
55
# array containing power of 2 whose xor and sum of elements equals x | function to return the required array ; store the power of 2 ; while n is greater than 0 ; if there is 1 in binary representation ; divide n by 2 multiply p2 by 2 ; 
def getArray(n):
    ans = []
    p2 = 1
    while (n > 0):
        if (n & 1):
            ans .append(p2)
        n >>= 1
        p2 *= 2
    return ans


if __name__ == "__main__":
    n = 15
    ans = getArray(n)
    for i in ans:
        print(i, end=" ")

# Expected output:
1 2 4 8
# find out the correct position of the ball after shuffling | python3 implementation of the above approach ; function to generate the index of the glass containing the ball ; change the index ; change the index ; print the index ; 

M = 3
N = 2


def getIndex(n, shuffle):
    for i in range(3):
        if (shuffle[i][0] == n):
            n = shuffle[i][1]
        elif (shuffle[i][1] == n):
            n = shuffle[i][0]
    print(n)


if __name__ == "__main__":
    n = 3
    shuffle = [[3, 1], [2, 1], [1, 2]]
    getIndex(n, shuffle)

# Expected output:
1
# count the number of subsequences of length k having equal lcm and hcf | returns factorial of n ; returns ncr for the given values of r and n ; map to store the frequencies of each elements ; loop to store the frequencies of elements in the map ; using ncr formula to calculate the number of subsequences of a given length ; 
def fact(n):
    res = 1
    for i in range(2, n + 1):
        res = res * i
    return res


def nCr(n, r):
    return fact(n) // (fact(r) * fact(n - r))


def number_of_subsequences(arr, k, n):
    s = 0
    m = dict()
    for i in arr:
        m[i] = m .get(i, 0) + 1
    for j in m:
        s = s + nCr(m[j], k)
    return s


arr = [1, 1, 1, 1, 2, 2, 2]
k = 2
n = len(arr)
print(number_of_subsequences(arr, k, n))

# Expected output:
9
# find the sum of all possible pairs in an array of n elements | function to return the sum of the elements of all possible pairs from the array ; to store the required sum ; for every element of the array ; it appears ( 2 * n ) times ; 
def sumPairs(arr, n):
    sum = 0
    for i in range(n):
        sum = sum + (arr[i] * (2 * n))
    return sum


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

# Expected output:
36
# minimum sum obtained from groups of four elements from the given array | function to return the minimum required sum ; to store the required sum ; sort the array in descending order ; the indices which give 0 or 1 as the remainder when divided by 4 will be the maximum two elements of the group ; 
def minSum(arr, n):
    sum = 0
    arr .sort(reverse=True)
    for i in range(n):
        if (i % 4 < 2):
            sum += arr[i]
    return sum


if __name__ == "__main__":
    arr = [1, 1, 10, 2, 2, 2, 1]
    n = len(arr)
    print(minSum(arr, n))

# Expected output:
14
# count of triples ( a , b , c ) where a * c is greater than b * b | function to return the count of the valid triplets ; 
def countTriplets(A, B, C):
    ans = 0
    for i in range(1, A + 1):
        for j in range(1, B + 1):
            for k in range(1, C + 1):
                if (i * k > j * j):
                    ans += 1
    return ans


A = 3
B = 2
C = 2
print(countTriplets(A, B, C))

# Expected output:
6
# count of triples ( a , b , c ) where a * c is greater than b * b | counts the number of triplets for a given value of b ; count all triples in which a = i ; smallest value j such that i * j > b2 ; count all ( i , b2 , x ) such that x >= j ; count all ( x , b2 , y ) such that x >= j this counts all such triples in which a >= j ; as all triples with a >= j have been counted reduce a to j - 1. ; counts the number of triples that satisfy the given constraints ; getcount of triples in which b = i ; 
def getCount(A, B2, C):
    count = 0
    i = 1
    while (i < A):
        j = (B2 // i) + 1
        if (C >= j):
            count = count + C - j + 1
        if (A >= j and C >= i):
            count = count + (C - i + 1) * (A - j + 1)
        if (A >= j):
            A = j - 1
        i += 1
    return count


def countTriplets(A, B, C):
    ans = 0
    for i in range(1, B + 1):
        ans = (ans + getCount(A, i * i, C))
    return ans


A = 3
B = 2
C = 2
print(countTriplets(A, B, C))

# Expected output:
6
# summation of floor of harmonic progression | python3 implementation of the approach ; function to return the summation of the given harmonic series ; to store the summation ; floor of sqrt ( n ) ; summation of floor ( n / i ) ; from the formula ; 

from math import floor, sqrt, ceil


def getSum(n):
    summ = 0
    k = (n)**(.5)
    for i in range(1, floor(k) + 1):
        summ += floor(n / i)
    summ *= 2
    summ -= pow(floor(k), 2)
    return summ


n = 5
print(getSum(n))

# Expected output:
10
# count of distinct remainders when n is divided by all the numbers from the range [ 1 , n ] | function to return the count of distinct remainders that can be obtained when n is divided by every element from the range [ 1 , n ] ; if n is even ; if n is odd ; 
def distinctRemainders(n):
    if n % 2 == 0:
        return n // 2
    return ((n // 2) + 1)


if __name__ == "__main__":
    n = 5
    print(distinctRemainders(n))

# Expected output:
3
# count total unset bits in all the numbers from 1 to n | function to return the count of unset bits in the binary representation of all the numbers from 1 to n ; to store the count of unset bits ; for every integer from the range [ 1 , n ] ; a copy of the current integer ; count of unset bits in the current integer ; if current bit is unset ; 
def countUnsetBits(n):
    cnt = 0
    for i in range(1, n + 1):
        temp = i
        while (temp):
            if (temp % 2 == 0):
                cnt += 1
            temp = temp // 2
    return cnt


if __name__ == "__main__":
    n = 5
    print(countUnsetBits(n))

# Expected output:
4
# find if a degree sequence can form a simple graph | havel | function that returns true if a simple graph exists ; keep performing the operations until one of the stopping condition is met ; sort the list in non - decreasing order ; check if all the elements are equal to 0 ; store the first element in a variable and delete it from the list ; check if enough elements are present in the list ; subtract first element from next v elements ; check if negative element is encountered after subtraction ; 
def graphExists(a):
    while True:
        a = sorted(a, reverse=True)
        if a[0] == 0 and a[len(a) - 1] == 0:
            return True
        v = a[0]
        a = a[1:]
        if v > len(a):
            return False
        for i in range(v):
            a[i] -= 1
            if a[i] < 0:
                return False


a = [3, 3, 3, 3]
if (graphExists(a)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# number of sub arrays with negative product | function to return the count of subarrays with negative product ; replace current element with 1 if it is positive else replace it with - 1 instead ; take product with previous element to form the prefix product ; count positive and negative elements in the prefix product array ; return the required count of subarrays ; 
def negProdSubArr(arr, n):
    positive = 1
    negative = 0
    for i in range(n):
        if (arr[i] > 0):
            arr[i] = 1
        else:
            arr[i] = -1
        if (i > 0):
            arr[i] *= arr[i - 1]
        if (arr[i] == 1):
            positive += 1
        else:
            negative += 1
    return (positive * negative)


arr = [5, -4, -3, 2, -5]
n = len(arr)
print(negProdSubArr(arr, n))

# Expected output:
8
# repeated sum of first n natural numbers | function to return the sum of the first n natural numbers ; function to return the repeated sum ; perform the operation exactly k times ; update n with the sum of first n natural numbers ; 
def sum(n):
    sum = (n * (n + 1)) // 2
    return sum


def repeatedSum(n, k):
    for i in range(k):
        n = sum(n)
    return n


n = 2
k = 2
print(repeatedSum(n, k))

# Expected output:
6
# difference between sum of cubes and sum of first n natural numbers | python3 program to find the difference between the sum of the cubes of the first n natural numbers and the sum of the first n natural number ; sum of first n natural numbers ; find the required difference ; 
def difference(n):
    S = (n * (n + 1)) // 2
    res = S * (S - 1)
    return res


if __name__ == "__main__":
    n = 5
    print(difference(n))

# Expected output:
210
# check if the sum of digits of number is divisible by all of its digits | function that returns true if all the digits of n divide the sum of the digits of n ; store a copy of the original number ; find the sum of the digits of n ; restore the original value ; check if all the digits divide the calculated sum ; if current digit doesn 't  divide the sum ; 
def isDivisible(n):
    temp = n
    sum = 0
    while (n):
        digit = n % 10
        sum += digit
        n //= 10
    n = temp
    while (n):
        digit = n % 10
        if (sum % digit != 0):
            return False
        n //= 10
    return True


n = 123
if (isDivisible(n)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# check if the sum of digits of number is divisible by all of its digits | python implementation of above approach ; converting integer to string ; initialising sum to 0 ; traversing through the string ; converting character to int ; comparing number and sum traversing again ; check if any digit is not dividing the sum ; return false ; if any value is not returned then all the digits are dividing the sum so return true ; 
def getResult(n):
    st = str(n)
    sum = 0
    length = len(st)
    for i in st:
        sum = sum + int(i)
    for i in st:
        if (sum % int(i) != 0):
            return 'No'
    return 'Yes'


n = 123
print(getResult(n))

# Expected output:
Yes
# make the list non | python3 implementation of the approach ; function to return the minimum element from the range [ prev , max ] such that it differs in at most 1 digit with cur ; to start with the value we have achieved in the last step ; store the value with which the current will be compared ; current value ; there are at most 4 digits ; if the current digit differs in both the numbers ; eliminate last digits in both the numbers ; if the number of different digits is at most 1 ; if we can 't find any number for which  the number of change is less than or  equal to 1 then return -1 ; function to get the non - decreasing list ; creating a vector for the updated list ; let 's assume that it is possible to  make the list non-decreasing ; element of the original array ; can 't make the list non-decreasing ; if possible then print the list ; 

DIGITS = 4
MIN = 1000
MAX = 9999


def getBest(prev, cur):
    maximum = max(MIN, prev)
    for i in range(maximum, MAX + 1):
        cnt = 0
        a = i
        b = cur
        for k in range(DIGITS):
            if (a % 10 != b % 10):
                cnt += 1
            a //= 10
            b //= 10
        if (cnt <= 1):
            return i
    return -1


def getList(arr, n):
    myList = []
    possible = True
    myList .append(0)
    for i in range(n):
        cur = arr[i]
        myList .append(getBest(myList[-1], cur))
        if (myList[-1] == -1):
            possible = False
            break
    if (possible):
        for i in range(1, len(myList)):
            print(myList[i], end=" ")
    else:
        print("-1")


if __name__ == "__main__":
    arr = [1095, 1094, 1095]
    n = len(arr)
    getList(arr, n)

# Expected output:
1005 1014 1015
# maximum items that can be bought with the given type of coins | python3 implementation of the approach ; function to find maximum fruits can buy from given values of x , y , z . ; items of type 1 that can be bought ; update the coins ; items of type 2 that can be bought ; update the coins ; items of type 3 that can be bought ; update the coins ; items of type 4 that can be bought to buy a type 4 item , a coin of each type is required ; total items that can be bought ; 

COST = 3


def maxItems(x, y, z):
    type1 = x // COST
    x %= COST
    type2 = y // COST
    y %= COST
    type3 = z // COST
    z %= COST
    type4 = min(x, min(y, z))
    maxItems = type1 + type2 + type3 + type4
    return maxItems


if __name__ == "__main__":
    x = 4
    y = 5
    z = 6
    print(maxItems(x, y, z))

# Expected output:
4
# count occurrences of a prime number in the prime factorization of every element from the given range | function to return the highest power of p that divides n ; function to return the count of times p appears in the prime factors of the elements from the range [ l , r ] ; to store the required count ; for every element of the range ; add the highest power of p that divides i ; 
def countFactors(n, p):
    pwr = 0
    while (n > 0 and n % p == 0):
        n //= p
        pwr += 1
    return pwr


def getCount(l, r, p):
    cnt = 0
    for i in range(l, r + 1):
        cnt += countFactors(i, p)
    return cnt


if __name__ == "__main__":
    l = 2
    r = 8
    p = 2
    print(getCount(l, r, p))

# Expected output:
7
# count occurrences of a prime number in the prime factorization of every element from the given range | function to return the count of times p appears in the prime factors of the elements from the range [ l , r ] ; to store the required count ; number of values in the range [ 0 , r ] that are divisible by val ; number of values in the range [ 0 , l - 1 ] that are divisible by val ; increment the power of the val ; ( a - b ) is the count of numbers in the range [ l , r ] that are divisible by val ; no values that are divisible by val thus exiting from the loop ; 
def getCount(l, r, p):
    cnt = 0
    val = p
    while (True):
        a = r // val
        b = (l - 1) // val
        val *= p
        if (a - b):
            cnt += (a - b)
        else:
            break
    return int(cnt)


l = 2
r = 8
p = 2
print(getCount(l, r, p))

# Expected output:
7
# check if the number is valid when flipped upside down | function that returns true if str is topsy turvy ; for every character of the string ; if the current digit cannot form a valid digit when turned upside - down ; 
def topsyTurvy(string):
    for i in range(len(string)):
        if (string[i] == '2' or string[i] == '4' or string[i] ==
                '5' or string[i] == '6' or string[i] == '7' or string[i] == '9'):
            return False
    return True


if __name__ == "__main__":
    string = "1234"
    if (topsyTurvy(string)):
        print("Yes")
    else:
        print("No")

# Expected output:
No
# find the count of subsequences where each element is divisible by k | function to return the count of all valid subsequences ; to store the count of elements which are divisible by k ; if current element is divisible by k then increment the count ; total ( 2 ^ n - 1 ) non - empty subsequences are possible with n element ; 
def countSubSeq(arr, n, k):
    count = 0
    for i in range(n):
        if (arr[i] % k == 0):
            count += 1
    return (2 ** count - 1)


if __name__ == "__main__":
    arr = [1, 2, 3, 6]
    n = len(arr)
    k = 3
    print(countSubSeq(arr, n, k))

# Expected output:
3
# count of numbers below n whose sum of prime divisors is k | python3 implementation of the approach ; function to return the count of numbers below n whose sum of prime factors is k ; to store the sum of prime factors for all the numbers ; if i is prime ; add i to all the numbers which are divisible by i ; to store the count of required numbers ; return the required count ; 

MAX = 1000001


def countNum(N, K):
    sumPF = [0] * MAX
    for i in range(2, N):
        if (sumPF[i] == 0):
            for j in range(i, N, i):
                sumPF[j] += i
    count = 0
    for i in range(2, N):
        if (sumPF[i] == K):
            count += 1
    return count


if __name__ == "__main__":
    N = 20
    K = 7
    print(countNum(N, K))

# Expected output:
2