giulio98/xlcost-single-prompt huggingface dataset:
# minimum sum possible by removing all occurrences of any array element | function to find minimum sum after deletion ; stores frequency of array elements ; traverse the array ; calculate sum ; update frequency of the current element ; stores the minimum sum required ; traverse map ; find the minimum sum obtained ; return minimum sum ; 
def minSum(A, N):
    mp = {}
    sum = 0
    for i in range(N):
        sum += A[i]
        if A[i] in mp:
            mp[A[i]] += 1
        else:
            mp[A[i]] = 1
    minSum = float('inf')
    for it in mp:
        minSum = min(minSum, sum - (it * mp[it]))
    return minSum


arr = [4, 5, 6, 6]
N = len(arr)
print(minSum(arr, N))

# Expected output:
9
# maximum difference between a pair of adjacent elements by excluding every element once | function to calculate maximum difference between adjacent elements excluding every array element once ; traverse the array ; stores the maximum diff ; check for maximum adjacent element ; exclude current element ; pdate maximum difference ; update previous value ; append the result into a vector ; print the result ; 
def maxAdjacent(arr, N):
    res = []
    for i in range(1, N - 1):
        prev = arr[0]
        maxi = -1 * float('inf')
        for j in range(1, N):
            if (i == j):
                continue
            maxi = max(maxi, abs(arr[j] - prev))
            prev = arr[j]
        res .append(maxi)
    for x in res:
        print(x, end=' ')
    print()


arr = [1, 3, 4, 7, 8]
N = len(arr)
maxAdjacent(arr, N)

# Expected output:
3 4 4
# count 1 s present in a range of indices [ l , r ] in a given array | function to find the size of the array if the array initially contains a single element ; base case ; p / 2 -> findsize ( n 2 ) p % 2 -> 1 p / 2 -> findsize ( n / 2 ) ; function to return the count of 1 s in the range [ l , r ] ; base case ; part 1 -> n / 2 [ 1 , siz_m ] ; update the right end point of the range to min ( siz_m , r ) ; part 2 -> n % 2 [ sizm + 1 , siz_m + 1 ] ; part 3 -> n / 2 [ sizm + 2 , 2 * siz_m - 1 ] same as part 1 property of symmetricity shift the coordinates according to part 1 subtract ( siz_m + 1 ) from both l , r ; 
def findSize(N):
    if (N == 0):
        return 1
    if (N == 1):
        return 1
    Size = 2 * findSize(N // 2) + 1
    return Size


def CountOnes(N, L, R):
    if (L > R):
        return 0
    if (N <= 1):
        return N
    ret = 0
    M = N // 2
    Siz_M = findSize(M)
    if (L <= Siz_M):
        ret += CountOnes(N // 2, L, min(Siz_M, R))
    if (L <= Siz_M + 1 and Siz_M + 1 <= R):
        ret += N % 2
    if (Siz_M + 1 < R):
        ret += CountOnes(N // 2, max(1, L - Siz_M - 1), R - Siz_M - 1)
    return ret


if __name__ == "__main__":
    N = 7
    L = 2
    R = 5
    print(CountOnes(N, L, R))

# Expected output:
4
# find the pair ( a , b ) with minimum lcm such that their sum is equal to n | function to check if number is prime or not ; as 1 is neither prime nor composite return false ; check if it is divided by any number then it is not prime , return false ; check if n is not divided by any number then it is prime and hence return true ; function to find the pair ( a , b ) such that sum is n & lcm is minimum ; check if the number is prime ; now , if it is not prime then find the least divisior ; check if divides n then it is a factor ; required output is a = n / i & b = n / i * ( n - 1 ) ; 
def prime(n):
    if (n == 1):
        return False
    for i in range(2, n + 1):
        if i * i > n:
            break
        if (n % i == 0):
            return False
    return True


def minDivisior(n):
    if (prime(n)):
        print(1, n - 1)
    else:
        for i in range(2, n + 1):
            if i * i > n:
                break
            if (n % i == 0):
                print(n // i, n // i * (i - 1))
                break


N = 4
minDivisior(N)

# Expected output:
2 2
# find landau 's function for a given number n | python3 program for the above approach ; to store landau 's function of the number ; function to return gcd of 2 numbers ; function to return lcm of two numbers ; function to find max lcm value among all representations of n ; calculate landau 's value ; recursive function to find different ways in which n can be written as sum of atleast one positive integers ; check if sum becomes n , consider this representation ; start from previous element in the representation till n ; include current element from representation ; call function again with reduced sum ; backtrack - remove current element from representation ; function to find the landau 's function ; using recurrence find different ways in which n can be written as a sum of atleast one + ve integers ; print the result ; 

import sys
Landau = -sys .maxsize - 1


def gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)


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


def findLCM(arr):
    global Landau
    nth_lcm = arr[0]
    for i in range(1, len(arr)):
        nth_lcm = lcm(nth_lcm, arr[i])
    Landau = max(Landau, nth_lcm)


def findWays(arr, i, n):
    if (n == 0):
        findLCM(arr)
    for j in range(i, n + 1):
        arr .append(j)
        findWays(arr, j, n - j)
        arr .pop()


def Landau_function(n):
    arr = []
    findWays(arr, 1, n)
    print(Landau)


N = 4
Landau_function(N)

# Expected output:
4
# check if the remainder of n | function to check if a number holds the condition ( n - 1 ) ! % n = n - 1 ; corner cases ; number divisible by 2 or 3 are not prime ; iterate from 5 and keep checking for prime ; function to check the expression for the value n ; 
def isPrime(n):
    if (n == 1):
        return True
    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 += 6
    return true


def checkExpression(n):
    if (isPrime(n)):
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    N = 3
    checkExpression(N)

# Expected output:
Yes
# check if it is possible to split given array into k odd | function to check if array can be split in required k subsets ; store count of odd numbers ; check if element is odd ; check if split is possible ; 
def checkArray(n, k, arr):
    cnt = 0
    for i in range(n):
        if (arr[i] & 1):
            cnt += 1
    if (cnt >= k and cnt % 2 == k % 2):
        return True
    else:
        return False


if __name__ == '__main__':
    arr = [1, 3, 4, 7, 5, 3, 1]
    n = len(arr)
    k = 4
    if (checkArray(n, k, arr)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# sum of division of the possible pairs for the given array | python3 program to compute the sum of division of all the possible pairs for the given array ; function to compute the sum ; counting frequency of each term and finding maximum among it ; making cumulative frequency ; taking the ceil value ; nos . in [ ( n - 0.5 ) x , ( n + 0.5 ) x ) range will add n to the ans ; return the final result ; 

from math import *


def func(arr, n):
    ans = 0
    maxx = 0
    freq = [0] * 100005
    temp = 0
    for i in range(n):
        temp = arr[i]
        freq[temp] += 1
        maxx = max(maxx, temp)
    for i in range(1, maxx + 1):
        freq[i] += freq[i - 1]
    for i in range(1, maxx + 1):
        if (freq[i]):
            value = 0
            cur = ceil(0.5 * i) - 1.0
            j = 1.5
            while (1):
                val = min(maxx, (ceil(i * j) - 1.0))
                times = (freq[i] - freq[i - 1])
                con = j - 0.5
                ans += times * con * (freq[int(val)] - freq[int(cur)])
                cur = val
                if (val == maxx):
                    break
                j += 1
    return int(ans)


if __name__ == '__main__':
    arr = [1, 2, 3]
    n = len(arr)
    print(func(arr, n))

# Expected output:
12
# count of elements to be inserted to make array sum twice the xor of array | function to find the minimum number of elements that need to be inserted such that the sum of the elements of the array is twice the xor of the array ; variable to store the xor of all the elements ; variable to store the sum of all elements ; loop to find the xor and the sum of the array ; if sum = 2 * xor ; no need to insert more elements ; we insert one more element which is sum ; we insert two more elements sum + xor and xor . ; print the number of elements inserted in the array ; print the elements that are inserted in the array ; 
def insert_element(a, n):
    Xor = 0
    Sum = 0
    for i in range(n):
        Xor ^= a[i]
        Sum += a[i]
    if (Sum == 2 * Xor):
        print(0)
        return
    if (Xor == 0):
        print(1)
        print(Sum)
        return
    num1 = Sum + Xor
    num2 = Xor
    print(2)
    print(num1, num2)


if __name__ == "__main__":
    a = [1, 2, 3]
    n = len(a)
    insert_element(a, n)

# Expected output:
1
6
# check if roots of a quadratic equation are reciprocal of each other or not | function to check if the roots of a quadratic equation are reciprocal of each other or not ; 
def checkSolution(a, b, c):
    if (a == c):
        print("Yes")
    else:
        print("No")


a = 2
b = 0
c = 2
checkSolution(a, b, c)

# Expected output:
Yes
# sunny number | python3 program for the above approach ; function check whether x is a perfect square or not ; find floating point value of square root of x . ; if square root is an integer ; function to check sunny number ; check if ( n + 1 ) is a perfect square or not ; if ( n + 1 ) is not a perfect square ; 

from math import *


def isPerfectSquare(x):
    sr = sqrt(x)
    return ((sr - floor(sr)) == 0)


def checkSunnyNumber(N):
    if (isPerfectSquare(N + 1)):
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    N = 8
    checkSunnyNumber(N)

# Expected output:
Yes
# count the numbers which can convert n to 1 using given operation | function to count the numbers which can convert n to 1 using the given operation ; iterate through all the integers ; check if n can be converted to 1 ; incrementing the count if it can be converted ; 
def countValues(n):
    answer = 0
    for i in range(2, n + 1, 1):
        k = n
        while (k >= i):
            if (k % i == 0):
                k //= i
            else:
                k -= i
        if (k == 1):
            answer += 1
    return answer


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

# Expected output:
3
# find k numbers with sum equal to n and sum of their squares maximized | function that prints the required k numbers ; print 1 , k - 1 times ; print ( n - k + 1 ) ; 
def printKNumbers(N, K):
    for i in range(K - 1):
        print(1, end=' ')
    print(N - K + 1)


if __name__ == '__main__':
    (N, K) = (10, 3)
    printKNumbers(N, K)

# Expected output:
1 1 8
# print nth stepping or autobiographical number | function to find the nth stepping natural number ; declare the queue ; enqueue 1 , 2 , ... , 9 in this order ; perform k operation on queue ; get the ith stepping number ; perform dequeue from the queue ; if x mod 10 is not equal to 0 ; then enqueue 10 x + ( x mod 10 ) - 1 ; enqueue 10 x + ( x mod 10 ) ; if x mod 10 is not equal to 9 ; then enqueue 10 x + ( x mod 10 ) + 1 ; return the dequeued number of the k - th operation as the nth stepping number ; 
def NthSmallest(K):
    Q = []
    for i in range(1, 10):
        Q .append(i)
    for i in range(1, K + 1):
        x = Q[0]
        Q .remove(Q[0])
        if (x % 10 != 0):
            Q .append(x * 10 + x % 10 - 1)
        Q .append(x * 10 + x % 10)
        if (x % 10 != 9):
            Q .append(x * 10 + x % 10 + 1)
    return x


if __name__ == '__main__':
    N = 16
    print(NthSmallest(N))

# Expected output:
32
# least number to be added to or subtracted from n to make it a perfect square | python3 implementation of the approach ; function to return the least number ; get the perfect square before and after n ; check which is nearest to n ; return the result ; 

from math import sqrt


def nearest(n):
    prevSquare = int(sqrt(n))
    nextSquare = prevSquare + 1
    prevSquare = prevSquare * prevSquare
    nextSquare = nextSquare * nextSquare
    ans = (
        prevSquare -
        n)if (
        n -
        prevSquare) < (
            nextSquare -
            n)else (
                nextSquare -
        n)
    return ans


if __name__ == "__main__":
    n = 14
    print(nearest(n))
    n = 16
    print(nearest(n))
    n = 18
    print(nearest(n))

# Expected output:
2
0
-2
# value of pi ( î ) up to 50 decimal places | python3 program to calculate the value of pi up to n decimal places ; function that prints the value of pi upto n decimal places ; find value of pi upto using acos ( ) function ; print value of pi upto n decimal places ; 

from math import acos


def printValueOfPi(N):
    b = '{:.' + str(N) + 'f}'
    pi = b .format(2 * acos(0.0))
    print(pi)


if __name__ == "__main__":
    N = 43
    printValueOfPi(N)

# Expected output:
3.1415926535897931159979634685441851615905762
# invert the kth most significant bit of n | python implementation of the approach ; function to convert decimal number n to its binary representation stored as an array arr [ ] ; function to convert the number represented as a binary array arr [ ] its decimal equivalent ; function to concatenate the binary numbers and return the decimal result ; number of bits in both the numbers ; convert the bits in both the gers to the arrays a [ ] and b [ ] ; the number of bits in n are less than k ; flip the kth bit ; return the decimal equivalent of the number ; 

import math


def decBinary(arr, n):
    k = int(math .log2(n))
    while (n > 0):
        arr[k] = n % 2
        k = k - 1
        n = n // 2


def binaryDec(arr, n):
    ans = 0
    for i in range(0, n):
        ans = ans + (arr[i] << (n - i - 1))
    return ans


def getNum(n, k):
    l = int(math .log2(n)) + 1
    a = [0 for i in range(0, l)]
    decBinary(a, n)
    if (k > l):
        return n
    if (a[k - 1] == 0):
        a[k - 1] = 1
    else:
        a[k - 1] = 0
    return binaryDec(a, l)


n = 56
k = 2
print(getNum(n, k))

# Expected output:
40
# queries for the product of first n factorials | python3 implementation of the approach ; declare result array globally ; function to precompute the product of factorials upto max ; initialize base condition if n = 0 then factorial of 0 is equal to 1 and answer for n = 0 is 1 ; iterate loop from 1 to max ; factorial ( i ) = factorial ( i - 1 ) * i ; result for current n is equal to result [ i - 1 ] multiplied by the factorial of i ; function to perform the queries ; precomputing the result timax ; perform queries ; 

MAX = 1000000
MOD = 10 ** 9 + 7
result = [0 for i in range(MAX + 1)]
fact = [0 for i in range(MAX + 1)]


def preCompute():
    fact[0] = 1
    result[0] = 1
    for i in range(1, MAX + 1):
        fact[i] = ((fact[i - 1] % MOD) * i) % MOD
        result[i] = ((result[i - 1] % MOD) * (fact[i] % MOD)) % MOD


def performQueries(q, n):
    preCompute()
    for i in range(n):
        print(result[q[i]])


q = [4, 5]
n = len(q)
performQueries(q, n)

# Expected output:
288
34560
# nth number in a set of multiples of a , b or c | python3 program to find nth term divisible by a , b or c ; function to return gcd of a and b ; function to return the count of integers from the range [ 1 , num ] which are divisible by either a , b or c ; calculate the number of terms divisible by a , b and c then remove the terms which are divisible by both ( a , b ) or ( b , c ) or ( c , a ) and then add the numbers which are divisible by a , b and c ; function for binary search to find the nth term divisible by a , b or c ; set low to 1 and high to long_max ; if the current term is less than n then we need to increase low to mid + 1 ; if current term is greater than equal to n then high = mid ; 

import sys


def gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)


def divTermCount(a, b, c, num):
    return ((num / a) + (num / b) + (num / c) - (num / ((a * b) / gcd(a,
                                                                      b))) - (num / ((c * b) / gcd(c,
                                                                                                   b))) - (num / ((a * c) / gcd(a,
                                                                                                                                c))) + (num / ((a * b * c) / gcd(gcd(a,
                                                                                                                                                                     b),
                                                                                                                                                                 c))))


def findNthTerm(a, b, c, n):
    low = 1
    high = sys .maxsize
    mid = 0
    while (low < high):
        mid = low + (high - low) / 2
        if (divTermCount(a, b, c, mid) < n):
            low = mid + 1
        else:
            high = mid
    return int(low)


a = 2
b = 3
c = 5
n = 100
print(findNthTerm(a, b, c, n))

# Expected output:
136
# angle between 3 given vertices in a n | function that checks whether given angle can be created using any 3 sides ; initialize x and y ; calculate the number of vertices between i and j , j and k ; calculate the angle subtended at the circumference ; angle subtended at j can be found using the fact that the sum of angles of a triangle is equal to 180 degrees ; 
def calculate_angle(n, i, j, k):
    x, y = 0, 0
    if (i < j):
        x = j - i
    else:
        x = j + n - i
    if (j < k):
        y = k - j
    else:
        y = k + n - j
    ang1 = (180 * x) // n
    ang2 = (180 * y) // n
    ans = 180 - ang1 - ang2
    return ans


n = 5
a1 = 1
a2 = 2
a3 = 5
print(calculate_angle(n, a1, a2, a3))

# Expected output:
36
# loss when two items are sold at same price and same percentage profit / loss | function that will find loss ; 
def Loss(SP, P):
    loss = 0
    loss = ((2 * P * P * SP) / (100 * 100 - P * P))
    print("Loss =", round(loss, 3))


if __name__ == "__main__":
    SP, P = 2400, 30
    Loss(SP, P)

# Expected output:
Loss = 474.725
# check for an array element that is co | python3 implementation of the approach ; stores smallest prime factor for every number ; hash to store prime factors count ; function to calculate spf ( smallestprime factor ) for every number till maxn ; separately marking spf for every even number as 2 ; checking if i is prime ; marking spf for all numbers divisible by i ; marking spf [ j ] if it is not previously marked ; function to store the prime factors after dividing by the smallest prime factor at every step ; storing the count of prime factors in hash ; function that returns true if there are no common prime factors between x and other numbers of the array ; checking whether it common prime factor with other numbers ; function that returns true if there is an element in the array which is coprime with all the other elements of the array ; using sieve for generating prime factors ; checking the common prime factors with other numbers ; 

MAXN = 1000001
spf = [i for i in range(MAXN)]
hash1 = [0 for i in range(MAXN)]


def sieve():
    for i in range(4, MAXN, 2):
        spf[i] = 2
    for i in range(3, MAXN):
        if i * i >= MAXN:
            break
        if (spf[i] == i):
            for j in range(i * i, MAXN, i):
                if (spf[j] == j):
                    spf[j] = i


def getFactorization(x):
    while (x != 1):
        temp = spf[x]
        if (x % temp == 0):
            hash1[spf[x]] += 1
            x = x // spf[x]
        while (x % temp == 0):
            x = x // temp


def check(x):
    while (x != 1):
        temp = spf[x]
        if (x % temp == 0 and hash1[temp] > 1):
            return False
        while (x % temp == 0):
            x = x // temp
    return True


def hasValidNum(arr, n):
    sieve()
    for i in range(n):
        getFactorization(arr[i])
    for i in range(n):
        if (check(arr[i])):
            return True
    return False


arr = [2, 8, 4, 10, 6, 7]
n = len(arr)
if (hasValidNum(arr, n)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# ways to remove edges from a complete graph to make odd edges | function to return the number of ways to remove edges from the graph so that odd number of edges are left in the graph ; total number of edges ; 
def countWays(N):
    E = (N * (N - 1)) / 2
    if (N == 1):
        return 0
    return int(pow(2, E - 1))


if __name__ == '__main__':
    N = 4
    print(countWays(N))

# Expected output:
32
# closest sum partition ( into two subsets ) of numbers from 1 to n | function to return the minimum required absolute difference ; 
def minAbsDiff(n):
    mod = n % 4
    if (mod == 0 or mod == 3):
        return 0
    return 1


if __name__ == "__main__":
    n = 5
    print(minAbsDiff(n))

# Expected output:
1
# check if the xor of the frequency of all digits of a number n is zero or not | python implementation of the above approach ; creating a frequency array ; finding the last digit of the number ; dividing the number by 10 to eliminate last digit ; counting frequency of each digit ; checking if the xor of all frequency is zero or not ; 
def check(s):
    freq = [0] * 10
    while (s != 0):
        r = s % 10
        s = s // 10
        freq[r] += 1
    xor = 0
    for i in range(10):
        xor = xor ^ freq[i]
    if (xor == 0):
        return True
    else:
        return False


s = 122233
if (check(s)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# print n lines of 4 numbers such that every pair among 4 numbers has a gcd k | function to print n lines ; iterate n times to print n lines ; 
def printLines(n, k):
    for i in range(n):
        print(k * (6 * i + 1), k * (6 * i + 2),
              k * (6 * i + 3), k * (6 * i + 5))


if __name__ == "__main__":
    n, k = 2, 2
    printLines(n, k)

# Expected output:
2 4 6 10
14 16 18 22
# Sum of first n term of Series 3 , 5 , 9 , 17 , 33. ... | Python program to find sum of n terms of the series ; Sn = n * ( 4 * n * n + 6 * n - 1 ) / 3 ; number of terms for the sum ; find the Sn
def calculateSum(n):
    return (2 ** (n + 1) + n - 2)


n = 4
print("Sum =", calculateSum(n))

# Expected output:
Sum = 34
# count the number of special strings of a given length n | python3 program to count the number of special strings of a given length n ; function to return count of special strings ; stores the answer for a particular value of n ; for n = 0 we have empty string ; for n = 1 we have 2 special strings ; calculate count of special string of length i ; fib [ n ] stores the count of special strings of length n ; 

mod = 1000000007


def count_special(n):
    fib = [0 for i in range(n + 1)]
    fib[0] = 1
    fib[1] = 2
    for i in range(2, n + 1, 1):
        fib[i] = (fib[i - 1] % mod + fib[i - 2] % mod) % mod
    return fib[n]


if __name__ == '__main__':
    n = 3
    print(count_special(n))

# Expected output:
5
# counts path in an array | python3 implementation of the above approach ; find the number of ways to reach the end ; base case ; recursive structure ; 

mod = 1e9 + 7


def ways(i, arr, n):
    if (i == n - 1):
        return 1
    sum = 0
    for j in range(1, arr[i] + 1):
        if (i + j < n):
            sum += (ways(i + j, arr, n)) % mod
            sum %= mod
    return int(sum % mod)


if __name__ == '__main__':
    arr = [5, 3, 1, 4, 3]
    n = len(arr)
    print(ways(0, arr, n))

# Expected output:
6
# counts path in an array | python3 implementation of above approach ; find the number of ways to reach the end ; dp to store value ; base case ; bottom up dp structure ; f [ i ] is dependent of f [ i + 1 ] to f [ i + k ] ; return value of dp [ 0 ] ; 

mod = 10 ** 9 + 7


def ways(arr, n):
    dp = [0] * (n + 1)
    dp[n - 1] = 1
    for i in range(n - 2, -1, -1):
        dp[i] = 0
        j = 1
        while ((j + i) < n and j <= arr[i]):
            dp[i] += dp[i + j]
            dp[i] %= mod
            j += 1
    return dp[0] % mod


arr = [5, 3, 1, 4, 3]
n = len(arr)
print(ways(arr, n) % mod)

# Expected output:
6