giulio98/xlcost-single-prompt huggingface dataset:
# last element of an array after repeatedly removing the first element and appending it to the end of the array twice exactly k times | function to find the last element after performing given operations ; length of the array ; increment j until condition is satisfied ; in each pair every value is repeating r number of times ; print the result according to the value of k ; 
def findLastElement(N, A):
    l = len(A)
    j = 0
    while (N > l * (2 ** j)):
        N = N - l * 2 ** j
        j += 1
    k = 1
    r = 2 ** j
    for i in range(1, l):
        if N > r * i:
            k += 1
    for i in range(0, len(A)):
        if (i + 1 == k):
            print(A[i])
            return


if __name__ == '__main__':
    K = 7
    A = [1, 2, 3]
    findLastElement(K, A)

# Expected output:
2
# minimum possible value of d which when added to or subtracted from k repeatedly obtains every array element | recursive function tox previous gcd of a and b ; function to find the maximum value of d such that every element in the array can be obtained by performing k + d or k - d ; traverse the array arr [ ] ; update arr [ i ] ; stores gcd of the array ; iterate over the range [ 1 , n ] ; update the value of d ; print the value of d ; 
def gcd(a, b):
    if (b == 0):
        return a
    return gcd(b, a % b)


def findMaxD(arr, N, K):
    for i in range(0, N):
        arr[i] = abs(arr[i] - K)
    D = arr[0]
    for i in range(1, N):
        D = gcd(D, arr[i])
    return D


arr = [1, 7, 11]
N = len(arr)
K = 3
print(findMaxD(arr, N, K))

# Expected output:
2
# maximize the number of times a character can be removed from substring 01 from given binary string | function to find the maximum moves that can be performed on a string ; stores 0 s in suffix ; stores 1 s in prefix ; iterate over the characters of the string ; iterate until i is greater than or equal to 0 ; if n is equal to x + y ; return answer ; 
def maxOperations(S, N):
    X = 0
    Y = 0
    for i in range(N):
        if (S[i] == '0'):
            break
        Y += 1
    i = N - 1
    while (i >= 0):
        if (S[i] == '1'):
            break
        X += 1
    if (N == X + Y):
        return 0
    return N - (X + Y) - 1


if __name__ == '__main__':
    S = "001111"
    N = len(S)
    print(maxOperations(S, N))

# Expected output:
5
# maximize frequency sum of k chosen characters from given string | function to find the maximum sum of frequencies of the exactly k chosen characters from the string s ; stores the resultant maximum sum ; stores the frequency of array elements ; find the frequency of character ; sort the frequency array in the descending order ; iterate to choose k elements greedily ; if the freq [ i ] cards are chosen ; k cards have been picked ; return the resultant sum ; 
def maximumSum(S, N, K):
    sum = 0
    freq = [0] * 256
    for i in range(N):
        freq[ord(S[i])] += 1
    freq = sorted(freq)[::-1]
    for i in range(256):
        if (K > freq[i]):
            sum += freq[i] * freq[i]
            K -= freq[i]
        else:
            sum += freq[i] * K
            break
    return sum


if __name__ == '__main__':
    S = "GEEKSFORGEEKS"
    K = 10
    N = len(S)
    print(maximumSum(S, N, K))

# Expected output:
28
# count of n | python3 program for the above approach ; function to count n - digit numbers having absolute difference between adjacent digits in non - increasing order ; if digit = n + 1 , a valid n - digit number has been formed ; if the state has already been computed ; if the current digit is 1 , then any digit from [ 1 - 9 ] can be placed ; if the current digit is 2 , any digit from [ 0 - 9 ] can be placed ; for other digits , any digit i can be placed which satisfies abs ( prev1 - i ) <= abs ( prev1 - prev2 ) ; if absolute difference is less than or equal to diff ; function to count n - digit numbers with absolute difference between adjacent digits in non increasing order ; initialize dp table with - 1 ; function call ; 

dp = [[[0 for i in range(10)]for col in range(10)]for row in range(100)]


def countOfNumbers(digit, prev1, prev2, n):
    if (digit == n + 1):
        return 1
    val = dp[digit][prev1][prev2]
    if (val != -1):
        return val
    val = 0
    if (digit == 1):
        i = 1
        if n == 1:
            i = 0
        for j in range(i, 10):
            val += countOfNumbers(digit + 1, j, prev1, n)
    elif (digit == 2):
        for i in range(0, 10):
            val += countOfNumbers(digit + 1, i, prev1, n)
    else:
        diff = abs(prev2 - prev1)
        for i in range(0, 10):
            if (abs(prev1 - i) <= diff):
                val += countOfNumbers(digit + 1, i, prev1, n)
    return val


def countNumbersUtil(N):
    for i in range(0, 100):
        for j in range(0, 10):
            for k in range(0, 10):
                dp[i][j][k] = -1
    print(countOfNumbers(1, 0, 0, N))


N = 3
countNumbersUtil(N)

# Expected output:
495
# print all numbers that can be obtained by adding a or b to n exactly m times | function to find all possible numbers that can be obtained by adding a or b to n exactly n times ; if number of steps is 0 and only possible number is n ; add a to n and make a recursive call for m - 1 steps ; add b to n and make a recursive call for m - 1 steps . ; 
def possibleNumbers(numbers, N, M, A, B):
    if (M == 0):
        numbers .add(N)
        return
    possibleNumbers(numbers, N + A, M - 1, A, B)
    possibleNumbers(numbers, N + B, M - 1, A, B)


if __name__ == '__main__':
    N = 5
    M = 3
    A = 4
    B = 6
    numbers = set()
    possibleNumbers(numbers, N, M, A, B)
    for x in numbers:
        print(x, end=" ")

# Expected output:
17 19 21 23
# maximum sum of array after removing a positive or negative subarray | python 3 program for the above approach ; function to find the maximum sum of array after removing either the contiguous positive or negative elements ; store the total sum of array ; store the maximum contiguous negative sum ; store the sum of current contiguous negative elements ; store the minimum element of array ; traverse the array , arr [ ] ; update the overall sum ; store minimum element of array ; if arr [ i ] is positive ; update temp_sum to 0 ; add arr [ i ] to temp_sum ; update max_neg ; if no negative element in array then remove smallest positive element ; print the required sum ; 

import sys


def maxSum(arr, n):
    sum = 0
    max_neg = sys .maxsize
    tempsum = 0
    small = sys .maxsize
    for i in range(n):
        sum += arr[i]
        small = min(small, arr[i])
        if (arr[i] > 0):
            tempsum = 0
        else:
            tempsum += arr[i]
        max_neg = min(max_neg, tempsum)
    if (max_neg == 0):
        max_neg = small
    print(sum - max_neg)


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

# Expected output:
4
# count characters of a string which when removed individually makes the string equal to another string | function to count characters from a whose removal makes the strings a and b equal ; stores the index of the longest prefix ; stores the index of the longest suffix ; traverse the b ; traverse the b ; if n - m is equal to 1 and y is less than or equal to x ; print count of characters ; print positions of the characters ; otherwise ; 
def RemoveOneChar(A, B, N, M):
    X = 0
    Y = N - 1
    for i in range(M):
        if (A[X] != B[i]):
            break
        X += 1
    for i in range(M - 1, -1, -1):
        if (A[Y] != B[i]):
            break
        Y -= 1
    if (N - M == 1 and Y < X):
        print(X - Y + 1)
        for i in range(Y, X + 1):
            print(i + 1, end=" ")
        print()
    else:
        print(-1)


if __name__ == '__main__':
    A = "abaac"
    B = "abac"
    N = len(A)
    M = len(B)
    RemoveOneChar(A, B, N, M)

# Expected output:
2
3 4
# minimum flips or swapping of adjacent characters required to make a string equal to another | function to find minimum operations required to convert a to b ; store the size of the string ; store the required result ; traverse the string , a ; if a [ i ] is equal to b [ i ] ; check if swapping adjacent characters make the same - indexed characters equal or not ; otherwise , flip the current bit ; print the minimum number of operations ; 
def minimumOperation(a, b):
    n = len(a)
    i = 0
    minoperation = 0
    while (i < n):
        if (a[i] == b[i]):
            i = i + 1
            continue
        elif (a[i] == b[i + 1] and a[i + 1] == b[i] and i < n - 1):
            minoperation += 1
            i = i + 2
        elif (a[i] != b[i]):
            minoperation += 1
            i = i + 1
        else:
            i += 1
    print(minoperation)


if __name__ == '__main__':
    a = "10010010"
    b = "00001000"
    minimumOperation(a, b)

# Expected output:
3
# minimum replacements required to make sum of all k | function to find minimum number of operations required to make sum of all subarrays of size k equal ; stores number of operations ; iterate in the range [ 0 , k - 1 ] ; stores frequency of elements separated by distance k ; stores maximum frequency and corresponding element ; find max frequency element and its frequency ; update the number of operations ; print the result ; 
def findMinOperations(arr, N, K):
    operations = 0
    for i in range(K):
        freq = {}
        for j in range(i, N, K):
            if arr[j] in freq:
                freq[arr[j]] += 1
            else:
                freq[arr[j]] = 1
        max1 = 0
        num = 0
        for key, value in freq .items():
            if (value > max1):
                max1 = value
                num = key
        for key, value in freq .items():
            if (key != num):
                operations += value
    print(operations)


if __name__ == '__main__':
    arr = [3, 4, 3, 5, 6]
    K = 2
    N = len(arr)
    findMinOperations(arr, N, K)

# Expected output:
2
# count of pairs of integers up to x and y that generates equal quotient and remainder | python 3 program for the above approach ; function to calculate the number of pairs satisfying ( m / n = m % n ) ; iterate from 1 to sqrt ( x ) ; combining the conditions - 1 ) n > k 2 ) n <= y 3 ) n <= ( x / k - 1 ) ; 

from math import sqrt


def countOfPairs(x, y):
    count = 0
    for k in range(1, int(sqrt(x)) + 1, 1):
        count += max(0, min(y, x / k - 1) - k)
    print(int(count))


if __name__ == '__main__':
    x = 4
    y = 5
    countOfPairs(x, y)

# Expected output:
2
# count permutations of first n natural numbers having sum of adjacent elements equal to a perfect square | python program for the above approach ; function to count total number of permutation of the first n natural number having the sum of adjacent elements as perfect square ; create an adjacency matrix ; bcount elements whose indegree bis 1 ; bgenerate adjacency matrix ; find the sum of i and j ; if sum is perfect square . then move from i to j ; add it in adjacency list of i ; if any list is of size 1 , then the indegree is 1 ; if there is no element whose indegree is 1 , then n such permutations are possible ; if there is 1 or 2 elements whose indegree is 1 , then 2 permutations are possible ; if there are more than 2 elements whose indegree is 1 , then return 0 ; 

from math import sqrt, floor, ceil


def countPermutations(N):
    adj = [[]for i in range(105)]
    indeg = 0
    for i in range(1, N + 1):
        for j in range(1, N + 1):
            if (i == j):
                continue
            sum = i + j
            if (ceil(sqrt(sum)) == floor(sqrt(sum))):
                adj[i].append(j)
        if (len(adj[i]) == 1):
            indeg += 1
    if (indeg == 0):
        return N
    elif (indeg <= 2):
        return 2
    else:
        return 0


if __name__ == '__main__':
    N = 17
    print(countPermutations(N))

# Expected output:
2
# generate a permutation of first n natural numbers having count of unique adjacent differences equal to k | function to construct the lst with exactly k unique adjacent element differences ; stores the resultant array ; stores the left and the right most element of the range ; traverse the array ; if k is even , the add left to array and increment the left ; if k is odd , the add right to array and decrement the right ; repeat the steps for k - 1 times ; print the resultant lst ; 
def makelst(N, K):
    lst = [0 for i in range(N)]
    left = 1
    right = N
    for i in range(N):
        if (K % 2 == 0):
            lst[i] = left
            left = left + 1
        else:
            lst[i] = right
            right = right - 1
        if (K > 1):
            K -= 1
    for i in range(N):
        print(lst[i], end=" ")


if __name__ == '__main__':
    N = 6
    K = 3
    makelst(N, K)

# Expected output:
6 1 5 4 3 2
# find the date after next half year from a given date | function to find the date after the next half - year ; stores the number of days in the months of a leap year ; list of months ; days in half of a year ; index of current month ; starting day ; decrement the value of cnt by 1 ; increment cur_date ; if cnt is equal to 0 , then break out of the loop ; update cur_month ; update cur_date ; print the resultant date ; 
def getDate(d, m):
    days = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    month = [
        'January',
        'February',
        'March',
        'April',
        'May',
        'June',
        'July',
        'August',
        'September',
        'October',
        'November',
        'December']
    cnt = 183
    cur_month = month .index(m)
    cur_date = d
    while (1):
        while (cnt > 0 and cur_date <= days[cur_month]):
            cnt -= 1
            cur_date += 1
        if (cnt == 0):
            break
        cur_month = (cur_month + 1) % 12
        cur_date = 1
    print(cur_date, month[cur_month])


D = 15
M = "January"
getDate(D, M)

# Expected output:
16 July
# maximum number made up of distinct digits whose sum is equal to n | function to find the largest positive number made up of distinct digits having the sum of its digits as n ; if given number is greater than 45 , print - 1 ; store the required number and the digit to be considered ; loop until n > 0 and digit > 0 ; if the current digit is at most n then , add it to number num ; update the value of num ; decrement n by digit ; consider the next lower digit ; add 0 at the end and return the number num ; 
def largestNumber(N):
    if (N > 45):
        return -1
    num = 0
    digit = 9
    while (N > 0 and digit > 0):
        if (digit <= N):
            num *= 10
            num += digit
            N -= digit
        digit -= 1
    return num * 10


N = 25
print(largestNumber(N))

# Expected output:
98710
# minimum number that can be obtained by applying ' + ' and ' * ' operations on array elements | function to find the smallest number that can be obtained after applying the arithmetic operations mentioned in the string s ; stores the count of multiplication operator in the string ; store the required result ; iterate in the range to create the mask ; checking the number of bits that are set in the mask ; check if the number of bits that are set in the mask is multiplication operation ; storing the elements that is to be added ; apply the multiplications operation first ; if sign is ' * ' , then multiply last element of deque with arr [ i ] ; append last multiplied element in the deque ; if the element is to be added , then add it to the deque ; add all the element of the deque ; minimize the answer with the given sum ; 
def minimumSum(A, N, S):
    mul = 0
    for i in range(len(S)):
        if (S[i] == "*"):
            mul += 1
    ans = 1000000
    for i in range(1 << (N - 1)):
        cnt = 0
        v = []
        for j in range(N - 1):
            if ((1 << j) & i):
                cnt += 1
                v .append("*")
            else:
                v .append("+")
        if (cnt == mul):
            d = []
            d .append(A[0])
            for j in range(N - 1):
                if (v[j] == "*"):
                    x = d[len(d) - 1]
                    d .pop()
                    x = x * A[j + 1]
                    d .append(x)
                else:
                    d .append(A[j + 1])
            sum = 0
            while (len(d) > 0):
                x = d[0]
                sum += x
                d .pop(0)
            ans = min(ans, sum)
    return ans


A = [2, 2, 2, 2]
S = "**+"
N = len(A)
print(minimumSum(A, N, S))

# Expected output:
8
# minimum sum of medians of all possible k length subsequences of a sorted array | function to find the minimum sum of all the medians of the k sized sorted arrays formed from the given array ; stores the distance between the medians ; stores the number of subsequences required ; stores the resultant sum ; iterate from start and add all the medians ; add the value of arr [ i ] to the variable minsum ; increment i by select the median to get the next median index ; decrement the value of totalarrays by 1 ; print the resultant minimum sum ; 
def sumOfMedians(arr, N, K):
    selectMedian = (K + 1) // 2
    totalArrays = N // K
    minSum = 0
    i = selectMedian - 1
    while (i < N and totalArrays != 0):
        minSum = minSum + arr[i]
        i = i + selectMedian
        totalArrays -= 1
    print(minSum)


if __name__ == '__main__':
    arr = [1, 2, 3, 4, 5, 6]
    N = len(arr)
    K = 2
    sumOfMedians(arr, N, K)

# Expected output:
6
# find k positive integers not exceeding n and having sum s | function to represent s as the sum of k positive integers less than or equal to n ; if s can cannot be represented as sum of k integers ; if sum of first i natural numbers exceeds s ; insert i into nums [ ] ; insert first k - 1 positive numbers into answer [ ] ; insert the k - th number ; traverse the array answer [ ] ; if current element exceeds n ; add the extra value to the previous element ; reduce current element to n ; printing the k numbers ; 
def solve(S, K, N):
    if (K > N):
        print("-1")
        return
    max_sum, min_sum = 0, 0
    for i in range(K + 1):
        min_sum += i
        max_sum += N - i + 1
    if (S < min_sum or S > max_sum):
        print("-1")
        return
    s1 = 0
    nums = []
    for i in range(1, N + 1):
        if (s1 > S):
            break
        s1 += i
        nums .append(i)
    answer = []
    s2 = 0
    for i in range(K - 1):
        answer .append(nums[i])
        s2 += nums[i]
    answer .append(S - s2)
    Max = N
    for i in range(len(answer) - 1, -1, -1):
        if (answer[i] > Max):
            extra = answer[i] - Max
            if (i - 1 >= 0):
                answer[i - 1] += extra
            answer[i] = Max
            Max -= 1
        else:
            break
    for x in answer:
        print(x, end=" ")


if __name__ == '__main__':
    S, K, N = 15, 4, 8
    solve(S, K, N)

# Expected output:
1 2 4 8
# generate an n | python3 program for the above approach ; function to minimize the maximum element present in an n - length array having sum of elements divisible by k ; return the ceil value of ( k / n ) ; 

import math


def minimumValue(N, K):
    return math .ceil(K / N)


N = 4
K = 50
print(minimumValue(N, K))

# Expected output:
13
# minimum removal of elements from end of an array required to obtain sum k | function to find the minimum number of elements required to be removed from the ends of an array to obtain a sum k ; number of elements removed from the left and right ends of the array ; sum of left and right subarrays ; no element is taken from left initially ; start taking elements from right side ; ( left + 1 ) : count of elements removed from the left ( n - right ) : count of elements removed from the right ; if sum is greater than k ; if it is not possible to obtain sum k ; 
def minSizeArr(A, N, K):
    leftTaken = N
    rightTaken = N
    leftSum = 0
    rightSum = 0
    for left in range(-1, N):
        if (left != -1):
            leftSum += A[left]
        rightSum = 0
        for right in range(N - 1, left, -1):
            rightSum += A[right]
            if (leftSum + rightSum == K):
                if (leftTaken + rightTaken > (left + 1) + (N - right)):
                    leftTaken = left + 1
                    rightTaken = N - right
                break
            if (leftSum + rightSum > K):
                break
    if (leftTaken + rightTaken <= N):
        for i in range(leftTaken):
            print(A[i], end=" ")
        for i in range(rightTaken):
            print(A[N - i - 1], end=" ")
    else:
        print(-1)


if __name__ == "__main__":
    N = 7
    A = [3, 2, 1, 1, 1, 1, 3]
    K = 10
    minSizeArr(A, N, K)

# Expected output:
3 2 3 1 1
# minimum removal of elements from end of an array required to obtain sum k | function to find the smallest array that can be removed from the ends of an array to obtain sum k ; sum of complete array ; if given number is greater than sum of the array ; if number is equal to the sum of array ; tar is sum of middle subarray ; find the longest subarray with sum equal to tar ; if there is no subarray with sum equal to tar ; 
def minSizeArr(A, N, K):
    sum = 0
    for i in range(N):
        sum += A[i]
    if (K > sum):
        print(-1)
        return
    if (K == sum):
        for i in range(N):
            print(A[i], end=" ")
        return
    tar = sum - K
    um = {}
    um[0] = -1
    left = 0
    right = 0
    cur = 0
    maxi = -1
    for i in range(N):
        cur += A[i]
        if ((cur - tar) in um and (i - um[cur - tar]) > maxi):
            maxi = i - um[cur - tar]
            right = i
            left = um[cur - tar]
        if (cur not in um):
            um[cur] = i
    if (maxi == -1):
        print(-1)
    else:
        for i in range(left + 1):
            print(A[i], end=" ")
        for i in range(right):
            print(A[N - i - 1], end=" ")


if __name__ == '__main__':
    N = 7
    A = [3, 2, 1, 1, 1, 1, 3]
    K = 10
    minSizeArr(A, N, K)

# Expected output:
3 2 3 1 1
# minimum product modulo n possible for any pair from a given range | function to return the minimum possible value of ( i * j ) % n ; stores the minimum remainder ; iterate from l to r ; iterate from l to r ; print the minimum value of remainder ; if r - l >= n ; 
def minModulo(L, R, N):
    if (R - L < N):
        ans = 10 ** 9
        for i in range(L, R + 1):
            for j in range(L, R + 1):
                if (i != j):
                    ans = min(ans, (i * j) % N)
        print(ans)
    else:
        print(0)


if __name__ == '__main__':
    L, R, N = 6, 10, 2019
    minModulo(L, R, N)

# Expected output:
42
# count numbers having gcd with n equal to the number itself | function to count numbers whose gcd with n is the number itself ; stores the count of factors of n ; iterate over the range [ 1 , sqrt ( n ) ] ; if i is divisible by i ; increment count ; avoid counting the same factor twice ; return the resultant count ; 
def countNumbers(N):
    count = 0
    for i in range(1, N + 1):
        if i * i > N:
            break
        if (N % i == 0):
            count += 1
            if (N // i != i):
                count += 1
    return count


if __name__ == '__main__':
    N = 10
    print(countNumbers(N))

# Expected output:
4
# quadratic equation whose roots are k times the roots of given equation | function to find the quadratic equation whose roots are k times the roots of the given equation ; prquadratic equation ; 
def findEquation(A, B, C, K):
    print(A, K * B, K * K * C)


if __name__ == '__main__':
    A, B, C, K = 1, 2, 1, 2
    findEquation(A, B, C, K)

# Expected output:
1 4 4