giulio98/xlcost-single-prompt huggingface dataset:
# count of triplets in an array with odd sum | function to count the number of unordered triplets such that their sum is an odd integer ; count the number of odd and even integers in the array ; number of ways to create triplets using one odd and two even integers ; number of ways to create triplets using three odd integers ; return answer ; 
def countTriplets(arr, n):
    odd = 0
    even = 0
    for i in range(n):
        if (arr[i] & 1):
            odd += 1
        else:
            even += 1
    c1 = odd * (even * (even - 1)) // 2
    c2 = (odd * (odd - 1) * (odd - 2)) // 6
    return c1 + c2


arr = [4, 5, 6, 4, 5, 10, 1, 7]
n = len(arr)
ans = countTriplets(arr, n)
print(ans)

# Expected output:
28
# longest remaining array of distinct elements possible after repeated removal of maximum and minimum elements of triplets | function to return length of longest remaining array of pairwise distinct array possible by removing triplets ; stores the frequency of array elements ; traverse the array ; iterate through the map ; if frequency of current element is even ; stores the required count of unique elements remaining ; if count is odd ; 
def maxUniqueElements(A, N):
    mp = {}
    for i in range(N):
        if A[i] in mp:
            mp[A[i]] += 1
        else:
            mp[A[i]] = 1
    cnt = 0
    for key, value in mp .items():
        if (value % 2 == 0):
            cnt += 1
    ans = len(mp)
    if (cnt % 2 == 1):
        ans -= 1
    return ans


if __name__ == '__main__':
    N = 5
    A = [1, 2, 1, 3, 7]
    print(maxUniqueElements(A, N))

# Expected output:
3
# count cells in a grid from which maximum number of cells can be reached by k vertical or horizontal jumps | function to count the number of cells in the grid such that maximum cell is reachable with a jump of k ; maximum reachable rows from the current row ; stores the count of cell that are reachable from the current row ; count of reachable rows ; update the maximum value ; add it to the count ; maximum reachable columns from the current column ; stores the count of cell that are reachable from the current column ; count of rechable columns ; update the maximum value ; add it to the count ; return the total count of cells ; 
def countCells(n, m, s):
    mx1 = -1
    cont1 = 0
    i = 0
    while (i < s and i < n):
        aux = (n - (i + 1)) // s + 1
        if (aux > mx1):
            mx1 = cont1 = aux
        elif (aux == mx1):
            cont1 += aux
        i += 1
    mx2 = -1
    cont2 = 0
    i = 0
    while (i < s and i < m):
        aux = (m - (i + 1)) // s + 1
        if (aux > mx2):
            mx2 = cont2 = aux
        elif (aux == mx2):
            cont2 += aux
        i += 1
    return cont1 * cont2


if __name__ == '__main__':
    N = 5
    M = 5
    K = 2
    print(countCells(N, M, K))

# Expected output:
9
# count of pairs from first n natural numbers with remainder at least k | function to count the number of pairs ( a , b ) such that a % b is at least k ; base case ; stores resultant count of pairs ; iterate over the range [ k + 1 , n ] ; find the cycled elements ; find the remaining elements ; return the resultant possible count of pairs ; 
def countTotalPairs(N, K):
    if (K == 0):
        return N * N
    ans = 0
    for b in range(K + 1, N + 1):
        ans += (N // b) * (b - K)
        ans += max(N % b - K + 1, 0)
    return ans


N = 5
K = 2
print(countTotalPairs(N, K))

# Expected output:
7
# minimize sum of absolute difference between all pairs of array elements by decrementing and incrementing pairs by 1 | function to find the minimum value of the sum of absolute difference between all pairs of arrays ; stores the sum of array elements ; find the sum of array element ; store the value of sum % n ; return the resultant value ; 
def minSumDifference(ar, n):
    sum = 0
    for i in range(n):
        sum += ar[i]
    rem = sum % n
    return rem * (n - rem)


if __name__ == '__main__':
    arr = [3, 6, 8, 5, 2, 1, 11, 7, 10, 4]
    N = len(arr)
    print(minSumDifference(arr, N))

# Expected output:
21
# count n | python program for the above approach ; function to find the value of x ^ y ; stores the value of x ^ y ; iterate until y is positive ; if y is odd ; divide y by 2 ; return the value of x ^ y ; function to find the number of n - digit integers satisfying the given criteria ; count of even positions ; count of odd positions ; return the resultant count ; 

import math
m = 10 ** 9 + 7


def power(x, y):
    res = 1
    while y > 0:
        if (y & 1) != 0:
            res = (res * x) % m
        y = y >> 1
        x = (x * x) % m
    return res


def countNDigitNumber(n: int) -> None:
    ne = N // 2 + N % 2
    no = N // 2
    return power(4, ne) * power(5, no)


if __name__ == '__main__':
    N = 5
    print(countNDigitNumber(N) % m)

# Expected output:
1600
# minimum jumps to traverse all integers in range [ 1 , n ] such that integer i can jump i steps | utility function to find minimum steps ; initialize count and result ; traverse over the range [ 1 , n ] ; update res ; increment count ; return res ; 
def minSteps(N):
    count = 1
    res = 0
    for i in range(1, N + 1, count):
        res = max(res, count)
        count += 1
    return res


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

# Expected output:
6
# minimum jumps to traverse all integers in range [ 1 , n ] such that integer i can jump i steps | python 3 implementation of the above approach ; utility function to find minimum steps ; 

from math import sqrt


def minSteps(N):
    res = int((sqrt(1 + 8 * N) - 1) // 2)
    return res


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

# Expected output:
3
# the dice problem | function to find number written on the opposite face of the dice ; stores number on opposite face of dice ; print the answer ; 
def oppositeFaceOfDice(N):
    ans = 7 - N
    print(ans)


N = 2
oppositeFaceOfDice(N)

# Expected output:
5
# count numbers from a given range that can be visited moving any number of steps from the range [ l , r ] | function to count points from the range [ x , y ] that can be reached by moving by l or r steps ; initialize difference array ; initialize count ; marking starting point ; iterating from x to y ; accumulate difference array ; if diff_arr [ i ] is greater than 1 ; updating difference array ; visited point found ; 
def countReachablePoints(X, Y, L, R):
    diff_arr = [0 for i in range(100000)]
    count = 0
    diff_arr[X] = 1
    diff_arr[X + 1] = -1
    for i in range(X, Y + 1, 1):
        diff_arr[i] += diff_arr[i - 1]
        if (diff_arr[i] >= 1):
            diff_arr[i + L] += 1
            diff_arr[i + R + 1] -= 1
            count += 1
    return count


if __name__ == '__main__':
    X = 3
    Y = 12
    L = 2
    R = 3
    print(countReachablePoints(X, Y, L, R))

# Expected output:
9
# minimum length of the subarray required to be replaced to make frequency of array elements equal to n / m | function to find the minimum length of the subarray to be changed . ; stores the frequencies of array elements ; stores the number of array elements that are present more than n / m times ; iterate over the range ; increment the frequency ; if the frequency of all array elements are already n / m ; stores the resultant length of the subarray ; the left and right pointers ; iterate over the range ; if the current element is ; if the value of c is 0 , then find the possible answer ; iterate over the range ; if the element at left is making it extra ; update the left pointer ; update the right pointer ; return the resultant length ; 
def minimumSubarray(arr, n, m):
    mapu = [0 for i in range(m + 1)]
    c = 0
    for i in range(n):
        mapu[arr[i]] += 1
        if (mapu[arr[i]] == (n // m) + 1):
            c += 1
    if (c == 0):
        return 0
    ans = n
    l = 0
    r = 0
    while (r < n):
        mapu[arr[r]] -= 1
        if (mapu[arr[r]] == (n // m)):
            c -= 1
        if (c == 0):
            while (l <= r and c == 0):
                ans = min(ans, r - l + 1)
                mapu[arr[l]] += 1
                if (mapu[arr[l]] > (n // m)):
                    c += 1
                l += 1
        r += 1
    return ans


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

# Expected output:
1
# check if the sum of k least and most frequent array elements are equal or not | function to compare the sum of k most and least occurrences ; stores frequency of array element ; stores the frequencies as indexes and putelements with the frequency in a vector ; find the frequency ; insert in the vector ; stores the count of elements ; traverse the frequency array ; find the kleastfreqsum ; if the count is k , break ; reinitialize the count to zero ; traverse the frequency ; find the kmostfreqsum ; if the count is k , break ; comparing the sum ; otherwise , return no ; 
def checkSum(arr, n, k):
    m = {}
    for i in range(n):
        if arr[i] in m:
            m[arr[i]] += 1
        else:
            m[arr[i]] = 1
    freq = [[]for i in range(n + 1)]
    for i in range(n):
        f = m[arr[i]]
        if (f != -1):
            freq[f].append(arr[i])
            m[arr[i]] = -1
    count = 0
    kleastfreqsum = 0
    for i in range(n + 1):
        for x in freq[i]:
            kleastfreqsum += x
            count += 1
            if (count == k):
                break
        if (count == k):
            break
    count = 0
    kmostfreqsum = 0
    i = n
    while (i >= 0):
        for x in freq[i]:
            kmostfreqsum += x
            count += 1
            if (count == k):
                break
        i -= 1
        if (count == k):
            break
    if (kleastfreqsum == kmostfreqsum):
        return "Yes"
    return "No"


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

# Expected output:
Yes
# print all numbers from a given range that are made up of consecutive digits | function to find the consecutive digit numbers in the given range ; initialize num as empty string ; stores the resultant number ; iterate over the range [ 1 , 9 ] ; check if the current number is within range ; iterate on the digits starting from i ; checking the consecutive digits numbers starting from i and ending at j is within range or not ; sort the numbers in the increasing order ; 
def solve(start, end):
    num = ""
    ans = []
    for i in range(1, 10, 1):
        num = str(i)
        value = int(num)
        if (value >= start and value <= end):
            ans .append(value)
        for j in range(i + 1, 10, 1):
            num += str(j)
            value = int(num)
            if (value >= start and value <= end):
                ans .append(value)
    ans .sort()
    return ans


if __name__ == '__main__':
    L = 12
    R = 87
    ans = solve(12, 87)
    for it in ans:
        print(it, end=" ")

# Expected output:
12 23 34 45 56 67 78
# minimum distance a person has to move in order to take a picture of every racer | python3 program for the above approach ; function to return the minimum distance the person has to move order to get the pictures ; to store the minimum ending point ; to store the maximum starting point ; find the values of minseg and maxseg ; impossible ; the person doesn 't need to move ; get closer to the left point ; get closer to the right point ; 

import sys


def minDistance(start, end, n, d):
    left = -sys .maxsize
    right = sys .maxsize
    for i in range(n):
        left = max(left, start[i])
        right = min(right, end[i])
    if (left > right):
        return -1
    if (d >= left and d <= right):
        return 0
    if (d < left):
        return (left - d)
    if (d > right):
        return (d - right)


start = [0, 2, 4]
end = [7, 14, 6]
n = len(start)
d = 3
print(minDistance(start, end, n, d))

# Expected output:
1
# maximum number of armstrong numbers present in a subarray of size k | function to calculate the value of x raised to the power y in o ( log y ) ; base case ; if the power y is even ; otherwise ; function to calculate the order of the number , i . e . count of digits ; stores the total count of digits ; iterate until num is 0 ; function to check a number is an armstrong number or not ; find the order of the number ; check for armstrong number ; if armstrong number condition is satisfied ; utility function to find the maximum sum of a subarray of size k ; if k is greater than n ; find the sum of first subarray of size k ; find the sum of the remaining subarray ; return the maximum sum of subarray of size k ; function to find all the armstrong numbers in the array ; traverse the array arr [ ] ; if arr [ i ] is an armstrong number , then replace it by 1. otherwise , 0 ; return the resultant count ; 
def power(x, y):
    if (y == 0):
        return 1
    if (y % 2 == 0):
        return power(x, y // 2) * power(x, y // 2)
    return x * power(x, y // 2) * power(x, y // 2)


def order(num):
    count = 0
    while (num):
        count += 1
        num = num // 10
    return count


def isArmstrong(N):
    r = order(N)
    temp = N
    sum = 0
    while (temp):
        d = temp % 10
        sum += power(d, r)
        temp = temp // 10
    return (sum == N)


def maxSum(arr, N, K):
    if (N < K):
        return -1
    res = 0
    for i in range(K):
        res += arr[i]
    curr_sum = res
    for i in range(K, N, 1):
        curr_sum += arr[i] - arr[i - K]
        res = max(res, curr_sum)
    return res


def maxArmstrong(arr, N, K):
    for i in range(N):
        arr[i] = isArmstrong(arr[i])
    return maxSum(arr, N, K)


if __name__ == '__main__':
    arr = [28, 2, 3, 6, 153, 99, 828, 24]
    K = 6
    N = len(arr)
    print(maxArmstrong(arr, N, K))

# Expected output:
4
# count ways to replace ' ? ' in a binary string to make the count of 0 s and 1 s same as that of another string | function to find the factorial of the given number n ; stores the factorial ; iterate over the range [ 2 , n ] ; return the resultant result ; function to find the number of ways of choosing r objects from n distinct objects ; function to find the number of ways to replace ' ? ' in string t to get the same count of 0 s and 1 s in the string s and t ; traverse the string s ; if the current character is 1 ; update the value of the sum1 ; otherwise ; traverse the string t ; if the current character is 1 , then update the value of sum2 ; if the current character is 0 ; otherwise , update the value of k ; check if p is greater than k or if k - p is odd ; print the count of ways ; 
def fact(n):
    res = 1
    for i in range(2, n + 1, 1):
        res = res * i
    return res


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


def countWays(s, t):
    n = len(s)
    sum1 = 0
    sum2 = 0
    K = 0
    for i in range(n):
        if (s[i] == '1'):
            sum1 += 1
        else:
            sum1 -= 1
    m = len(t)
    for i in range(m):
        if (t[i] == '1'):
            sum2 += 1
        elif (t[i] == '0'):
            sum2 -= 1
        else:
            K += 1
    P = abs(sum1 - sum2)
    if (P > K or (K - P) % 2):
        print(0)
        return
    print(nCr(K, (P + K) // 2))


if __name__ == '__main__':
    S1 = "1010"
    S2 = "10??"
    countWays(S1, S2)

# Expected output:
2
# count pairs of indices having sum of indices same as the sum of elements at those indices | function to find all possible pairs of the given array such that the sum of arr [ i ] + arr [ j ] is i + j ; stores the total count of pairs ; iterate over the range ; iterate over the range ; print the total count ; 
def countPairs(arr, N):
    answer = 0
    for i in range(N):
        for j in range(i + 1, N):
            if arr[i] + arr[j] == i + j:
                answer += 1
    print(answer)


arr = [0, 1, 2, 3, 4, 5]
N = len(arr)
countPairs(arr, N)

# Expected output:
15
# distribute r , b beans such that each packet has at least 1 r and 1 b bean with absolute difference at most d | function to check if it is possible to distribute r red and b blue beans in packets such that the difference between the beans in each packet is atmost d ; check for the condition to distributing beans ; print the answer ; distribution is not possible ; 
def checkDistribution(R, B, D):
    if (max(R, B) <= min(R, B) * (D + 1)):
        print("Yes")
    else:
        print("No")


R = 1
B = 1
D = 0
checkDistribution(R, B, D)

# Expected output:
Yes
# generate a sequence with product n such that for every pair of indices ( i , j ) and i < j , arr [ j ] is divisible by arr [ i ] | python3 program for the above approach ; function to calculate the prime factors of n with their count ; initialize a vector , say v ; initialize the count ; count the number of divisors ; divide the value of n by 2 ; for factor 2 divides it ; find all prime factors ; count their frequency ; push it to the vector ; push n if it is not 1 ; function to print the array that have the maximum size ; stores the all prime factor and their powers ; traverse the vector and find the maximum power of prime factor ; if max size is less than 2 ; otherwise ; print the maximum size of sequence ; print the final sequence ; print the prime factor ; print the last value of the sequence ; 

from math import sqrt


def primeFactor(N):
    v = []
    count = 0
    while ((N % 2) == 0):
        N >>= 1
        count += 1
    if (count):
        v .append([2, count])
    for i in range(3, int(sqrt(N)) + 1, 2):
        count = 0
        while (N % i == 0):
            count += 1
            N = N / i
        if (count):
            v .append([i, count])
    if (N > 2):
        v .append([N, 1])
    return v


def printAnswer(n):
    v = primeFactor(n)
    maxi_size = 0
    prime_factor = 0
    for i in range(len(v)):
        if (maxi_size < v[i][1]):
            maxi_size = v[i][1]
            prime_factor = v[i][0]
    if (maxi_size < 2):
        print(1, n)
    else:
        product = 1
        print(maxi_size)
        for i in range(maxi_size - 1):
            print(prime_factor, end=" ")
            product *= prime_factor
        print(n // product)


if __name__ == '__main__':
    N = 360
    printAnswer(N)

# Expected output:
3
2 2 90
# check if it is possible to reach destination in even number of steps in an infinite matrix | function to check destination can be reached from source in even number of steps ; coordinates differences ; minimum number of steps required ; minsteps is even ; minsteps is odd ; 
def IsEvenPath(Source, Destination):
    x_dif = abs(Source[0] - Destination[0])
    y_dif = abs(Source[1] - Destination[1])
    minsteps = x_dif + y_dif
    if (minsteps % 2 == 0):
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    Source = [2, 1]
    Destination = [1, 4]
    IsEvenPath(Source, Destination)

# Expected output:
Yes
# find the closest fraction to given fraction having minimum absolute difference | function to find the absolute value of x ; function to find the fraction with minimum absolute difference ; initialize the answer variables ; iterate over the range ; nearest fraction ; x / y - d / i < x / y - a / b ( b * x - y * a ) * ( i * y ) > ( i * x - y * d ) * ( b * y ) ; check for d + 1 ; print the answer ; 
def ABS(x):
    return max(x, -x)


def findFraction(x, y, n):
    A = -1
    B = -1
    for i in range(1, n + 1):
        d = (i * x) // y
        if (d >= 0 and (A == -1 or ABS(B * x - y * A) *
                        ABS(i * y) > ABS(i * x - y * d) * ABS(B * y))):
            A = d
            B = i
        d += 1
        if (d >= 0 and (A == -1 or ABS(B * x - y * A) *
                        ABS(i * y) > ABS(i * x - y * d) * ABS(B * y))):
            A = d
            B = i
    print(str(A) + "/" + str(B))


if __name__ == '__main__':
    x = 3
    y = 7
    n = 6
    findFraction(x, y, n)

# Expected output:
2/5
# represent a number n in base | function to convert n to equivalent representation in base - 2 ; stores the required answer ; iterate until n is not equal to zero ; if n is even ; add char '0' in front of string ; add char '1' in front of string ; decrement n by 1 ; divide n by - 2 ; if string is empty , that means n is zero ; put '0' in string s ; 
def BaseConversion(N):
    s = ""
    while (N != 0):
        if (N % 2 == 0):
            s = "0" + s
        else:
            s = "1" + s
            N -= 1
        N /= -2
    if (s == ""):
        s = "0"
    return s


N = -9
print(BaseConversion(N))

# Expected output:
1011
# find instances at end of time frame after auto scaling | python program for the above approach ; function to find the number of instances after completion ; traverse the array , arr [ ] ; if current element is less than 25 ; divide instances by 2 ; if the current element is greater than 60 ; double the instances ; print the instances at the end of the traversal ; 

from math import ceil


def finalInstances(instances, arr):
    i = 0
    while i < len(arr):
        if arr[i] < 25 and instances > 1:
            instances = ceil(instances / 2)
            i += 10
        elif arr[i] > 60 and instances <= 10 ** 8:
            instances *= 2
            i += 10
        i += 1
    print(instances)


instances = 2
arr = [25, 23, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 76, 80]
finalInstances(instances, arr)

# Expected output:
2
# modify array by making all array elements equal to 0 by subtracting k ^ i from an array element in every i | function to check whether all array elements can be made zero or not ; stores if a power of k has already been subtracted or not ; stores all the kth power ; iterate until x is less than int_max ; traverse the array arr [ ] ; iterate over the range [ 0 , m ] ; if mp [ v [ j ] ] is 0 and v [ j ] is less than or equal to arr [ i ] ; if arr [ i ] is not 0 ; if i is less than n - 1 ; otherwise , ; 
def isMakeZero(arr, N, K):
    MP = {}
    V = []
    X = 1
    while (X > 0 and X < 10 ** 20):
        V .append(X)
        X *= K
    for i in range(0, N, 1):
        for j in range(len(V) - 1, -1, -1):
            if (V[j]not in MP and V[j] <= arr[i]):
                arr[i] -= V[j]
                MP[V[j]] = 1
        if (arr[i] != 0):
            break
    if (i < N - 1):
        return "No"
    else:
        return "Yes"


arr = [8, 0, 3, 4, 80]
N = len(arr)
K = 2
print(isMakeZero(arr, N, K))

# Expected output:
Yes
# check whether the product of every subsequence is a perfect square or not | python 3 program for the above approach ; function to check if the product of every subsequence of the array is a perfect square or not ; traverse the given array ; if arr [ i ] is a perfect square or not ; return " yes " ; 

from math import sqrt


def perfectSquare(arr, N):
    for i in range(N):
        p = sqrt(arr[i])
        if (p * p != arr[i]):
            return "No"
    return "Yes"


if __name__ == '__main__':
    arr = [1, 4, 100]
    N = len(arr)
    print(perfectSquare(arr, N))

# Expected output:
Yes
# count of subarrays with average k | function to count subarray having average exactly equal to k ; to store the final answer ; calculate all subarrays ; calculate required average ; check if average is equal to k ; required average found ; increment res ; 
def countKAverageSubarrays(arr, n, k):
    res = 0
    for L in range(n):
        sum = 0
        for R in range(L, n, 1):
            sum += arr[R]
            len1 = (R - L + 1)
            if (sum % len1 == 0):
                avg = sum // len1
                if (avg == k):
                    res += 1
    return res


if __name__ == '__main__':
    K = 6
    arr = [12, 5, 3, 10, 4, 8, 10, 12, -6, -1]
    N = len(arr)
    print(countKAverageSubarrays(arr, N, K))

# Expected output:
4