giulio98/xlcost-single-prompt huggingface dataset:
# last digit of a number raised to last digit of n factorial | function to find a ^ b using binary exponentiation ; initialise result ; if b is odd then , multiply result by a ; b must be even now change b to b / 2 ; change a = a ^ 2 ; function to find the last digit of the given equation ; to store cyclicity ; store cyclicity from 1 - 10 ; observation 1 ; observation 3 ; to store the last digits of factorial 2 , 3 , and 4 ; find the last digit of x ; step 1 ; divide a [ n ] by cyclicity of v ; if remainder is 0 ; step 1.1 ; step 1.2 ; step 1.3 ; if r is non - zero , then return ( l ^ r ) % 10 ; else return 0 ; else return 1 ; 
def power(a, b, c):
    result = 1
    while (b > 0):
        if ((b & 1) == 1):
            result = (result * a) % c
        b //= 2
        a = (a * a) % c
    return result


def calculate(X, N):
    a = 10 * [0]
    cyclicity = 11 * [0]
    cyclicity[1] = 1
    cyclicity[2] = 4
    cyclicity[3] = 4
    cyclicity[4] = 2
    cyclicity[5] = 1
    cyclicity[6] = 1
    cyclicity[7] = 4
    cyclicity[8] = 4
    cyclicity[9] = 2
    cyclicity[10] = 1
    if (N == 0 or N == 1):
        return (X % 10)
    elif (N == 2 or N == 3 or N == 4):
        temp = 1e18
        a[2] = 2
        a[3] = 6
        a[4] = 4
        v = X % 10
        if (v != 0):
            u = cyclicity[v]
            r = a[N] % u
            if (r == 0):
                if (v == 2 or v == 4 or v == 6 or v == 8):
                    return 6
                elif (v == 5):
                    return 5
                elif (v == 1 or v == 3 or v == 7 or v == 9):
                    return 1
            else:
                return (power(v, r, temp) % 10)
        else:
            return 0
    return 1


if __name__ == "__main__":
    X = 18
    N = 4
    result = calculate(X, N)
    print(result)

# Expected output:
6
# maximize 3 rd element sum in quadruplet sets formed from 
def formQuadruplets(arr, n):
    ans = 0
    pairs = 0
    pairs = n // 4
    arr .sort(reverse=True)
    for i in range(0, n - pairs, 3):
        ans += arr[i + 2]
    return ans


arr = [2, 1, 7, 5, 5, 4, 1, 1, 3, 3, 2, 2]
n = len(arr)
print(formQuadruplets(arr, n))

# Expected output:
10
# minimum concatenation required to get strictly lis for array with repetitive elements | set | python3 implementation to find the minimum concatenation required to get strictly longest increasing subsequence for the given array with repetitive elements ; ordered map containing value and a vector containing index of it 's occurrences  <int, vector<int> > m; ; mapping index with their values in ordered map ; k refers to present minimum index ; stores the number of concatenation required ; iterate over map m ; it . second . back refers to the last element of corresponding vector ; find the index of next minimum element in the sequence ; return the final answer ; 

from bisect import bisect_left


def LIS(arr, n):
    m = {}
    for i in range(n):
        m[arr[i]] = m .get(arr[i], [])
        m[arr[i]].append(i)
    k = n
    ans = 1
    for key, value in m .items():
        if (value[len(value) - 1] < k):
            k = value[0]
            ans += 1
        else:
            k = bisect_left(value, k)
    print(ans)


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

# Expected output:
2
# two balls reachability game | recursive function to return gcd of a and b ; function returns if it 's possible to have x white and y black balls or not. ; finding gcd of ( x , y ) and ( a , b ) ; if gcd is same , it 's always  possible to reach (x, y) ; here it 's never possible  if gcd is not same ; 
def gcd(a, b):
    if (b == 0):
        return a
    return gcd(b, a % b)


def IsPossible(a, b, x, y):
    final = gcd(x, y)
    initial = gcd(a, b)
    if (initial == final):
        print("POSSIBLE")
    else:
        print("NOT POSSIBLE")


if __name__ == "__main__":
    A = 1
    B = 2
    X = 4
    Y = 11
    IsPossible(A, B, X, Y)
    A = 2
    B = 2
    X = 3
    Y = 6
    IsPossible(A, B, X, Y)

# Expected output:
POSSIBLE
NOT POSSIBLE
# number of ways to color n | python3 program for the above approach ; function to count the ways to color block ; for storing powers of 2 ; for storing binomial coefficient values ; calculating binomial coefficient using dp ; calculating powers of 2 ; sort the indices to calculate length of each section ; initialise answer to 1 ; find the length of each section ; merge this section ; return the final count ; 

mod = 1000000007


def waysToColor(arr, n, k):
    global mod
    powOf2 = [0 for i in range(500)]
    c = [[0 for i in range(500)]for j in range(500)]
    for i in range(n + 1):
        c[i][0] = 1
        for j in range(1, i + 1):
            c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod
    powOf2[0] = 1
    powOf2[1] = 1
    for i in range(2, n + 1):
        powOf2[i] = (powOf2[i - 1] * 2) % mod
    rem = n - k
    arr[k] = n + 1
    k += 1
    arr .sort()
    answer = 1
    for i in range(k):
        x = 0
        if i - 1 >= 0:
            x = arr[i] - arr[i - 1] - 1
        else:
            x = arr[i] - 1
        answer = answer * (c[rem][x] % mod) * \
            ((powOf2[x]if (i != 0 and i != k - 1)else 1)) % mod
        rem -= x
    return answer


if __name__ == '__main__':
    N = 6
    K = 3
    arr = [1, 2, 6, 0]
    print(waysToColor(arr, N, K))

# Expected output:
4
# rearrange array such that difference of adjacent elements is in descending order | function to print array in given order ; sort the array ; check elements till the middle index ; check if length is odd print the middle index at last ; print the remaining elements in the described order ; 
def printArray(a, n):
    a .sort()
    i = 0
    j = n - 1
    while (i <= j):
        if (i == j):
            print(a[i], end=" ")
        else:
            print(a[j], end=" ")
            print(a[i], end=" ")
        i = i + 1
        j = j - 1
    print()


if __name__ == "__main__":
    arr1 = [1, 2, 3, 4, 5, 6]
    n1 = len(arr1)
    printArray(arr1, n1)

# Expected output:
6 1 5 2 4 3
# find the smallest number that divides x ^ x | function to find the required smallest number ; finding smallest number that divides n ; i divides n and return this value immediately ; if n is a prime number then answer should be n , as we can 't take 1 as our answer. ; 
def SmallestDiv(n):
    i = 2
    while i * i <= n:
        if (n % i == 0):
            return i
        i += 1
    return n


if __name__ == "__main__":
    X = 385
    ans = SmallestDiv(X)
    print(ans)

# Expected output:
5
# find an array of size n that satisfies the given conditions | utility function to print the contents of an array ; function to generate and print the required array ; initially all the positions are empty ; to store the count of positions i such that arr [ i ] = s ; to store the final array elements ; set arr [ i ] = s and the gap between them is exactly 2 so in for loop we use i += 2 ; mark the i 'th position as visited  as we put arr[i] = s ; increment the count ; finding the next odd number after s ; if the i 'th position is not visited  it means we did not put any value  at position i so we put 1 now ; print the final array ; 
def printArr(arr, n):
    for i in range(n):
        print(arr[i], end=" ")


def findArray(n, k, s):
    vis = [0] * n
    cnt = 0
    arr = [0] * n
    i = 0
    while (i < n and cnt < k):
        arr[i] = s
        vis[i] = 1
        cnt += 1
        i += 2
    val = s
    if (s % 2 == 0):
        val += 1
    else:
        val = val + 2
    for i in range(n):
        if (vis[i] == 0):
            arr[i] = val
    printArr(arr, n)


if __name__ == "__main__":
    n = 8
    k = 3
    s = 12
    findArray(n, k, s)

# Expected output:
12 13 12 13 12 13 13 13
# count total number of even sum sequences | python3 implementation of the approach ; iterative function to calculate ( x ^ y ) % p in o ( log y ) ; initialize result ; update x if it is greater than or equal to p ; if y is odd then multiply x with the result ; y must be even now y = y >> 1 y = y / 2 ; function to return n ^ ( - 1 ) mod p ; function to return ( ncr % p ) using fermat 's little theorem ; base case ; fill factorial array so that we can find all factorial of r , n and n - r ; function to return the count of odd numbers from 1 to n ; function to return the count of even numbers from 1 to n ; function to return the count of the required sequences ; take i even and n - i odd numbers ; number of odd numbers must be even ; total ways of placing n - i odd numbers in the sequence of n numbers ; add this number to the final answer ; 
def power(x, y, p):
    res = 1
    x = x % p

    while (y > 0):
        if (y & 1):
            res = (res * x) % p
        y = y >> 1
        x = (x * x) % p

    return res

# Function to return n^(-1) mod p


def modInverse(n, p):

    return power(n, p - 2, p)


def nCrModPFermat(n, r, p):

    # Base case
    if (r == 0):
        return 1
    fac = [0] * (n+1)
    fac[0] = 1
    for i in range(1, n+1):
        fac[i] = fac[i - 1] * i % p

    return (fac[n] * modInverse(fac[r], p) % p *
            modInverse(fac[n - r], p) % p) % p


def countOdd(n):

    x = n // 2
    if (n % 2 == 1):
        x += 1
    return x


def counteEven(n):

    x = n // 2
    return x


def CountEvenSumSequences(n):

    count = 0

    for i in range(n + 1):
        even = i
        odd = n - i

        if (odd % 2 == 1):
            continue
        tot = (power(countOdd(n), odd, M) *
               nCrModPFermat(n, odd, M)) % M
        tot = (tot * power(counteEven(n), i, M)) % M
        count += tot
        count %= M

    return count


M = 1000000007
n = 5
print(CountEvenSumSequences(n))

# Expected output:
1562
# find the sum of prime numbers in the kth array | python3 implementation of the approach ; function for sieve of eratosthenes ; create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . a value in prime [ i ] will finally be false if i is not a prime , else true . ; if prime [ p ] is not changed then it is a prime ; update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; function to return the sum of primes in the kth array ; update vector v to store all the prime numbers upto max ; to store the sum of primes in the kth array ; count of primes which are in the arrays from 1 to k - 1 ; k is the number of primes in the kth array ; a prime has been added to the sum ; 

from math import sqrt

MAX = 1000000
prime = [True] * MAX


def SieveOfEratosthenes():

    for p in range(2, int(sqrt(MAX)) + 1):
        if (prime[p]):
            for i in range(p * p, MAX, p):
                prime[i] = False


def sumPrime(k):
    SieveOfEratosthenes()
    v = []
    for i in range(2, MAX):
        if (prime[i]):
            v.append(i)
    sum = 0
    skip = (k * (k - 1)) // 2
    while (k > 0):
        sum += v[skip]
        skip += 1
        k -= 1

    return sum


if __name__ == "__main__":
    k = 3
    print(sumPrime(k))

# Expected output:
31
# count all substrings having character k | function to return the index of the next occurrence of character ch in strr starting from the given index ; return the index of the first occurrence of ch ; no occurrence found ; function to return the count of all the substrings of strr which contain the character ch at least one ; to store the count of valid substrings ; index of the first occurrence of ch in strr ; no occurrence of ch after index i in strr ; substrings starting at index i and ending at indices j , j + 1 , ... , n - 1 are all valid substring ; 
def nextOccurrence(strr, n, start, ch):
    for i in range(start, n):
        if (strr[i] == ch):
            return i
    return -1


def countSubStr(strr, n, ch):
    cnt = 0
    j = nextOccurrence(strr, n, 0, ch)
    for i in range(n):
        while (j != -1 and j < i):
            j = nextOccurrence(strr, n, j + 1, ch)
        if (j == -1):
            break
        cnt += (n - j)
    return cnt


strr = "geeksforgeeks"
n = len(strr)
ch = 'k'
print(countSubStr(strr, n, ch))

# Expected output:
56
# check whether bitwise and of n numbers is even or odd | function to check if the bitwise and of the array elements is even or odd ; if at least an even element is present then the bitwise and of the array elements will be even ; 
def checkEvenOdd(arr, n):
    for i in range(n):
        if (arr[i] % 2 == 0):
            print("Even", end="")
            return
    print("Odd", end="")


if __name__ == "__main__":
    arr = [2, 12, 20, 36, 38]
    n = len(arr)
    checkEvenOdd(arr, n)

# Expected output:
Even
# longest substring of only 4 's from the first n characters of the infinite string | python 3 implementation of the approach ; function to return the length of longest contiguous string containing only 4 atms from the first n characters of the string ; initialize prefix sum array of characters and product variable ; preprocessing of prefix sum array ; finding the string length where n belongs to ; 

MAXN = 30


def countMaxLength(N):
    pre = [0 for i in range(MAXN)]
    p = 1
    pre[0] = 0
    for i in range(1, MAXN, 1):
        p *= 2
        pre[i] = pre[i - 1] + i * p
    for i in range(1, MAXN, 1):
        if (pre[i] >= N):
            ind = i
            break
    x = N - pre[ind - 1]
    y = 2 * ind - 1
    if (x >= y):
        res = min(x, y)
    else:
        res = max(x, 2 * (ind - 2) + 1)
    return res


if __name__ == '__main__':
    N = 25
    print(countMaxLength(N))

# Expected output:
5
# in | function to reverse arr [ ] from start to end ; create a copy array and store reversed elements ; now copy reversed elements back to arr [ ] ; 
def revereseArray(arr, n):
    rev = n * [0]
    for i in range(0, n):
        rev[n - i - 1] = arr[i]
    for i in range(0, n):
        arr[i] = rev[i]


if __name__ == "__main__":
    arr = [1, 2, 3, 4, 5, 6]
    n = len(arr)
    print(*arr)
    revereseArray(arr, n)
    print("Reversed array is")
    print(*arr)

# Expected output:
1 2 3 4 5 6
Reversed array is
6 5 4 3 2 1
# in | function to reverse arr [ ] from start to end ; 
def revereseArray(arr, n):
    for i in range(0, int(n / 2)):
        arr[i], arr[n - i - 1] = arr[n - i - 1], arr[i]


if __name__ == "__main__":
    arr = [1, 2, 3, 4, 5, 6]
    n = len(arr)
    print(*arr)
    revereseArray(arr, n)
    print("Reversed array is")
    print(*arr)

# Expected output:
1 2 3 4 5 6
Reversed array is
6 5 4 3 2 1
# find binary string by converting all 01 or 10 to 11 after m iterations | function to find the modified binary string after m iterations ; set the value of m to the minimum of n or m . ; declaration of current string state ; loop over m iterations ; set the current state as null before each iteration ; check if this zero has exactly one 1 as neighbour ; flip the zero ; if there is no change , then no need for further iterations . ; set the current state as the new previous state ; 
def findString(str, M):
    N = len(str)
    M = min(M, N)
    s1 = ""
    while (M != 0):
        s1 = ""
        for i in range(N - 1):
            if (str[i] == '0'):
                if ((str[i - 1] == '1' and str[i + 1] != '1')
                        or (str[i - 1] != '1' and str[i + 1] == '1')):
                    s1 += '1'
                else:
                    s1 += '0'
            else:
                s1 += '1'
        if (str == s1):
            break
        s1 += '1'
        str = s1
        M -= 1
    print(s1)


if __name__ == '__main__':
    str = "0110100"
    M = 3
    findString(str, M)

# Expected output:
1110111
# count of subsequences with a sum in range [ l , r ] and difference between max and min element at least x | python3 program for the above approach ; function to find the number of subsequences of the given array with a sum in range [ l , r ] and the difference between the maximum and minimum element is at least x ; initialize answer as 0 ; creating mask from [ 0 , 2 ^ n - 1 ] ; stores the count and sum of selected elements respectively ; variables to store the value of minimum and maximum element ; traverse the array ; if the jth bit of the ith mask is on ; add the selected element ; update maxval and minval value ; check if the given conditions are true , increment ans by 1. ; 

import sys


def numberofSubsequences(a, L, R, X, n):
    ans = 0
    for i in range(0, (1 << n), 1):
        cnt = 0
        sum = 0
        minVal = sys .maxsize
        maxVal = -sys .maxsize - 1
        for j in range(n):
            if ((i & (1 << j))):
                cnt += 1
                sum += a[j]
                maxVal = max(maxVal, a[j])
                minVal = min(minVal, a[j])
        if (cnt >= 2 and sum >= L and sum <= R and (maxVal - minVal >= X)):
            ans += 1
    return ans


if __name__ == '__main__':
    a = [10, 20, 30, 25]
    L = 40
    R = 50
    X = 10
    N = len(a)
    print(numberofSubsequences(a, L, R, X, N))

# Expected output:
2
# minimum count of numbers needed from 1 to n that yields the sum as k | function to find minimum number of elements required to obtain sum k ; stores the maximum sum that can be obtained ; if k is greater than the maximum sum ; if k is less than or equal to to n ; stores the sum ; stores the count of numbers needed ; iterate until n is greater than or equal to 1 and sum is less than k ; increment count by 1 ; increment sum by n ; update the sum ; finally , return the count ; 
def Minimum(N, K):
    sum = N * (N + 1) // 2
    if (K > sum):
        return -1
    if (K <= N):
        return 1
    sum = 0
    count = 0
    while (N >= 1 and sum < K):
        count += 1
        sum += N
        N -= 1
    return count


if __name__ == '__main__':
    N = 5
    K = 10
    print(Minimum(N, K))

# Expected output:
3
# maximize x such that sum of numbers in range [ 1 , x ] is at most k | function to count the elements with sum of the first that many natural numbers less than or equal to k ; if k equals to 0 ; stores the result ; iterate until low is less than or equal to high ; stores the sum of first mid natural numbers ; if sum is less than or equal to k ; update res and low ; otherwise , ; update ; return res ; 
def Count(N, K):
    if (K == 0):
        return 0
    res = 0
    low = 1
    high = N
    while (low <= high):
        mid = (low + high) // 2
        sum = (mid * mid + mid) // 2
        if (sum <= K):
            res = max(res, mid)
            low = mid + 1
        else:
            high = mid - 1
    return res


if __name__ == '__main__':
    N = 6
    K = 14
    print(Count(N, K))

# Expected output:
4
# largest number having both positive and negative values present in the array | function to find the largest number k such that both k and - k are present in the array ; stores the array elements ; initialize a variable res as 0 to store maximum element while traversing the array ; iterate through array arr ; add the current element into the st ; check if the negative of this element is also present in the st or not ; return the resultant element ; 
def largestNum(arr, n):
    st = set([])
    res = 0
    for i in range(n):
        st .add(arr[i])
        if (-1 * arr[i]) in st:
            res = max(res, abs(arr[i]))
    return res


arr = [3, 2, -2, 5, -3]
n = len(arr)
print(largestNum(arr, n))

# Expected output:
3
# check if an element is present in an array using at most floor ( n / 2 ) + 2 comparisons | function to check whether x is present in the array a [ ] ; initialise a pointer ; store the number of comparisons ; variable to store product ; check is n is odd ; update i and t ; traverse the array ; check if i < n ; update t ; check if t is equal to 0 ; 
def findElement(A, N, X):
    i = 0
    Comparisons = 0
    T = 1
    Found = "No"
    Comparisons += 1
    if (N % 2 == 1):
        i = 1
        T *= (A[0] - X)
    while (i < N):
        Comparisons += 1
        T *= (A[i] - X)
        T *= (A[i + 1] - X)
        i += 2
    Comparisons += 1
    if (T == 0):
        print("Yes", Comparisons)
    else:
        print("No")


if __name__ == '__main__':
    A = [-3, 5, 11, 3, 100, 2, 88, 22, 7, 900, 23, 4, 1]
    N = len(A)
    X = 1
    findElement(A, N, X)

# Expected output:
Yes 8
# search an element in a sorted array formed by reversing subarrays from a random index | function to search an element in a sorted array formed by reversing subarrays from a random index ; set the boundaries for binary search ; apply binary search ; initialize the middle element ; if element found ; random point is on right side of mid ; from l to mid arr is reverse sorted ; random point is on the left side of mid ; from mid to h arr is reverse sorted ; return not found ; 
def find(arr, N, key):
    l = 0
    h = N - 1
    while l <= h:
        mid = (l + h) // 2
        if arr[mid] == key:
            return mid
        if arr[l] >= arr[mid]:
            if arr[l] >= key >= arr[mid]:
                h = mid - 1
            else:
                l = mid + 1
        else:
            if arr[mid] >= key >= arr[h]:
                l = mid + 1
            else:
                h = mid - 1
    return -1


if __name__ == "__main__":
    arr = [10, 8, 6, 5, 2, 1, 13, 12]
    N = len(arr)
    key = 8
    ans = find(arr, N, key)
    print(ans)

# Expected output:
1
# maximum elements that can be removed from front of two arrays such that their sum is at most k | function to find the maximum number of items that can be removed from both the arrays ; stores the maximum item count ; stores the prefix sum of the cost of items ; build the prefix sum for the array a [ ] ; update the value of a [ i ] ; build the prefix sum for the array b [ ] ; update the value of b [ i ] ; iterate through each item of the array a [ ] ; if a [ i ] exceeds k ; store the remaining amount after taking top i elements from the array a ; store the number of items possible to take from the array b [ ] ; store low and high bounds for binary search ; binary search to find number of item that can be taken from stack b in rem amount ; calculate the mid value ; update the value of j and lo ; update the value of the hi ; store the maximum of total item count ; print the result ; 
def maxItems(n, m, a, b, K):
    count = 0
    A = [0 for i in range(n + 1)]
    B = [0 for i in range(m + 1)]
    for i in range(1, n + 1, 1):
        A[i] = a[i - 1] + A[i - 1]
    for i in range(1, m + 1, 1):
        B[i] = b[i - 1] + B[i - 1]
    for i in range(n + 1):
        if (A[i] > K):
            break
        rem = K - A[i]
        j = 0
        lo = 0
        hi = m
        while (lo <= hi):
            mid = (lo + hi) // 2
            if (B[mid] <= rem):
                j = mid
                lo = mid + 1
            else:
                hi = mid - 1
        count = max(j + i, count)
    print(count)


if __name__ == '__main__':
    n = 4
    m = 5
    K = 7
    A = [2, 4, 7, 3]
    B = [1, 9, 3, 4, 5]
    maxItems(n, m, A, B, K)

# Expected output:
3
# number which is co | function to check whether the given number n is prime or not ; base case ; if n has more than one factor , then return false ; otherwise , return true ; function to find x which is co - prime with the integers from the range [ l , r ] ; store the resultant number ; check for prime integers greater than r ; if the current number is prime , then update coprime and break out of loop ; print the resultant number ; 
def isPrime(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 findCoPrime(L, R):
    coPrime, i = 0, R + 1
    while True:
        if (isPrime(i)):
            coPrime = i
            break
        i += 1
    return coPrime


if __name__ == '__main__':
    L, R = 16, 17
    print(findCoPrime(L, R))

# Expected output:
19