giulio98/xlcost-single-prompt huggingface dataset:
# distribute the white and black objects into maximum groups under certain constraints | function to check if it is possible to distribute w and b into maximum groups possible ; if w is greater than b , swap them ; distribution is not possible ; distribution is possible ; 
def isPossible(W, B, D):
    if (W > B):
        temp = W
        W = B
        B = temp
    if (B > W * (D + 1)):
        print("NO")
    else:
        print("YES")


if __name__ == '__main__':
    W = 2
    B = 5
    D = 2
    isPossible(W, B, D)

# Expected output:
YES
# find the maximum gcd possible for some pair in a 
def GCD(a, b):
    if (b == 0):
        return a
    return GCD(b, a % b)


def maxGCDInRange(L, R):
    ans = 1
    for Z in range(R, 1, -1):
        if (((R // Z) - (L - 1) // Z) > 1):
            ans = Z
            break
    return ans


L = 102
R = 139
print(maxGCDInRange(L, R))

# Expected output:
34
# number of ways to form a number with maximum ks in it | function to calculate number of ways a number can be formed that has the maximum number of ks ; convert to string ; calculate length of subarrays that can contribute to the answer ; count length of subarray where adjacent digits add up to k ; current subarray can contribute to the answer only if it is odd ; return the answer ; 
def noOfWays(N, K):
    S = str(N)
    ans = 1
    for i in range(1, len(S)):
        count = 1
        while (i < len(S) and ord(S[i]) + ord(S[i - 1]) - 2 * ord('0') == K):
            count += 1
            i += 1
        if (count % 2):
            ans *= (count + 1) // 2
    return ans


if __name__ == '__main__':
    N = 1454781
    K = 9
    print(noOfWays(N, K))

# Expected output:
2
# minimum bit swaps between given numbers to make their bitwise or equal to bitwise and | function for counting number of set bit ; function to return the count of minimum operations required ; cnt to count the number of bits set in a and in b ; if odd numbers of total set bits ; one_zero = 1 in a and 0 in b at ith bit similarly for zero_one ; when bitpos is set in b , unset in b ; when bitpos is set in a , unset in b ; number of moves is half of number pairs of each group ; odd number pairs ; 
def countSetBits(n):
    count = 0
    while (n):
        n = n & (n - 1)
        count += 1
    return count


def minOperations(A, B):
    cnt1 = 0
    cnt2 = 0
    cnt1 += countSetBits(A)
    cnt2 += countSetBits(B)
    if ((cnt1 + cnt2) % 2 != 0):
        return -1
    oneZero = 0
    zeroOne = 0
    ans = 0
    for i in range(max(cnt1, cnt2)):
        bitpos = 1 << i
        if ((not (bitpos & A)) and (bitpos & B)):
            zeroOne += 1
        if ((bitpos & A) and (not (bitpos & B))):
            oneZero += 1
    ans = (zeroOne // 2) + (oneZero // 2)
    if (zeroOne % 2 != 0):
        ans += 2
    return ans


if __name__ == '__main__':
    A = 27
    B = 5
    print(minOperations(A, B))

# Expected output:
3
# largest sum subarray of size k containing consecutive elements | python3 program for the above approach ; function to find the largest sum subarray such that it contains k consecutive elements ; stores sum of subarray having k consecutive elements ; stores the maximum sum among all subarrays of size k having consecutive elements ; traverse the array ; store k elements of one subarray at a time ; sort the duplicate array in ascending order ; checks if elements in subarray are consecutive or not ; traverse the k elements ; if not consecutive , break ; if flag is true update the maximum sum ; stores the sum of elements of the current subarray ; update the max_sum ; reset curr_sum ; return the result ; 

import sys


def maximumSum(A, N, K):
    curr_sum = 0
    max_sum = -sys .maxsize - 1
    for i in range(N - K + 1):
        dupl_arr = sorted(A[i:i + K])
        flag = True
        for j in range(1, K, 1):
            if (dupl_arr[j] - dupl_arr[j - 1] != 1):
                flag = False
                break
        if (flag):
            temp = 0
            curr_sum = temp
            curr_sum = sum(dupl_arr)
            max_sum = max(max_sum, curr_sum)
            curr_sum = 0
    return max_sum


if __name__ == '__main__':
    arr = [10, 12, 9, 8, 10, 15, 1, 3, 2]
    K = 3
    N = len(arr)
    print(maximumSum(arr, N, K))

# Expected output:
27
# count pair sums that are factors of the sum of the array | function to find the number of pairs whose sums divides the sum of array ; initialize the totalsum and count as 0 ; calculate the total sum of array ; generate all possible pairs ; if the sum is a factor of totalsum or not ; increment count by 1 ; print the total count obtained ; 
def countPairs(arr, N):
    count = 0
    totalSum = 0
    for i in range(N):
        totalSum += arr[i]
    for i in range(N):
        for j in range(i + 1, N, 1):
            if (totalSum % (arr[i] + arr[j]) == 0):
                count += 1
    print(count)


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

# Expected output:
3
# maximize average of the ratios of n pairs by m increments | function to find the change in the ratio in pair after applying operation ; stores the current ratio ; stores the new ratio ; stores the increase in ratio ; returns the change ; function to find the maximum average of the ratio of the pairs by applying m increments ; stores the required result ; declare a priority queue for storing the increments ; store the increase in the ratio after applying one operation ; push the increased value and index value in priority queue ; store the ratio ; update the value of sum ; iterate while m > 0 ; add the maximum change to the sum ; remove the element from the priority queue ; increase the pairs elements by 1 on which operation is applied ; push the updated change of the pair in priority queue ; decrease the operation count ; update the value of the sum by dividing it by n ; return the result ; 
def change(pas, total):
    currentPassRatio = pas / total
    newPassRatio = (pas + 1) / (total + 1)
    increase = newPassRatio - currentPassRatio
    return increase


def maximumAverage(v, M, N):
    sum = 0
    increase, average = 0, 0
    pq = []
    for i in range(N):
        increase = change(v[i][0], v[i][1])
        pq .append([increase, i])
        average = v[i][0] * 1.0 / v[i][1]
        sum += average
    pq = sorted(pq)
    while (M > 0):
        sum += pq[-1][0]
        i = pq[-1][1]
        del pq[-1]
        v[i][0] += 1
        v[i][1] += 1
        pq .append([change(v[i][0], v[i][1]), i])
        M -= 1
        pq = sorted(pq)
    ans = sum / N
    return ans


if __name__ == '__main__':
    V = [[1, 2], [3, 5], [2, 2]]
    M = 2
    N = len(V)
    print(round(maximumAverage(V, M, N), 6))

# Expected output:
0.783333
# print all numbers that are divisors of n and are co | python 3 program for the above approach ; function to print all numbers that are divisors of n and are co - prime with the quotient of their division ; iterate upto square root of n ; if divisors are equal and gcd is 1 , then print only one of them ; otherwise print both ; 

from math import sqrt, gcd


def printUnitaryDivisors(n):
    for i in range(1, int(sqrt(n)) + 1, 1):
        if (n % i == 0):
            if (n // i == i and gcd(i, n // i) == 1):
                print(i)
            else:
                if (gcd(i, n // i) == 1):
                    print(i, n // i, end=" ")


if __name__ == '__main__':
    N = 12
    printUnitaryDivisors(N)

# Expected output:
1 12 3 4
# maximize k to make given array palindrome when each element is replaced by its remainder with k | utility function to calculate the gcd of two numbers ; function to calculate the largest k , replacing all elements of an array a by their modulus with k , makes a a palindromic array ; check if a is palindrome ; a is not palindromic ; k can be infitely large in this case ; variable to store the largest k that makes a palindromic ; return the required answer ; 
def gcd(a, b):
    if (b == 0):
        return a
    else:
        return gcd(b, a % b)


def largestK(A, N):
    l, r, flag = 0, N - 1, 0
    while (l < r):
        if (A[l] != A[r]):
            flag = 1
            break
        l += 1
        r -= 1
    if (flag == 0):
        return -1
    K = abs(A[0] - A[N - 1])
    for i in range(1, N // 2):
        K = gcd(K, abs(A[i] - A[N - i - 1]))
    return K


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

# Expected output:
-1
# check if n can be represented as sum of distinct powers of 3 | function to try all permutations of distinct powers ; base case ; if the distinct powers sum is obtained ; otherwise ; if current element not selected in power [ ] ; if current element selected in power [ ] ; return 1 if any permutation found ; function to check the n can be represented as the sum of the distinct powers of 3 ; stores the all distincts powers of three to [ 0 , 15 ] ; function call ; print ; 
def PermuteAndFind(power, idx, SumSoFar, target):
    if (idx == len(power)):
        if (SumSoFar == target):
            return True
        return False
    select = PermuteAndFind(power, idx + 1, SumSoFar, target)
    notselect = PermuteAndFind(power, idx + 1, SumSoFar + power[idx], target)
    return (select or notselect)


def DistinctPowersOf3(N):
    power = [0 for x in range(16)]
    power[0] = 1
    for i in range(1, 16):
        power[i] = 3 * power[i - 1]
    found = PermuteAndFind(power, 0, 0, N)
    if (found):
        print("Yes")
    else:
        print("No")


N = 91
DistinctPowersOf3(N)

# Expected output:
Yes
# check if n can be represented as sum of distinct powers of 3 | function to check whether the given n can be represented as the sum of the distinct powers of 3 ; iterate until n is non - zero ; termination condition ; right shift ternary bits by 1 for the next digit ; if n can be expressed as the sum of perfect powers of 3 ; 
def DistinctPowersOf3(N):
    while (N > 0):
        if (N % 3 == 2):
            cout << "No"
            return
        N //= 3
    print("Yes")


if __name__ == '__main__':
    N = 91
    DistinctPowersOf3(N)

# Expected output:
Yes
# generate an n | function to print permutation of size n with absolute difference of adjacent elements in range [ 2 , 4 ] ; if n is less than 4 ; check if n is even ; traverse through odd integers ; update the value of i ; traverse through even integers ; 
def getPermutation(N):
    if (N <= 3):
        print(-1)
        return
    i = N
    if (N % 2 == 0):
        i -= 1
    while (i >= 1):
        print(i, end=" ")
        i -= 2
    print(4, 2, end=" ")
    i = 6
    while (i <= N):
        print(i, end=" ")
        i += 2


if __name__ == '__main__':
    N = 9
    getPermutation(N)

# Expected output:
9 7 5 3 1 4 2 6 8
# count of distinct values till c formed by adding or subtracting a , b , or 0 any number of times | function to calculate gcd ; function to find number of possible final values ; find the gcd of two numbers ; calculate number of distinct values ; return values ; 
def gcd(A, B):
    if (B == 0):
        return A
    else:
        return gcd(B, A % B)


def getDistinctValues(A, B, C):
    g = gcd(A, B)
    num_values = C / g
    return int(num_values)


A = 2
B = 3
C = 10
print(getDistinctValues(A, B, C))

# Expected output:
10
# maximize score of same | function to calculate the score of same - indexed subarrays selected from the arrays a [ ] and b [ ] ; traverse the current subarray ; finding the score without reversing the subarray ; calculating the score of the reversed subarray ; return the score of subarray ; function to find the subarray with the maximum score ; stores the maximum score and the starting and the ending point of subarray with maximum score ; traverse all the subarrays ; store the score of the current subarray ; update the maximum score ; prthe maximum score ; 
def currSubArrayScore(a, b, l, r):
    straightScore = 0
    reverseScore = 0
    for i in range(l, r + 1):
        straightScore += a[i] * b[i]
        reverseScore += a[r - (i - l)] * b[i]
    return max(straightScore, reverseScore)


def maxScoreSubArray(a, b, n):
    res = 0
    start = 0
    end = 0
    for i in range(n):
        for j in range(i, n):
            currScore = currSubArrayScore(a, b, i, j)
            if (currScore > res):
                res = currScore
                start = i
                end = j
    print(res)


A = [13, 4, 5]
B = [10, 22, 2]
N = len(A)
maxScoreSubArray(A, B, N)

# Expected output:
326
# maximize score of same | function to calculate the score of same - indexed subarrays selected from the arrays a [ ] and b [ ] ; store the required result ; iterate in the range [ 0 , n - 1 ] ; consider the case of odd length subarray ; update the maximum score ; expanding the subarray in both directions with equal length so that mid poremains same ; update both the scores ; consider the case of even length subarray ; update both the scores ; print the result ; 
def maxScoreSubArray(a, b, n):
    res = 0
    for mid in range(n):
        straightScore = a[mid] * b[mid]
        reverseScore = a[mid] * a[mid]
        prev = mid - 1
        next = mid + 1
        res = max(res, max(straightScore, reverseScore))
        while (prev >= 0 and next < n):
            straightScore += (a[prev] * b[prev] + a[next] * b[next])
            reverseScore += (a[prev] * b[next] + a[next] * b[prev])
            res = max(res, max(straightScore, reverseScore))
            prev -= 1
            next += 1
        straightScore = 0
        reverseScore = 0
        prev = mid - 1
        next = mid
        while (prev >= 0 and next < n):
            straightScore += (a[prev] * b[prev] + a[next] * b[next])
            reverseScore += (a[prev] * b[next] + a[next] * b[prev])
            res = max(res, max(straightScore, reverseScore))
            prev -= 1
            next += 1
    print(res)


if __name__ == '__main__':
    A = [13, 4, 5]
    B = [10, 22, 2]
    N = len(A)
    maxScoreSubArray(A, B, N)

# Expected output:
326
# length of the smallest subarray with maximum possible sum | function to find the minimum length of the subarray whose sum is maximum ; stores the starting and the ending index of the resultant subarray ; traverse the array until a non - zero element is encountered ; if the array contains only of 0 s ; traverse the array in reverse until a non - zero element is encountered ; return the resultant size of the subarray ; 
def minimumSizeSubarray(arr, N):
    i, j = 0, N - 1
    while (i < N and arr[i] == 0):
        i += 1
    if (i == N):
        return 1
    while (j >= 0 and arr[j] == 0):
        j -= 1
    return (j - i + 1)


if __name__ == '__main__':
    arr = [0, 2, 0, 0, 12, 0, 0, 0]
    N = len(arr)
    print(minimumSizeSubarray(arr, N))

# Expected output:
4
# count ways to represent n as xor of distinct integers not exceeding n | python program for the above approach ; function to count number of ways to represent n as the bitwise xor of distinct integers ; count number of subsets using above - mentioned formula ; print the resultant count ; 

from math import *


def countXorPartition(N):
    a = 2 ** floor(N - log(N + 1) / log(2))
    print(int(a))


N = 5
countXorPartition(N)

# Expected output:
4
# count numbers less than n whose modulo with a is equal to b | function to count numbers less than n , whose modulo with a gives b ; if the value of b at least a ; if the value of b is 0 or not ; stores the resultant count of numbers less than n ; update the value of ans ; print the value of ans ; 
def countValues(A, B, C):
    if (B >= A):
        print(0)
        return
    if (B == 0):
        print(C // A)
        return
    ans = C // A
    if (ans * A + B <= C):
        ans += 1
    print(ans)


if __name__ == '__main__':
    A = 6
    B = 3
    N = 15
    countValues(A, B, N)

# Expected output:
3
# maximum sum of a subsequence whose bitwise and is non | function to find the maximum sum of a subsequence whose bitwise and is non - zero ; stores the resultant maximum sum of the subsequence ; iterate over all the bits ; stores the sum of array elements whose i - th bit is set ; traverse the array elements ; if the bit is set , then add its value to the sum ; update the resultant maximum sum ; return the maximum sum ; 
def maximumSum(arr, N):
    ans = 0
    for bit in range(32):
        sum = 0
        for i in range(N):
            if (arr[i] & (1 << bit)):
                sum += arr[i]
        ans = max(ans, sum)
    return ans


if __name__ == '__main__':
    arr = [5, 4, 1, 7, 11]
    N = len(arr)
    print(maximumSum(arr, N))

# Expected output:
24
# check if an array can be reduced to at most length k by removal of distinct elements | function to check if it is possible to reduce the size of the array to k by removing the set of the distinct array elements ; stores all distinct elements present in the array arr [ ] ; traverse the given array ; insert array elements into the set ; condition for reducing size of the array to at most k ; 
def maxCount(arr, N, K):
    st = set()
    for i in range(N):
        st .add(arr[i])
    if (N - len(st) <= K):
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    arr = [2, 2, 2, 3]
    K = 3
    N = len(arr)
    maxCount(arr, N, K)

# Expected output:
Yes
# maximum element present in the array after performing queries to add k to range of indices [ l , r ] | function to find the max sum after processing q queries ; store the cumulative sum ; store the maximum sum ; iterate over the range 0 to q ; variables to extract values from vector ; iterate over the range [ 1 , n ] ; calculate cumulative sum ; calculate maximum sum ; return the maximum sum after q queries ; stores the size of array and number of queries ; stores the sum ; storing 
def max_sum(a, v, q, n):
    x = 0
    m = -10 ** 9
    for i in range(q):
        p = v[i][0][0]
        q = v[i][0][1]
        k = v[i][1]
        a[p] += k
        if (q + 1 <= n):
            a[q + 1] -= k
    for i in range(1, n + 1):
        x += a[i]
        m = max(m, x)
    return m


n = 10
q = 3
a = [0] * (n + 5)
v = [[[0 for i in range(2)]for x in range(2)]for z in range(q)]
v[0][0][0] = 1
v[0][0][1] = 5
v[0][1] = 3
v[1][0][0] = 4
v[1][0][1] = 8
v[1][1] = 7
v[2][0][0] = 6
v[2][0][1] = 9
v[2][1] = 1
print(max_sum(a, v, q, n))

# Expected output:
10
# check if a pair of integers from two ranges exists such that their bitwise xor exceeds both the ranges | function to check if there exists any pair ( p , q ) whose bitwise xor is greater than the bitwise xor of x and y ; stores the bitwise xor of x & y ; traverse all possible pairs ; if a pair exists ; if a pair is found ; 
def findWinner(X, Y):
    playerA = (X ^ Y)
    flag = False
    for i in range(1, X + 1, 1):
        for j in range(1, Y + 1, 1):
            val = (i ^ j)
            if (val > playerA):
                flag = True
                break
        if (flag):
            break
    if (flag):
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    A = 2
    B = 4
    findWinner(A, B)

# Expected output:
No
# check if a pair of integers from two ranges exists such that their bitwise xor exceeds both the ranges | function to check if there exists any pair ( p , q ) whose bitwise xor is greater than the bitwise xor of x and y ; check for the invalid condition ; otherwise , ; 
def findWinner(X, Y):
    first = (X ^ Y)
    second = (X + Y)
    if (first == second):
        print("No")
    else:
        print("Yes")


if __name__ == '__main__':
    A, B = 2, 4
    findWinner(A, B)

# Expected output:
No
# smallest number required to be added to m to make it divisible by n | function to find the smallest number greater than or equal to n , that is divisible by k ; function to find the smallest number required to be added to to m to make it divisible by n ; stores the smallest multiple of n , greater than or equal to m ; return the result ; 
def findNum(N, K):
    rem = (N + K) % K
    if (rem == 0):
        return N
    else:
        return N + K - rem


def findSmallest(M, N):
    x = findNum(M, N)
    return x - M


if __name__ == '__main__':
    M = 100
    N = 28
    print(findSmallest(M, N))

# Expected output:
12
# prefix factorials of a prefix sum array | function to find the factorial of prefix sum at every possible index ; find the prefix sum array ; stores the factorial of all the element till the sum of array ; find the factorial array ; find the factorials of each array element ; print the resultant array ; 
def prefixFactorialArray(A, N):
    for i in range(1, N):
        A[i] += A[i - 1]
    fact = [0 for x in range(A[N - 1] + 1)]
    fact[0] = 1
    for i in range(1, A[N - 1] + 1):
        fact[i] = i * fact[i - 1]
    for i in range(0, N):
        A[i] = fact[A[i]]
    for i in range(0, N):
        print(A[i], end=" ")


arr = [1, 2, 3, 4]
N = len(arr)
prefixFactorialArray(arr, N)

# Expected output:
1 6 720 3628800
# maximum frequency of any array element possible by at most k increments | function to find the maximum possible frequency of a most frequent element after at most k increment operations ; sort the input array ; stores the sum of sliding window and the maximum possible frequency of any array element ; traverse the array ; add the current element to the window ; if it is not possible to make the array elements in the window equal ; update the value of sum ; increment the value of start ; update the maximum possible frequency ; print the frequency of the most frequent array element after k increments ; 
def maxFrequency(arr, N, K):
    arr .sort()
    start = 0
    end = 0
    sum = 0
    res = 0
    for end in range(N):
        sum += arr[end]
        while ((end - start + 1) * arr[end] - sum > K):
            sum -= arr[start]
            start += 1
        res = max(res, end - start + 1)
    print(res)


if __name__ == '__main__':
    arr = [1, 4, 8, 13]
    N = 4
    K = 5
    maxFrequency(arr, N, K)

# Expected output:
2
# count pairs ( i , j ) from an array such that i < j and arr [ j ] | function to count the number of pairs ( i , j ) such that i < j and arr [ i ] - arr [ j ] = x * ( j - i ) ; stores the count of all such pairs that satisfies the condition . ; stores count of distinct values of arr [ i ] - x * i ; iterate over the map ; increase count of pairs ; print the count of such pairs ; 
def countPairs(arr, n, x):
    count = 0
    mp = {}
    for i in range(n):
        if ((arr[i] - x * i) in mp):
            mp[arr[i] - x * i] += 1
        else:
            mp[arr[i] - x * i] = 1
    for key, value in mp .items():
        n = value
        count += (n * (n - 1)) // 2
    print(count)


if __name__ == '__main__':
    n = 6
    x = 3
    arr = [5, 4, 8, 11, 13, 16]
    countPairs(arr, n, x)

# Expected output:
4
# check if a triplet of buildings can be selected such that the third building is taller than the first building and smaller than the second building | function to check if it is possible to select three buildings that satisfy the given condition ; stores prefix min array ; iterate over the range [ 1 , n - 1 ] ; stores the element from the ending in increasing order ; iterate until j is greater than or equal to 0 ; if current array element is greater than the prefix min upto j ; iterate while stack is not empty and top element is less than or equal to premin [ j ] ; remove the top element ; if stack is not empty and top element of the stack is less than the current element ; push the arr [ j ] in stack ; if none of the above case satisfy then return " no " ; 
def recreationalSpot(arr, N):
    if (N < 3):
        return "No"
    preMin = [0] * N
    preMin[0] = arr[0]
    for i in range(1, N):
        preMin[i] = min(preMin[i - 1], arr[i])
    stack = []
    for j in range(N - 1, -1, -1):
        if (arr[j] > preMin[j]):
            while (len(stack) > 0 and stack[-1] <= preMin[j]):
                del stack[-1]
            if (len(stack) > 0 and stack[-1] < arr[j]):
                return "Yes"
            stack .append(arr[j])
    return "No"


if __name__ == '__main__':
    arr = [4, 7, 11, 5, 13, 2]
    size = len(arr)
    print(recreationalSpot(arr, size))

# Expected output:
Yes