giulio98/xlcost-single-prompt huggingface dataset:
# find a pair from the given array with maximum ncr value | function to print the pair that gives maximum ncr ; this gives the value of n in ncr ; case 1 : when n is odd ; case 2 : when n is even ; 
def printMaxValPair(v, n):
    v .sort()
    N = v[n - 1]
    if N % 2 == 1:
        first_maxima = N // 2
        second_maxima = first_maxima + 1
        ans1, ans2 = 3 * (10 ** 18), 3 * (10 ** 18)
        from_left, from_right = -1, -1
        _from = -1
        for i in range(0, n):
            if v[i] > first_maxima:
                _from = i
                break
            else:
                diff = first_maxima - v[i]
                if diff < ans1:
                    ans1 = diff
                    from_left = v[i]
        from_right = v[_from]
        diff1 = first_maxima - from_left
        diff2 = from_right - second_maxima
        if diff1 < diff2:
            print(N, from_left)
        else:
            print(N, from_right)
    else:
        maxima = N // 2
        ans1 = 3 * (10 ** 18)
        R = -1
        for i in range(0, n - 1):
            diff = abs(v[i] - maxima)
            if diff < ans1:
                ans1 = diff
                R = v[i]
        print(N, R)


if __name__ == "__main__":
    v = [1, 1, 2, 3, 6, 1]
    n = len(v)
    printMaxValPair(v, n)

# Expected output:
6 3
# find the number of good permutations | function to return the count of good permutations ; for m = 0 , ans is 1 ; if k is greater than 1 ; if k is greater than 2 ; if k is greater than 3 ; 
def Permutations(n, k):
    ans = 1
    if k >= 2:
        ans += (n) * (n - 1) // 2
    if k >= 3:
        ans += ((n) * (n - 1) * (n - 2) * 2 // 6)
    if k >= 4:
        ans += ((n) * (n - 1) * (n - 2) * (n - 3) * 9 // 24)
    return ans


if __name__ == "__main__":
    n, k = 5, 2
    print(Permutations(n, k))

# Expected output:
11
# count integers in a range which are divisible by their euler totient value | function to return a ^ n ; function to return count of integers that satisfy n % phi ( n ) = 0 ; 
def power(a, n):
    if n == 0:
        return 1
    p = power(a, n // 2)
    p = p * p
    if n & 1:
        p = p * a
    return p


def countIntegers(l, r):
    ans, i = 0, 1
    v = power(2, i)
    while v <= r:
        while v <= r:
            if v >= l:
                ans += 1
            v = v * 3
        i += 1
        v = power(2, i)
    if l == 1:
        ans += 1
    return ans


if __name__ == "__main__":
    l, r = 12, 21
    print(countIntegers(l, r))

# Expected output:
3
# number of pairs from the first n natural numbers whose sum is divisible by k | function to find the number of pairs from the set of natural numbers up to n whose sum is divisible by k ; declaring a hash to store count ; storing the count of integers with a specific remainder in hash array ; check if k is even ; count of pairs when both integers are divisible by k ; count of pairs when one remainder is r and other remainder is k - r ; count of pairs when both the remainders are k / 2 ; count of pairs when both integers are divisible by k ; count of pairs when one remainder is r and other remainder is k - r ; 
def findPairCount(N, K):
    count = 0
    rem = [0] * K
    rem[0] = N // K
    for i in range(1, K):
        rem[i] = (N - i) // K + 1
    if (K % 2 == 0):
        count += (rem[0] * (rem[0] - 1)) // 2
        for i in range(1, K // 2):
            count += rem[i] * rem[K - i]
        count += (rem[K // 2] * (rem[K // 2] - 1)) // 2
    else:
        count += (rem[0] * (rem[0] - 1)) // 2
        for i in rage(1, K // 2 + 1):
            count += rem[i] * rem[K - i]
    return count


if __name__ == "__main__":
    N = 10
    K = 4
    print(findPairCount(N, K))

# Expected output:
10
# smallest and largest n | python3 implementation of the approach ; function to print the largest and the smallest n - digit perfect squares ; smallest n - digit perfect square ; largest n - digit perfect square ; 

import math


def nDigitPerfectSquares(n):
    print(pow(math .ceil(math .sqrt(pow(10, n - 1))), 2), end=" ")
    print(pow(math .ceil(math .sqrt(pow(10, n))) - 1, 2))


n = 4
nDigitPerfectSquares(n)

# Expected output:
1024 9801
# maximum trace possible for any sub | python 3 implementation of the approach ; function to return the maximum trace possible for a sub - matrix of the given matrix ; calculate the trace for each of the sub - matrix with top left corner at cell ( r , s ) ; update the maximum trace ; return the maximum trace ; 

N = 3


def MaxTraceSub(mat):
    max_trace = 0
    for i in range(N):
        for j in range(N):
            r = i
            s = j
            trace = 0
            while (r < N and s < N):
                trace += mat[r][s]
                r += 1
                s += 1
                max_trace = max(trace, max_trace)
    return max_trace


if __name__ == '__main__':
    mat = [[10, 2, 5], [6, 10, 4], [2, 7, -10]]
    print(MaxTraceSub(mat))

# Expected output:
20
# check if matrix can be converted to another matrix by transposing square sub | python 3 implementation of the approach ; function that returns true if matrix1 can be converted to matrix2 with the given operation ; traverse all the diagonals starting at first column ; traverse in diagonal ; store the diagonal elements ; move up ; sort the elements ; check if they are same ; traverse all the diagonals starting at last row ; traverse in the diagonal ; store diagonal elements ; sort all elements ; check for same ; if every element matches ; 

n = 3
m = 3


def check(a, b):
    for i in range(n):
        v1 = []
        v2 = []
        r = i
        col = 0
        while (r >= 0 and col < m):
            v1 .append(a[r][col])
            v2 .append(b[r][col])
            r -= 1
            col += 1
        v1 .sort(reverse=False)
        v2 .sort(reverse=False)
        for i in range(len(v1)):
            if (v1[i] != v2[i]):
                return False
    for j in range(1, m):
        v1 = []
        v2 = []
        r = n - 1
        col = j
        while (r >= 0 and col < m):
            v1 .append(a[r][col])
            v2 .append(b[r][col])
            r -= 1
            col += 1
        v1 .sort(reverse=False)
        v2 .sort(reverse=False)
        for i in range(len(v1)):
            if (v1[i] != v2[i]):
                return False
    return True


if __name__ == '__main__':
    a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    b = [[1, 4, 7], [2, 5, 6], [3, 8, 9]]
    if (check(a, b)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# last digit of product of two large or small numbers ( a * b ) | function to print the last digit of product a * b ; 
def lastDigit(a, b):
    lastDig = ((int(a[len(a) - 1]) - int('0'))
               * (int(b[len(b) - 1]) - int('0')))
    print(lastDig % 10)


if __name__ == '__main__':
    a, b = "1234567891233", "1234567891233156"
    lastDigit(a, b)

# Expected output:
8
# smallest and largest palindrome with n digits | python 3 implementation of the above approach ; function to print the smallest and largest palindrome with n digits ; 

from math import pow


def printPalindrome(n):
    if (n == 1):
        print("Smallest Palindrome: 0")
        print("Largest Palindrome: 9")
    else:
        print("Smallest Palindrome:", int(pow(10, n - 1)) + 1)
        print("Largest Palindrome:", int(pow(10, n)) - 1)


if __name__ == '__main__':
    n = 4
    printPalindrome(n)

# Expected output:
Smallest Palindrome: 1001
Largest Palindrome: 9999
# addition of two numbers without propagating carry | function to prsum of 2 numbers without propagating carry ; reverse a ; reverse b ; generate sum since length of both a and b are same , take any one of them . ; extract digits from a and b and add ; if sum is single digit ; if sum is not single digit reverse sum ; extract digits from sum and append to result ; 
def printSum(a, b):
    res, temp1, temp2 = 0, 0, 0
    while a > 0:
        temp1 = temp1 * 10 + (a % 10)
        a //= 10
    a = temp1
    while b > 0:
        temp2 = temp2 * 10 + (b % 10)
        b //= 10
    b = temp2
    while a:
        Sum = a % 10 + b % 10
        if Sum // 10 == 0:
            res = res * 10 + Sum
        else:
            temp1 = 0
            while Sum > 0:
                temp1 = temp1 * 10 + (Sum % 10)
                Sum //= 10
            Sum = temp1
            while Sum > 0:
                res = res * 10 + (Sum % 10)
                Sum //= 10
        a //= 10
        b //= 10
    return res


if __name__ == "__main__":
    a, b = 7752, 8834
    print(printSum(a, b))

# Expected output:
151586
# number of digits before the decimal point in the division of two numbers | function to return the number of digits before the decimal in a / b ; absolute value of a / b ; if result is 0 ; count number of digits in the result ; return the required count of digits ; 
def countDigits(a, b):
    count = 0
    p = abs(a // b)
    if (p == 0):
        return 1
    while (p > 0):
        count = count + 1
        p = p // 10
    return count


a = 100
b = 10
print(countDigits(a, b))

# Expected output:
2
# number of digits before the decimal point in the division of two numbers | python3 implementation of the approach ; function to return the number of digits before the decimal in a / b ; return the required count of digits ; 

import math


def countDigits(a, b):
    return math .floor(math .log10(abs(a)) - math .log10(abs(b))) + 1


a = 100
b = 10
print(countDigits(a, b))

# Expected output:
2
# smallest odd number with n digits | function to return smallest even number with n digits ; 
def smallestOdd(n):
    if (n == 1):
        return 1
    return pow(10, n - 1) + 1


n = 4
print(smallestOdd(n))

# Expected output:
1001
# largest even and odd n | function to print the largest n - digit even and odd numbers ; 
def findNumbers(n):
    odd = pow(10, n) - 1
    even = odd - 1
    print("Even = ", even)
    print("Odd = ", odd)


n = 4
findNumbers(n)

# Expected output:
Even =  9998
Odd =  9999
# longest sub | function to return the length of the longest sub - array whose product of elements is 0 ; 
def longestSubArray(arr, n):
    isZeroPresent = False
    for i in range(0, n):
        if (arr[i] == 0):
            isZeroPresent = True
            break
    if (isZeroPresent):
        return n
    return 0


arr = [1, 2, 3, 0, 1, 2, 0]
n = len(arr)
print(longestSubArray(arr, n))

# Expected output:
7
# smallest even number with n digits | function to return smallest even number with n digits ; 
def smallestEven(n):
    if (n == 1):
        return 0
    return pow(10, n - 1)


n = 4
print(smallestEven(n))

# Expected output:
1000
# maximize profit when divisibility by two numbers have associated profits | python3 implementation of the approach ; function to return the maximum profit ; min ( x , y ) * n / lcm ( a , b ) ; 

from math import gcd


def maxProfit(n, a, b, x, y):
    res = x * (n // a)
    res += y * (n // b)
    res -= min(x, y) * (n // ((a * b) // gcd(a, b)))
    return res


if __name__ == "__main__":
    n = 6
    a = 6
    b = 2
    x = 8
    y = 2
    print(maxProfit(n, a, b, x, y))

# Expected output:
12
# series summation if t ( n ) is given and n is very large | python 3 implementation of the approach ; function to return the sum of the given series ; 

from math import pow
MOD = 1000000007


def sumOfSeries(n):
    ans = pow(n % MOD, 2)
    return (ans % MOD)


if __name__ == '__main__':
    n = 10
    print(int(sumOfSeries(n)))

# Expected output:
100
# kth odd number in an array | function to return the kth odd element from the array ; traverse the array ; if current element is odd ; if kth odd element is found ; total odd elements in the array are < k ; 
def kthOdd(arr, n, k):
    for i in range(n):
        if (arr[i] % 2 == 1):
            k -= 1
        if (k == 0):
            return arr[i]
    return -1


arr = [1, 2, 3, 4, 5]
n = len(arr)
k = 2
print(kthOdd(arr, n, k))

# Expected output:
3
# find last five digits of a given five digit number raised to power five | function to find the last five digits of a five digit number raised to power five ; 
def lastFiveDigits(n):
    n = ((int)(n / 10000) * 10000 + ((int)(n / 100) %
                                     10) * 1000 + (n %
                                                   10) * 100 + ((int)(n / 10) %
                                                                10) * 10 + (int)(n / 1000) %
         10)
    ans = 1
    for i in range(5):
        ans *= n
        ans %= 100000
    print(ans)


if __name__ == '__main__':
    n = 12345
    lastFiveDigits(n)

# Expected output:
71232
# sum of ( maximum element | function to return a ^ n % mod ; compute sum of max ( a ) - min ( a ) for all subsets ; sort the array . ; maxs = 2 ^ i - 1 ; mins = 2 ^ ( n - 1 - i ) - 1 ; 
def power(a, n):
    if n == 0:
        return 1
    p = power(a, n // 2) % mod
    p = (p * p) % mod
    if n & 1 == 1:
        p = (p * a) % mod
    return p


def computeSum(arr, n):
    arr .sort()
    Sum = 0
    for i in range(0, n):
        maxs = (power(2, i) - 1 + mod) % mod
        maxs = (maxs * arr[i]) % mod
        mins = (power(2, n - 1 - i) - 1 + mod) % mod
        mins = (mins * arr[i]) % mod
        V = (maxs - mins + mod) % mod
        Sum = (Sum + V) % mod
    return Sum


if __name__ == "__main__":
    mod = 1000000007
    arr = [4, 3, 1]
    n = len(arr)
    print(computeSum(arr, n))

# Expected output:
9
# count of all n digit numbers such that num + rev ( num ) = 10 ^ n | function to return the count of such numbers ; if n is odd ; 
def countNumbers(n):
    if n % 2 == 1:
        return 0
    return (9 * pow(10, n // 2 - 1))


if __name__ == "__main__":
    n = 2
    print(countNumbers(n))

# Expected output:
9
# count of numbers having only 1 set bit in the range [ 0 , n ] | function to return the required count ; to store the count of numbers ; every power of 2 contains only 1 set bit ; 
def count(n):
    cnt = 0
    p = 1
    while (p <= n):
        cnt = cnt + 1
        p *= 2
    return cnt


n = 7
print(count(n))

# Expected output:
3
# find the k | function to find the k - th minimum element from an array concatenated m times ; sort the elements in ascending order ; return k 'th min element  present at k-1 index ; 
def KthMinValAfterMconcatenate(A, N, M, K):
    V = []
    for i in range(0, M):
        for j in range(0, N):
            V .append(A[j])
    V .sort()
    return V[K - 1]


if __name__ == "__main__":
    A = [3, 1, 2]
    M, K = 3, 4
    N = len(A)
    print(KthMinValAfterMconcatenate(A, N, M, K))

# Expected output:
2
# sum of all i such that ( 2 ^ i + 1 ) % 3 = 0 where i is in range [ 1 , n ] | function to return the required sum ; total odd numbers from 1 to n ; sum of first n odd numbers ; 
def sumN(n):
    n = (n + 1) // 2
    return (n * n)


n = 3
print(sumN(n))

# Expected output:
4
# numbers that are not divisible by any number in the range [ 2 , 10 ] | function to return the count of numbers from 1 to n which are not divisible by any number in the range [ 2 , 10 ] ; 
def countNumbers(n):
    return (
        n -
        n //
        2 -
        n //
        3 -
        n //
        5 -
        n //
        7 +
        n //
        6 +
        n //
        10 +
        n //
        14 +
        n //
        15 +
        n //
        21 +
        n //
        35 -
        n //
        30 -
        n //
        42 -
        n //
        70 -
        n //
        105 +
        n //
        210)


if __name__ == '__main__':
    n = 20
    print(countNumbers(n))

# Expected output:
5
# maximum primes whose sum is equal to given n | function to find max count of primes ; if n is even n / 2 is required answer if n is odd floor ( n / 2 ) = ( int ) ( n / 2 ) is required answer ; 
def maxPrmimes(n):
    return n // 2


n = 17
print(maxPrmimes(n))

# Expected output:
8
# sum of the series ( 1 * 2 ) + ( 2 * 3 ) + ( 3 * 4 ) + ... ... upto n terms | function to return sum ; 
def Sum(n):
    return n * (n + 1) * (n + 2) // 3


if __name__ == "__main__":
    n = 2
    print(Sum(n))

# Expected output:
8
# smallest divisor d of n such that gcd ( d , m ) is greater than 1 | python3 implementation of the above approach ; function to find the minimum divisor ; iterate for all factors of n ; check for gcd > 1 ; check for gcd > 1 ; if gcd is m itself ; 

import math


def findMinimum(n, m):
    mini, i = m, 1
    while i * i <= n:
        if n % i == 0:
            sec = n // i
            if math .gcd(m, i) > 1:
                return i
            elif math .gcd(sec, m) > 1:
                mini = min(sec, mini)
        i += 1
    if mini == m:
        return -1
    else:
        return mini


if __name__ == "__main__":
    n, m = 8, 10
    print(findMinimum(n, m))

# Expected output:
2
# find nth term of the series 1 , 5 , 32 , 288 ... | function to generate a fixed number ; finding nth term ; 
def nthTerm(N):
    nth = 0
    for i in range(N, 0, -1):
        nth += pow(i, i)
    return nth


N = 3
print(nthTerm(N))

# Expected output:
32
# find kth smallest number in range [ 1 , n ] when all the odd numbers are deleted | function to return the kth smallest element from the range [ 1 , n ] after removing all the odd elements ; 
def kthSmallest(n, k):
    return 2 * k


n = 8
k = 4
print(kthSmallest(n, k))

# Expected output:
8
# check if a number can be represented as sum of non zero powers of 2 | function that return true if n can be represented as the sum of powers of 2 without using 2 ^ 0 ; 
def isSumOfPowersOfTwo(n):
    if n % 2 == 1:
        return False
    else:
        return True


n = 10
if isSumOfPowersOfTwo(n):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# minimize sum of adjacent difference with removal of one element from array | function to find the element ; value variable for storing the total value ; declaring maximum value as zero ; if array contains on element ; storing the maximum value in temp variable ; adding the adjacent difference modulus values of removed element . removing adjacent difference modulus value after removing element ; returning total value - maximum value ; 
def findMinRemoval(arr, n):
    value = 0
    maximum = 0
    if (n == 1):
        return 0
    for i in range(n):
        if (i != 0 and i != n - 1):
            value = value + abs(arr[i] - arr[i + 1])
            temp = (abs(arr[i] - arr[i + 1]) + abs(arr[i] -
                    arr[i - 1]) - abs(arr[i - 1] - arr[i + 1]))
        elif (i == 0):
            value = value + abs(arr[i] - arr[i + 1])
            temp = abs(arr[i] - arr[i + 1])
        else:
            temp = abs(arr[i] - arr[i - 1])
        maximum = max(maximum, temp)
    return (value - maximum)


if __name__ == "__main__":
    arr = [1, 5, 3, 2, 10]
    n = len(arr)
    print(findMinRemoval(arr, n))

# Expected output:
7
# time until distance gets equal to x between two objects moving in opposite direction | function to return the time for which the two policemen can communicate ; time = distance / speed ; 
def getTime(u, v, x):
    speed = u + v
    time = x / speed
    return time


if __name__ == "__main__":
    u, v, x = 3, 3, 3
    print(getTime(u, v, x))

# Expected output:
0.5
# given number of matches played , find number of teams in tournament | python implementation of the approach ; function to return the number of teams ; to store both roots of the equation ; sqrt ( b ^ 2 - 4 ac ) ; first root ( - b + sqrt ( b ^ 2 - 4 ac ) ) / 2 a ; second root ( - b - sqrt ( b ^ 2 - 4 ac ) ) / 2 a ; return the positive root ; 

import math


def number_of_teams(M):
    N1, N2, sqr = 0, 0, 0
    sqr = math .sqrt(1 + (8 * M))
    N1 = (1 + sqr) / 2
    N2 = (1 - sqr) / 2
    if (N1 > 0):
        return int(N1)
    return int(N2)


def main():
    M = 45
    print(number_of_teams(M))


if __name__ == '__main__':
    main()

# Expected output:
10
# sum of numbers from 1 to n which are in lucas sequence | function to return the required sum ; generate lucas number and keep on adding them ; 
def LucasSum(N):
    Sum = 0
    a = 2
    b = 1
    c = 0
    Sum += a
    while (b <= N):
        Sum += b
        c = a + b
        a = b
        b = c
    return Sum


N = 20
print(LucasSum(N))

# Expected output:
46
# count of all even numbers in the range [ l , r ] whose sum of digits is divisible by 3 | function to return the count of required numbers ; count of numbers in range which are divisible by 6 ; 
def countNumbers(l, r):
    return ((r // 6) - (l - 1) // 6)


if __name__ == "__main__":
    l = 1000
    r = 6000
    print(countNumbers(l, r))

# Expected output:
834
# sum of minimum element of all sub | function to find the sum of minimum of all subsequence ; 
def findMinSum(arr, n):
    occ = n - 1
    Sum = 0
    for i in range(n):
        Sum += arr[i] * pow(2, occ)
        occ -= 1
    return Sum


arr = [1, 2, 4, 5]
n = len(arr)
print(findMinSum(arr, n))

# Expected output:
29
# number of trailing zeroes in base b representation of n ! | python 3 program to find the number of trailing zeroes in base b representation of n ! ; to find the power of a prime p in factorial n ; calculating floor ( n / r ) and adding to the count ; increasing the power of p from 1 to 2 to 3 and so on ; returns all the prime factors of k ; vector to store all the prime factors along with their number of occurrence in factorization of b ; returns largest power of b that divides n ! ; calculating minimum power of all the prime factors of b ; 

import sys


def findPowerOfP(N, p):
    count = 0
    r = p
    while (r <= N):
        count += int(N / r)
        r = r * p

    return count


def primeFactorsofB(B):
    ans = []
    i = 2

    while (B != 1):
        if (B % i == 0):
            count = 0
            while (B % i == 0):

                B = int(B / i)
                count += 1

            ans.append((i, count))

        i += 1

    return ans


def largestPowerOfB(N, B):
    vec = []
    vec = primeFactorsofB(B)
    ans = sys.maxsize
    ans = min(ans, int(findPowerOfP(N, vec[0][0]) /
                       vec[0][1]))

    return ans


if __name__ == '__main__':
    print(largestPowerOfB(5, 2))
    print(largestPowerOfB(6, 9))

# Expected output:
3
1
# count numbers in range 1 to n which are divisible by x but not by y | function to count total numbers divisible by x but not y in range 1 to n ; check if number is divisible by x but not y if yes , increment count ; 
def countNumbers(X, Y, N):
    count = 0
    for i in range(1, N + 1):
        if ((i % X == 0) and (i % Y != 0)):
            count += 1
    return count


X = 2
Y = 3
N = 10
print(countNumbers(X, Y, N))

# Expected output:
4
# position of a person diametrically opposite on a circle | function to return the required position ; 
def getPosition(n, m):
    if (m > (n // 2)):
        return (m - (n // 2))
    return (m + (n // 2))


n = 8
m = 5
print(getPosition(n, m))

# Expected output:
1