giulio98/xlcost-single-prompt huggingface dataset:
# minimizing array sum by applying xor operation on all elements of the array | python3 implementation of the approach ; function to return the minimized sum ; to store the frequency of bit in every element ; finding element x ; taking xor of elements and finding sum ; 

MAX = 25


def getMinSum(arr, n):
    bits_count = [0] * MAX
    max_bit = 0
    sum = 0
    ans = 0
    for d in range(n):
        e = arr[d]
        f = 0
        while (e > 0):
            rem = e % 2
            e = e // 2
            if (rem == 1):
                bits_count[f] += rem
            f += 1
        max_bit = max(max_bit, f)
    for d in range(max_bit):
        temp = pow(2, d)
        if (bits_count[d] > n // 2):
            ans = ans + temp
    for d in range(n):
        arr[d] = arr[d] ^ ans
        sum = sum + arr[d]
    return sum


if __name__ == "__main__":
    arr = [3, 5, 7, 11, 15]
    n = len(arr)
    print(getMinSum(arr, n))

# Expected output:
26
# maximum money that can be withdrawn in two steps | function to return the maximum coins we can get ; update elements such that x > y ; take from the maximum ; refill ; again , take the maximum ; 
def maxCoins(X, Y):
    if (X < Y):
        X, Y = Y, X
    coins = X
    X -= 1
    coins += max(X, Y)
    return coins


if __name__ == "__main__":
    X = 7
    Y = 5
    print(maxCoins(X, Y))

# Expected output:
13
# minimum value of x to make all array elements equal by either decreasing or increasing by x | function that returns the minimum value of x ; declare a set ; iterate in the array element and insert them into the set ; if unique elements is 1 ; unique elements is 2 ; get both el2 and el1 ; check if they are even ; if there are 3 unique elements ; get three unique elements ; check if their difference is same ; more than 3 unique elements ; 
def findMinimumX(a, n):
    st = set()
    for i in range(n):
        st .add(a[i])
    if (len(st) == 1):
        return 0
    if (len(st) == 2):
        st = list(st)
        el1 = st[0]
        el2 = st[1]
        if ((el2 - el1) % 2 == 0):
            return int((el2 - el1) / 2)
        else:
            return (el2 - el1)
    if (len(st) == 3):
        st = list(st)
        el1 = st[0]
        el2 = st[1]
        el3 = st[2]
        if ((el2 - el1) == (el3 - el2)):
            return el2 - el1
        else:
            return -1
    return -1


if __name__ == '__main__':
    a = [1, 4, 4, 7, 4, 1]
    n = len(a)
    print(findMinimumX(a, n))

# Expected output:
3
# maximum elements which can be crossed using given units of a and b | function to find the number of elements crossed ; keep a copy of a ; iterate in the binary array ; if no a and b left to use ; if there is no a ; use b and increase a by 1 if arr [ i ] is 1 ; simply use b ; use a if theres no b ; increase a and use b if arr [ i ] == 1 ; use a ; 
def findElementsCrossed(arr, a, b, n):
    aa = a
    ans = 0
    for i in range(n):
        if (a == 0 and b == 0):
            break
        elif (a == 0):
            if (arr[i] == 1):
                b -= 1
                a = min(aa, a + 1)
            else:
                b -= 1
        elif (b == 0):
            a -= 1
        elif (arr[i] == 1 and a < aa):
            b -= 1
            a = min(aa, a + 1)
        else:
            a -= 1
        ans += 1
    return ans


arr = [1, 0, 0, 1, 0, 1]
n = len(arr)
a = 1
b = 2
print(findElementsCrossed(arr, a, b, n))

# Expected output:
3
# count the pairs of vowels in the given string | function that return true if character ch is a vowel ; function to return the count of adjacent vowel pairs in the given string ; if current character and the character after it are both vowels ; 
def isVowel(ch):
    if ch in ['a', 'e', 'i', 'o', 'u']:
        return True
    else:
        return False


def vowelPairs(s, n):
    cnt = 0
    for i in range(n - 1):
        if (isVowel(s[i]) and isVowel(s[i + 1])):
            cnt += 1
    return cnt


s = "abaebio"
n = len(s)
print(vowelPairs(s, n))

# Expected output:
2
# minimum possible final health of the last monster in a game | function to return the gcd of two numbers ; function to return the minimum possible health for the monster ; gcd of first and second element ; gcd for all subsequent elements ; 
def gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)


def solve(health, n):
    currentgcd = gcd(health[0], health[1])
    for i in range(2, n):
        currentgcd = gcd(currentgcd, health[i])
    return currentgcd


health = [4, 6, 8, 12]
n = len(health)
print(solve(health, n))

# Expected output:
2
# divide array into increasing and decreasing subsequence without changing the order | function to print strictly increasing and strictly decreasing sequence if possible ; arrays to store strictly increasing and decreasing sequence ; initializing last element of both sequence ; iterating through the array ; if current element can be appended to both the sequences ; if next element is greater than the current element then append it to the strictly increasing array ; otherwise append it to the strictly decreasing array ; if current element can be appended to the increasing sequence only ; if current element can be appended to the decreasing sequence only ; else we can not make such sequences from the given array ; print the required sequences ; 
def Find_Sequence(array, n):
    inc_arr, dec_arr = [], []
    inc, dec = -1, 1e7
    for i in range(n):
        if inc < array[i] < dec:
            if array[i] < array[i + 1]:
                inc = array[i]
                inc_arr .append(array[i])
            else:
                dec = array[i]
                dec_arr .append(array[i])
        elif inc < array[i]:
            inc = array[i]
            inc_arr .append(array[i])
        elif dec > array[i]:
            dec = array[i]
            dec_arr .append(array[i])
        else:
            print('-1')
            break
    else:
        print(inc_arr, dec_arr)


arr = [5, 1, 3, 6, 8, 2, 9, 0, 10]
n = len(arr)
Find_Sequence(arr, n)

# Expected output:
[1, 3, 6, 8, 9, 10] [5, 2, 0]
# find the sum of digits of a number at even and odd places | function to return the reverse of a number ; function to find the sum of the odd and even positioned digits in a number ; if c is even number then it means digit extracted is at even place ; 
def reverse(n):
    rev = 0
    while (n != 0):
        rev = (rev * 10) + (n % 10)
        n //= 10
    return rev


def getSum(n):
    n = reverse(n)
    sumOdd = 0
    sumEven = 0
    c = 1
    while (n != 0):
        if (c % 2 == 0):
            sumEven += n % 10
        else:
            sumOdd += n % 10
        n //= 10
        c += 1
    print("Sum odd =", sumOdd)
    print("Sum even =", sumEven)


n = 457892
getSum(n)

# Expected output:
Sum odd = 20
Sum even = 15
# find the sum of digits of a number at even and odd places | function to find the sum of the odd and even positioned digits in a number ; if n is odd then the last digit will be odd positioned ; to store the respective sums ; while there are digits left process ; if current digit is odd positioned ; even positioned digit ; invert state ; remove last digit ; 
def getSum(n):
    if (n % 2 == 1):
        isOdd = True
    else:
        isOdd = False
    sumOdd = 0
    sumEven = 0
    while (n != 0):
        if (isOdd):
            sumOdd += n % 10
        else:
            sumEven += n % 10
        isOdd = not isOdd
        n //= 10
    print("Sum odd = ", sumOdd)
    print("Sum even = ", sumEven)


if __name__ == "__main__":
    n = 457892
    getSum(n)

# Expected output:
Sum odd =  20
Sum even =  15
# count the number of currency notes needed | function to return the amount of notes with value a required ; if possible ; 
def bankNotes(A, B, S, N):
    numerator = S - (B * N)
    denominator = A - B
    if (numerator % denominator == 0):
        return (numerator // denominator)
    return -1


A, B, S, N = 1, 2, 7, 5
print(bankNotes(A, B, S, N))

# Expected output:
3
# length of the longest substring with no consecutive same letters | function to return the length of the required sub - string ; get the length of the string ; iterate in the string ; check for not consecutive ; if cnt greater than maxi ; re - initialize ; check after iteration is complete ; 
def longestSubstring(s):
    cnt = 1
    maxi = 1
    n = len(s)
    for i in range(1, n):
        if (s[i] != s[i - 1]):
            cnt += 1
        else:
            maxi = max(cnt, maxi)
            cnt = 1
    maxi = max(cnt, maxi)
    return maxi


if __name__ == "__main__":
    s = "ccccdeededff"
    print(longestSubstring(s))

# Expected output:
5
# minimum number of changes such that elements are first negative and then positive | function to return the count of minimum operations required ; to store the count of negative integers on the right of the current index ( inclusive ) ; find the count of negative integers on the right ; if current element is negative ; to store the count of positive elements ; find the positive integers on the left ; if current element is positive ; update the answer ; return the required answer ; 
def Minimum_Operations(a, n):
    np = [0 for i in range(n + 1)]
    for i in range(n - 1, -1, -1):
        np[i] = np[i + 1]
        if (a[i] <= 0):
            np[i] += 1
    pos = 0
    ans = n
    for i in range(n - 1):
        if (a[i] >= 0):
            pos += 1
        ans = min(ans, pos + np[i + 1])
    return ans


a = [-1, 0, 1, 2]
n = len(a)
print(Minimum_Operations(a, n))

# Expected output:
1
# sum of elements in an array whose difference with the mean of another array is less than k | function for finding sum of elements whose diff with mean is not more than k ; find the mean of second array ; find sum of elements from array1 whose difference with mean is not more than k ; return result ; 
def findSumofEle(arr1, m, arr2, n, k):
    arraySum = 0
    for i in range(n):
        arraySum += arr2[i]
    mean = arraySum / n
    sumOfElements = 0
    difference = 0
    for i in range(m):
        difference = arr1[i] - mean
        if ((difference < 0) and (k > (-1) * difference)):
            sumOfElements += arr1[i]
        if ((difference >= 0) and (k > difference)):
            sumOfElements += arr1[i]
    return sumOfElements


arr1 = [1, 2, 3, 4, 7, 9]
arr2 = [0, 1, 2, 1, 1, 4]
k = 2
m = len(arr1)
n = len(arr2)
print(findSumofEle(arr1, m, arr2, n, k))

# Expected output:
6
# find n positive integers that satisfy the given equations | function to find n positive integers that satisfy the given conditions ; to store n positive integers ; place n - 1 one 's ; if can not place ( y - ( n - 1 ) ) as the nth integer ; place nth integer ; to store the sum of squares of n integers ; if it is less than x ; print the required integers ; 
def findIntegers(n, x, y):
    ans = []
    for i in range(n - 1):
        ans .append(1)
    if (y - (n - 1) <= 0):
        print("-1", end="")
        return
    ans .append(y - (n - 1))
    store = 0
    for i in range(n):
        store += ans[i] * ans[i]
    if (store < x):
        print("-1", end="")
        return
    for i in range(n):
        print(ans[i], end=" ")


n, x, y = 3, 254, 18
findIntegers(n, x, y)

# Expected output:
1 1 16
# find the minimum number of steps to reach m from n | function to find a minimum number of steps to reach m from n ; continue till m is greater than n ; if m is odd ; add one ; divide m by 2 ; return the required answer ; 
def Minsteps(n, m):
    ans = 0
    while (m > n):
        if (m & 1):
            m += 1
            ans += 1
        m //= 2
        ans += 1
    return ans + n - m


n = 4
m = 6
print(Minsteps(n, m))

# Expected output:
2
# find the number of jumps to reach x in the number line from zero | utility function to calculate sum of numbers from 1 to x ; function to find the number of jumps to reach x in the number line from zero ; first make number positive answer will be same either it is positive or negative ; to store the required answer ; continue till number is lesser or not in same parity ; return the required answer ; 
def getsum(x):
    return int((x * (x + 1)) / 2)


def countJumps(n):
    n = abs(n)
    ans = 0
    while (getsum(ans) < n or (getsum(ans) - n) & 1):
        ans += 1
    return ans


if __name__ == '__main__':
    n = 9
    print(countJumps(n))

# Expected output:
5
# maximum number of candies that can be bought | function to return the maximum candies that can be bought ; buy all the candies of the last type ; starting from second last ; amount of candies of the current type that can be bought ; add candies of current type that can be bought ; update the previous bought amount ; 
def maxCandies(arr, n):
    prevBought = arr[n - 1]
    candies = prevBought
    for i in range(n - 2, -1, -1):
        x = min(prevBought - 1, arr[i])
        if (x >= 0):
            candies += x
            prevBought = x
    return candies


if __name__ == "__main__":
    arr = [1, 2, 1, 3, 6]
    n = len(arr)
    print(maxCandies(arr, n))

# Expected output:
10
# minimum cuts required to convert a palindromic string to a different palindromic string | function to check if string is palindrome or not ; function to check if it is possible to get result by making just one cut ; appending last element in front ; removing last element ; checking whether string s2 is palindrome and different from s . ; if length is <= 3 then it is impossible ; array to store frequency of characters ; store count of characters in a array ; condition for edge cases ; return 1 if it is possible to get palindromic string in just one cut . else we can always reached in two cuttings . ; 
def isPalindrome(s):
    for i in range(len(s)):
        if (s[i] != s[len(s) - i - 1]):
            return False
    return true


def ans(s):
    s2 = s
    for i in range(len(s)):
        s2 = s2[len(s2) - 1] + s2
        s2 = s2[0:len(s2) - 1]
        if (s != s2 and isPalindrome(s2)):
            return True
    return False


def solve(s):
    if (len(s) <= 3):
        return -1
    cnt = [0 for i in range(26)]
    for i in range(len(s)):
        cnt[ord(s[i]) - ord('a')] += 1
    max = cnt[0]
    for i in range(len(cnt)):
        if cnt[i] > max:
            max = cnt[i]
    if (max >= len(s) - 1):
        return -1
    else:
        if ans(s):
            return 1
        else:
            return 2


if __name__ == '__main__':
    s = "nolon"
    print(solve(s))

# Expected output:
2
# minimum cuts required to convert a palindromic string to a different palindromic string | recursive function to find minimum number of cuts if length of string is even ; if length is odd then return 2 ; to check if half of palindromic string is itself a palindrome ; if not then return 1 ; else call function with half palindromic string ; function to find minimum number of cuts if length of string is odd ; if length is <= 3 then it is impossible ; array to store frequency of characters ; store count of characters in a array ; condition for edge cases ; if length is even ; if length is odd ; 
def solveEven(s):
    if len(s) % 2 == 1:
        return 2
    ls = s[0:len(s) // 2]
    rs = s[len(s) // 2:len(s)]
    if ls != rs:
        return 1
    return solveEven(ls)


def solveOdd(s):
    return 2


def solve(s):
    if len(s) <= 3:
        return -1
    cnt = [0] * 25
    for i in range(0, len(s)):
        cnt[ord(s[i]) - ord('a')] += 1
    if max(cnt) >= len(s) - 1:
        return -1
    if len(s) % 2 == 0:
        return solveEven(s)
    if len(s) % 2 == 1:
        return solveOdd(s)


if __name__ == "__main__":
    s = "nolon"
    print(solve(s))

# Expected output:
2
# minimum changes required such that the string satisfies the given condition | function to return the minimum changes required ; to store the count of minimum changes , number of ones and the number of zeroes ; first character has to be '1 ; if condition fails changes need to be made ; return the required count ; 
def minChanges(str, n):
    count, zeros, ones = 0, 0, 0
    if (ord(str[0]) != ord('1')):
        count += 1
        ones += 1

    for i in range(1, n):
        if (ord(str[i]) == ord('0')):
            zeros += 1
        else:
            ones += 1
        if (zeros > ones):
            zeros -= 1
            ones += 1
            count += 1
    return count


if __name__ == '__main__':
    str = "0000"
    n = len(str)
    print(minChanges(str, n))

# Expected output:
2
# count possible moves in the given direction in a grid | function to return the count of possible steps in a single direction ; it can cover infinite steps ; we are approaching towards x = n ; we are approaching towards x = 1 ; function to return the count of steps ; take the minimum of both moves independently ; update count and current positions ; 
def steps(cur, x, n):
    if x == 0:
        return float('inf')
    elif x > 0:
        return abs((n - cur) // x)
    else:
        return abs(int((cur - 1) / x))


def countSteps(curx, cury, n, m, moves):
    count = 0
    k = len(moves)
    for i in range(0, k):
        x = moves[i][0]
        y = moves[i][1]
        stepct = min(steps(curx, x, n), steps(cury, y, m))
        count += stepct
        curx += stepct * x
        cury += stepct * y
    return count


if __name__ == "__main__":
    n, m, x, y = 4, 5, 1, 1
    moves = [[1, 1], [1, 1], [0, -2]]
    print(countSteps(x, y, n, m, moves))

# Expected output:
4
# minimum number of elements that should be removed to make the array good | function to return the minimum number of elements that must be removed to make the given array good ; count frequency of each element ; for each element check if there exists another element that makes a valid pair ; if does not exist then increment answer ; 
def minimumRemoval(n, a):
    c = dict .fromkeys(a, 0)
    for i in range(n):
        c[a[i]] += 1
    ans = 0
    for i in range(n):
        ok = False
        for j in range(31):
            x = (1 << j) - a[i]
            if (x in c and (c[x] > 1 or (c[x] == 1 and x != a[i]))):
                ok = True
                break
        if (not ok):
            ans += 1
    return ans


if __name__ == "__main__":
    a = [4, 7, 1, 5, 4, 9]
    n = len(a)
    print(minimumRemoval(n, a))

# Expected output:
1
# minimum elements to be removed such that sum of adjacent elements is always odd | returns the minimum number of eliminations ; stores the previous element ; stores the new value ; check if the previous and current values are of same parity ; previous value is now the current value ; return the counter variable ; 
def min_elimination(n, arr):
    count = 0
    prev_val = arr[0]
    for i in range(1, n):
        curr_val = arr[i]
        if (curr_val % 2 == prev_val % 2):
            count = count + 1
        prev_val = curr_val
    return count


arr = [1, 2, 3, 7, 9]
n = len(arr)
print(min_elimination(n, arr))

# Expected output:
2
# count all possible n digit numbers that satisfy the given condition | function to return the count of required numbers ; if n is odd then return 0 ; 
def getCount(N):
    if (N % 2 == 1):
        return "0"
    result = "9"
    for i in range(1, N // 2):
        result = result + "0"
    return result


N = 4
print(getCount(N))

# Expected output:
90
# maximum number of teams that can be formed with given persons | function that returns true if it possible to form a team with the given n and m ; 1 person of type1 and 2 persons of type2 can be chosen ; 1 person of type2 and 2 persons of type1 can be chosen ; cannot from a team ; function to return the maximum number of teams that can be formed ; to store the required count of teams formed ; choose 2 persons of type1 ; and 1 person of type2 ; choose 2 persons of type2 ; and 1 person of type1 ; another team has been formed ; 
def canFormTeam(n, m):
    if (n >= 1 and m >= 2):
        return True
    if (m >= 1 and n >= 2):
        return True
    return False


def maxTeams(n, m):
    count = 0
    while (canFormTeam(n, m)):
        if (n > m):
            n -= 2
            m -= 1
        else:
            m -= 2
            n -= 1
        count += 1
    return count


if __name__ == '__main__':
    n = 4
    m = 5
    print(maxTeams(n, m))

# Expected output:
3
# find the side of the smallest square that can contain given 4 big squares | function to find the maximum of two values ; function to find the smallest side of the suitable suitcase ; sort array to find the smallest and largest side of suitcases ; side of the suitcase will be smallest if they arranged in 2 x 2 way so find all possible sides of that arrangement ; since suitcase should be square so find maximum of all four side ; now find greatest side and that will be the smallest square ; return the result ; 
def max(a, b):
    if (a > b):
        return a
    else:
        return b


def smallestSide(a):
    a .sort(reverse=False)
    side1 = a[0] + a[3]
    side2 = a[1] + a[2]
    side3 = a[0] + a[1]
    side4 = a[2] + a[3]
    side11 = max(side1, side2)
    side12 = max(side3, side4)
    sideOfSquare = max(side11, side12)
    return sideOfSquare


if __name__ == '__main__':
    side = [0 for i in range(4)]
    print("Test Case 1")
    side[0] = 2
    side[1] = 2
    side[2] = 2
    side[3] = 2
    print(smallestSide(side))
    print("",  end="")
    print("Test Case 2")
    side[0] = 100000000000000
    side[1] = 123450000000000
    side[2] = 987650000000000
    side[3] = 987654321000000
    print(smallestSide(side))

# Expected output:
Test Case 1
4
Test Case 2
1975304321000000
# rectangle with minimum possible difference between the length and the width | python3 implementation of the approach ; function to print the length ( l ) and breadth ( b ) of the rectangle having area = n and | l - b | as minimum as possible ; i is a factor ; l >= sqrt ( area ) >= i ; so here l is + ve always ; here l and b are length and breadth of the rectangle ; 

import math as mt


def find_rectangle(area):
    l, b = 0, 0
    M = mt .ceil(mt .sqrt(area))
    ans = 0
    for i in range(M, 0, -1):
        if (area % i == 0):
            l = (area // i)
            b = i
            break
    print("l =", l, ", b =", b)


area = 99
find_rectangle(area)

# Expected output:
l = 11 , b = 9
# rectangle with minimum possible difference between the length and the width | python3 implementation of the approach ; function to print the length ( l ) and breadth ( b ) of the rectangle having area = n and | l - b | as minimum as possible ; 

import math


def find_rectangle(area):
    for i in range(int(math .ceil(math .sqrt(area))), area + 1):
        if ((int(area / i) * i) == area):
            print(i, int(area / i))
            return


area = 99
find_rectangle(area)

# Expected output:
11 9
# largest sub | python3 implementation of the approach ; function to return the size of the required sub - set ; sort the array ; set to store the contents of the required sub - set ; insert the elements satisfying the conditions ; return the size of the set ; 

import math as mt


def sizeSubSet(a, k, n):
    a .sort()
    s = set()
    for i in range(n):
        if (a[i] % k != 0 or a[i] // k not in s):
            s .add(a[i])
    return len(s)


a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
n = len(a)
k = 2
print(sizeSubSet(a, k, n))

# Expected output:
6
# minimum number of sets with numbers less than y | python3 program to find the minimum number sets with consecutive numbers and less than y ; function to find the minimum number of shets ; variable to count the number of sets ; iterate in the string ; add the number to string ; mark that we got a number ; check if previous was anytime less than y ; current number ; check for current number ; check for last added number ; 

import math as mt


def minimumSets(s, y):
    cnt = 0
    num = 0
    l = len(s)
    f = 0
    for i in range(l):
        num = num * 10 + (ord(s[i]) - ord('0'))
        if (num <= y):
            f = 1
            if (f):
                cnt += 1
            num = ord(s[i]) - ord('0')
            f = 0
            if (num <= y):
                f = 1
            else:
                num = 0
    if (f):
        cnt += 1
    return cnt


s = "1234"
y = 30
print(minimumSets(s, y))

# Expected output:
4