giulio98/xlcost-single-prompt huggingface dataset:
# maximum the value of a given expression for any pair of coordinates on a 2d plane | function to find the maximum value of the given expression possible for any pair of co - ordinates ; stores the differences between pairs ; stores the maximum value ; traverse the array arr [ ] [ ] ; while pq is not empty and difference between point [ 0 ] and pq . top ( ) [ 1 ] > k ; removes the top element ; if pq is not empty ; update the value res ; push pair { point [ 1 ] - point [ 0 ] , point [ 0 ] } in pq ; prthe result ; 
def findMaxValueOfEquation(arr, K):
    pq = []
    res = -10 ** 8
    for point in arr:
        while (len(pq) > 0 and point[0] - pq[-1][1] > K):
            del pq[-1]
        if (len(pq) > 0):
            res = max(res, pq[-1][0] + point[0] + point[1])
        pq .append([point[1] - point[0], point[0]])
        pq = sorted(pq)
    print(res)


if __name__ == '__main__':
    arr = [[1, 3], [2, 0], [5, 10], [6, -10]]
    K = 1
    findMaxValueOfEquation(arr, K)

# Expected output:
4
# minimum increments required to make absolute difference of all pairwise adjacent array elements even | function to find the minimum number of increments of array elements required to make difference between all pairwise adjacent elements even ; stores the count of odd and even elements ; traverse the array ; increment odd count ; increment even count ; return the minimum number of operations required ; 
def minOperations(arr, n):
    oddcount, evencount = 0, 0
    for i in range(n):
        if (arr[i] % 2 == 1):
            oddcount += 1
        else:
            evencount += 1
    return min(oddcount, evencount)


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

# Expected output:
2
# count numbers up to c that can be reduced to 0 by adding or subtracting a or b | function to calculate gcd of the two numbers a and b ; base case ; recursively find the gcd ; function to count the numbers up to c that can be reduced to 0 by adding or subtracting a or b ; stores gcd of a and b ; stores the count of multiples of g in the range ( 0 , c ] ; print the result ; 
def gcd(a, b):
    if (b == 0):
        return a
    return gcd(b, a % b)


def countDistinctNumbers(A, B, C):
    g = gcd(A, B)
    count = C // g
    print(count)


A = 2
B = 3
C = 5
countDistinctNumbers(A, B, C)

# Expected output:
5
# find the last element after repeatedly removing every second element from either end alternately | function to find the last element remaining in the array after performing the given operations ; checks if traversal is from left to right or vice versa ; store the elements currently present in the array ; store the distance between 2 consecutive array elements ; store the first element of the remaining array ; iterate while elements are greater than 1 ; if left to right turn ; update head ; otherwise , check if the remaining elements are odd ; if true , update head ; eleminate half of the array elements ; double the steps after each turn ; alter the turn ; print the remaining element ; 
def printLastElement(arr, N):
    leftTurn = True
    remainElements = N
    step = 1
    head = 1
    while (remainElements > 1):
        if (leftTurn):
            head = head + step
        else:
            if (remainElements % 2 == 1):
                head = head + step
        remainElements = remainElements // 2
        step = step * 2
        leftTurn = not leftTurn
    print(arr[head - 1])


if __name__ == "__main__":
    arr = [2, 3, 5, 6]
    N = len(arr)
    printLastElement(arr, N)

# Expected output:
3
# distributed c candies among n boys such that difference between maximum and minimum candies received is k | function to calculate the maximum and minimum number of candies a boy can possess ; all candies will be given to one boy ; all the candies will be given to 1 boy ; give k candies to 1 st boy initially ; count remaining candies ; if the last candy of remaining candies is given to the last boy , i . e nth boy ; increase minimum count ; 
def max_min(N, C, K):
    maximum = 0
    minimum = 0
    if (N == 1):
        maximum = minimum = C
    elif (K >= C):
        maximum = C
        minimum = 0
    else:
        maximum = K
        minimum = 0
        remain_candy = C - K
        maximum += remain_candy // N
        minimum = remain_candy // N
    if (remain_candy % N == N - 1):
        minimum += 1
    print("Maximum = {}".format(maximum))
    print("Minimum = {}".format(minimum))


N = 4
C = 12
K = 3
max_min(N, C, K)

# Expected output:
Maximum = 5
Minimum = 2
# length of smallest subarray required to be removed to make remaining elements consecutive | function to find the length of the smallest subarray to be removed to make remaining array elements consecutive ; store the ending index of the longest prefix consecutive array ; traverse the array to find the longest prefix consecutive sequence ; a [ 0. . . left_index ] is the prefix consecutive sequence ; store the starting index of the longest suffix consecutive sequence ; traverse the array to find the longest suffix consecutive sequence ; a [ right_index ... n - 1 ] is the consecutive sequence ; store the smallest subarray required to be removed ; check if subarray from the middle can be removed ; update the right index s . t . a [ 0 , n - 1 ] is consecutive ; if updated_right < n , then update the minimumlength ; print the required result ; 
def shortestSubarray(A, N):
    i = 0
    left_index = 0
    for i in range(N - 1):
        if (A[i] + 1 != A[i + 1]):
            break
    left_index = i
    right_index = 0
    i = N - 1
    while (i >= 1):
        if (A[i] != A[i - 1] + 1):
            break
        i -= 1
    right_index = i
    updated_right = 0
    minLength = min(N - left_index - 1, right_index)
    if (A[right_index] <= A[left_index] + 1):
        updated_right = (right_index + A[left_index] - A[right_index] + 1)
        if (updated_right < N):
            minLength = min(minLength, updated_right - left_index - 1)
    print(minLength)


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

# Expected output:
4
# check if a string can be split into 3 substrings such that one of them is a substring of the other two | function to check if string s contains any character with frequency >= 3 or not ; stores frequency of characters ; iterate over the string ; update the frequency of current character ; iterate over the hash array ; if any character has frequency >= 3 ; otherwise ; 
def freqCheck(S, N):
    hash = [0] * 26
    for i in range(N):
        hash[ord(S[i]) - ord('a')] += 1
    for i in range(26):
        if (hash[i] > 2):
            return "Yes"
    return "No"


if __name__ == "__main__":
    S = "geekseekforgeeks"
    N = len(S)
    print(freqCheck(S, N))

# Expected output:
Yes
# minimize length of a string by removing suffixes and prefixes of same characters | function to find the minimum length of the string after removing the same characters from the end and front of the two strings after dividing into 2 substrings ; initialize two pointers ; traverse the string s ; current char on left pointer ; shift i towards right ; shift j towards left ; return the minimum possible length of string ; 
def minLength(s):
    i = 0
    j = len(s) - 1
    while (i < j and s[i] == s[j]):
        d = s[i]
        while (i <= j and s[i] == d):
            i += 1
        while (i <= j and s[j] == d):
            j -= 1
    return j - i + 1


if __name__ == "__main__":
    S = "aacbcca"
    print(minLength(S))

# Expected output:
1
# number of binary search trees of height h consisting of h + 1 nodes | function to calculate x ^ y modulo 1000000007 in o ( log y ) ; stores the value of x ^ y ; update x if it exceeds mod ; if x is divisible by mod ; if y is odd , then multiply x with result ; divide y by 2 ; update the value of x ; return the value of x ^ y ; function to count the number of of bsts of height h consisting of ( h + 1 ) nodes ; 
def power(x, y):
    mod = 1000000007
    res = 1
    x = x % mod
    if (x == 0):
        return 0
    while (y > 0):
        if (y & 1):
            res = (res * x) % mod
        y = y >> 1
        x = (x * x) % mod
    return res


def CountBST(H):
    return power(2, H)


H = 2
print(CountBST(H))

# Expected output:
4
# check if any pair of consecutive 1 s can be separated by at most m 0 s by circular rotation of a binary string | function to check if any pair of consecutive 1 s can be separated by at most m 0 s by circular rotation of string s ; stores the indices of all 1 s ; store the number of pairs separated by at least m 0 s ; traverse the string ; store the current index ; traverse the array containing indices ; if the number of 0 s > m , then increment cnt by 1 ; increment cnt ; check if at least m '0' s lie between the first and last occurrence of '1 ; increment cnt ; if the value of cnt <= 1 , then rotation of string is possible ; otherwise ; 
def rotateString(n, m, s):
    v = []
    cnt = 0
    for i in range(n):
        if (s[i] == '1'):
            v.append(i)
    for i in range(1, len(v)):
        if ((v[i] - v[i - 1] - 1) > m):
            cnt += 1
    if (len(v) >= 2 and
       (n - (v[-1] - v[0]) - 1) > m):
        cnt += 1
    if (cnt <= 1):
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':

    S = "101001"
    M = 1
    N = len(S)

    rotateString(N, M, S)

# Expected output:
Yes
# number obtained by reducing sum of digits of 2 n into a single digit | function to find the number obtained by reducing sum of digits of 2 ^ n into a single digit ; stores answers for different values of n ; 
def findNumber(N):
    ans = [1, 2, 4, 8, 7, 5]
    return ans[N % 6]


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

# Expected output:
1
# check if two piles of coins can be emptied by repeatedly removing 2 coins from a pile and 1 coin from the other | function to check if two given piles can be emptied by repeatedly removing 2 coins from a pile and 1 coin from the other ; if maximum of a & b exceeds the twice of minimum of a & b ; not possible to empty the piles ; if sum of both the coins is divisible by 3 , then print yes ; otherwise , print " no " ; 
def canBeEmptied(A, B):
    if (max(A, B) > 2 * min(A, B)):
        print("No")
        return
    if ((A + B) % 3 == 0):
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    A = 1
    B = 2
    canBeEmptied(A, B)

# Expected output:
Yes
# maximum index a pointer can reach in n steps by avoiding a given index b | set 2 | function to find the maximum index the pointer can reach ; initialize two pointers ; stores number of steps ; stores sum of first n natural numbers ; increment i with j ; increment j with 1 ; increment count ; if i points to b ; break ; print the pointer index ; 
def maximumIndex(N, B):
    i, j = 0, 1
    cnt = 0
    sum = N * (N + 1) // 2
    flag = False
    while (cnt < N):
        i += j
        j += 1
        cnt += 1
        if (i == B):
            flag = True
            break
    if (not flag):
        print(sum)
    else:
        print(sum - 1)


if __name__ == '__main__':
    N, B = 4, 6
    maximumIndex(N, B)

# Expected output:
9
# generate longest possible array with product k such that each array element is divisible by its previous adjacent element | function to construct longest array with product k such that each element is divisible by its previous element ; stores the prime factors of k ; stores the power to which primefactor i is raised ; sort prime factors in descending order ; stores the final array ; multiply the last element by k ; print the constructed array ; 
def findLongestArray(K):
    primefactors = []
    K_temp = K
    i = 2
    while i * i <= K:
        count = 0
        while (K_temp % i == 0):
            K_temp //= i
            count += 1
        if (count > 0):
            primefactors .append([count, i])
        i += 1
    if (K_temp != 1):
        primefactors .append([1, K_temp])
    primefactors .sort()
    answer = [primefactors[0][0], primefactors[0][1]]
    answer[-1] *= K
    for i in range(primefactors[0][0]):
        answer[-1] //= primefactors[0][1]
    print("{", end="")
    for i in range(len(answer)):
        if (i == len(answer) - 1):
            print(answer[i], end="}")
        else:
            print(answer[i], end=", ")


if __name__ == "__main__":
    K = 4
    findLongestArray(K)

# Expected output:
{2, 2}
# minimum number of steps required to place all 1 s at a single index | function to print minimum steps required to shift all 1 s to a single index in a binary array ; size of array ; used to store cumulative sum ; initialize count ; traverse the array in forward direction ; steps needed to store all previous ones to ith index ; count number of 1 s present till i - th index ; initialize count ; traverse the array in backward direction ; steps needed to store all 1 s to the right of i at current index ; count number of 1 s present after i - th index ; print the number of steps required ; 
def minsteps(A):
    n = len(A)
    left, right, res = [0] * n, [0] * n, [0] * n
    count = A[0]
    for i in range(1, n):
        left[i] = left[i - 1] + count
        count += A[i]
    count = A[n - 1]
    for i in range(n - 2, -1, -1):
        right[i] = right[i + 1] + count
        count += A[i]
    for i in range(n):
        res[i] = left[i] + right[i]
        print(res[i], end=" ")
    print()


if __name__ == '__main__':
    A = [1, 0, 1, 0]
    minsteps(A)

# Expected output:
2 2 2 4
# minimize flips on k | function to find the minimum number k - length subarrays required to be flipped to make all array elements 1 ; stores whether an element can be flipped or not ; store the required number of flips ; traverse the array , a [ ] ; find the prefix sum for the indices i > 0 ; check if the current element is required to be flipped ; if subarray of size k is not possible , then print - 1 and return ; increment ans by 1 ; change the current state of the element ; decrement isflipped [ i + k ] ; if subarray of size k is not possible , then print - 1 and return ; increment ans by 1 ; change the current state of the element ; decrement isflipped [ i + k ] ; print the result ; 
def minimumOperations(A, K):
    isflipped = [0] * (len(A) + 1)
    ans = 0
    for i in range(len(A)):
        if (i > 0):
            isflipped[i] += isflipped[i - 1]
            isflipped[i] %= 2
        if (A[i] == 0 and not isflipped[i]):
            if ((len(A) - i + 1) <= K):
                print(-1)
                return
            ans += 1
            isflipped[i] += 1
            isflipped[i + K] -= 1
        elif (A[i] == 1 and isflipped[i]):
            if ((len(A) - i + 1) <= K):
                print(-1)
                return
            ans += 1
            isflipped[i] += 1
            isflipped[i + K] -= 1
    print(ans)


if __name__ == "__main__":
    arr = [0, 1, 0]
    K = 1
    minimumOperations(arr, K)

# Expected output:
2
# calculate sum of scores after n days based on given conditions | function to c sum of calculate sum of scores after n days ; store the required sum ; store the score on previous monday and current day respectively ; iterate over the range [ 1 , n ] ; if the current day is monday ; increment score of prev_monday by 1 ; update score of current day ; add score of current day and increment score for next day ; print the result ; 
def findScoreSum(n):
    total = 0
    prev_monday, curr_day = 0, 0
    for day in range(1, n + 1):
        if (day % 7 == 1):
            prev_monday += 1
            curr_day = prev_monday
        total += curr_day
        curr_day += 1
    print(total)


if __name__ == '__main__':
    N = 8
    findScoreSum(N)

# Expected output:
30
# calculate sum of scores after n days based on given conditions | function to calculate sum of scores after n days ; store the number of full weeks ; stores the remaining days in the last week ; store the sum of scores in the first f full weeks ; store the sum of scores in the last week ; print the result ; 
def findScoreSum(n):
    F = n // 7
    D = n % 7
    fullWeekScore = (49 + 7 * F) * F // 2
    lastNonFullWeekScore = (2 * F + D + 1) * D // 2
    print(fullWeekScore + lastNonFullWeekScore)


if __name__ == '__main__':
    N = 8
    findScoreSum(N)

# Expected output:
30
# largest number up to n whose modulus with x is equal to y modulo x | function to print the largest number upto n whose modulus with x is same as y % x ; stores the required number ; update num as the result ; return the resultant number ; 
def maximumNum(X, Y, N):
    num = 0
    if (N - N % X + Y <= N):
        num = N - N % X + Y
    else:
        num = N - N % X - (X - Y)
    return num


if __name__ == '__main__':
    X = 10
    Y = 5
    N = 15
    print(maximumNum(X, Y, N))

# Expected output:
15
# number of points lying inside a rectangle as well as a triangle | function to calculate area of a triangle ; return the resultant area ; function to check if a point lies inside a triangle or not ; calculate area of triangle abc ; calculate area of triangle formed by connecting b , c , point ; calculate area of triangle formed by connecting a , c , point ; calculate area of triangle formed by connecting a , b , point ; check if the sum of the areas of above three triangles the same as abc ; function to count the number of points lying inside a triangle & rectangle ; stores the coordinates of the vertices of the triangles ; stores the number of points lying inside the triangle and rectangle ; traverse the array of points ; stores whether the current point lies inside triangle1 or not ; stores whether the current point lies inside triangle2 or not ; stores whether the current point lies inside triangle3 or not ; stores whether the current point lies inside triangle4 or not ; stores whether the current point lies inside given triangle or not ; if current point lies inside given triangle as well as inside any of the four obtained triangles ; print the count of points ; 
def getArea(x1, y1, x2, y2, x3, y3):
    return abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2)


def isInside(triangle, point):
    A, B, C = triangle
    x, y = point
    ABC = getArea(A[0], A[1], B[0], B[1], C[0], C[1])
    BPC = getArea(x, y, B[0], B[1], C[0], C[1])
    APC = getArea(A[0], A[1], x, y, C[0], C[1])
    APB = getArea(A[0], A[1], B[0], B[1], x, y)
    return ABC == (APC + APB + BPC)


def countPoints(rectangle, triangle, points):
    triangle1 = rectangle[1:]
    triangle2 = rectangle[:3]
    triangle3 = rectangle[:2]
    triangle3 .append(rectangle[3])
    triangle4 = rectangle[-2:]
    triangle4 .append(rectangle[0])
    ans = 0
    for point in points:
        condOne = isInside(triangle1, point)
        condTwo = isInside(triangle2, point)
        condThree = isInside(triangle3, point)
        condFour = isInside(triangle4, point)
        condFive = isInside(triangle, point)
        if (condOne or condTwo or condThree or condFour) and condFive:
            ans += 1
    print(ans)


rectangle = [[6, 5], [2, 2], [2, 1], [5, 5]]
points = [[1, 1], [6, 1], [6, 6], [1, 6]]
triangle = [[4, 4], [0, 4], [0, -2]]
countPoints(points, triangle, rectangle)

# Expected output:
2
# generate an n | function to calculate gcd of two integers ; function to calculate gcd of a given array ; utility function to check for all the possible combinations ; if an n - length sequence is obtained ; if gcd of the sequence is k ; otherwise ; add an element from the first array ; recursively proceed further ; if combination satisfies the necessary condition ; remove the element from the combination ; add an element from the second array ; recursive proceed further ; if combination satisfies the necessary condition ; remove the element from the combination ; function to check all the possible combinations ; stores the subsequence ; if gcd is not equal to k for any combination ; given arrays ; 
def GCD(a, b):
    if not b:
        return a
    return GCD(b, a % b)


def GCDArr(a):
    ans = a[0]
    for i in a:
        ans = GCD(ans, i)
    return ans


def findSubseqUtil(a, b, ans, k, i):
    if len(ans) == len(a):
        if GCDArr(ans) == k:
            print(ans)
            return True
        else:
            return False
    ans .append(a[i])
    temp = findSubseqUtil(a, b, ans, k, i + 1)
    if temp:
        return True
    ans .pop()
    ans .append(b[i])
    temp = findSubseqUtil(a, b, ans, k, i + 1)
    if temp:
        return True
    ans .pop()


def findSubseq(A, B, K, i):
    ans = []
    findSubseqUtil(A, B, ans, K, i)
    if not ans:
        print(-1)


A = [5, 3, 6, 2, 9]
B = [21, 7, 14, 12, 28]
K = 3
ans = findSubseq(A, B, K, 0)

# Expected output:
[21, 3, 6, 12, 9]
# minimize count of swaps of adjacent elements required to make an array increasing | function to count minimum number of operations required to obtain an increasing array from given array a [ ] ; store the required result ; traverse the array a [ ] ; if the current element is not in its correct position ; check if it is present at index i - 1 ; check if it is present at index i - 2 ; otherwise , print - 1 ( since a [ i ] can not be swapped more than twice ) ; print the result ; 
def minimumOperations(A, n):
    cnt = 0
    for i in range(n - 1, -1, -1):
        if (A[i] != (i + 1)):
            if (((i - 1) >= 0) and A[i - 1] == (i + 1)):
                cnt += 1
                A[i], A[i - 1] = A[i - 1], A[i]
            elif (((i - 2) >= 0) and A[i - 2] == (i + 1)):
                cnt += 2
                A[i - 2] = A[i - 1]
                A[i - 1] = A[i]
                A[i] = i + 1
            else:
                print(-1)
                return
    print(cnt)


if __name__ == "__main__":
    A = [7, 3, 2, 1, 4]
    n = len(A)
    minimumOperations(A, n)

# Expected output:
-1
# count occurrences of an element in a matrix of size n * n generated such that each element is equal to product of its indices | set | function to count the occurrences of x in the generated square matrix ; store the required result ; iterate over the range [ 1 , n ] ; check if x is a multiple of i or not ; check if the other multiple exists in the range or not ; print the result ; 
def countOccurrences(n, x):
    count = 0
    for i in range(1, n + 1):
        if (x % i == 0):
            if (x // i <= n):
                count += 1
    print(count)


if __name__ == "__main__":
    N = 7
    X = 12
    countOccurrences(N, X)

# Expected output:
4
# reduce all array elements to zero by performing given operations thrice | function to reduce all array elements to zero ; if size of array is 1 ; first operation ; 2 nd operation ; 3 rd operation ; otherwise ; 1 st operation ; 2 nd operation ; 3 rd operation ; 
def ConvertArray(arr, N):
    if (N == 1):
        print("Operation 1 :", 1, 1)
        print("Added elements:", -1 * arr[0])
        print("", end="")
        print("Operation 2 :", 1, 1)
        print("Added elements:", 1 * arr[0])
        print("", end="")
        print("Operation 3 :", 1, 1)
        print("Added elements:", -1 * arr[0])
        print("", end="")
    else:
        print("Operation 1 :", 1, N)
        print("Added elements:", end=" ")
        for i in range(N):
            print(-1 * arr[i] * N, end=" ")
        print("")
        print("Operation 2 :", 1, N - 1)
        print("Added elements:", end=" ")
        for i in range(N - 1):
            print(arr[i] * (N - 1), end=" ")
        print("")
        print("Operation 3 : ", N, N)
        print("Added elements:", end=" ")
        print(arr[N - 1] * (N - 1))


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

# Expected output:
Operation 1 : 1 4
Added elements: -4 -12 -8 -16 
Operation 2 : 1 3
Added elements: 3 9 6 
Operation 3 :  4 4
Added elements: 12