giulio98/xlcost-single-prompt huggingface dataset:
# find the minimum number of elements that should be removed to make an array good | function to remove minimum elements to make the given array good ; to store count of each subsequence ; increase the count of subsequence [ 0 ] ; if previous element subsequence count is greater than zero then increment subsequence count of current element and decrement subsequence count of the previous element . ; return the required answer ; 
def MinRemove(a, n, k):
    cnt = [0] * k
    for i in range(n):
        if (a[i] == 0):
            cnt[0] += 1
        elif (cnt[a[i] - 1] > 0):
            cnt[a[i] - 1] -= 1
            cnt[a[i]] += 1
    return n - (k * cnt[k - 1])


if __name__ == "__main__":
    a = [0, 1, 2, 3, 4, 0, 1, 0, 1, 2, 3, 4]
    k = 5
    n = len(a)
    print(MinRemove(a, n, k))

# Expected output:
2
# print first n mosaic numbers | function to return the nth mosaic number ; iterate from 2 to the number ; if i is the factor of n ; find the count where i ^ count is a factor of n ; divide the number by i ; increase the count ; multiply the answer with count and i ; return the answer ; function to print first n mosaic numbers ; 
def mosaic(n):
    ans = 1
    for i in range(2, n + 1):
        if (n % i == 0 and n > 0):
            count = 0
            while (n % i == 0):
                n = n // i
                count += 1
            ans *= count * i
    return ans


def nMosaicNumbers(n):
    for i in range(1, n + 1):
        print(mosaic(i),)


n = 10
nMosaicNumbers(n)

# Expected output:
1
2
3
4
5
6
7
6
6
10
# count of nodes which are at a distance x from root and leaves | python3 implementation of the approach ; function to find the count of the required nodes ; height of the complete binary tree with n nodes ; if x > height then no node can be present at that level ; corner case ; maximum total nodes that are possible in complete binary tree with height h ; nodes at the last level ; to store the count of nodes x dist away from root ; to store the count of nodes x dist away from leaf ; if x = h then prnodes at last level else nodes at xth level ; 2 ^ x ; number of left leaf nodes at ( h - 1 ) th level observe that if nodes are not present at last level then there are a / 2 leaf nodes at ( h - 1 ) th level ; if x = h then prleaf nodes at the last h level + leaf nodes at ( h - 1 ) th level ; first calculate nodes for leaves present at height h ; then calculate nodes for leaves present at height h - 1 ; add both the resuls ; 

from math import log2, ceil, floor


def countNodes(N, X):
    height = floor(log2(N))
    if (X > height):
        print("00")
        return
    if (N == 1):
        print("11")
        return
    max_total_nodes = (1 << (height + 1)) - 1
    nodes_last_level = (1 << height) - (max_total_nodes - N)
    from_root = 0
    from_leaf = 0
    if (X == height):
        from_root = nodes_last_level
    else:
        from_root = 1 << X
    left_leaf_nodes = ((1 << height) - nodes_last_level) // 2
    if (X == 0):
        from_leaf = nodes_last_level + left_leaf_nodes
    else:
        i = X
        while (nodes_last_level > 1 and i > 0):
            nodes_last_level = ceil(nodes_last_level / 2)
            i -= 1
        from_leaf = nodes_last_level
        i = X
        while (left_leaf_nodes > 1 and i > 0):
            left_leaf_nodes = ceil(left_leaf_nodes / 2)
            i -= 1
        from_leaf += left_leaf_nodes
    print(from_root)
    print(from_leaf)


if __name__ == '__main__':
    N, X = 38, 3
    countNodes(N, X)

# Expected output:
8
3
# number of ways of writing n as a sum of 4 squares | number of ways of writing n as a sum of 4 squares ; sum of odd and even factor ; iterate from 1 to the number ; if i is the factor of n ; if factor is even ; if factor is odd ; n / i is also a factor ; if factor is even ; if factor is odd ; if n is odd ; if n is even ; 
def sum_of_4_squares(n):
    i, odd, even = 0, 0, 0
    for i in range(1, int(n ** (.5)) + 1):
        if (n % i == 0):
            if (i % 2 == 0):
                even += i
            else:
                odd += i
            if ((n // i) != i):
                if ((n // i) % 2 == 0):
                    even += (n // i)
                else:
                    odd += (n // i)
    if (n % 2 == 1):
        return 8 * (odd + even)
    else:
        return 24 * (odd)


n = 4
print(sum_of_4_squares(n))

# Expected output:
24
# find the nth mosaic number | function to return the nth mosaic number ; iterate from 2 to the number ; if i is the factor of n ; find the count where i ^ count is a factor of n ; divide the number by i ; increase the count ; multiply the answer with count and i ; return the answer ; 
def mosaic(n):
    i = 0
    ans = 1
    for i in range(2, n + 1):
        if (n % i == 0 and n > 0):
            count = 0
            while (n % i == 0):
                n //= i
                count += 1
            ans *= count * i
    return ans


n = 36
print(mosaic(n))

# Expected output:
24
# seating arrangement of n boys sitting around a round table such that two particular boys sit together | function to return the total count of ways ; find ( n - 1 ) factorial ; return ( n - 1 ) ! * 2 ! ; 
def Total_Ways(n):
    fac = 1
    for i in range(2, n):
        fac = fac * i
    return (fac * 2)


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

# Expected output:
48
# find the maximum number of elements divisible by 3 | function to find the maximum number of elements divisible by 3 ; to store frequency of each number ; store modulo value ; store frequency ; add numbers with zero modulo to answer ; find minimum of elements with modulo frequency one and zero ; add k to the answer ; remove them from frequency ; add numbers possible with remaining frequency ; return the required answer ; 
def MaxNumbers(a, n):
    fre = [0 for i in range(3)]
    for i in range(n):
        a[i] %= 3
        fre[a[i]] += 1
    ans = fre[0]
    k = min(fre[1], fre[2])
    ans += k
    fre[1] -= k
    fre[2] -= k
    ans += fre[1] // 3 + fre[2] // 3
    return ans


a = [1, 4, 10, 7, 11, 2, 8, 5, 9]
n = len(a)
print(MaxNumbers(a, n))

# Expected output:
5
# count pairs with set bits sum equal to k | function to return the count of set bits in n ; function to return the count of required pairs ; to store the count ; sum of set bits in both the integers ; if current pair satisfies the given condition ; 
def countSetBits(n):
    count = 0
    while (n):
        n &= (n - 1)
        count += 1
    return count


def pairs(arr, n, k):
    count = 0
    for i in range(n):
        for j in range(i + 1, n):
            sum = countSetBits(arr[i]) + countSetBits(arr[j])
            if (sum == k):
                count += 1
    return count


if __name__ == "__main__":
    arr = [1, 2, 3, 4, 5]
    n = len(arr)
    k = 4
    print(pairs(arr, n, k))

# Expected output:
1
# count pairs with set bits sum equal to k | python implementation of the approach ; function to return the count of set bits in n ; function to return the count of required pairs ; to store the count ; frequency array ; if current pair satisfies the given condition ; ( arr [ i ] , arr [ i ] ) cannot be a valid pair ; 

MAX = 32


def countSetBits(n):
    count = 0
    while (n):
        n &= (n - 1)
        count += 1
    return count


def pairs(arr, n, k):
    count = 0
    f = [0 for i in range(MAX + 1)]
    for i in range(n):
        f[countSetBits(arr[i])] += 1
    for i in range(MAX + 1):
        for j in range(1, MAX + 1):
            if (i + j == k):
                if (i == j):
                    count += ((f[i] * (f[i] - 1)) / 2)
                else:
                    count += (f[i] * f[j])
    return count


arr = [1, 2, 3, 4, 5]
n = len(arr)
k = 4
print(pairs(arr, n, k))

# Expected output:
1.0
# find the total number of composite factor for a given number | function to return the count of prime factors of n ; initialise array with 0 ; stored i value into an array ; every non - zero value at a [ i ] denotes that i is a factor of n ; find if i is prime ; if i is a factor of n and i is not prime ; 
def composite_factors(n):
    count = 0
    a = [0] * (n + 1)
    for i in range(1, n + 1):
        if (n % i == 0):
            a[i] = i
    for i in range(2, n + 1):
        j = 2
        p = 1
        while (j < a[i]):
            if (a[i] % j == 0):
                p = 0
                break
            j += 1
        if (p == 0 and a[i] != 0):
            count += 1
    return count


if __name__ == "__main__":
    n = 100
    print(composite_factors(n))

# Expected output:
6
# sum of all mersenne numbers present in an array | function that returns true if n is a mersenne number ; function to return the sum of all the mersenne numbers from the given array ; to store the required sum ; if current element is a mersenne number ; 
def isMersenne(n):
    while (n != 0):
        r = n % 2
        if (r == 0):
            return False
        n //= 2
    return True


def sumOfMersenne(arr, n):
    sum = 0
    for i in range(n):
        if (arr[i] > 0 and isMersenne(arr[i])):
            sum += arr[i]
    return sum


if __name__ == "__main__":
    arr = [17, 6, 7, 63, 3]
    n = len(arr)
    print(sumOfMersenne(arr, n))

# Expected output:
73
# sum of all perfect numbers present in an array | function to return the sum of all the proper factors of n ; f is the factor of n ; function to return the required sum ; to store the sum ; if current element is non - zero and equal to the sum of proper factors of itself ; 
def sumOfFactors(n):
    sum = 0
    for f in range(1, n // 2 + 1):
        if (n % f == 0):
            sum += f
    return sum


def getSum(arr, n):
    sum = 0
    for i in range(n):
        if (arr[i] > 0 and arr[i] == sumOfFactors(arr[i])):
            sum += arr[i]
    return sum


arr = [17, 6, 10, 6, 4]
n = len(arr)
print(getSum(arr, n))

# Expected output:
12
# check if two numbers have same number of digits | function that return true if a and b have same number of digits ; both must be 0 now if they had same lengths ; 
def sameLength(A, B):
    while (A > 0 and B > 0):
        A = A / 10
        B = B / 10
    if (A == 0 and B == 0):
        return True
    return False


A = 21
B = 1
if (sameLength(A, B)):
    print("Yes")
else:
    print("No")

# Expected output:
No
# sum of xor of all sub | sum of xor of all k length sub - array of an array ; if the length of the array is less than k ; array that will store xor values of subarray from 1 to i ; traverse through the array ; if i is greater than zero , store xor of all the elements from 0 to i ; if it is the first element ; if i is greater than k ; xor of values from 0 to i ; now to find subarray of length k that ends at i , xor sum with x [ i - k ] ; add the xor of elements from i - k + 1 to i ; return the resultant sum ; ; 
def FindXorSum(arr, k, n):
    if (n < k):
        return 0
    x = [0] * n
    result = 0
    for i in range(n):
        if (i > 0):
            x[i] = x[i - 1] ^ arr[i]
        else:
            x[i] = arr[i]
        if (i >= k - 1):
            sum = 0
            sum = x[i]
            if (i - k > -1):
                sum ^= x[i - k]
            result += sum
    return result


arr = [1, 2, 3, 4]
n = 4
k = 2
print(FindXorSum(arr, k, n))

# Expected output:
11
# count the number of digits of palindrome numbers in an array | function to return the reverse of n ; function that returns true if n is a palindrome ; function to return the count of digits of n ; function to return the count of digits in all the palindromic numbers of arr [ ] ; if arr [ i ] is a one digit number or it is a palindrome ; 
def reverse(n):
    rev = 0
    while (n > 0):
        d = n % 10
        rev = rev * 10 + d
        n = n // 10
    return rev


def isPalin(n):
    return (n == reverse(n))


def countDigits(n):
    c = 0
    while (n > 0):
        n = n // 10
        c += 1
    return c


def countPalinDigits(arr, n):
    s = 0
    for i in range(n):
        if (arr[i] < 10 or isPalin(arr[i])):
            s += countDigits(arr[i])
    return s


arr = [121, 56, 434]
n = len(arr)
print(countPalinDigits(arr, n))

# Expected output:
6
# sum of all palindrome numbers present in an array | function to reverse a number n ; function to check if a number n is palindrome ; if n is equal to the reverse of n it is a palindrome ; function to calculate sum of all array elements which are palindrome ; summation of all palindrome numbers present in array ; 
def reverse(n):
    d = 0
    s = 0
    while (n > 0):
        d = n % 10
        s = s * 10 + d
        n = n // 10
    return s


def isPalin(n):
    return n == reverse(n)


def sumOfArray(arr, n):
    s = 0
    for i in range(n):
        if ((arr[i] > 10) and isPalin(arr[i])):
            s += arr[i]
    return s


if __name__ == "__main__":
    n = 6
    arr = [12, 313, 11, 44, 9, 1]
    print(sumOfArray(arr, n))

# Expected output:
368
# check if the given prufer sequence is valid or not | function that returns true if given prufer sequence is valid ; iterate in the prufer sequence ; if out of range ; 
def isValidSeq(a, n):
    nodes = n + 2
    for i in range(n):
        if (a[i] < 1 or a[i] > nodes):
            return False
    return True


if __name__ == "__main__":
    a = [4, 1, 3, 4]
    n = len(a)
    if (isValidSeq(a, n)):
        print("Valid")
    else:
        print("Invalid")

# Expected output:
Valid
# program to calculate e ^ x by recursion ( using taylor series ) | recursive function global variables p and f ; termination condition ; recursive call ; update the power of x ; factorial ; 

p = 1.0
f = 1.0


def e(x, n):
    global p, f
    if (n == 0):
        return 1
    r = e(x, n - 1)
    p = p * x
    f = f * n
    return (r + p / f)


x = 4
n = 15
print(e(x, n))

# Expected output:
54.5978829056501
# count number of ways to divide an array into two halves with same sum | function to count the number of ways to divide an array into two halves with same sum ; if length of array is 1 answer will be 0 as we have to split it into two non - empty halves ; variables to store total sum , current sum and count ; finding total sum ; checking if sum equals total_sum / 2 ; 
def cntWays(arr, n):
    if (n == 1):
        return 0
    tot_sum = 0
    sum = 0
    ans = 0
    for i in range(0, n):
        tot_sum += arr[i]
    for i in range(0, n - 1):
        sum += arr[i]
        if (sum == tot_sum / 2):
            ans += 1
    return ans


arr = [1, -1, 1, -1, 1, -1]
n = len(arr)
print(cntWays(arr, n))

# Expected output:
2
# count of all possible values of x such that a % x = b | function to return the count of all possible values for x such that ( a % x ) = b ; case 1 ; case 2 ; case 3 ; find the number of divisors of x which are greater than b ; 
def countX(a, b):
    if (b > a):
        return 0
    elif (a == b):
        return -1
    else:
        x = a - b
        ans = 0
        i = 1
        while i * i <= x:
            if (x % i == 0):
                d1 = i
                d2 = b - 1
                if (i * i != x):
                    d2 = x // i
                if (d1 > b):
                    ans += 1
                if (d2 > b):
                    ans += 1
            i += 1
        return ans


if __name__ == "__main__":
    a = 21
    b = 5
    print(countX(a, b))

# Expected output:
2
# find the sum of numbers from 1 to n excluding those which are powers of k | function to return the sum of all the powers of k from the range [ 1 , n ] ; to store the sum of the series ; while current power of k <= n ; add current power to the sum ; next power of k ; return the sum of the series ; find to return the sum of the elements from the range [ 1 , n ] excluding those which are powers of k ; sum of all the powers of k from [ 1 , n ] ; sum of all the elements from [ 1 , n ] ; return the required sum ; 
def sumPowersK(n, k):
    sum = 0
    num = 1
    while (num <= n):
        sum += num
        num *= k
    return sum


def getSum(n, k):
    pwrK = sumPowersK(n, k)
    sumAll = (n * (n + 1)) / 2
    return (sumAll - pwrK)


if __name__ == "__main__":
    n = 10
    k = 3
    print(getSum(n, k))

# Expected output:
42.0
# maximum number of people that can be killed with strength p | python3 implementation of the approach ; function to return the maximum number of people that can be killed ; loop will break when the ith person cannot be killed ; 

from math import sqrt


def maxPeople(p):
    tmp = 0
    count = 0
    for i in range(1, int(sqrt(p)) + 1):
        tmp = tmp + (i * i)
        if (tmp <= p):
            count += 1
        else:
            break
    return count


if __name__ == "__main__":
    p = 14
    print(maxPeople(p))

# Expected output:
3
# maximum number of people that can be killed with strength p | python3 implementation of the approach ; function to return the maximum number of people that can be killed ; storing the sum beforehand so that it can be used in each query ; lower_bound returns an iterator pointing to the first element greater than or equal to your val ; previous value ; returns the index in array upto which killing is possible with strength p ; 

kN = 1000000


def lower_bound(a, low, high, element):
    while (low < high):
        middle = int(low + (high - low) / 2)
        if (element > a[middle]):
            low = middle + 1
        else:
            high = middle
    return low


def maxPeople(p):
    sums = [0] * kN
    sums[0] = 0
    for i in range(1, kN):
        sums[i] = (i * i) + sums[i - 1]
    it = lower_bound(sums, 0, kN, p)
    if (it > p):
        it -= 1
    return it


if __name__ == '__main__':
    p = 14
    print(maxPeople(p))

# Expected output:
3
# Maximum number of people that can be killed with strength P | helper function which returns the sum of series ( 1 ^ 2 + 2 ^ 2 + ... + n ^ 2 ) ; maxPeople function which returns appropriate value using Binary Search in O ( logn ) ; Set the lower and higher values ; calculate the mid using low and high ; compare value with n ; return the ans
def squareSeries(n):
    return (n * (n + 1) * (2 * n + 1)) // 6


def maxPeople(n):
    low = 0
    high = 1000000000000000
    while low <= high:
        mid = low + ((high - low) // 2)
        value = squareSeries(mid)
        if value <= n:
            ans = mid
            low = mid + 1
        else:
            high = mid - 1
    return ans


if __name__ == '__main__':
    p = 14
    print(maxPeople(p))

# Expected output:
3
# find the index which is the last to be reduced to zero after performing a given operation | function that returns the index which will be the last to become zero after performing given operation ; initialize the result ; finding the ceil value of each index ; finding the index with maximum ceil value ; 
def findIndex(a, n, k):
    index = -1
    max_ceil = -10 ** 9
    for i in range(n):
        a[i] = (a[i] + k - 1) // k
    for i in range(n):
        if (a[i] >= max_ceil):
            max_ceil = a[i]
            index = i
    return index


arr = [31, 12, 25, 27, 32, 19]
K = 5
N = len(arr)
print(findIndex(arr, N, K))

# Expected output:
4
# queries to find the maximum xor value between x and the nodes of a given level of a perfect binary tree | python3 implementation of the approach ; function to solve queries of the maximum xor value between the nodes in a given level l of a perfect binary tree and a given value x ; initialize result ; initialize array to store bits ; initialize a copy of x and size of array ; storing the bits of x in the array a [ ] ; filling the array b [ ] ; initializing variable which gives maximum xor ; getting the maximum xor value ; return the result ; 

MAXN = 60


def solveQuery(L, X):
    res = 0
    a = [0 for i in range(MAXN)]
    b = [0 for i in range(MAXN)]
    ref = X
    size_a = 0
    while (ref > 0):
        a[size_a] = ref % 2
        ref //= 2
        size_a += 1
    for i in range(min(size_a, L)):
        if (a[i] == 1):
            b[i] = 0
        else:
            b[i] = 1
    for i in range(min(size_a, L), L):
        b[i] = 1
    b[L - 1] = 1
    temp = 0
    p = 1
    for i in range(L):
        temp += b[i] * p
        p *= 2
    res = temp ^ X
    return res


queries = [[2, 5], [3, 15]]
q = len(queries)
for i in range(q):
    print(solveQuery(queries[i][0], queries[i][1]))

# Expected output:
7
11
# count index pairs which satisfy the given condition | function to return the count of required index pairs ; to store the required count ; array to store the left elements upto which current element is maximum ; iterating through the whole permutation except first and last element ; if current element can be maximum in a subsegment ; current maximum ; iterating for smaller values then current maximum on left of it ; storing left borders of the current maximum ; iterating for smaller values then current maximum on right of it ; condition satisfies ; return count of subsegments ; 
def Count_Segment(p, n):
    count = 0
    upto = [False] * (n + 1)
    for i in range(1, n - 1):
        if p[i] > p[i - 1] and p[i] > p[i + 1]:
            curr = p[i]
            j = i - 1
            while j >= 0 and p[j] < curr:
                upto[p[j]] = curr
                j -= 1
            j = i + 1
            while j < n and p[j] < curr:
                if upto[curr - p[j]] == curr:
                    count += 1
                j += 1
    return count


if __name__ == "__main__":
    p = [3, 4, 1, 5, 2]
    n = len(p)
    print(Count_Segment(p, n))

# Expected output:
2
# check whether a number can be represented as sum of k distinct positive integers | function that returns true if n can be represented as the sum of exactly k distinct positive integers ; if n can be represented as 1 + 2 + 3 + ... + ( k - 1 ) + ( k + x ) ; 
def solve(n, k):
    if (n >= (k * (k + 1)) // 2):
        return True
    return False


if __name__ == '__main__':
    n = 12
    k = 4
    if (solve(n, k)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# check whether product of integers from a to b is positive , negative or zero | function to check whether the product of integers of the range [ a , b ] is positive , negative or zero ; if both a and b are positive then the product will be positive ; if a is negative and b is positive then the product will be zero ; if both a and b are negative then we have to find the count of integers in the range ; total integers in the range ; if n is even then the resultant product is positive ; if n is odd then the resultant product is negative ; 
def solve(a, b):
    if (a > 0 and b > 0):
        print("Positive")
    elif (a <= 0 and b >= 0):
        print("Zero")
    else:
        n = abs(a - b) + 1
        if (n % 2 == 0):
            print("Positive")
        else:
            print("Negative")


if __name__ == '__main__':
    a = -10
    b = -2
    solve(a, b)

# Expected output:
Negative
# bitwise and of sub | function to return the minimum possible value of | k - x | where x is the bitwise and of the elements of some sub - array ; check all possible sub - arrays ; find the overall minimum ; 
def closetAND(arr, n, k):
    ans = 10 ** 9
    for i in range(n):
        X = arr[i]
        for j in range(i, n):
            X &= arr[j]
            ans = min(ans, abs(k - X))
    return ans


arr = [4, 7, 10]
n = len(arr)
k = 2
print(closetAND(arr, n, k))

# Expected output:
0
# program to find the rate percentage from compound interest of consecutive years | function to return the required rate percentage ; 
def Rate(N1, N2):
    rate = (N2 - N1) * 100 // (N1)
    return rate


if __name__ == "__main__":
    N1 = 100
    N2 = 120
    print(Rate(N1, N2), " %")

# Expected output:
20  %