giulio98/xlcost-single-prompt huggingface dataset:
# find n distinct numbers whose bitwise xor is equal to k | function to find n integers having bitwise xor equal to k ; base cases ; assign values to p and q ; stores bitwise xor of the first ( n - 3 ) elements ; print the first n - 3 elements ; calculate bitwise xor of first ( n - 3 ) elements ; 
def findArray(N, K):
    if (N == 1):
        print(K, end=" ")
        return
    if (N == 2):
        print("0", end=" ")
        print(K, end=" ")
        return
    P = N - 2
    Q = N - 1
    VAL = 0
    for i in range(1, N - 2):
        print(i, end=" ")
        VAL ^= i
    if (VAL == K):
        print(P, end=" ")
        print(Q, end=" ")
        print(P ^ Q, end=" ")
    else:
        print("0", end=" ")
        print(P, end=" ")
        print(P ^ K ^ VAL, end=" ")


N = 4
X = 6
findArray(N, X)

# Expected output:
1 0 2 5
# flip consecutive set bits starting from lsb of a given number | function to find the number after converting 1 s from end to 0 s ; count of 1 s ; and operation of n and 1 ; left shift n by count ; 
def findNumber(N):
    count = 0
    while ((N & 1) == 1):
        N = N >> 1
        count += 1
    return N << count


if __name__ == "__main__":
    N = 39
    print(findNumber(N))

# Expected output:
32
# flip consecutive set bits starting from lsb of a given number | function to find the number after converting 1 s from end to 0 s ; return the logical and of n and ( n + 1 ) ; 
def findNumber(N):
    return N & (N + 1)


if __name__ == '__main__':
    N = 39
    print(findNumber(N))

# Expected output:
32
# count of substrings of a string containing another given string as a substring | function to store all substrings of s ; stores the substrings of s ; pick start point in outer loop and lengths of different strings for a given starting point ; return the array containing substrings of s ; function to check if a is present in another string ; check if target is in the str or not ; function to count the subof s containing t in it as substring ; store all substrings of s in the array v [ ] ; store required count of substrings ; iterate through all the substrings of s ; if t is present in the current substring , then increment the ans ; print the answer ; 
def subString(s, n):
    v = []
    for i in range(n):
        for len in range(1, n - i + 1):
            find = s[i:i + len]
            v .append(find)
    return v


def IsPresent(str, target):
    if (target in str):
        return 1
    return -1


def countSubstrings(S, T):
    v = subString(S, len(S))
    ans = 0
    for it in v:
        if (IsPresent(it, T) != -1):
            ans += 1
    print(ans)


if __name__ == '__main__':
    S = "dabc"
    T = "ab"
    countSubstrings(S, T)

# Expected output:
4
# minimum flips of odd indexed elements from odd length subarrays to make two given arrays equal | function to find the minimum flip of subarrays required at alternate index to make binary arrays equals ; stores count of total operations ; stores count of consecutive unequal elements ; loop to run on odd positions ; incrementing the global counter ; change count to 0 ; if all last elements are equal ; loop to run on even positions ; incrementing the global counter ; change count to 0 ; print minimum operations ; 
def minOperation(X, Y, n):
    C = 0
    count = 0
    for i in range(1, n, 2):
        if (X[i] != Y[i]):
            count += 1
        else:
            if (count != 0):
                C += 1
            count = 0
    if (count != 0):
        C += 1
    count = 0
    for i in range(0, n, 2):
        if (X[i] != Y[i]):
            count += 1
        else:
            if (count != 0):
                C += 1
            count = 0
    if (count != 0):
        C += 1
    print(C)


if __name__ == '__main__':
    X = [1, 0, 0, 0, 0, 1]
    Y = [1, 1, 0, 1, 1, 1]
    N = len(X)
    minOperation(X, Y, N)

# Expected output:
2
# minimum subarray reversals required to make given binary array alternating | function to count minimum subarray reversal operations required to make array alternating ; stores minimum count of operations required to make array alternating ; traverse the array ; if arr [ i ] is greater than arr [ i + 1 ] ; update cntop ; 
def minimumcntOperationReq(arr, N):
    cntOp = 0
    for i in range(N - 1):
        if (arr[i] == arr[i + 1]):
            cntOp += 1
    return (cntOp + 1) // 2


arr = [1, 1, 1, 0, 1, 0, 0, 0]
N = len(arr)
print(minimumcntOperationReq(arr, N))

# Expected output:
2
# modify sequence of first n natural numbers to a given array by replacing pairs with their gcd | function to check if array arr [ ] can be obtained from first n natural numbers or not ; 
def isSequenceValid(B, N):
    for i in range(N):
        if ((i + 1) % B[i] != 0):
            print("No")
            return
    print("Yes")


N = 4
arr = [1, 2, 3, 2]
isSequenceValid(arr, N)

# Expected output:
Yes
# construct an array of first n natural numbers such that every adjacent pair is coprime | function to construct an arrary with adjacent elements as co - prime numbers ; iterate over the range [ 1 , n ] ; print i ; 
def ConstArrayAdjacentCoprime(N):
    for i in range(1, N + 1):
        print(i, end=" ")


if __name__ == "__main__":
    N = 6
    ConstArrayAdjacentCoprime(N)

# Expected output:
1 2 3 4 5 6
# minimum cost to convert one given string to another using swap , insert or delete operations | function to find the minimum cost to convert a to b ; stores the frequency of string a and b respectively ; store the frequencies of characters in a ; store the frequencies of characters in b ; minimum cost to convert a to b ; find the minimum cost ; print the minimum cost ; 
def minimumCost(a, b):
    fre1 = [0] * (256)
    fre2 = [0] * (256)
    for c in a:
        fre1[ord(c)] += 1
    for c in b:
        fre2[ord(c)] += 1
    mincost = 0
    for i in range(256):
        mincost += abs(fre1[i] - fre2[i])
    print(mincost)


if __name__ == '__main__':
    A = "1AB+-"
    B = "cc"
    minimumCost(A, B)

# Expected output:
7
# minimum cost required to move all elements to the same position | function to find the minimum cost required to place all elements in the same position ; stores the count of even and odd elements ; traverse the array arr [ ] ; count even elements ; count odd elements ; print the minimum count ; 
def minCost(arr):
    odd = 0
    even = 0
    for i in range(len(arr)):
        if (arr[i] % 2 == 0):
            even += 1
        else:
            odd += 1
    print(min(even, odd))


if __name__ == '__main__':
    arr = [1, 2, 3]
    minCost(arr)

# Expected output:
1
# maximize array sum by alternating the signs of adjacent elements | function to find the maximum sum by alternating the signs of adjacent elements of the array ; stores count of negative elements in the array ; stores maximum sum by alternating the signs of adjacent elements ; stores smallest absolute value of array elements ; stores sum of absolute value of array elements ; traverse the array ; if arr [ i ] is a negative number ; update cntneg ; update sum ; update smvalue ; update maxaltsum ; if cntneg is an odd number ; update maxaltsum ; 
def findMaxSumByAlternatingSign(arr, N):
    cntNeg = 0
    MaxAltSum = 0
    SmValue = 0
    sum = 0
    for i in range(N):
        if (arr[i] < 0):
            cntNeg += 1
        sum += abs(arr[i])
        SmValue = min(SmValue, abs(arr[i]))
    MaxAltSum = sum
    if (cntNeg & 1):
        MaxAltSum -= 2 * SmValue
    return MaxAltSum


if __name__ == '__main__':
    arr = [1, 1, -2, -4, 5]
    N = len(arr)
    print(findMaxSumByAlternatingSign(arr, N))

# Expected output:
13
# construct array with sum of product of same indexed elements in the given array equal to zero | function to generate a new array with product of same indexed elements with arr [ ] equal to 0 ; stores sum of same indexed array elements of arr and new array ; traverse the array ; if i is an even number ; insert arr [ i + 1 ] into the new array newarr [ ] ; insert - arr [ i - 1 ] into the new array newarr [ ] ; print new array elements ; 
def constructNewArraySumZero(arr, N):
    newArr = [0] * N
    for i in range(N):
        if (i % 2 == 0):
            newArr[i] = arr[i + 1]
        else:
            newArr[i] = -arr[i - 1]
    for i in range(N):
        print(newArr[i], end=" ")


arr = [1, 2, 3, -5, -6, 8]
N = len(arr)
constructNewArraySumZero(arr, N)

# Expected output:
2 -1 -5 -3 8 6
# generate bitonic sequence of length n from integers in a given range | python3 program for the above approach ; function to construct bitonic sequence of length n from integers in the range [ l , r ] ; if sequence is not possible ; store the resultant list ; if size of deque < n ; add elements from start ; print the stored in the list ; 

from collections import deque


def bitonicSequence(num, lower, upper):
    if (num > (upper - lower) * 2 + 1):
        print(-1)
        return
    ans = deque()
    for i in range(min(upper - lower + 1, num - 1)):
        ans .append(upper - i)
    for i in range(num - len(ans)):
        ans .appendleft(upper - i - 1)
    print(list(ans))


if __name__ == '__main__':
    N = 5
    L = 3
    R = 10
    bitonicSequence(N, L, R)

# Expected output:
[9, 10, 9, 8, 7]
# count substrings of same length differing by a single character from two given strings | function to count the number of substrings of equal length which differ by a single character ; stores the count of pairs of substrings ; traverse the string s ; traverse the string t ; different character ; increment the answer ; count equal substrings from next index ; increment the count ; increment q ; check the condition ; increment k ; add q to count ; decrement z ; return the final count ; 
def countSubstrings(s, t):
    answ = 0
    for i in range(len(s)):
        for j in range(len(t)):
            if t[j] != s[i]:
                answ += 1
                k = 1
                z = -1
                q = 1
                while (j + z >= 0 <= i + z and s[i + z] == t[j + z]):
                    z -= 1
                    answ += 1
                    q += 1
                while (len(s) > i + k and j + k <
                       len(t) and s[i + k] == t[j + k]):
                    k += 1
                    answ += q
                    z = -1
    return answ


S = "aba"
T = "baba"
print(countSubstrings(S, T))

# Expected output:
6
# minimum flips to make all 1 s in right and 0 s in left | function to find the minimum count of flips required to make all 1 s on the right and all 0 s on the left of the given string ; stores length of str ; store count of 0 s in the string ; traverse the string ; if current character is 0 ; update zeros ; if count of 0 s in the string is 0 or n ; store minimum count of flips required to make all 0 s on the left and all 1 s on the right ; stores count of 1 s on the left of each index ; stores count of flips required to make all 0 s on the left and all 1 s on the right ; traverse the string ; if current character is 1 ; update currones ; update flips ; update the minimum count of flips ; 
def minimumCntOfFlipsRequired(str):
    n = len(str)
    zeros = 0
    for i in range(n):
        if (str[i] == '0'):
            zeros += 1
    if (zeros == 0 or zeros == n):
        return 0
    minFlips = 10000001
    currOnes = 0
    flips = 0
    for i in range(n):
        if (str[i] == '1'):
            currOnes += 1
        flips = currOnes + (zeros - (i + 1 - currOnes))
        minFlips = min(minFlips, flips)
    return minFlips


if __name__ == '__main__':
    str = "100101"
    print(minimumCntOfFlipsRequired(str))

# Expected output:
2
# maximize length of longest non | function to find the length of the longest non - decreasing array that can be generated ; stores the length of the longest non - decreasing array that can be generated from the array ; stores index of start pointer ; stores index of end pointer ; stores previously inserted element into the new array ; traverse the array ; if a [ start ] is less than or equal to a [ end ] ; if no element inserted into the newly generated array ; update prev ; update res ; update start ; if a [ start ] is greater than or equal to prev ; update res ; update prev ; update start ; if a [ end ] is greater than or equal to prev ; update res ; update prev ; update end ; if a [ end ] is greater than a [ start ] ; if no element inserted into the newly generated array ; update prev ; update res ; update end ; if a [ end ] is greater than or equal to prev ; update res ; update prev ; update end ; if a [ start ] is greater than or equal to prev ; update res ; update prev ; update start ; 
def findLongestNonDecreasing(A, N):
    res = 0
    start = 0
    end = N - 1
    prev = -1
    while (start <= end):
        if (A[start] <= A[end]):
            if (prev == -1):
                prev = A[start]
                res += 1
                start += 1
            else:
                if (A[start] >= prev):
                    res += 1
                    prev = A[start]
                    start += 1
                elif (A[end] >= prev):
                    res += 1
                    prev = A[end]
                    end -= 1
                else:
                    break
        else:
            if (prev == -1):
                prev = A[end]
                res += 1
                end -= 1
            else:
                if (A[end] >= prev):
                    res += 1
                    prev = A[end]
                    end -= 1
                elif (A[start] >= prev):
                    res += 1
                    prev = A[start]
                    start += 1
                else:
                    break
    return res


if __name__ == "__main__":
    A = [1, 1, 3, 5, 4, 3, 6, 2, 1]
    N = len(A)
    print(findLongestNonDecreasing(A, N))

# Expected output:
7
# non | function to print all pairs whose sum of bitwise or and and is n ; iterate from i = 0 to n ; print pairs ( i , n - i ) ; 
def findPairs(N):
    for i in range(0, N + 1):
        print("(", i, ",", N - i, "), ", end="")


if __name__ == "__main__":
    N = 5
    findPairs(N)

# Expected output:
( 0 , 5 ), ( 1 , 4 ), ( 2 , 3 ), ( 3 , 2 ), ( 4 , 1 ), ( 5 , 0 ),
# minimum index to split array into subarrays with co | function to find the gcd of 2 numbers ; base case ; find the gcd recursively ; function to find the minimum partition index k s . t . product of both subarrays around that partition are co - prime ; stores the prefix and suffix array product ; update the prefix array ; update the suffix array ; iterate the given array ; check if prefix [ k ] and suffix [ k + 1 ] are co - prime ; if no index for partition exists , then return - 1 ; 
def GCD(a, b):
    if (b == 0):
        return a
    return GCD(b, a % b)


def findPartition(nums, N):
    prefix = [0] * N
    suffix = [0] * N
    prefix[0] = nums[0]
    for i in range(1, N):
        prefix[i] = (prefix[i - 1] * nums[i])
    suffix[N - 1] = nums[N - 1]
    for i in range(N - 2, -1, -1):
        suffix[i] = (suffix[i + 1] * nums[i])
    for k in range(N - 1):
        if (GCD(prefix[k], suffix[k + 1]) == 1):
            return k
    return -1


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

# Expected output:
2
# minimum subarray reversals required such that sum of all pairs of adjacent elements is odd | function to count reversals to separate elements with same parity ; traverse the given array ; count size of subarray having integers with same parity only ; otherwise ; reversals required is equal to one less than subarray size ; return the total reversals ; function to print the array elements ; function to count the minimum reversals required to make make sum of all adjacent elements odd ; stores operations required for separating adjacent odd elements ; stores operations required for separating adjacent even elements ; maximum of two is the return ; 
def separate(arr, n, parity):
    count = 1
    res = 0
    for i in range(1, n):
        if (((arr[i] + parity) & 1) and ((arr[i - 1] + parity) & 1)):
            count += 1
        else:
            if (count > 1):
                res += count - 1
            count = 1
    return res


def printArray(arr, n):
    for i in range(n):
        print(arr[i], end=" ")


def requiredOps(arr, N):
    res1 = separate(arr, N, 0)
    res2 = separate(arr, N, 1)
    print(max(res1, res2))


if __name__ == "__main__":
    arr = [13, 2, 6, 8, 3, 5, 7, 10, 14, 15]
    N = len(arr)
    requiredOps(arr, N)

# Expected output:
3
# count prime pairs whose difference is also a prime number | python3 program to implement the above approach ; function to find all prime numbers in the range [ 1 , n ] ; isprime [ i ] : stores if i is a prime number or not ; calculate all prime numbers up to max using sieve of eratosthenes ; if p is a prime number ; set all multiple of p as non - prime ; update isprime ; function to count pairs of prime numbers in the range [ 1 , n ] whose difference is prime ; function to count pairs of prime numbers whose difference is also a prime number ; isprime [ i ] : stores if i is a prime number or not ; iterate over the range [ 2 , n ] ; if i and i - 2 is a prime number ; update cntpairs ; 

from math import sqrt


def SieveOfEratosthenes(N):
    isPrime = [True for i in range(N + 1)]
    isPrime[0] = False
    isPrime[1] = False
    for p in range(2, int(sqrt(N)) + 1, 1):
        if (isPrime[p]):
            for i in range(p * p, N + 1, p):
                isPrime[i] = False
    return isPrime


def cntPairsdiffOfPrimeisPrime(N):
    cntPairs = 0
    isPrime = SieveOfEratosthenes(N)
    for i in range(2, N + 1, 1):
        if (isPrime[i] and isPrime[i - 2]):
            cntPairs += 2
    return cntPairs


if __name__ == '__main__':
    N = 5
    print(cntPairsdiffOfPrimeisPrime(N))

# Expected output:
2
# length of longest subsequence consisting of distinct adjacent elements | function that finds the length of longest subsequence having different adjacent elements ; stores the length of the longest subsequence ; traverse the array ; if previous and current element are not same ; increment the count ; print the maximum length ; 
def longestSubsequence(arr, N):
    count = 1
    for i in range(1, N, 1):
        if (arr[i] != arr[i - 1]):
            count += 1
    print(count)


if __name__ == '__main__':
    arr = [7, 8, 1, 2, 2, 5, 5, 1]
    N = len(arr)
    longestSubsequence(arr, N)

# Expected output:
6
# count of substrings having the most frequent character in the string as first character | python3 program for the above approach ; function to find all substrings whose first character occurs maximum number of times ; stores frequency of characters ; stores character that appears maximum number of times max_char = ; stores max frequency of character ; updates frequency of characters ; update maxfreq ; character that occures maximum number of times ; update the maximum frequency character ; stores all count of substrings ; traverse over string ; get the current character ; update count of substrings ; return the count of all valid substrings ; 

import sys


def substringCount(s):
    freq = [0 for i in range(26)]
    maxfreq = -sys .maxsize - 1
    for i in range(len(s)):
        freq[ord(s[i]) - ord('a')] += 1
        if (maxfreq < freq[ord(s[i]) - ord('a')]):
            maxfreq = freq[ord(s[i]) - ord('a')]
    for i in range(26):
        if (maxfreq == freq[i]):
            max_char = chr(i + ord('a'))
            break
    ans = 0
    for i in range(len(s)):
        ch = s[i]
        if (max_char == ch):
            ans += (len(s) - i)
    return ans


if __name__ == '__main__':
    S = "abcab"
    print(substringCount(S))

# Expected output:
7
# minimum sum possible by assigning every increasing / decreasing consecutive pair with values in that order | function to print the minimum sum of values assigned to each element of the array as per given conditions ; initialize vectors with value 1 ; traverse from left to right ; update if ans [ i ] > ans [ i - 1 ] ; traverse from right to left ; update as ans [ i ] > ans [ i + 1 ] if arr [ i ] > arr [ i + 1 ] ; find the minimum sum ; print the sum ; 
def minSum(arr, n):
    ans = [1] * (n)
    for i in range(1, n):
        if (arr[i] > arr[i - 1]):
            ans[i] = max(ans[i], ans[i - 1] + 1)
    for i in range(n - 2, -1, -1):
        if (arr[i] > arr[i + 1]):
            ans[i] = max(ans[i], ans[i + 1] + 1)
    s = 0
    for x in ans:
        s = s + x
    print(s)


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

# Expected output:
4
# maximize sum of assigned weights by flipping at most k bits in given binary string | function to find maximum sum of weights of binary string after at most k flips ; stores lengths of substrings of the form 1. . 00. . 1 s ; stores the index of last 1 encountered in the string ; stores the index of first 1 encountered ; stores lengths of all substrings having of 0 s enclosed by 1 s at both ends ; traverse the string ; if character is 0 ; if character is 1 first priority ; second priority ; add according to the first priority ; stores length of the shortest substring of 0 s ; convert shortest substrings of 0 s to 1 s ; add according to the first priority ; add according to the third priority ; if more 0 s can be made into 1 , then check for 0 s at ends ; update the ans ; if k is non - zero , then flip 0 s at the beginning ; return the final weights ; 
def findMax(s, n, k):

    ans = 0
    l = 0
    ind = -1
    indf = -1
    ls = set([])
    for i in range(n):
        if (s[i] == '0'):
            l += 1
        elif (s[i] == '1'
              and l > 0 and ans != 0):
            ls.add(l)
            l = 0
        if (s[i] == '1'):
            ind = i
            l = 0
            if (indf == -1):
                indf = i
            if (i > 0 and s[i - 1] == '1'):
                ans += 2
            else:
                ans += 1
    curr = 0
    while (k > 0 and len(ls) != 0):
        for i in ls:
            curr = i
            break
        if (k >= curr):
            ans += (2 * curr + 1)
            k -= curr
        else:
            ans += (2 * k)
            k = 0

        ls.remove(curr)
    if (k > 0):
        ans += (2 * min(k,
                        n - (ind + 1))
                - 1)
        k -= min(k, n - (ind + 1))

        if (ind > -1):
            ans += 1
    if (k > 0):
        ans += (min(indf, k) * 2 - 1)

        if (indf > -1):
            ans += 1
    return ans


if __name__ == "__main__":
    s = "1110000101"
    N = len(s)
    K = 3
    print(findMax(s, N, K))

# Expected output:
14
# minimum removal of consecutive similar characters required to empty a binary string | python3 program for the above approach ; function to find minimum steps to make the empty ; stores the modified string ; size of string ; removing substring of same character from modified string ; print the minimum steps required ; 

from math import ceil


def minSteps(S):
    new_str = ""
    N = len(S)
    i = 0
    while (i < N):
        new_str += S[i]
        j = i
        while (i < N and S[i] == S[j]):
            i += 1
    print(ceil((len(new_str) + 1) / 2))


if __name__ == '__main__':
    S = "0010100"
    minSteps(S)

# Expected output:
3
# count pairs with equal bitwise and and bitwise or value | function to count pairs in an array whose bitwise and equal to bitwise or ; store count of pairs whose bitwise and equal to bitwise or ; stores frequency of distinct elements of array ; traverse the array ; increment the frequency of arr [ i ] ; traverse map ; 
def countPairs(arr, N):
    cntPairs = 0
    mp = {}
    for i in range(0, N):
        if arr[i] in mp:
            mp[arr[i]] = mp[arr[i]] + 1
        else:
            mp[arr[i]] = 1
    for freq in mp:
        cntPairs += int((mp[freq] * (mp[freq] - 1)) / 2)
    return cntPairs


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

# Expected output:
4