giulio98/xlcost-single-prompt huggingface dataset:
# find if the vacation can be taken or not | function to find if the vacation is possible or not ; find the required number of hours of study ; find the hours of study that can be done if the vacation is taken ; check if the required hours are less than or equal to the hours of study that can be done if the vacation is taken ; 
def isPossible(N, S, C, H, L, T):
    total_time_required = S * C * H
    available_time_after_vacation = (N - L) * T
    if (available_time_after_vacation >= total_time_required):
        return 1
    return 0


N = 12
S = 5
C = 8
H = 3
L = 2
T = 20
if (isPossible(N, S, C, H, L, T)):
    print("Yes")
else:
    print("No")
N = 1
S = 2
C = 3
H = 4
L = 5
T = 6
if (isPossible(N, S, C, H, L, T)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
No
# find an array of size n having exactly k subarrays with sum s | function to find array with k subarrays with sum s ; 
def SubarraysWithSumS(n, k, s):
    for i in range(k):
        print(s, end=" ")
    for i in range(k, n):
        print(s + 1, end=" ")


n = 4
k = 2
s = 3
SubarraysWithSumS(n, k, s)

# Expected output:
3 3 4 4
# minimum number of cuts required to pay salary from n length gold bar | python implementation to find the minimum number of cuts to pay the worker . ; function to find the minimum number of cuts to pay the worker . ; nearest integer to the log value of the number n ; 

import math


def pay(n):
    cuts = int(math .log(n, 2))
    return cuts


if __name__ == "__main__":
    n = 5
    cuts = pay(n)
    print(cuts)
    n = 15
    cuts = pay(n)
    print(cuts)

# Expected output:
2
3
# sum of maximum and minimum prime factor of every number in the array | python3 implementation of the approach ; max_prime [ i ] represent maximum prime number that divides the number i ; min_prime [ i ] represent minimum prime number that divides the number i ; function to store the minimum prime factor and the maximum prime factor in two arrays ; check for prime number if min_prime [ i ] > 0 , then it is not a prime number ; if i is a prime number min_prime number that divide prime number and max_prime number that divide prime number is the number itself . ; if this number is being visited for first time then this divisor must be the smallest prime number that divides this number ; the last prime number that divides this number will be maximum . ; function to find the sum of the minimum and the maximum prime factors of every number from the given array ; pre - calculation ; for every element of the given array ; the sum of its smallest and largest prime factor ; 

MAX = 100000
max_prime = [0] * (MAX + 1)
min_prime = [0] * (MAX + 1)


def sieve(n):
    for i in range(2, n + 1):
        if (min_prime[i] > 0):
            continue
        min_prime[i] = i
        max_prime[i] = i
        j = i + i
        while (j <= n):
            if (min_prime[j] == 0):
                min_prime[j] = i
            max_prime[j] = i
            j += i


def findSum(arr, n):
    sieve(MAX)
    for i in range(n):
        sum = min_prime[arr[i]] + max_prime[arr[i]]
        print(sum, end=" ")


arr = [5, 10, 15, 20, 25, 30]
n = len(arr)
findSum(arr, n)

# Expected output:
10 7 8 7 10 7
# pair of integers ( a , b ) which satisfy the given equations | function to count valid pairs ; 
def pairCount(n, m):
    cnt = 0
    for b in range(int(pow(m, 1 / 2))):
        a = m - b * b
        if (a * a + b == n):
            cnt += 1
    return cnt


if __name__ == '__main__':
    n = 9
    m = 3
    print(pairCount(n, m))

# Expected output:
1
# largest divisor for each element in an array other than 1 and the number itself | python3 implementation of the approach ; divisors array to keep track of the maximum divisor ; function to pre - compute the prime numbers and largest divisors ; visited array to keep track of prime numbers ; 0 and 1 are not prime numbers ; initialising divisors [ i ] = i ; for all the numbers divisible by 2 the maximum divisor will be number / 2 ; if divisors [ i ] is not equal to i then this means that divisors [ i ] contains minimum prime divisor for the number ; update the answer to i / smallest_prime_divisor [ i ] ; condition if i is a prime number ; if divisors [ j ] is equal to j then this means that i is the first prime divisor for j so we update divi [ j ] = i ; if the current element is prime then it has no divisors other than 1 and itself ; 

maxin = 100001
divisors = [0] * (maxin + 1)


def Calc_Max_Div(arr, n):
    vis = [1] * (maxin + 1)
    vis[0] = vis[1] = 0
    for i in range(1, maxin + 1):
        divisors[i] = i
    for i in range(4, maxin + 1, 2):
        vis[i] = 0
        divisors[i] = i // 2
    for i in range(3, maxin + 1, 2):
        if (divisors[i] != i):
            divisors[i] = i // divisors[i]
        if (vis[i] == 1):
            for j in range(i * i, maxin, i):
                vis[j] = 0
                if (divisors[j] == j):
                    divisors[j] = i
    for i in range(n):
        if (divisors[arr[i]] == arr[i]):
            print("-1 ", end="")
        else:
            print(divisors[arr[i]], end=" ")


if __name__ == "__main__":
    arr = [5, 6, 7, 8, 9, 10]
    n = len(arr)
    Calc_Max_Div(arr, n)

# Expected output:
-1 3 -1 4 3 5
# given a number n in decimal base , find the sum of digits in any base b | function to compute sum of digits of number n in base b ; initialize sum with 0 ; compute unit digit of the number ; add unit digit in sum ; update value of number ; 
def sumOfDigit(n, b):
    unitDigit = 0
    sum = 0
    while (n > 0):
        unitDigit = n % b
        sum += unitDigit
        n = n // b
    return sum


n = 50
b = 2
print(sumOfDigit(n, b))

# Expected output:
3
# find the nth digit from right in base b of the given number in decimal base | function to compute nth digit from right in base b ; skip n - 1 digits in base b ; nth digit from right in base b ; 
def nthDigit(a, n, b):
    for i in range(1, n):
        a = a // b
    return a % b


a = 100
n = 3
b = 4
print(nthDigit(a, n, b))

# Expected output:
2
# print all maximal increasing contiguous sub | function to print each of maximal contiguous increasing subarray ; loop to iterate through the array and print the maximal contiguous increasing subarray . ; condition to check whether the element at i , is greater than its next neighbouring element or not . ; 
def printmaxSubseq(arr, n):
    for i in range(n - 1):
        if (arr[i] < arr[i + 1]):
            print(arr[i], end=" ")
        else:
            print(arr[i])
    print(arr[n - 1])


if __name__ == "__main__":
    arr = [9, 8, 11, 13, 10, 15, 14, 16, 20, 5]
    n = len(arr)
    printmaxSubseq(arr, n)

# Expected output:
9
8 11 13
10 15
14 16 20
5
# count of non decreasing arrays of length n formed with values in range l to r | function to return the count of different arrays ; no such combination exists ; arrays formed with single elements ; 
def countSum(N, L, R):
    if (L > R):
        return 0
    if (N == 1):
        return R - L + 1
    if (N > 1):
        return (N - 2) * (R - L) + 1
    return 0


if __name__ == '__main__':
    N, L, R = 4, 4, 6
    print(countSum(N, L, R))

# Expected output:
5
# minimum decrement operations to make array elements equal by only decreasing k each time | python3 implementation of the above approach ; finding the minimum element ; loop over all the elements and find the difference ; solution found and returned ; 

import sys


def solve(arr, n, k):
    minx = sys .maxsize
    for i in range(n):
        minx = min(minx, arr[i])
    decrements = 0
    for i in range(n):
        if ((arr[i] - minx) % k != 0):
            return -1
        else:
            decrements += ((arr[i] - minx) // k)
    return decrements


if __name__ == "__main__":
    n = 3
    k = 3
    arr = [12, 9, 15]
    print(solve(arr, n, k))

# Expected output:
3
# count non decreasing subarrays of size n from n natural numbers | returns value of binomial coefficient c ( n , k ) ; since nc0 is 1 ; compute next row of pascal triangle using the previous row ; function to find the count of required subarrays ; the required count is the binomial coefficient as explained in the approach above ; 
def binomialCoeff(n, k):
    C = [0] * (k + 1)
    C[0] = 1
    for i in range(1, n + 1):
        for j in range(min(i, k), 0, -1):
            C[j] = C[j] + C[j - 1]
    return C[k]


def count_of_subarrays(N):
    count = binomialCoeff(2 * N - 1, N)
    return count


if __name__ == "__main__":
    N = 3
    print(count_of_subarrays(N))

# Expected output:
10
# length of smallest subarray in range 1 to n with sum greater than a given value | function to do a binary search on a given range . ; total sum is the sum of n numbers . ; sum until mid ; if remaining sum is < the required value , then the required number is in the right half ; 
def usingBinarySearch(start, end, N, S):
    if (start >= end):
        return start
    mid = start + (end - start) // 2
    totalSum = (N * (N + 1)) // 2
    midSum = (mid * (mid + 1)) // 2
    if ((totalSum - midSum) <= S):
        return usingBinarySearch(start, mid, N, S)
    return usingBinarySearch(mid + 1, end, N, S)


if __name__ == "__main__":
    N = 5
    S = 11
    print(N - usingBinarySearch(1, N, N, S) + 1)

# Expected output:
3
# distribution of a number in array within a range | function for the distribution of the number ; distribute the number among k elements ; if there is some remaining sum to distribute ; if there are elements remaining to distribute i . e . ( n - k ) ; divide the remaining sum into n - k elements ; print the distribution ; 
def distribution(n, k, l, r, S, Sk):
    a = [0] * n
    len = k
    temp, rem, s = 0, 0, 0
    diff = S - Sk
    for i in range(len):
        temp = Sk / k
        rem = Sk % k
        if (temp + rem >= l and temp + rem <= r):
            a[i] = temp
        elif (temp + rem > r):
            a[i] = r
        elif (temp + rem < r):
            print("-1")
            return
        Sk = Sk - a[i]
        k = k - 1
    if (Sk > 0):
        print("-1")
        return
    if (len != 0):
        k = n - len
        for i in range(len, n):
            temp = diff / k
            rem = diff % k
            if (temp + rem >= l and temp + rem <= r):
                a[i] = temp
            elif (temp + rem > r):
                a[i] = r
            elif (temp + rem < r):
                print("-1")
                return
            diff = diff - a[i]
            k = k - 1
        if (diff != 0):
            print("-1")
            return
    for i in range(n):
        print(int(a[i]), end=" ")


if __name__ == '__main__':
    n, k, l, r, S, Sk = 5, 3, 1, 5, 13, 9
    distribution(n, k, l, r, S, Sk)

# Expected output:
3 3 3 2 2
# number of valid indices in the permutation of first n natural numbers | python3 implementation of the approach ; function to return the number of i 's such that pi <= pj for all 1 <= j <= i in the permutation of first n natural numbers ; to store the count of such indices ; store the mini value ; for all the elements ; return the required answer ; 

import sys
INT_MAX = sys .maxsize


def min_index(p, n):
    ans = 0
    mini = INT_MAX
    for i in range(n):
        if (p[i] <= mini):
            mini = p[i]
        if (mini == p[i]):
            ans += 1
    return ans


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

# Expected output:
3
# check if sum and xor of all elements of array is equal | function to check if sum and xor of all elements of array is equal ; sum and xor of all elements ; checking sum and xor to be equal ; 
def equal_xor_sum(arr, n):
    Sum = 0
    Xor = 0
    for i in range(n):
        Sum = Sum + arr[i]
        Xor = Xor ^ arr[i]
    if (Sum == Xor):
        print("YES")
    else:
        print("NO")


if __name__ == "__main__":
    arr = [6, 3, 7, 10]
    n = len(arr)
    equal_xor_sum(arr, n)

# Expected output:
NO
# count of odd length contiguous palindromic sequences in a matrix | python code to count the odd length contiguous palindromic sequences in the matrix ; function to count the number of contiguous palindromic sequences in the matrix ; add the total number of elements in the matrix to the count ; length of possible sequence to be checked for palindrome horizontally and vertically ; iterate through each element of the matrix and count the number of palindromic sequences in each row and column ; find the possible length of sequences that can be a palindrome ; from i , check if the sequence formed by elements to its left and right is palindrome or not ; if the sequence [ i , j - k ] to [ i , j + k ] is a palindrome , increment the count by 1 ; from i , check if the sequence formed by elements to its above and below is palindrome or not ; if the sequence [ i - k , j ] to [ i + k , j ] is a palindrome , increment the count by 1 ; return the total count of the palindromic sequences ; 

MAX = 10


def countPalindromes(n, m, matrix):
    count = n * m
    length_of_sequence_row = 0
    length_of_sequence_column = 0
    for i in range(n):
        for j in range(m):
            length_of_sequence_row = min(j, m - 1 - j)
            length_of_sequence_column = min(i, n - i - 1)
            for k in range(1, length_of_sequence_row + 1):
                if (matrix[i][j - k] == matrix[i][j + k]):
                    count += 1
                else:
                    break
            for k in range(1, length_of_sequence_column + 1):
                if (matrix[i - k][j] == matrix[i + k][j]):
                    count += 1
                else:
                    break
    return count


if __name__ == '__main__':
    m = 3
    n = 3
    matrix = [2, 1, 2], [1, 1, 1], [2, 1, 2]
    print(countPalindromes(n, m, matrix))

# Expected output:
15
# reduce a number to 1 by performing given operations | set 2 | function to return the number of set bits in n ; function to return the minimum steps required to reach 1 ; if n is even then divide it by 2 ; if n is 3 or the number of set bits in ( n - 1 ) is less than the number of set bits in ( n + 1 ) ; increment the number of steps ; return the minimum number of steps ; 
def set_bits(n):
    count = 0
    while (n):
        count += n % 2
        n //= 2
    return count


def minSteps(n):
    ans = 0
    while (n != 1):
        if (n % 2 == 0):
            n //= 2
        elif (n == 3 or set_bits(n - 1) < set_bits(n + 1)):
            n -= 1
        else:
            n += 1
        ans += 1
    return ans


n = 15
print(minSteps(n))

# Expected output:
5
# minimum integer that can be obtained by swapping adjacent digits of different parity | function to return the minimum number ; store the elements which are divisible by two in stack1 ; store the elements which are not divisible by two in stack2 . ; concatenate the answer with smaller value of the topmost elements of both the stacks and then pop that element ; concatenate the answer with remaining values of stack1 . ; concatenate the answer with remaining values of stack2 . ; 
def minimumNo(n):
    ans = 0
    stack1 = []
    stack2 = []
    while (n != 0):
        r = n % 10
        if (r % 2 == 0):
            stack1 .append(r)
        else:
            stack2 .append(r)
        n = n // 10
    while (len(stack1) > 0 and len(stack2) > 0):
        if (stack1[-1] < stack2[-1]):
            ans = ans * 10 + stack1[-1]
            del stack1[-1]
        else:
            ans = ans * 10 + stack2[-1]
            del stack2[-1]
    while (len(stack1) > 0):
        ans = ans * 10 + stack1[-1]
        del stack1[-1]
    while (len(stack2) > 0):
        ans = ans * 10 + stack2[-1]
        del stack2[-1]
    return ans


n1 = 64432
print(minimumNo(n1))
n2 = 3137
print(minimumNo(n2))

# Expected output:
36442
3137
# count of possible arrays from prefix | function to find power of a number . ; function to find factorial of a number . ; function to print no of arrays ; c variable counts the no of pairs ; map to store the frequency of each element ; sum of all elements of the array ; variable to check if it is possible to make any array ; first element of suffix array and the last element of prefix array ; check if the element exists in the map ; if elements of any pair are equal and their frequency is not divisible by 2 update the isarraypossible variable to false and break through the loop ; if elements of any pair are not equal and their frequency is not same update the isarraypossible variable to false and break through the loop ; check if frequency is greater than zero ; update the count of pairs ; multiply the answer by 2 ^ ( frequency of pairs ) since the elements of the pair are not the same in this condition ; divide the answer by the factorial of no of similar pairs ; make frequency of both these elements 0 ; update the count of pairs ; divide the answer by the factorial of no . of similar pairs ; make frequency of this element 0 ; check if it is possible to make the array and there are n - 1 pairs whose sum will be equal to s1 ; 
def power(a, b):
    result = 1
    while (b > 0):
        if (b % 2 == 1):
            result = result * a
        a = a * a
        b = b // 2
    return result


def factorial(n):
    fact = 1
    for i in range(1, n + 1):
        fact = fact * i
    return fact


def findNoOfArrays(a, n):
    sum = 0
    c = 0
    mp = dict .fromkeys(a, 0)
    for i in range(2 * n):
        mp[a[i]] += 1
        sum = sum + a[i]
    isArrayPossible = True
    ans = factorial(n - 1)
    s1 = sum // (n + 1)
    if (mp[s1] >= 2):
        mp[s1] = mp[s1] - 2
    else:
        isArrayPossible = False
    if (isArrayPossible):
        for first, second in mp .items():
            if (first == s1 - first):
                if (mp[first] % 2 != 0):
                    isArrayPossible = False
                    break
            if (first != s1 - first):
                if s1 - first in mp:
                    if (mp[first] != mp[s1 - first]):
                        isArrayPossible = False
                        break
            if (second > 0):
                if (first != s1 - first):
                    c = c + second
                    ans = ans * power(2, second)
                    ans = ans / factorial(second)
                    mp[first] = 0
                    mp[s1 - first] = 0
                if (first == s1 - first):
                    c = c + second // 2
                    ans = ans // factorial(second // 2)
                    mp[first] = 0
    if (c < n - 1 or isArrayPossible == False):
        print("0")
    else:
        print(ans)


if __name__ == "__main__":
    arr1 = [5, 2, 3, 5]
    n1 = len(arr1)
    findNoOfArrays(arr1, n1 // 2)
    arr2 = [-1, -1, -1, 0, 1, 0, 1, 0, 1, 0, 0, 0]
    n2 = len(arr2)
    findNoOfArrays(arr2, n2 // 2)

# Expected output:
2.0
80.0
# find two numbers with the given lcm and minimum possible difference | python3 implementation of the approach ; function to return the lcm of a and b ; function to find and print the two numbers ; to find the factors ; to check if i is a factor of x and the minimum possible number satisfying the given conditions ; 

from math import gcd as __gcd, sqrt, ceil


def lcm(a, b):
    return (a // __gcd(a, b) * b)


def findNums(x):
    ans = 0
    for i in range(1, ceil(sqrt(x))):
        if (x % i == 0 and lcm(i, x // i) == x):
            ans = i
    print(ans, (x // ans))


x = 12
findNums(x)

# Expected output:
3 4
# find an integer that is common in the maximum number of given arithmetic progressions | python implementation of the approach ; function to return element common in maximum number of aps ; initialize the count variable ; increment count for every element of an ap ; find the index with maximum count ; return the maximum common element ; 

MAXN = 1000000


def maxCommonElement(A, D, N):
    cnt = [0] * MAXN
    for i in range(N):
        for j in range(A[i], MAXN, D[i]):
            cnt[j] += 1
    ans = 0
    com = 0
    for i in range(MAXN):
        if cnt[i] > ans:
            ans = cnt[i]
            com = i
    return com


A = [13, 1, 2, 5]
D = [5, 10, 1, 12]
N = len(A)
print(maxCommonElement(A, D, N))

# Expected output:
41
# number of trailing zeros in n * ( n | function to return the count of trailing 0 s in the given function ; if n is odd ; if n is even ; find the trailing zeros in n / 2 factorial ; return the required answer ; 
def findTrailingZeros(n):
    if (n & 1):
        return 0
    else:
        ans = 0
        n //= 2
        while (n):
            ans += n // 5
            n //= 5
        return ans


n = 12
print(findTrailingZeros(n))

# Expected output:
1
# divide n into k unique parts such that gcd of those parts is maximum | python3 implementation of the approach ; function to calculate maximum gcd ; minimum possible sum for k unique positive integers ; it is not possible to divide n into k unique parts ; all the factors greater than sqrt ( n ) are complementary of the factors less than sqrt ( n ) ; if i is a factor of n ; 

from math import sqrt, ceil, floor


def maxGCD(N, K):
    minSum = (K * (K + 1)) / 2
    if (N < minSum):
        return -1
    i = ceil(sqrt(N))
    res = 1
    while (i >= 1):
        if (N % i == 0):
            if (i >= minSum):
                res = max(res, N / i)
            if (N / i >= minSum):
                res = max(res, i)
        i -= 1
    return res


N = 18
K = 3
print(maxGCD(N, K))

# Expected output:
3
# radius of the inscribed circle within three tangent circles | python3 implementation of the approach ; radius of the 3 

import math
r1 = 0
r2 = 0
r3 = 0


def area(a, b, c):
    p = (a + b + c) / 2
    return ((math .sqrt(p)) * (math .sqrt(p - a)) *
            (math .sqrt(p - b)) * (math .sqrt(p - c)))


def binary_search():
    global r1, r2, r3
    s = area(r1 + r2, r2 + r3, r3 + r1)
    l = 0
    h = s / (r1 + r2 + r3)
    while (h - l > 0.00000001):
        mid = (l + h) / 2
        s1 = area(mid + r1, mid + r2, r1 + r2)
        s2 = area(mid + r1, mid + r3, r1 + r3)
        s3 = area(mid + r2, mid + r3, r2 + r3)
        if (s1 + s2 + s3 < s):
            l = mid
        else:
            h = mid
    return ((l + h) / 2)


r1 = 1
r2 = 2
r3 = 3
print("{0:.6f}".format(binary_search()))

# Expected output:
0.260870
# make all elements zero by decreasing any two elements by one at a time | function that returns true if all the array elements can be made 0 with the given operation ; find the maximum element and the sum ; check the required condition ; 
def checkZeroArray(arr, n):
    sum = 0
    maximum = -10 ** 9
    for i in range(n):
        sum = sum + arr[i]
        maximum = max(maximum, arr[i])
    if (sum % 2 == 0 and maximum <= sum // 2):
        return True
    return False


arr = [1, 2, 1, 2, 2]
n = len(arr)
if (checkZeroArray(arr, n)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# subarray permutation that satisfies the given condition | function that returns true if the required subarray exists in the given array ; map to store the positions of each integer in the original permutation ; to store the address of each entry in arr [ n ] but with 1 - based indexing ; to track minimum position sumcur for sum of a positions ti this position ; summing up addresses ; tracking minimum address encountered ti now ; the sum of the addresses if it forms the required subarray ; if current sum of address is equal to val ; 
def subArray(arr, n, m):
    i = 0
    mp = dict()
    for i in range(n):
        mp[arr[i]] = i + 1
    sumcur = 0
    p = 10 ** 9
    ans = []
    for i in range(1, m + 1):
        sumcur += mp[i]
        p = min(p, mp[i])
        val = p * i - i + (i * (i + 1)) / 2
        if (i == m):
            if (val == sumcur):
                return True
            else:
                return False


arr = [4, 5, 1, 3, 2, 6]
n = len(arr)
m = 3
if (subArray(arr, n, m)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes