giulio98/xlcost-single-prompt huggingface dataset:
# check if permutation of a given string can be made palindromic by removing at most k characters | function to check if the satisfies the given conditions or not ; stores length of given string ; stores frequency of each character of str ; update frequency of current character ; stores count of distinct character whose frequency is odd ; traverse the cntfreq [ ] array . ; if frequency of character i is odd ; update cntoddfreq ; if count of distinct character having odd frequency is <= k + 1 ; 
def checkPalinK(str, K):
    N = len(str)
    cntFreq = [0] * 256
    for i in range(N):
        cntFreq[ord(str[i])] += 1
    cntOddFreq = 0
    for i in range(256):
        if (cntFreq[i] % 2 == 1):
            cntOddFreq += 1
    if (cntOddFreq <= (K + 1)):
        return True
    return False


if __name__ == '__main__':
    str = "geeksforgeeks"
    K = 2
    if (checkPalinK(str, K)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# maximum number of elements that can be removed such that mex of the given array remains unchanged | function to find the maximum number of elements that can be removed ; initialize hash [ ] with 0 s ; initialize mex ; set hash [ i ] = 1 , if i is present in arr [ ] ; find mex from the hash ; print the maximum numbers that can be removed ; 
def countRemovableElem(arr, N):
    hash = [0] * (N + 1)
    mex = N + 1
    for i in range(0, N):
        if (arr[i] <= N):
            hash[arr[i]] = 1
    for i in range(1, N + 1):
        if (hash[i] == 0):
            mex = i
            break
    print(N - (mex - 1))


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

# Expected output:
2
# print all positions of a given string having count of smaller characters equal on both sides | function to find indexes of the given that satisfy the condition ; stores length of given string ; stores frequency of each character of strr ; update frequency of current character ; cntleftfreq [ i ] stores frequency of characters present on the left side of index i . ; traverse the given string ; stores count of smaller characters on left side of i . ; stores count of smaller characters on right side of i . ; traverse smaller characters on left side of index i . ; update cntleft ; update cntright ; update cntleftfreq [ strr [ i ] ] ; if count of smaller elements on both sides equal ; print current index ; 
def printIndexes(strr):
    N = len(strr)
    cntFreq = [0] * 256
    for i in range(N):
        cntFreq[ord(strr[i])] += 1
    cntLeftFreq = [0] * 256
    for i in range(N):
        cntLeft = 0
        cntRight = 0
        for j in range(ord(strr[i]) - 1, -1, -1):
            cntLeft += cntLeftFreq[j]
            cntRight += (cntFreq[j] - cntLeftFreq[j])
        cntLeftFreq[ord(strr[i])] += 1
        if (cntLeft == cntRight and cntLeft != 0):
            print(i, end=" ")


if __name__ == '__main__':
    strr = "aabacdabbb"
    printIndexes(strr)

# Expected output:
2 4
# count three | function to count three - digit numbers having difference x with its reverse ; if x is not multiple of 99 ; no solution exists ; generate all possible pairs of digits [ 1 , 9 ] ; if any pair is obtained with difference x / 99 ; increase count ; return the count ; 
def Count_Number(x):
    ans = 0
    if (x % 99 != 0):
        ans = -1
    else:
        diff = x / 99
        for i in range(1, 10):
            for j in range(1, 10):
                if ((i - j) == diff):
                    ans += 10
    return ans


if __name__ == '__main__':
    x = 792
    print(Count_Number(x))

# Expected output:
10
# check if a point having maximum x and y coordinates exists or not | python3 program for the above approach ; initialize inf as inifnity ; function to return the pohaving maximum x and y coordinates ; base case ; stores if valid poexists ; if poarr [ i ] is valid ; check for the same point ; check for a valid point ; if current pois the required point ; otherwise ; function to find the required point ; stores the powith maximum x and y - coordinates ; if no required poexists ; 

import sys
INF = sys .maxsize


def findMaxPoint(arr, i, n):
    if (i == n):
        return [INF, INF]
    flag = True
    for j in range(n):
        if (j == i):
            continue
        if (arr[j][0] >= arr[i][0] or arr[j][1] >= arr[i][1]):
            flag = False
            break
    if (flag):
        return arr[i]
    return findMaxPoint(arr, i + 1, n)


def findMaxPoints(arr, n):
    ans = findMaxPoint(arr, 0, n)
    if (ans[0] == INF):
        print(-1)
    else:
        print("(", ans[0], " ", ans[1], ")")


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

# Expected output:
( 5   5 )
# check if a point having maximum x and y coordinates exists or not | python3 program for the above approach ; function to find the pohaving max x and y coordinates ; initialize maxx and maxy ; length of the given array ; get maximum x & y coordinates ; check if the required point i . e . , ( maxx , maxy ) is present ; if powith maximum x and y coordinates is present ; if no such poexists ; 

import sys


def findMaxPoint(arr):
    maxX = -sys .maxsize
    maxY = -sys .maxsize
    n = len(arr)
    for i in range(n):
        maxX = max(maxX, arr[i][0])
        maxY = max(maxY, arr[i][1])
    for i in range(n):
        if (maxX == arr[i][0] and maxY == arr[i][1]):
            print("(", maxX, ", ", maxY, ")")
            return
    print(-1)


if __name__ == '__main__':
    arr = [[1, 2], [2, 1], [3, 4], [4, 3], [5, 5]]
    findMaxPoint(arr)

# Expected output:
( 5 ,  5 )
# search insert position of k in a sorted array | function to find insert position of k ; traverse the array ; if k is found ; if arr [ i ] exceeds k ; if all array elements are smaller ; 
def find_index(arr, n, K):
    for i in range(n):
        if arr[i] == K:
            return i
        elif arr[i] > K:
            return i
    return n


arr = [1, 3, 5, 6]
n = len(arr)
K = 2
print(find_index(arr, n, K))

# Expected output:
1
# count subarrays for every array element in which they are the minimum | set 2 | function to calculate total number of sub - arrays for each element where that element is occurring as the minimum element ; map for storing the number of sub - arrays for each element ; traverse over all possible subarrays ; minimum in each subarray ; print result ; 
def minSubarray(arr, N):
    m = {}
    for i in range(N):
        mini = 10 ** 9
        for j in range(i, N):
            mini = min(mini, arr[j])
            m[mini] = m .get(mini, 0) + 1
    for i in arr:
        print(m[i], end=" ")


if __name__ == '__main__':
    arr = [3, 2, 1, 4]
    N = len(arr)
    minSubarray(arr, N)

# Expected output:
1 2 6 1
# count subarrays for every array element in which they are the minimum | set 2 | function to count subarrays for each element where it is the minimum ; for the length of strictly larger numbers on the left of a [ i ] ; storing x in result [ i ] ; for the length of strictly larger numbers on the right of a [ i ] ; store x * y in result array ; print the result ; 
def minSubarray(arr, N):
    result = [0] * N
    l = []
    r = []
    for i in range(N):
        count = 1
        while (len(l) != 0 and l[-1][0] > arr[i]):
            count += l[-1][1]
            l .pop()
        l .append([arr[i], count])
        result[i] = count
    for i in range(N - 1, -1, -1):
        count = 1
        while (len(r) != 0 and r[-1][0] >= arr[i]):
            count += r[-1][1]
            r .pop()
        r .append([arr[i], count])
        result[i] *= count
    for i in range(N):
        print(result[i], end=" ")


if __name__ == "__main__":
    arr = [3, 2, 1, 4]
    N = len(arr)
    minSubarray(arr, N)

# Expected output:
1 2 6 1
# count full prime numbers in a given range | function to check if a number is prime or not ; if a divisor of n exists ; function to check if a number is full prime or not ; if n is not a prime ; otherwise ; extract digit ; if any digit of n is non - prime ; function to prcount of full primes in a range [ l , r ] ; stores count of full primes ; check if i is full prime ; 
def isPrime(num):
    if (num <= 1):
        return False
    for i in range(2, num + 1):
        if i * i > num:
            break
        if (num % i == 0):
            return False
    return True


def isFulPrime(n):
    if (not isPrime(n)):
        return False
    else:
        while (n > 0):
            rem = n % 10
            if (not (rem == 2 or rem == 3 or rem == 5 or rem == 7)):
                return False
            n = n // 10
    return True


def countFulPrime(L, R):
    cnt = 0
    for i in range(L, R + 1):
        if ((i % 2) != 0 and isFulPrime(i)):
            cnt += 1
    return cnt


if __name__ == '__main__':
    L = 1
    R = 100
    ans = 0
    if (L < 3):
        ans += 1
    print(ans + countFulPrime(L, R))

# Expected output:
8
# print alternate elements of an array | function to print alternate elements of the given array ; print elements at odd positions ; if currindex stores even index or odd position ; 
def printAlter(arr, N):
    for currIndex in range(0, N):
        if (currIndex % 2 == 0):
            print(arr[currIndex], end=" ")


if __name__ == "__main__":
    arr = [1, 2, 3, 4, 5]
    N = len(arr)
    printAlter(arr, N)

# Expected output:
1 3 5
# minimize deletions in a binary string to remove all subsequences of the form "0101" | function to find minimum characters to be removed such that no subsequence of the form "0101" exists in the string ; stores the partial sums ; calculate partial sums ; setting endpoints and deleting characters indices . ; return count of deleted characters ; 
def findmin(s):
    n = len(s)
    maximum = 0
    incr = [0] * (n + 1)
    for i in range(0, n):
        incr[i + 1] = incr[i]
        if (s[i] == '0'):
            incr[i + 1] = incr[i + 1] + 1
    for i in range(0, n + 1):
        for j in range(i + 1, n):
            maximum = max(
                maximum, incr[i] + j - i + 1 - (incr[j + 1] - incr[i]) + incr[n] - incr[j + 1])
    return n - maximum


if __name__ == "__main__":
    S = "0110100110"
    minimum = findmin(S)
    print(minimum)

# Expected output:
3
# length of longest subarray with product equal to a power of 2 | function to check whether a number is power of 2 or not ; function to find maximum length subarray having product of element as a perfect power of 2 ; stores current subarray length ; stores maximum subarray length ; traverse the given array ; if arr [ i ] is power of 2 ; increment max_length ; update max_len_subarray ; otherwise ; prthe maximum length ; 
def isPower(x):
    if (x != 0 and (x & (x - 1)) == 0):
        return True
    return False


def maximumlength(arr, N):
    max_length = 0
    max_len_subarray = 0
    for i in range(N):
        if (isPower(arr[i])):
            max_length += 1
            max_len_subarray = max(max_length, max_len_subarray)
        else:
            max_length = 0
    print(max_len_subarray)


if __name__ == '__main__':
    arr = [2, 5, 4, 6, 8, 8, 2]
    N = len(arr)
    maximumlength(arr, N)

# Expected output:
3
# length of smallest subarray to be removed such that the remaining array is sorted | python3 program for the above approach ; find the length of the shortest subarray ; to store the result ; calculate the possible length of the sorted subarray from left ; array is sorted ; calculate the possible length of the sorted subarray from left ; update the result ; calculate the possible length in the middle we can delete and update the result ; update the result ; return the result ; 

import sys


def findLengthOfShortestSubarray(arr):
    minlength = sys .maxsize
    left = 0
    right = len(arr) - 1
    while left < right and arr[left + 1] >= arr[left]:
        left += 1
    if left == len(arr) - 1:
        return 0
    while right > left and arr[right - 1] <= arr[right]:
        right -= 1
    minlength = min(len(arr) - left - 1, right)
    j = right
    for i in range(left + 1):
        if arr[i] <= arr[j]:
            minlength = min(minlength, j - i - 1)
        elif j < len(arr) - 1:
            j += 1
        else:
            break
    return minlength


arr = [6, 3, 10, 11, 15, 20, 13, 3, 18, 12]
print(findLengthOfShortestSubarray(arr))

# Expected output:
8
# smallest number to be subtracted to convert given number to a palindrome | function to evaluate minimum subtraction required to make a number palindrome ; counts number of subtractions required ; run a loop till n >= 0 ; store the current number ; store reverse of current number ; reverse the number ; check if n is palindrome ; increment the counter ; reduce the number by 1 ; print the result ; 
def minSub(N):
    count = 0
    while (N >= 0):
        num = N
        rev = 0
        while (num != 0):
            digit = num % 10
            rev = (rev * 10) + digit
            num = num // 10
        if (N == rev):
            break
        count += 1
        N -= 1
    print(count)


if __name__ == '__main__':
    N = 3456
    minSub(N)

# Expected output:
13
# count subarrays of atleast size 3 forming a geometric progression ( gp ) | function to count all the subarrays of size at least 3 forming gp ; if array size is less than 3 ; stores the count of subarray ; stores the count of subarray for each iteration ; traverse the array ; check if l [ i ] forms gp ; otherwise , update count to 0 ; update the final count ; return the final count ; 
def numberOfGP(L, N):
    if (N <= 2):
        return 0
    count = 0
    res = 0
    for i in range(2, N):
        if (L[i - 1] * L[i - 1] == L[i] * L[i - 2]):
            count += 1
        else:
            count = 0
        res += count
    return res


if __name__ == '__main__':
    arr = [1, 2, 4, 8, 16, 24]
    N = len(arr)
    print(numberOfGP(arr, N))

# Expected output:
6
# count even length subarrays having bitwise xor equal to 0 | function to count the number of even - length subarrays having bitwise xor equal to 0 ; stores the count of required subarrays ; stores prefix - xor of arr [ i , i + 1 , ... n - 1 ] ; traverse the array ; calculate the prefix - xor of current subarray ; check if xor of the current subarray is 0 and length is even ; 
def cntSubarr(arr, N):
    res = 0
    prefixXor = 0
    for i in range(N - 1):
        prefixXor = arr[i]
        for j in range(i + 1, N):
            prefixXor ^= arr[j]
            if (prefixXor == 0 and (j - i + 1) % 2 == 0):
                res += 1
    return res


if __name__ == '__main__':
    arr = [2, 2, 3, 3, 6, 7, 8]
    N = len(arr)
    print(cntSubarr(arr, N))

# Expected output:
3
# count even length subarrays having bitwise xor equal to 0 | python3 program to implement the above approach ; function to get the count of even length subarrays having bitwise xor 0 ; stores prefix - xor of the given array ; stores prefix - xor at even index of the array . ; stores prefix - xor at odd index of the array . ; stores count of subarrays that satisfy the condition ; length from 0 index to odd index is even ; traverse the array . ; take prefix - xor ; if index is odd ; calculate pairs ; increment prefix - xor at odd index ; calculate pairs ; increment prefix - xor at odd index ; 

M = 1000000


def cntSubXor(arr, N):
    prefixXor = 0
    Even = [0] * M
    Odd = [0] * M
    cntSub = 0
    Odd[0] = 1
    for i in range(0, N):
        prefixXor ^= arr[i]
        if (i % 2 == 1):
            cntSub += Odd[prefixXor]
            Odd[prefixXor] += 1
        else:
            cntSub += Even[prefixXor]
            Even[prefixXor] += 1
    return cntSub


if __name__ == '__main__':
    arr = [2, 2, 3, 3, 6, 7, 8]
    N = len(arr)
    print(cntSubXor(arr, N))

# Expected output:
3
# maximize sum of array elements removed by performing the given operations | function to find the maximum sum of the array arr [ ] where each element can be reduced to at most min [ i ] ; stores the pair of arr [ i ] & min [ i ] ; sorting vector of pairs ; traverse the vector of pairs ; add to the value of s ; update k ; 
def findMaxSum(arr, n, min, k, S):
    A = []
    for i in range(n):
        A .append((arr[i], min[i]))
    A = sorted(A)
    A = A[::-1]
    K = 0
    for i in range(n):
        S += max(A[i][0] - K, A[i][1])
        K += k
    return S


if __name__ == '__main__':
    arr, min = [], []
    arr = [3, 5, 2, 1]
    min = [3, 2, 1, 3]
    N = len(arr)
    K = 3
    S = 0
    S = findMaxSum(arr, N, min, K, S)
    print(S)

# Expected output:
12
# length of smallest substring of a given string which contains another string as subsequence | python3 program to implement the above approach ; function to find the length of smallest substring of a having string b as a subsequence ; stores the characters present in string b ; find index of characters of a that are also present in string b ; if character is present in string b ; store the index of character ; flag is used to check if substring is possible ; assume that substring is possible ; stores first and last indices of the substring respectively ; for first character of string b ; if the first character of b is not present in a ; if the first character of b is present in a ; remove the index from map ; update indices of the substring ; for the remaining characters of b ; if index possible for current character ; if no index is possible ; if no more substring is possible ; update the minimum length of substring ; return the result ; 

import sys


def minLength(a, b):
    Char = {}
    for i in range(len(b)):
        Char[b[i]] = Char .get(b[i], 0) + 1
    CharacterIndex = {}
    for i in range(len(a)):
        x = a[i]
        if (x in Char):
            CharacterIndex[x] = CharacterIndex .get(x, [])
            CharacterIndex[x].append(i)
    l = sys .maxsize
    while (True):
        flag = 1
        firstVar = 0
        lastVar = 0
        for i in range(len(b)):
            if (i == 0):
                if (b[i]not in CharacterIndex):
                    flag = 0
                    break
                else:
                    x = CharacterIndex[b[i]][0]
                    CharacterIndex[b[i]].remove(CharacterIndex[b[i]][0])
                    firstVar = x
                    lastVar = x
            else:
                elementFound = 0
                for e in CharacterIndex[b[i]]:
                    if (e > lastVar):
                        elementFound = 1
                        lastVar = e
                        break
                if (elementFound == 0):
                    flag = 0
                    break
        if (flag == 0):
            break
        l = min(l, abs(lastVar - firstVar) + 1)
    return l


if __name__ == '__main__':
    a = "abcdefababaef"
    b = "abf"
    l = minLength(a, b)
    if (l != sys .maxsize):
        print(l)
    else:
        print("Impossible")

# Expected output:
5
# count smaller primes on the right of each array element | function to check if a number is prime or not ; function to find the count of smaller primes on the right of each array element ; stores the count of smaller primes ; if a [ j ] <= a [ i ] and a [ j ] is prime ; increase count ; print the count for the current element ; 
def is_prime(n):
    if (n <= 1):
        return 0
    for i in range(2, n + 1):
        if i * i > n:
            break
        if (n % i == 0):
            return 0
    return 1


def countSmallerPrimes(ar, N):
    for i in range(N):
        count = 0
        for j in range(i + 1, N):
            if (ar[j] <= ar[i] and is_prime(ar[j])):
                count += 1
        print(count, end=" ")


if __name__ == '__main__':
    ar = [43, 3, 5, 7, 2, 41]
    N = len(ar)
    countSmallerPrimes(ar, N)

# Expected output:
5 1 1 1 0 0
# check if a string is concatenation of another given string | function to check if a is concatenation of another string ; stores the length of str2 ; stores the length of str1 ; if m is not multiple of n ; traverse both the strings ; if str1 is not concatenation of str2 ; 
def checkConcat(str1, str2):
    N = len(str1)
    M = len(str2)
    if (N % M != 0):
        return False
    for i in range(N):
        if (str1[i] != str2[i % M]):
            return False
    return True


if __name__ == '__main__':
    str1 = "abcabcabc"
    str2 = "abc"
    if (checkConcat(str1, str2)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# minimize length of string by replacing k pairs of distinct adjacent characters | function to minimize the length of the by replacing distinct pairs of adjacent characters ; stores the length of the given string ; stores the index of the given string ; traverse the given string ; if two adjacent haracters are distinct ; if all characters are equal ; if all characters are distinct ; 
def MinLen(str, K):
    N = len(str)
    i = 0
    while (i < N - 1):
        if (str[i] != str[i + 1]):
            break
        i += 1
    if (i == N - 1):
        return N
    return max(1, N - K)


if __name__ == '__main__':
    str = "aabc"
    K = 1
    print(MinLen(str, K))

# Expected output:
3
# length of smallest sequence having sum x and product y | python3 program for the above approach ; function for checking valid or not ; function for checking boundary of binary search ; function to calculate the minimum sequence size using binary search ; initialize high and low ; base case ; print - 1 if a sequence cannot be generated ; otherwise ; iterate until difference between high and low exceeds 1 ; calculate mid ; reset values of high and low accordingly ; print the answer ; 

from math import floor, exp


def temp(n, x):
    return pow(x * 1 / n, n)


def check(n, y, x):
    v = temp(n, x)
    return (v >= y)


def find(x, y):
    high = floor(x / exp(1.0))
    low = 1
    if (x == y):
        print(1)
    elif (not check(high, y, x)):
        print(-1)
    else:
        while (high - low > 1):
            mid = (high + low) // 2
            if (check(mid, y, x)):
                high = mid
            else:
                low = mid
        print(high)


if __name__ == '__main__':
    x = 9
    y = 8
    find(x, y)

# Expected output:
2
# smallest array that can be obtained by replacing adjacent pairs with their products | function to minimize the size of array by performing given operations ; if all array elements are not same ; if all array elements are same ; 
def minLength(arr, N):
    for i in range(1, N):
        if (arr[0] != arr[i]):
            return 1
    return N


if __name__ == "__main__":
    arr = [2, 1, 3, 1]
    N = len(arr)
    print(minLength(arr, N))

# Expected output:
1
# subarrays whose sum is a perfect square | python3 program to implement the above approach ; function to print the start and end indices of all subarrays whose sum is a perfect square ; stores the current subarray sum ; update current subarray sum ; stores the square root of currsubsum ; check if currsubsum is a perfect square or not ; 

import math


def PrintIndexes(arr, N):
    for i in range(N):
        currSubSum = 0
        for j in range(i, N, 1):
            currSubSum += arr[j]
            sq = int(math .sqrt(currSubSum))
            if (sq * sq == currSubSum):
                print("(", i, ",", j, ")", end=" ")


arr = [65, 79, 81]
N = len(arr)
PrintIndexes(arr, N)

# Expected output:
( 0 , 1 ) ( 0 , 2 ) ( 2 , 2 )
# count positions in binary matrix having equal count of set bits in corresponding row and column | function to return the count of indices in from the given binary matrix having equal count of set bits in its row and column ; stores count of set bits in corresponding column and row ; traverse matrix ; since 1 contains a set bit ; update count of set bits for current row and col ; stores the count of required indices ; traverse matrix ; if current row and column has equal count of set bits ; return count of required position ; 
def countPosition(mat):
    n = len(mat)
    m = len(mat[0])
    row = [0] * n
    col = [0] * m
    for i in range(n):
        for j in range(m):
            if (mat[i][j] == 1):
                col[j] += 1
                row[i] += 1
    count = 0
    for i in range(n):
        for j in range(m):
            if (row[i] == col[j]):
                count += 1
    return count


mat = [[0, 1], [1, 1]]
print(countPosition(mat))

# Expected output:
2