giulio98/xlcost-single-prompt huggingface dataset:
# maximum value of arr [ i ] + arr [ j ] + i â €“ j for any pair of an array | function to find the maximum value of arr [ i ] + arr [ j ] + i - j over all pairs ; stores the required result ; traverse over all the pairs ( i , j ) ; calculate the value of the expression and update the overall maximum value ; print the result ; 
def maximumValue(arr, n):
    ans = 0
    for i in range(n):
        for j in range(i + 1, n):
            ans = max(ans, arr[i] + arr[j] + i - j)
    print(ans)


arr = [1, 9, 3, 6, 5]
N = len(arr)
maximumValue(arr, N)

# Expected output:
13
# maximum value of arr [ i ] + arr [ j ] + i â €“ j for any pair of an array | function to find the maximum value of ( arr [ i ] + arr [ j ] + i - j ) possible for a pair in the array ; stores the maximum value of ( arr [ i ] + i ) ; stores the required result ; traverse the array arr [ ] ; calculate for current pair and update maximum value ; update maxvalue if ( arr [ i ] + i ) is greater than maxvalue ; print the result ; 
def maximumValue(arr, n):
    maxvalue = arr[0]
    result = 0
    for i in range(1, n):
        result = max(result, maxvalue + arr[i] - i)
        maxvalue = max(maxvalue, arr[i] + i)
    print(result)


arr = [1, 9, 3, 6, 5]
N = len(arr)
maximumValue(arr, N)

# Expected output:
13
# minimum sum of absolute differences of pairs in a triplet from three arrays | lower_bound function ; function to find the value closest to k in the array a [ ] ; initialize close value as the end element ; find lower bound of the array ; if lower_bound is found ; if lower_bound is not the first array element ; if * ( it - 1 ) is closer to k ; return closest value of k ; function to find the minimum sum of abs ( arr [ i ] - brr [ j ] ) and abs ( brr [ j ] - crr [ k ] ) ; sort the vectors arr and crr ; initialize minimum as let_max ; traverse the array brr [ ] ; stores the element closest to val from the array arr [ ] ; stores the element closest to val from the array crr [ ] ; if sum of differences is minimum ; update the minimum ; print the minimum absolute difference possible ; 
def lower_bound(arr, key):
    low = 0
    high = len(arr) - 1
    while (low < high):
        mid = low + (high - low) // 2
        if (arr[mid] >= key):
            high = mid
        else:
            low = mid + 1
    return low


def closestValue(A, k):
    close = A[-1]
    it = lower_bound(A, k)
    if (it != len(A)):
        close = A[it]
        if (it != 0):
            if ((k - A[it - 1]) < (close - k)):
                close = A[it - 1]
    return close


def minPossible(arr, brr, crr):
    arr .sort()
    crr .sort()
    minimum = 10 ** 9
    for val in brr:
        arr_close = closestValue(arr, val)
        crr_close = closestValue(crr, val)
        if (abs(val - arr_close) + abs(val - crr_close) < minimum):
            minimum = abs(val - arr_close) + abs(val - crr_close)
    print(minimum)


a = [1, 8, 5]
b = [2, 9]
c = [5, 4]
minPossible(a, b, c)

# Expected output:
3
# minimize swaps required to make the first and last elements the largest and smallest elements in the array respectively | function to find the minimum number of swaps required to make the first and the last elements the largest and smallest element in the array ; stores the count of swaps ; stores the maximum element ; stores the minimum element ; if the array contains a single distinct element ; stores the indices of the maximum and minimum elements ; if the first index of the maximum element is found ; if current index has the minimum element ; update the count of operations to place largest element at the first ; update the count of operations to place largest element at the last ; if smallest element is present before the largest element initially ; 
def minimum_swaps(arr, n):
    count = 0
    max_el = max(arr)
    min_el = min(arr)
    if (min_el == max_el):
        return 0
    index_max = -1
    index_min = -1
    for i in range(n):
        if (arr[i] == max_el and index_max == -1):
            index_max = i
        if (arr[i] == min_el):
            index_min = i
    count += index_max
    count += (n - 1 - index_min)
    if (index_min < index_max):
        count -= 1
    return count


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

# Expected output:
4
# maximum difference between node and its ancestor in a directed acyclic graph ( dag ) | python3 program for the above approach ; function to perform dfs traversal on the given graph ; update the value of ans ; update the currentmin and currentmax ; traverse the adjacency list of the node src ; recursively call for the child node ; function to calculate maximum absolute difference between a node and its ancestor ; stores the adjacency list of graph ; create adjacency list ; add a directed edge ; perform dfs traversal ; print the maximum absolute difference ; 

ans = 0


def DFS(src, Adj, arr, currentMin, currentMax):
    global ans
    ans = max(ans,
              max(abs(currentMax - arr[src - 1]),
                  abs(currentMin - arr[src - 1])))
    currentMin = min(currentMin, arr[src - 1])
    currentMax = min(currentMax, arr[src - 1])
    for child in Adj[src]:
        DFS(child, Adj, arr, currentMin, currentMax)


def getMaximumDifference(Edges, arr, N, M):
    global ans
    Adj = [[]for i in range(N + 1)]
    for i in range(M):
        u = Edges[i][0]
        v = Edges[i][1]
        Adj[u].append(v)
    DFS(1, Adj, arr, arr[0], arr[0])
    print(ans)


if __name__ == '__main__':
    N = 5
    M = 4
    Edges = [[1, 2], [2, 3], [4, 5], [1, 3]]
    arr = [13, 8, 3, 15, 18]
    getMaximumDifference(Edges, arr, N, M)

# Expected output:
10
# minimum cost path from source node to destination node via k intermediate nodes | function to find the minimum cost path from the source vertex to destination vertex via k intermediate vertices ; initialize the adjacency list ; generate the adjacency list ; initialize the minimum priority queue ; stores the minimum cost to travel between vertices via k intermediate nodes ; push the starting vertex , cost to reach and the number of remaining vertices ; pop the top element of the stack ; if destination is reached ; return the cost ; if all stops are exhausted ; find the neighbour with minimum cost ; pruning ; update cost ; update priority queue ; if no path exists ; 
def leastWeightedSumPath(n, edges, src, dst, K):
    graph = [[]for i in range(3)]
    for edge in edges:
        graph[edge[0]].append([edge[1], edge[2]])
    pq = []
    costs = [[10 ** 9 for i in range(K + 2)]for i in range(n)]
    costs[src][K + 1] = 0
    pq .append([0, src, K + 1])
    pq = sorted(pq)[::-1]
    while (len(pq) > 0):
        top = pq[-1]
        del pq[-1]
        if (top[1] == dst):
            return top[0]
        if (top[2] == 0):
            continue
        for neighbor in graph[top[1]]:
            if (costs[neighbor[0]][top[2] - 1] < neighbor[1] + top[0]):
                continue
            costs[neighbor[0]][top[2] - 1] = neighbor[1] + top[0]
            pq .append([neighbor[1] + top[0], neighbor[0], top[2] - 1])
        pq = sorted(pq)[::-1]
    return -1


if __name__ == '__main__':
    n, src, dst, k = 3, 0, 2, 1
    edges = [[0, 1, 100], [1, 2, 100], [0, 2, 500]]
    print(leastWeightedSumPath(n, edges, src, dst, k))

# Expected output:
200
# check if a given string is a comment or not | function to check if the given string is a comment or not ; if two continuous slashes preceeds the comment ; 
def isComment(line):
    if (line[0] == '/' and line[1] == '/' and line[2] != '/'):
        print("It is a single-line comment")
        return
    if (line[len(line) - 2] == '*' and line[len(line) - 1]
            == '/' and line[0] == '/' and line[1] == '*'):
        print("It is a multi-line comment")
        return
    print("It is not a comment")


if __name__ == '__main__':
    line = "GeeksForGeeks GeeksForGeeks"
    isComment(line)

# Expected output:
It is not a comment
# print digits for each array element that does not divide any digit of that element | function to find digits for each array element that doesn 't divide any digit of the that element ; traverse the array arr [ ] ; iterate over the range [ 2 , 9 ] ; stores if there exists any digit in arr [ i ] which is divisible by j ; if any digit of the number is divisible by j ; if the digit j doesn 't  divide any digit of arr[i] ; 
def indivisibleDigits(arr, N):
    for i in range(N):
        num = 0
        print(arr[i], end=' ')
        for j in range(2, 10):
            temp = arr[i]
            flag = True
            while (temp > 0):
                if ((temp % 10) != 0 and (temp % 10) % j == 0):
                    flag = False
                    break
                temp //= 10
            if (flag):
                print(j, end=' ')
        print()


arr = [4162, 1152, 99842]
N = len(arr)
indivisibleDigits(arr, N)

# Expected output:
4162 5 7 8 9 
1152 3 4 6 7 8 9 
99842 5 6 7
# length of longest non | function to find the longest non - decreasing subsequence with difference between adjacent elements exactly equal to 1 ; base case ; sort the array in ascending order ; stores the maximum length ; traverse the array ; if difference between current pair of adjacent elements is 1 or 0 ; extend the current sequence update len and max_len ; otherwise , start a new subsequence ; print the maximum length ; 
def longestSequence(arr, N):
    if (N == 0):
        print(0)
        return
    arr .sort()
    maxLen = 1
    len = 1
    for i in range(1, N):
        if (arr[i] == arr[i - 1] or arr[i] == arr[i - 1] + 1):
            len += 1
            maxLen = max(maxLen, len)
        else:
            len = 1
    print(maxLen)


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

# Expected output:
3
# queries to find the minimum array sum possible by removing elements from either end | function to find the minimum sum for each query after removing elements from either ends ; traverse the query array ; traverse the array from the front ; if element equals val , then break out of loop ; traverse the array from rear ; if element equals val , break ; prthe minimum of the two as the answer ; 
def minSum(arr, N, Q, M):
    for i in range(M):
        val = Q[i]
        front, rear = 0, 0
        for j in range(N):
            front += arr[j]
            if (arr[j] == val):
                break
        for j in range(N - 1, -1, -1):
            rear += arr[j]
            if (arr[j] == val):
                break
        print(min(front, rear), end=" ")


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

# Expected output:
17 11
# frequency of lexicographically kth smallest character in the a string | function to find the frequency of the lexicographically kth smallest character ; convert the string to array of characters ; sort the array in ascending order ; store the kth character ; store the frequency of the k - th character ; count the frequency of the k - th character ; prthe count ; 
def KthCharacter(S, N, K):
    strarray = [char for char in S]
    strarray .sort()
    ch = strarray[K - 1]
    count = 0
    for c in strarray:
        if (c == ch):
            count += 1
    print(count)


if __name__ == '__main__':
    S = "geeksforgeeks"
    N = len(S)
    K = 3
    KthCharacter(S, N, K)

# Expected output:
4
# longest substring with no pair of adjacent characters are adjacent english alphabets | function to find the longest substring satisfying the given condition ; stores all temporary substrings ; stores the longest substring ; stores the length of the subt ; stores the first character of s ; traverse the string ; if the absolute difference is 1 ; update the length of subt ; update the longest substring ; otherwise , stores the current character ; again checking for longest substring and update accordingly ; print the longest substring ; 
def findSubstring(S):
    T = ""
    ans = ""
    l = 0
    T += S[0]
    for i in range(1, len(S)):
        if (abs(ord(S[i]) - ord(S[i - 1])) == 1):
            l = len(T)
            if (l > len(ans)):
                ans = T
            T = ""
            T += S[i]
        else:
            T += S[i]
    l = len(T)
    if (l > len(ans)):
        ans = T
    print(ans)


if __name__ == '__main__':
    S = "aabdml"
    findSubstring(S)

# Expected output:
bdm
# minimize length of an array by removing similar subarrays from both ends | function to minimize length of the array by removing similar subarrays from both ends of the array ; initialize two pointers ; stores the current integer ; check if the elements at both ends are same or not ; move the front pointer ; move the rear pointer ; print the minimized length of the array ; 
def findMinLength(arr, N):
    front = 0
    back = N - 1
    while (front < back):
        x = arr[front]
        if arr[front] != arr[back]:
            break
        while (arr[front] == x and front <= back):
            front += 1
        while (arr[back] == x and front <= back):
            back -= 1
    print(back - front + 1)


arr = [1, 1, 2, 3, 3, 1, 2, 2, 1]
N = len(arr)
findMinLength(arr, N)

# Expected output:
3
# queries to find the maximum and minimum array elements excluding elements from a given range | function to find the maximum and minimum array elements up to the i - th index ; traverse the array ; compare current value with maximum and minimum values up to previous index ; function to find the maximum and minimum array elements from i - th index ; traverse the array in reverse ; compare current value with maximum and minimum values in the next index ; function to find the maximum and minimum array elements for each query ; if no index remains after excluding the elements in a given range ; find maximum and minimum from from the range [ r + 1 , n - 1 ] ; find maximum and minimum from from the range [ 0 , n - 1 ] ; find maximum and minimum values from the ranges [ 0 , l - 1 ] and [ r + 1 , n - 1 ] ; print maximum and minimum value ; function to perform queries to find the minimum and maximum array elements excluding elements from a given range ; size of the array ; size of query array ; prefix [ i ] [ 0 ] : stores the maximum prefix [ i ] [ 1 ] : stores the minimum value ; suffix [ i ] [ 0 ] : stores the maximum suffix [ i ] [ 1 ] : stores the minimum value ; function calls to store maximum and minimum values for respective ranges ; 
def prefixArr(arr, prefix, N):
    for i in range(N):
        if (i == 0):
            prefix[i][0] = arr[i]
            prefix[i][1] = arr[i]
        else:
            prefix[i][0] = max(prefix[i - 1][0], arr[i])
            prefix[i][1] = min(prefix[i - 1][1], arr[i])
    return prefix


def suffixArr(arr, suffix, N):
    for i in range(N - 1, -1, -1):
        if (i == N - 1):
            suffix[i][0] = arr[i]
            suffix[i][1] = arr[i]
        else:
            suffix[i][0] = max(suffix[i + 1][0], arr[i])
            suffix[i][1] = min(suffix[i + 1][1], arr[i])
    return suffix


def maxAndmin(prefix, suffix, N, L, R):
    maximum, minimum = 0, 0
    if (L == 0 and R == N - 1):
        print("No maximum and minimum value")
        return
    elif (L == 0):
        maximum = suffix[R + 1][0]
        minimum = suffix[R + 1][1]
    elif (R == N - 1):
        maximum = prefix[L - 1][0]
        minimum = prefix[R - 1][1]
    else:
        maximum = max(prefix[L - 1][0], suffix[R + 1][0])
        minimum = min(prefix[L - 1][1], suffix[R + 1][1])
    print(maximum, minimum)


def MinMaxQueries(a, queries):
    N = len(arr)
    q = len(queries)
    prefix = [[0 for i in range(2)]for i in range(N)]
    suffix = [[0 for i in range(2)]for i in range(N)]
    prefix = prefixArr(arr, prefix, N)
    suffix = suffixArr(arr, suffix, N)
    for i in range(q):
        L = queries[i][0]
        R = queries[i][1]
        maxAndmin(prefix, suffix, N, L, R)


if __name__ == '__main__':
    arr = [2, 3, 1, 8, 3, 5, 7, 4]
    queries = [[4, 6], [0, 4], [3, 7], [2, 5]]
    MinMaxQueries(arr, queries)

# Expected output:
8 1
7 4
3 1
7 2
# digits whose alphabetic representations are jumbled in a given string | function to convert the jumbled into digits ; strings of digits 0 - 9 ; initialize vector ; initialize answer ; size of the string ; traverse the string ; update the elements of the vector ; print the digits into their original format ; return answer ; 
def finddigits(s):
    num = [
        "zero",
        "one",
        "two",
        "three",
        "four",
        "five",
        "six",
        "seven",
        "eight",
        "nine"]
    arr = [0] * (10)
    ans = ""
    n = len(s)
    for i in range(n):
        if (s[i] == 'z'):
            arr[0] += 1
        if (s[i] == 'w'):
            arr[2] += 1
        if (s[i] == 'g'):
            arr[8] += 1
        if (s[i] == 'x'):
            arr[6] += 1
        if (s[i] == 'v'):
            arr[5] += 1
        if (s[i] == 'o'):
            arr[1] += 1
        if (s[i] == 's'):
            arr[7] += 1
        if (s[i] == 'f'):
            arr[4] += 1
        if (s[i] == 'h'):
            arr[3] += 1
        if (s[i] == 'i'):
            arr[9] += 1
    arr[7] -= arr[6]
    arr[5] -= arr[7]
    arr[4] -= arr[5]
    arr[1] -= (arr[2] + arr[4] + arr[0])
    arr[3] -= arr[8]
    arr[9] -= (arr[5] + arr[6] + arr[8])
    for i in range(10):
        for j in range(arr[i]):
            ans += chr((i) + ord('0'))
    return ans


if __name__ == '__main__':
    s = "owoftnuoer"
    print(finddigits(s))

# Expected output:
124
# check if two binary strings can be made equal by swapping pairs of unequal characters | function to check if a string s1 can be converted into s2 ; count of '0' in strings in s1 and s2 ; iterate both the strings and count the number of occurrences of ; count is not equal ; iterating over both the arrays and count the number of occurrences of '0 ; if the count of occurrences of '0' in s2 exceeds that in s1 ; 
def check(s1, s2):
    s1_0 = 0
    s2_0 = 0
    for i in range(len(s1)):
        if (s1[i] == '0'):
            s1_0 += 1
        if (s2[i] == '0'):
            s2_0 += 1
    if (s1_0 != s2_0):
        print("NO")
        return
    else:
        Count1 = 0
        Count2 = 0

    for i in range(len(s1)):
        if (s1[i] == '0'):
            Count1 += 1
        if (s2[i] == '0'):
            Count2 += 1
        if (Count1 < Count2):
            print("NO")
            return
    print("YES")


if __name__ == "__main__":
    s1 = "100111"
    s2 = "111010"
    check(s1, s2)
    s1 = "110100"
    s2 = "010101"
    check(s1, s2)

# Expected output:
YES
NO
# check if two binary strings can be made equal by swapping 1 s occurring before 0 s | function to check if it is possible to make two binary strings equal by given operations ; stores count of 1 ' s ▁ and ▁ 0' s of the string str1 ; stores count of 1 ' s ▁ and ▁ 0' s of the string str2 ; stores current count of 1 's  presenty in the string str1 ; count the number of 1 ' s ▁ and ▁ 0' s present in the strings str1 and str2 ; if the number of 1 ' s ▁ and ▁ 0' s are not same of the strings str1 and str2 then prnot possible ; traversing through the strings str1 and str2 ; if the str1 character not equals to str2 character ; swaps 0 with 1 of the string str1 ; breaks the loop as the count of 1 's is zero. hence, no swaps possible ; swaps 1 with 0 in the string str1 ; prnot possible ; 
def isBinaryStringsEqual(list1, list2):
    str1 = list(list1)
    str2 = list(list2)
    str1Zeros = 0
    str1Ones = 0
    str2Zeros = 0
    str2Ones = 0
    flag = 0
    curStr1Ones = 0
    for i in range(len(str1)):
        if (str1[i] == '1'):
            str1Ones += 1
        elif (str1[i] == '0'):
            str1Zeros += 1
        if (str2[i] == '1'):
            str2Ones += 1
        elif (str2[i] == '0'):
            str2Zeros += 1
    if (str1Zeros != str2Zeros and str1Ones != str2Ones):
        print("Not Possible")
    else:
        for i in range(len(str1)):
            if (str1[i] != str2[i]):
                if (str1[i] == '0' and curStr1Ones > 0):
                    str1[i] = '1'
                    curStr1Ones -= 1
                if (str1[i] == '0' and curStr1Ones == 0):
                    flag += 1
                    break
                if (str1[i] == '1' and str2[i] == '0'):
                    str1[i] = '0'
                    curStr1Ones += 1
        if (flag == 0):
            print("Possible")
        else:
            print("Not Possible")


str1 = "0110"
str2 = "0011"
isBinaryStringsEqual(str1, str2)

# Expected output:
Possible
# count array elements whose all distinct digits appear in k | function to the count of array elements whose distinct digits are a subst of the digits of k ; stores distinct digits of k ; iterate over all the digits of k ; insert current digit into st ; update k ; stores the count of array elements whose distinct digits are a subst of the digits of k ; traverse the array , arr [ ] ; stores current element ; check if all the digits of arr [ i ] are present in k or not ; iterate over all the digits of arr [ i ] ; stores current digit ; if digit not present in the st ; update flag ; update no ; if all the digits of arr [ i ] present in st ; update count ; 
def noOfValidKbers(K, arr):
    st = {}
    while (K != 0):
        if (K % 10 in st):
            st[K % 10] = 1
        else:
            st[K % 10] = st .get(K % 10, 0) + 1
        K = K // 10
    count = 0
    for i in range(len(arr)):
        no = arr[i]
        flag = True
        while (no != 0):
            digit = no % 10
            if (digit not in st):
                flag = False
                break
            no = no // 10
        if (flag):
            count += 1
    return count


if __name__ == '__main__':
    K = 12
    arr = [1, 12, 1222, 13, 2]
    print(noOfValidKbers(K, arr))

# Expected output:
4