giulio98/xlcost-single-prompt huggingface dataset:
# minimize increments or decrements required to make sum and product of array elements non | function to find the sum of array ; return the sum ; function that counts the minimum operations required to make the sum and product of array non - zero ; stores count of zero elements ; iterate over the array to count zero elements ; sum of elements of the array ; print result ; 
def array_sum(arr, n):
    sum = 0
    for i in range(n):
        sum += arr[i]
    return sum


def countOperations(arr, N):
    count_zeros = 0
    for i in range(N):
        if (arr[i] == 0):
            count_zeros += 1
    sum = array_sum(arr, N)
    if (count_zeros):
        return count_zeros
    if (sum == 0):
        return 1
    return 0


if __name__ == '__main__':
    arr = [-1, -1, 0, 0]
    N = len(arr)
    print(countOperations(arr, N))

# Expected output:
2
# bitwise or of all unordered pairs from a given array | function to find the bitwise or of all possible pairs of the array ; stores bitwise or of all possible pairs of arr ; traverse the array arr ; update totalor ; return bitwise or of all possible pairs of arr ; 
def TotalBitwiseORPair(arr, N):
    totalOR = 0
    for i in range(N):
        totalOR |= arr[i]
    return totalOR


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

# Expected output:
15
# difference between lexicographical ranks of two given permutations | function the print the difference between the lexicographical ranks ; store the permutations in lexicographic order ; initial permutation ; initial variables ; check permutation ; initialize second permutation ; check permutation ; print difference ; given array p [ ] ; given array q [ ] ; 
def next_permutation(p):
    for a in range(len(p) - 2, -1, -1):
        if (p[a] < p[a + 1]):
            b = len(p) - 1
            while True:
                if (p[b] > p[a]):
                    t = p[a]
                    p[a] = p[b]
                    p[b] = t
                    a += 1
                    b = len(p) - 1
                    while a < b:
                        t = p[a]
                        p[a] = p[b]
                        p[b] = t
                        a += 1
                        b -= 1
                    return True
                b -= 1
    return False


def findDifference(p, q, N):
    A = [0] * N
    for i in range(N):
        A[i] = i + 1
    IsCorrect = False
    a, b = 1, 1
    while True:
        IsCorrect = True
        for i in range(N):
            if (A[i] != p[i]):
                IsCorrect = False
                break
        if (IsCorrect):
            break
        a += 1
        if (not next_permutation(A)):
            break
    for i in range(N):
        A[i] = i + 1
    while True:
        IsCorrect = True
        for i in range(N):
            if (A[i] != q[i]):
                IsCorrect = False
                break
        if (IsCorrect):
            break
        b += 1
        if (not next_permutation(A)):
            break
    print(abs(a - b))


p = [1, 3, 2]
q = [3, 1, 2]
n = len(p)
findDifference(p, q, n)

# Expected output:
3
# count of packets placed in each box after performing given operations | function to print final array after performing all the operations ; initialize variables ; traverse through all operations ; operation type ; move left ; move right ; pick a packet ; drop a packet ; exit ; print final array ; 
def printFinalArray(a, n, operations, p, capacity):
    curr = 0
    picked = False
    for i in range(p):
        s = operations[i]
        flag = False
        if (curr != 0):
            curr -= 1
            break
        if (curr != n - 1):
            curr += 1
            break
        if (picked == False and a[curr] != 0):
            picked = True
            a[curr] -= 1
            break
        if (picked and a[curr] != capacity):
            picked = False
            a[curr] += 1
            break
        else:
            flag = True
        if (flag):
            break
    for i in range(n):
        print(a[i], end=" ")


if __name__ == "__main__":
    capacity = 5
    a = [2, 5, 2]
    N = len(a)
    operations = [3, 2, 4, 1, 4, 5]
    M = len(operations)
    printFinalArray(a, N, operations, M, capacity)

# Expected output:
2 5 2
# check if sum of count of digits of array elements is prime or not | python3 program for the above approach ; function to check whether a number is prime or not ; corner cases ; if given number is a multiple of 2 or 3 ; function to check if sum of count of digits of all array elements is prime or not ; initialize sum with 0 ; traverse over the array ; convert array element to string ; add the count of digits to sum ; print the result ; 

import math


def isPrime(n):
    if (n <= 1):
        return False
    if (n <= 3):
        return True
    if (n % 2 == 0 or n % 3 == 0):
        return False
    for i in range(5, int(math .sqrt(n) + 1), 6):
        if (n % i == 0 or n % (i + 2) == 0):
            return False
    return True


def CheckSumPrime(A, N):
    sum = 0
    for i in range(0, N):
        s = str(A[i])
        sum += len(s)
    if (isPrime(sum)):
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    A = [1, 11, 12]
    N = len(A)
    CheckSumPrime(A, N)

# Expected output:
Yes
# probability of obtaining prime numbers as product of values obtained by throwing n dices | function to find the value of power ( x , n ) ; stores the value of ( x ^ n ) ; calculate the value of power ( x , n ) ; if n is odd ; update res ; update x ; update n ; function to find the probability of obtaining a prime number as the product of n thrown dices ; stores count of favorable outcomes ; stores count of sample space ; prthe required probability ; 
def power(x, N):
    res = 1
    while (N > 0):
        if (N % 2 == 1):
            res = (res * x)
        x = (x * x)
        N = N >> 1
    return res


def probablityPrimeprod(N):
    N_E = 3 * N
    N_S = power(6, N)
    print(N_E, " / ", N_S)


if __name__ == '__main__':
    N = 2
    probablityPrimeprod(N)

# Expected output:
6  /  36
# smaller palindromic number closest to n | function to check if a number is palindrome or not ; stores reverse of n ; stores the value of n ; calculate reverse of n ; update rev ; update n ; update n ; if n is equal to rev of n ; function to find the closest smaller palindromic number to n ; calculate closest smaller palindromic number to n ; update n ; 
def checkPalindrome(N):
    rev = 0
    temp = N
    while (N != 0):
        rev = rev * 10 + N % 10
        N = N // 10
    N = temp
    if (N == rev):
        return True
    return False


def closestSmallerPalindrome(N):
    while N >= 0 and not checkPalindrome(N):
        N -= 1
    return N


if __name__ == '__main__':
    N = 4000
    print(closestSmallerPalindrome(N))

# Expected output:
3993
# minimize increments or decrements by 2 to convert given value to a perfect square | function to find the minimum number of operations required to make n a perfect square ; stores count of operations by performing decrements ; stores value of n ; decrement the value of temp ; stores square root of temp ; if temp is a perfect square ; update temp ; store count of operations by performing increments ; increment the value of n ; stores sqrt of n ; if n is a perfect square ; update temp ; return the minimum count ; 
def MinimumOperationReq(N):
    cntDecr = 0
    temp = N
    while (temp > 0):
        X = int(pow(temp, 1 / 2))
        if (X * X == temp):
            break
        temp = temp - 2
        cntDecr += 1
    cntIncr = 0
    while (True):
        X = int(pow(N, 1 / 2))
        if (X * X == N):
            break
        N = N + 2
        cntIncr += 1
    return min(cntIncr, cntDecr)


if __name__ == '__main__':
    N = 15
    print(MinimumOperationReq(N))

# Expected output:
3
# first term from given nth term of the equation f ( n ) = ( 2 * f ( n | python3 program 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 value of f ( 1 ) ; stores power ( 2 , n - 1 ) ; stores modulo multiplicative inverse of p_2 under modulo m ; stores the value of f ( 1 ) ; update res ; 

M = 1000000007


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


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


def F_1(N, F_N):
    P_2 = power(2, N - 1)
    modInv = moduloInverse(P_2)
    res = 0
    res = ((modInv % M) * (F_N % M)) % M
    return res


if __name__ == '__main__':
    N = 3
    F_N = 6
    print(F_1(N, F_N))

# Expected output:
500000005
# check if a subarray of size k exists whose elements form a number divisible by 3 | function to find the k size subarray ; check if the first k elements forms a number which is divisible by 3 ; using sliding window technique ; calculate sum of next k size subarray ; check if sum is divisible by 3 ; update the indices of the subarray ; if no such subarray is found ; print the subarray ; 
def findSubArray(arr, k):
    ans = [(0, 0)]
    sm = 0
    i = 0
    found = 0
    while (i < k):
        sm += arr[i]
        i += 1
    if (sm % 3 == 0):
        ans = [(0, i - 1)]
        found = 1
    for j in range(i, len(arr), 1):
        if (found == 1):
            break
        sm = sm + arr[j] - arr[j - k]
        if (sm % 3 == 0):
            ans = [(j - k + 1, j)]
            found = 1
    if (found == 0):
        ans = [(-1, 0)]
    if (ans[0][0] == -1):
        print(-1)
    else:
        for i in range(ans[0][0], ans[0][1] + 1, 1):
            print(arr[i], end=" ")


if __name__ == '__main__':
    arr = [84, 23, 45, 12, 56, 82]
    K = 3
    findSubArray(arr, K)

# Expected output:
12 56 82
# sum of all ordered pair | function to calculate the sum of all pair - products ; stores sum of array ; update sum of the array ; 
def sumOfProd(arr, N):
    sum = 0
    for i in range(N):
        sum += arr[i]
    return sum * sum


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

# Expected output:
225
# check if a given number is one less than twice its reverse | iterative function to reverse digits of num ; loop to extract all digits of the number ; function to check if n satisfies given equation ; 
def rev(num):
    rev_num = 0
    while (num > 0):
        rev_num = (rev_num * 10 + num % 10)
        num = num // 10
    return rev_num


def check(n):
    return (2 * rev(n) == n + 1)


n = 73
if (check(n)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# check if a given number is one less than twice its reverse | python3 program to implement the above approach ; function to check y is a power of x ; logarithm function to calculate value ; compare to the result1 or result2 both are equal ; function to check if n satisfies the equation 2 * reverse ( n ) = n + 1 ; 

import math


def isPower(x, y):
    res1 = math .log(y) // math .log(x)
    res2 = math .log(y) // math .log(x)
    return (res1 == res2)


def check(n):
    x = (n + 7) // 8
    if ((n + 7) % 8 == 0 and isPower(10, x)):
        return True
    else:
        return False


n = 73
if (check(n) != 0):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# nth term of a recurrence relation generated by two given arrays | python3 program for the above approach ; function to calculate nth term of general recurrence relations ; stores the generated sequence ; current term is sum of previous k terms ; print the nth term ; 

mod = 1e9 + 7


def NthTerm(F, C, K, n):
    ans = [0] * (n + 1)
    i = 0
    while i < K:
        ans[i] = F[i]
        i += 1
    i = K
    while i <= n:
        j = i - K
        while j < i:
            ans[i] += ans[j]
            ans[i] %= mod
            j += 1
        i += 1
    print(int(ans[n]))


if __name__ == '__main__':
    F = [0, 1]
    C = [1, 1]
    K = 2
    N = 10
    NthTerm(F, C, K, N)

# Expected output:
55
# mode of frequencies of given array elements | python3 program of the above approach ; function to find the mode of the frequency of the array ; stores the frequencies of array elements ; traverse through array elements and count frequencies ; stores the frequencies 's of  frequencies of array elements ; stores the minimum value ; find the mode in 2 nd map ; search for this mode ; when mode is find then return to main function . ; if mode is not found ; 

from collections import defaultdict
import sys


def countFreq(arr, n):
    mp1 = defaultdict(int)
    for i in range(n):
        mp1[arr[i]] += 1
        mp2 = defaultdict(int)
        for it in mp1:
            mp2[mp1[it]] += 1
        M = -sys .maxsize - 1
        for it in mp2:
            M = max(M, mp2[it])
            for it in mp2:
                if (M == mp2[it]):
                    return it
            return 0


if __name__ == "__main__":
    arr = [6, 10, 3, 10, 8, 3, 6, 4]
    n = len(arr)
    print(countFreq(arr, n))

# Expected output:
1
# maximize count of nodes disconnected from all other nodes in a graph | function which returns the maximum number of isolated nodes ; used nodes ; remaining edges ; count nodes used ; if given edges are non - zero ; 
def maxDisconnected(N, E):
    curr = 1
    rem = E
    while (rem > 0):
        rem = rem - min(curr, rem)
        curr += 1
    if (curr > 1):
        return N - curr
    else:
        return N


if __name__ == '__main__':
    N = 5
    E = 1
    print(maxDisconnected(N, E))

# Expected output:
3
# check if number is palindrome or not in base b | function to check if n in base b is palindrome or not ; stores the reverse of n ; stores the value of n ; extract all the digits of n ; generate its reverse ; 
def checkPalindromeB(N, B):
    rev = 0
    N1 = N
    while (N1 > 0):
        rev = rev * B + N1 % B
        N1 = N1 // B
    return N == rev


if __name__ == '__main__':
    N = 5
    B = 2
    if (checkPalindromeB(N, B)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# product of all numbers up to n that are co | function to return gcd of a and b ; base case ; recursive gcd ; function to find the product of all the numbers till n that are relatively prime to n ; stores the resultant product ; iterate over [ 2 , n ] ; if gcd is 1 , then find the product with result ; return the final product ; 
def gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)


def findProduct(N):
    result = 1
    for i in range(2, N):
        if (gcd(i, N) == 1):
            result *= i
    return result


if __name__ == '__main__':
    N = 5
    print(findProduct(N))

# Expected output:
24
# maximize count of equal numbers in array of numbers upto n by replacing pairs with their sum | function to count maximum number of array elements equal ; 
def countEqual(n):
    return (n + 1) // 2


lst = [1, 2, 3, 4, 5, 6]
n = len(lst)
print(countEqual(n))

# Expected output:
3
# minimize count of unequal elements at corresponding indices between given arrays | function that count of the mismatched pairs in bot the array ; create a parent array and initialize it ; preprocessing of the given pairs of indices ; 1 - based indexing ; if both indices doesn 't belong  to same component ; insert the indices in same component ; hashmap to get the indices of array a ; if current element is not present in array b then count this as mismatched ; get the index of the element in array b ; check if both indices belong to same connected component if not increment the count ; return answer ; function that gives the connected component of each index ; function that creates the connected components ; find parent of a and b recursively ; update the parent of a ; 
def find(par, x):
    if (par[x] == x):
        return x
    else:
        par[x] = find(par, par[x])
        return par[x]


def union(par, a, b):
    a = find(par, a)
    b = find(par, b)
    if (a == b):
        return
    par[a] = b


def countPairs(A, B, N, M, List):
    count = 0
    par = [0] * (N + 1)
    for i in range(N + 1):
        par[i] = i
    for i in range(M):
        index1 = find(par, List[i][0] - 1)
        index2 = find(par, List[i][1] - 1)
        if (index1 != index2):
            union(par, index1, index2)
    map = {}
    for i in range(N):
        map[A[i]] = i
    for i in range(N):
        if (A[i] != B[i]):
            if (B[i]not in map .keys()):
                count += 1
                continue
            j = map[B[i]]
            if (find(par, i) != find(par, j)):
                count += 1
    return count


N = 5
M = 4
A = [1, 5, 9, 2, 3]
B = [2, 4, 5, 1, 3]
List = [[1, 4], [2, 3], [3, 5], [2, 5]]
print(countPairs(A, B, N, M, List))

# Expected output:
1
# maximum value of expression ( arr [ i ] + arr [ j ] * arr [ k ] ) formed from a valid triplet | function that generate all valid triplets and calculate the value of the valid triplets ; generate all triplets ; check whether the triplet is valid or not ; update the value ; print the maximum value ; 
def max_valid_triplet(A, n):
    ans = -1
    for i in range(0, n - 2):
        for j in range(i + 1, n - 1):
            for k in range(j + 1, n):
                if (A[i] < A[j] and A[j] < A[k]):
                    value = A[i] + A[j] * A[k]
                    if (value > ans):
                        ans = value
    print(ans)


if __name__ == '__main__':
    arr = [7, 9, 3, 8, 11, 10]
    n = len(arr)
    max_valid_triplet(arr, n)

# Expected output:
106
# highest power of 2 that divides the lcm of first n natural numbers . | function to find lcm of first n natural numbers ; initialize result ; ans contains lcm of 1 , 2 , 3 , . . i after i 'th iteration ; function to find the highest power of 2 which divides lcm of first n natural numbers ; find lcm of first n natural numbers ; to store the highest required power of 2 ; counting number of consecutive zeros from the end in the given binary string ; 
def findlcm(n):
    ans = 1
    for i in range(1, n + 1):
        ans = (((i * ans)) // (__gcd(i, ans)))
    return ans


def __gcd(a, b):
    if (b == 0):
        return a
    else:
        return __gcd(b, a % b)


def highestPower(n):
    lcm = findlcm(n)
    ans = 0
    for i in range(1, n):
        x = int(pow(2, i))
        if (lcm % x == 0):
            ans = i
        if (x > n):
            break
    return ans


if __name__ == '__main__':
    n = 15
    print(highestPower(n))

# Expected output:
3
# highest power of 2 that divides the lcm of first n natural numbers . | python3 implementation of the approach ; function to find the highest power of 2 which divides lcm of first n natural numbers ; 

import math


def highestPower(n):
    return int((math .log(n) // math .log(2)))


if __name__ == '__main__':
    n = 15
    print(highestPower(n))

# Expected output:
3
# check if number can be made prime by deleting a single digit | function to check if n is prime from builtins import range ; corner cases ; this is checked so that we can skip middle five numbers in below loop ; function to delete character at index i from given string str ; deletes character at position 4 ; function to check if a number becomes prime by deleting any digit ; converting the number to string ; length of string ; number should not be of single digit ; loop to find all numbers after deleting a single digit ; deleting ith character from the string ; converting string to int ; 
def isPrime(n):
    if (n <= 1):
        return False
    if (n <= 3):
        return True
    if (n % 2 == 0 or n % 3 == 0):
        return False
    for i in range(5, int(n ** 1 / 2), 6):
        if (n % i == 0 or n % (i + 2) == 0):
            return False
    return True


def deleteIth(str, i):
    str = str[0:i] + str[i + 1:]
    return str


def isPrimePossible(N):
    s = str(N)
    l = len(s)
    if (l < 2):
        return False
    for i in range(l):
        str1 = deleteIth(s, i)
        num = int(str1)
        if (isPrime(num)):
            return True
    return False


if __name__ == '__main__':
    N = 610
    if (isPrimePossible(N)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# check if a number has an odd count of odd divisors and even count of even divisors | function to find the count of even and odd factors of n ; loop runs till square root ; condition to check if the even factors of the number n is is even and count of odd factors is odd ; 
def checkFactors(N):
    ev_count = 0
    od_count = 0
    for i in range(1, int(pow(N, 1 / 2)) + 1):
        if (N % i == 0):
            if (i == N / i):
                if (i % 2 == 0):
                    ev_count += 1
                else:
                    od_count += 1
            else:
                if (i % 2 == 0):
                    ev_count += 1
                else:
                    od_count += 1
                if ((N / i) % 2 == 0):
                    ev_count += 1
                else:
                    od_count += 1
    if (ev_count % 2 == 0 and od_count % 2 == 1):
        print("Yes" + "")
    else:
        print("No" + "")


if __name__ == '__main__':
    N = 36
    checkFactors(N)

# Expected output:
Yes
# count of distinct permutations of length n having no similar adjacent characters | function to print the number of permutations possible ; 
def countofPermutations(N):
    return int((3 * pow(2, N - 1)))


if __name__ == '__main__':
    N = 5
    print(countofPermutations(N))

# Expected output:
48
# find two distinct numbers such that their lcm lies in given range | function to find two distinct numbers x and y s . t . their lcm lies between l and r and x , y are minimum possible ; check if 2 * l lies in range l , r ; print the answer ; 
def answer(L, R):
    if (2 * L <= R):
        print(L, ",", 2 * L)
    else:
        print(-1)


L = 3
R = 8
answer(L, R)

# Expected output:
3 , 6
# maximum count of pairwise co | function to find the gcd of two numbers ; function to of pairwise co - prime and common divisors of two numbers ; initialize answer with 1 , to include 1 in the count ; count of primes of gcd ( n , m ) ; finding prime factors of gcd ; increment count if it is divisible by i ; return the total count ; function call for each pair to calculate the count of pairwise co - prime divisors ; 
def gcd(x, y):
    if (x % y == 0):
        return y
    else:
        return gcd(y, x % y)


def countPairwiseCoprime(N, M):
    answer = 1
    g = gcd(N, M)
    temp = g
    for i in range(2, g + 1):
        if i * i > g:
            break
        if (temp % i == 0):
            answer += 1
            while (temp % i == 0):
                temp //= i
    if (temp != 1):
        answer += 1
    return answer


def countCoprimePair(arr, N):
    for i in range(N):
        print(countPairwiseCoprime(arr[i][0], arr[i][1]), end=" ")


if __name__ == '__main__':
    arr = [[12, 18], [420, 660]]
    N = len(arr)
    countCoprimePair(arr, N)

# Expected output:
3 4
# product of absolute difference of every pair in given array | function to return the product of abs diff of all pairs ( x , y ) ; to store product ; iterate all possible pairs ; find the product ; return product ; 
def getProduct(a, n):
    p = 1
    for i in range(n):
        for j in range(i + 1, n):
            p *= abs(a[i] - a[j])
    return p


if __name__ == "__main__":
    arr = [1, 2, 3, 4]
    N = len(arr)
    print(getProduct(arr, N))

# Expected output:
12
# sum of bit differences for numbers from 0 to n | set 2 | recursive function to find sum of different bits between consecutive numbers from 0 to n ; base case ; calculate the nth term ; 
def totalCountDifference(n):
    if (n == 1):
        return 1
    return n + totalCountDifference(n // 2)


N = 5
print(totalCountDifference(N))

# Expected output:
8