giulio98/xlcost-single-prompt huggingface dataset:
# chocolate distribution problem | set 2 | function to return minimum number of chocolates ; decreasing sequence ; add the chocolates received by that person ; end point of decreasing sequence ; val = 1 reset value at that index ; increasing sequence ; flat sequence ; add value of chocolates at position n - 1 ; helper function to get sum of decreasing sequence ; value obtained from decreasing sequence also the count of values in the sequence ; assigning max of values obtained from increasing and decreasing sequences ; sum of count - 1 values & peak value sum of natural numbers : ( n * ( n + 1 ) ) / 2 ; 
def get_sum(peak, start, end):
    count = end - start + 1
    peak = max(peak, count)
    s = peak + (((count - 1) * count) >> 1)
    return s

def minChocolates(a, n):
    i, j = 0, 0
    val, res = 1, 0
    while (j < n - 1):
        if (a[j] > a[j + 1]):
            j += 1
            continue
        if (i == j):
            res += val
        else:
            res += get_sum(val, i, j)
        if (a[j] < a[j + 1]):
            val += 1
        else:
            val = 1
        j += 1
        i = j
    if (i == j):
        res += val
    else:
        res += get_sum(val, i, j)
    return res


if __name__ == '__main__':
    a = [5, 5, 4, 3, 2, 1]
    n = len(a)
    print('Minimum number of chocolates =', minChocolates(a, n))

# Expected output:
Minimum number of chocolates = 16
# remove array elements to reduce frequency of each array element to at most k | function to remove array elements such that frequency of each distinct array element is at most k ; base case ; stores index of array element by removing the array element such that the frequency of array elements is at most k ; traverse the array ; if j < k or arr [ i ] > arr [ j - k ] ; update arr [ j ] ; remove array elements ; function to print the array ; traverse the array ; utility function to remove array elements such that frequency of each distinct array element is at most k ; print updated array ; 
def RemoveElemArr(arr, n, k):
    if (n == 0 or n == 1):
        return arr
    j = 0
    for i in range(n):
        if (j < k or arr[i] > arr[j - k]):
            arr[j], j = arr[i], j + 1
    while (len(arr) > j):
        del arr[-1]
    return arr


def printArray(arr):
    for i in arr:
        print(i, end=" ")


def UtilRemov(arr, n, k):
    arr = RemoveElemArr(arr, n, k)
    printArray(arr)


if __name__ == '__main__':
    arr = [1, 2, 2, 3, 4, 4, 4, 5, 5]
    k = 2
    n = len(arr)
    UtilRemov(arr, n, k)

# Expected output:
1 2 2 3 4 4 5 5
# count pairs of equal array elements remaining after every removal | function to count pairs of equal elements by removing arr [ i ] from the array ; stores total count of pairs of equal elements ; store frequency of each distinct array element ; traverse the array ; update frequency of arr [ i ] ; traverse the map ; stores key of an element ; traverse the array ; stores count of pairs of equal element by removing arr [ i ] ; 
def pairs_after_removing(arr, N):
    cntPairs = 0
    mp = {}
    for i in arr:
        mp[i] = mp .get(i, 0) + 1
    for element in mp:
        i = element
        cntPairs += mp[i] * (mp[i] - 1) // 2
    for i in range(N):
        pairs_after_arr_i_removed = cntPairs + 1 - mp[arr[i]]
        print(pairs_after_arr_i_removed, end=' ')
    return


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

# Expected output:
1 1 2 1 1
# modify n by adding its smallest positive divisor exactly k times | function to find the smallest divisor of n greater than 1 ; if i is a divisor of n ; if n is a prime number ; function to find the value of n by performing the operations k times ; if n is an even number ; update n ; if n is an odd number ; update n ; 
def smallestDivisorGr1(N):
    for i in range(sqrt(N)):
        i += 1
    if (N % i == 0):
        return i
    return N


def findValOfNWithOperat(N, K):
    if (N % 2 == 0):
        N += K * 2
    else:
        N += smallestDivisorGr1(N) + (K - 1) * 2
    return N


N = 6
K = 4
print(findValOfNWithOperat(N, K))

# Expected output:
14
# partition array into minimum number of equal length subsets consisting of a single distinct value | python3 program to implement the above approach ; function to find the minimum count of subsets by partitioning the array with given conditions ; store frequency of each distinct element of the array ; traverse the array ; update frequency of arr [ i ] ; stores gcd of frequency of each distinct element of the array ; update freqgcd ; 

from math import gcd


def CntOfSubsetsByPartitioning(arr, N):
    freq = {}
    for i in range(N):
        freq[arr[i]] = freq .get(arr[i], 0) + 1
    freqGCD = 0
    for i in freq:
        freqGCD = gcd(freqGCD, freq[i])
    return (N) // freqGCD


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

# Expected output:
4
# construct a matrix such that each cell consists of sum of adjacent elements of respective cells in given matrix | initialize rows and columns ; store all 8 directions ; function to check if a cell ( i , j ) is valid or not ; function to find sum of adjacent cells for cell ( i , j ) ; initialize sum ; visit all 8 directions ; check if cell is valid ; return sum ; function to print sum of adjacent elements ; stores the resultant matrix ; iterate each elements of matrix ; find adjacent sum ; 

r, c = 0, 0
dir = [[1, 0], [-1, 0], [0, 1], [0, -1], [-1, -1], [-1, 1], [1, 1], [1, -1]]


def valid(i, j):
    if (i >= 0 and j >= 0 and i < r and j < c):
        return True
    return False


def find(i, j, v):
    s = 0
    for k in range(8):
        ni = i + dir[k][0]
        nj = j + dir[k][1]
        if (valid(ni, nj)):
            s += v[ni][nj]
    return s


def findsumofneighbors(M):
    v = [[0 for i in range(c)]for j in range(r)]
    for i in range(r):
        for j in range(c):
            v[i][j] = find(i, j, M)
            print(v[i][j], end=" ")
        print("")


if __name__ == '__main__':
    M = [[1, 4, 1], [2, 4, 5], [3, 1, 2]]
    r = len(M[0])
    c = len(M[1])
    findsumofneighbors(M)

# Expected output:
10 13 13 
13 19 12 
7 16 10
# nth term of a recurrence relation generated by two given arrays | python3 program for the above approach ; declare t [ ] [ ] as global matrix ; result matrix ; function to multiply two matrices ; create an auxiliary matrix to store elements of the multiplication matrix ; iterate over range [ 0 , k ] ; update temp [ i ] [ j ] ; update the final matrix ; function to multiply two matrices ; create an auxiliary matrix to store elements of the multiplication matrix ; iterate over range [ 0 , k ] ; update temp [ i ] [ j ] ; update the final matrix ; function to calculate matrix ^ n using binary exponentaion ; initialize result matrix and unity matrix ; function to calculate nth term of general recurrence ; fill t [ ] [ ] with appropriate value ; function call to calculate t ^ n ; calculate nth term as first element of f * ( t ^ n ) ; print the result ; 

mod = 1e9 + 7
T = [[0 for x in range(2000)]for y in range(2000)]
result = [[0 for x in range(2000)]for y in range(2000)]


def mul_2(K):
    temp = [[0 for x in range(K + 1)]for y in range(K + 1)]
    for i in range(1, K + 1):
        for j in range(1, K + 1):
            for k in range(1, K + 1):
                temp[i][j] = ((temp[i][j] + (T[i][k] * T[k][j]) % mod) % mod)
    for i in range(1, K + 1):
        for j in range(1, K + 1):
            T[i][j] = temp[i][j]


def mul_1(K):
    temp = [[0 for x in range(K + 1)]for y in range(K + 1)]
    for i in range(1, K + 1):
        for j in range(1, K + 1):
            for k in range(1, K + 1):
                temp[i][j] = (
                    (temp[i][j] + (result[i][k] * T[k][j]) % mod) % mod)
    for i in range(1, K + 1):
        for j in range(1, K + 1):
            result[i][j] = temp[i][j]


def matrix_pow(K, n):
    for i in range(1, K + 1):
        for j in range(1, K + 1):
            if (i == j):
                result[i][j] = 1
    while (n > 0):
        if (n % 2 == 1):
            mul_1(K)
        mul_2(K)
        n //= 2


def NthTerm(F, C, K, n):
    for i in range(1, K + 1):
        T[i][K] = C[K - i]
    for i in range(1, K + 1):
        T[i + 1][i] = 1
    matrix_pow(K, n)
    answer = 0
    for i in range(1, K + 1):
        answer += F[i - 1] * result[i][1]
    print(int(answer))


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

# Expected output:
423
# check if a number has an odd count of odd divisors and even count of even divisors | python3 implementation of the above approach ; function to check if the number is a perfect square ; find floating povalue of square root of x . ; if square root is an integer ; function to check if count of even divisors is even and count of odd divisors is odd ; 

import math


def isPerfectSquare(x):
    sr = pow(x, 1 / 2)
    return ((sr - math .floor(sr)) == 0)


def checkFactors(x):
    if (isPerfectSquare(x)):
        print("Yes")
    else:
        print("No")


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

# Expected output:
Yes
# check whether a given number is an ugly number or not | function to check if a number is an ugly number or not ; base cases ; condition to check if the number is divided by 2 , 3 , or 5 ; 
def isUgly(n):
    if (n == 1):
        return 1
    if (n <= 0):
        return 0
    if (n % 2 == 0):
        return (isUgly(n // 2))
    if (n % 3 == 0):
        return (isUgly(n // 3))
    if (n % 5 == 0):
        return (isUgly(n // 5))
    return 0


if __name__ == "__main__":
    no = isUgly(14)
    if (no == 1):
        print("Yes")
    else:
        print("No")

# Expected output:
No
# maximum possible gcd for a pair of integers with sum n | function to find the required gcd value ; if i is a factor of n ; return the largest factor possible ; if n is a prime number ; 
def maxGCD(N):
    i = 2
    while (i * i <= N):
        if (N % i == 0):
            return N // i
        i += 1
    return 1


N = 33
print("Maximum Possible GCD value is : ", maxGCD(N))

# Expected output:
Maximum Possible GCD value is :  11
# minimize k whose xor with given array elements leaves array unchanged | function to find the minimum value of k in given range ; declare a set ; initialize count variable ; iterate in range [ 1 , 1024 ] ; counter set as 0 ; iterating through the set ; check if the xor calculated is present in the set ; if the value of bitwise xor inside the given set then increment count ; check if the value of count is equal to the size of set ; return minimum value of k ; if no such k is found ; 
def min_value(arr, N):
    x, X, K = 0, 0, 0
    S = set()
    for i in range(N):
        S .add(arr[i])
    count = 0
    for i in range(1, 1024):
        count = 0
        for it in S:
            X = ((i | it) - (i & it))
            if X in S:
                count += 1
        if (count == len(S)):
            K = i
            return K
    return -1


arr = [1, 0, 3, 3, 0, 2]
N = len(arr)
print(min_value(arr, N))

# Expected output:
1
# junction numbers | python3 program for the above approach ; function to find the sum of digits of n ; to store sum of n and sumofdigitsof ( n ) ; extracting digit ; function to check junction numbers ; to store count of ways n can be represented as i + sod ( i ) ; 

import math


def sum1(n):
    sum1 = 0
    while (n != 0):
        r = n % 10
        sum1 = sum1 + r
        n = n // 10
    return sum1


def isJunction(n):
    count = 0
    for i in range(1, n + 1):
        if (i + sum1(i) == n):
            count = count + 1
    return count >= 2


if __name__ == '__main__':
    n = 111
    if (isJunction(n) == 1):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# pointer | function to find the product of digits of a number n ; function that returns true if n is prime else returns false ; corner cases ; this is checked so that we can skip middle five numbers in below loop ; function to return the smallest prime number greater than n ; base case ; loop continuously until isprime returns true for a number greater than n ; function to check pointer - prime numbers ; 
def digProduct(n):
    product = 1
    while (n != 0):
        product = product * (n % 10)
        n = int(n / 10)
    return product


def isPrime(n):
    if (n <= 1):
        return False
    if (n <= 3):
        return True
    if (n % 2 == 0 or n % 3 == 0):
        return False
    i = 5
    while (i * i <= n):
        if (n % i == 0 or n % (i + 2) == 0):
            return False
        i = i + 6
    return True


def nextPrime(N):
    if (N <= 1):
        return 2
    prime = N
    found = False
    while (not found):
        prime = prime + 1
        if (isPrime(prime)):
            found = True
    return prime


def isPointerPrime(n):
    if (isPrime(n) and (n + digProduct(n) == nextPrime(n))):
        return True
    else:
        return False


if __name__ == "__main__":
    N = 23
    if (isPointerPrime(N)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# perfect digital invariants number | function to find the sum of divisors ; function to check whether the given number is perfect digital invariant number or not ; for each digit in temp ; if satisfies perfect digital invariant condition ; if sum exceeds n , then not possible ; 
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 isPerfectDigitalInvariant(x):
    fixed_power = 0
    while True:
        fixed_power += 1
        temp = x
        summ = 0
        while (temp):
            r = temp % 10
            summ = summ + power(r, fixed_power)
            temp = temp // 10
        if (summ == x):
            return (True)
        if (summ > x):
            return (False)


N = 4150
if (isPerfectDigitalInvariant(N)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# brilliant numbers | python3 program for the above approach ; function to generate all prime numbers less than n ; initialize all entries of boolean array as true . a value in isprime [ i ] will finally be false if i is not a prime ; if isprime [ p ] is not changed , then it is a prime ; update all multiples of p ; function to return the number of digits in a number ; function to check if n is a brilliant number ; generating primes using sieve ; traversing all numbers to find first pair ; 

import math


def SieveOfEratosthenes(n, isPrime):
    isPrime[0] = isPrime[1] = False
    for i in range(2, n + 1, 1):
        isPrime[i] = True
    p = 2
    while (p * p <= n):
        if (isPrime[p]):
            for i in range(p * 2, n + 1, p):
                isPrime[i] = False
        p += 1


def countDigit(n):
    return math .floor(math .log10(n) + 1)


def isBrilliant(n):
    flag = 0
    isPrime = [0] * (n + 1)
    SieveOfEratosthenes(n, isPrime)
    for i in range(2, n, 1):
        x = n // i
        if (isPrime[i] and isPrime[x] and x * i == n):
            if (countDigit(i) == countDigit(x)):
                return True
    return False


n = 1711
if (isBrilliant(n)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# icosikaienneagonal number | function to find the nth icosikaienneagonal number ; 
def icosikaienneagonalNum(n):
    return (27 * n * n - 25 * n) // 2


N = 3
print(icosikaienneagonalNum(N))

# Expected output:
84
# find the largest contiguous pair sum in given array | python3 program to find the a contiguous pair from the which has the largest sum ; function to find and return the largest sum contiguous pair ; stores the contiguous pair ; initialize maximum sum ; compare sum of pair with max_sum ; insert the pair ; 

import sys


def largestSumpair(arr, n):
    pair = []
    max_sum = -sys .maxsize - 1
    for i in range(1, n):
        if max_sum < (arr[i] + arr[i - 1]):
            max_sum = arr[i] + arr[i - 1]
            if pair == []:
                pair .append(arr[i - 1])
                pair .append(arr[i])
            else:
                pair[0] = arr[i - 1]
                pair[1] = arr[i]
    return pair


arr = [11, -5, 9, -3, 2]
N = len(arr)
pair = largestSumpair(arr, N)
print(pair[0], end=" ")
print(pair[1], end=" ")

# Expected output:
11 -5
# sum of minimum value of x and y satisfying the equation ax + by = c | python3 program for the above approach ; x and y store solution of equation ax + by = g ; euclidean algorithm ; store_gcd returns the gcd of a and b ; function to find any possible solution ; condition if solution does not exists ; adjusting the sign of x0 and y0 ; function to shift solution ; shifting to obtain another solution ; function to find minimum value of x and y ; g is the gcd of a and b ; store sign of a and b ; if x is less than 0 , then shift solution ; if y is less than 0 , then shift solution ; find intersection such that both x and y are positive ; miny is value of y corresponding to minx ; returns minimum value of x + y ; 

x, y, x1, y1 = 0, 0, 0, 0
x0, y0, g = 0, 0, 0


def gcd(a, b):
    global x, y, x1, y1
    if (b == 0):
        x = 1
        y = 0
        return a
    store_gcd = gcd(b, a % b)
    x = y1
    y = x1 - y1 * (a // b)
    return store_gcd


def possible_solution(a, b, c):
    global x0, y0, g
    g = gcd(abs(a), abs(b))
    if (c % g != 0):
        return 0
    x0 *= c // g
    y0 *= c // g
    if (a < 0):
        x0 *= -1
    if (b < 0):
        y0 *= -1
    return 1


def shift_solution(a, b, shift_var):
    global x, y
    x += shift_var * b
    y -= shift_var * a


def find_min_sum(a, b, c):
    global x, y, g
    x, y, g = 0, 0, 0
    if (possible_solution(a, b, c) == 0):
        return -1
    if (g != 0):
        a //= g
        b //= g
    if a > 0:
        sign_a = 1
    else:
        sign_a = -1
    if b > 0:
        sign_b = 1
    else:
        sign_b = -1
    shift_solution(a, b, -x // b)
    if (x < 0):
        shift_solution(a, b, sign_b)
    minx1 = x
    shift_solution(a, b, y // a)
    if (y < 0):
        shift_solution(a, b, -sign_a)
    minx2 = x
    if (minx2 > x):
        temp = minx2
        minx2 = x
        x = temp
    minx = max(minx1, minx2)
    if (minx > x):
        return -1
    miny = (c - a * x) // b
    return (miny + minx)


a, b, c = 2, 2, 0
print(find_min_sum(a, b, c))

# Expected output:
0
# super | function to check if n is a super - d number ; 
def isSuperdNum(n):
    for d in range(2, 10):
        substring = str(d) * d
        if substring in str(d * pow(n, d)):
            return True
    return False


n = 261
if isSuperdNum(n):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# second hexagonal numbers | function to find n - th term in the series ; 
def findNthTerm(n):
    print(n * (2 * n + 1))


N = 4
findNthTerm(N)

# Expected output:
36
# apocalyptic number | function to check if a number n is apocalyptic ; 
def isApocalyptic(n):
    if '666' in str(2 ** n):
        return True
    return False


N = 157
if (isApocalyptic(157)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# check if given number contains a digit which is the average of all other digits | function which checks if a digits exists in n which is the average of all other digits ; calculate sum of digits in n ; store digits ; increase the count of digits in n ; if average of all digits is integer ; otherwise ; 
def check(n):
    digits = set()
    temp = n
    Sum = 0
    count = 0
    while (temp > 0):
        Sum += temp % 10
        digits .add(temp % 10)
        count += 1
        temp = temp // 10
    if ((Sum % count == 0) and ((int)(Sum / count) in digits)):
        print("Yes")
    else:
        print("No")


n = 42644
check(n)

# Expected output:
Yes
# count of lexicographically smaller characters on right | function to count the smaller characters at the right of index i ; store the length of string ; for each index initialize count as zero ; increment the count if characters are smaller than at ith index ; print the count of characters smaller than the index i ; 
def countSmaller(str):
    n = len(str)
    for i in range(n):
        cnt = 0
        for j in range(i + 1, n):
            if (str[j] < str[i]):
                cnt += 1
        print(cnt, end=" ")


if __name__ == '__main__':
    str = "edcba"
    countSmaller(str)

# Expected output:
4 3 2 1 0
# find the smallest contiguous sum pair in an array | python3 program to find the smallest sum contiguous pair ; function to find the smallest sum contiguous pair ; contiguous pair ; isntialize minimum sum with maximum value ; checking for minimum value ; add to pair ; updating pair ; 

import sys


def smallestSumpair(arr, n):
    pair = []
    min_sum = sys .maxsize
    for i in range(1, n):
        if min_sum > (arr[i] + arr[i - 1]):
            min_sum = arr[i] + arr[i - 1]
            if pair == []:
                pair .append(arr[i - 1])
                pair .append(arr[i])
            else:
                pair[0] = arr[i - 1]
                pair[1] = arr[i]
    return pair


arr = [4, 9, -3, 2, 0]
n = len(arr)
pair = smallestSumpair(arr, n)
print(pair[0], pair[1])

# Expected output:
-3 2
# sum of the products of same placed digits of two numbers | function to find the sum of the products of their corresponding digits ; loop until one of the numbers have no digits remaining ; 
def sumOfProductOfDigits(n1, n2):
    sum1 = 0
    while (n1 > 0 and n2 > 0):
        sum1 += ((n1 % 10) * (n2 % 10))
        n1 = n1 // 10
        n2 = n2 // 10
    return sum1


n1 = 25
n2 = 1548
print(sumOfProductOfDigits(n1, n2))

# Expected output:
48
# program to convert degree to radian | function for conversion ; 
def Convert(degree):
    pi = 3.14159265359
    return (degree * (pi / 180))


degree = 30
radian = Convert(degree)
print(radian)

# Expected output:
0.5235987755983333
# program to convert a binary number to hexa | function to convert bcd to hexadecimal ; iterating through the bits backwards ; computing the hexadecimal number formed so far and storing it in a vector . ; reinitializing all variables for next group . ; printing the hexadecimal number formed so far . ; 
def bcdToHexaDecimal(s):
    len1 = len(s)
    check = 0
    num = 0
    sum = 0
    mul = 1
    ans = []
    i = len1 - 1
    while (i >= 0):
        sum += (ord(s[i]) - ord('0')) * mul
        mul *= 2
        check += 1
        if (check == 4 or i == 0):
            if (sum <= 9):
                ans .append(chr(sum + ord('0')))
            else:
                ans .append(chr(sum + 55))
            check = 0
            sum = 0
            mul = 1
        i -= 1
    len1 = len(ans)
    i = len1 - 1
    while (i >= 0):
        print(ans[i], end="")
        i -= 1


if __name__ == '__main__':
    s = "100000101111"
    bcdToHexaDecimal(s)

# Expected output:
82F
# program to check if n is a myriagon number | python3 implementation to check that a number is a myriagon number or not ; function to check that the number is a myriagon number ; condition to check if the number is a myriagon number ; 

import math


def isMyriagon(N):
    n = (9996 + math .sqrt(79984 * N + 99920016)) / 19996
    return (n - int(n)) == 0


n = 10000
if (isMyriagon(n)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# program to check if n is a heptagonal number | python3 program for the above approach ; function to check if n is a heptagonal number ; condition to check if the number is a heptagonal number ; 

import math


def isheptagonal(N):
    n = (3 + math .sqrt(40 * N + 9)) / 10
    return (n - int(n)) == 0


N = 7
if (isheptagonal(N)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# program to check if n is a enneadecagonal number | python3 program for the above approach ; function to check if number n is a enneadecagonal number ; condition to check if n is a enneadecagonal number ; 

import math


def isenneadecagonal(N):
    n = (15 + math .sqrt(136 * N + 225)) / 34
    return (n - int(n)) == 0


N = 19
if (isenneadecagonal(N)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes