giulio98/xlcost-single-prompt huggingface dataset:
# check whether we can sort two arrays by swapping a [ i ] and b [ i ] | function to check whether both the array can be sorted in ( strictly increasing ) ascending order ; traverse through the array and find out the min and max variable at each position make one array of min variables and another of maximum variable ; maximum and minimum variable ; assign min value to b [ i ] and max value to a [ i ] ; now check whether the array is sorted or not ; 
def IsSorted(A, B, n):
    for i in range(n):
        x = max(A[i], B[i])
        y = min(A[i], B[i])
        A[i] = x
        B[i] = y
    for i in range(1, n):
        if (A[i] <= A[i - 1] or B[i] <= B[i - 1]):
            return False
    return True


if __name__ == "__main__":
    A = [1, 4, 3, 5, 7]
    B = [2, 2, 5, 8, 9]
    n = len(A)
    if (IsSorted(A, B, n)):
        print(True)
    else:
        print(False)

# Expected output:
True
# find the largest possible k | function to find the largest possible k - multiple set ; sort the given array ; to store k - multiple set ; traverse through the whole array ; check if x / k is already present or not ; print the k - multiple set ; 
def K_multiple(a, n, k):
    a .sort()
    s = set()
    for i in range(n):
        if ((a[i] % k == 0 and a[i] // k not in s) or a[i] % k != 0):
            s .add(a[i])
    for i in s:
        print(i, end=" ")


if __name__ == "__main__":
    a = [2, 3, 4, 5, 6, 10]
    k = 2
    n = len(a)
    K_multiple(a, n, k)

# Expected output:
2 3 5
# maximum water that can be stored between two buildings | return the maximum water that can be stored ; to store the maximum water so far ; both the pointers are pointing at the first and the last buildings respectively ; while the water can be stored between the currently chosen buildings ; update maximum water so far and increment i ; update maximum water so far and decrement j ; any of the pointers can be updated ( or both ) ; 
def maxWater(height, n):
    maximum = 0
    i = 0
    j = n - 1
    while (i < j):
        if (height[i] < height[j]):
            maximum = max(maximum, (j - i - 1) * height[i])
            i += 1
        elif (height[j] < height[i]):
            maximum = max(maximum, (j - i - 1) * height[j])
            j -= 1
        else:
            maximum = max(maximum, (j - i - 1) * height[i])
            i += 1
            j -= 1
    return maximum


height = [2, 1, 3, 4, 6, 5]
n = len(height)
print(maxWater(height, n))

# Expected output:
8
# count the maximum number of elements that can be selected from the array | function to return the maximum count of selection possible from the given array following the given process ; initialize result ; sorting the array ; initialize the select variable ; loop through array ; if selection is possible ; increment result ; increment selection variable ; 
def maxSelectionCount(a, n):
    res = 0
    a.sort()
    select = 1
    for i in range(n):
        if (a[i] >= select):
            res += 1
            select += 1

    return res


arr = [4, 2, 1, 3, 5, 1, 4]
N = len(arr)
print(maxSelectionCount(arr, N))

# Expected output:
5
# print combinations of distinct numbers which add up to give sum n | arr [ ] to store all the distinct elements index - next location in array num - given number reducednum - reduced number ; set to store all the distinct elements ; base condition ; iterate over all the elements and store it into the set ; calculate the sum of all the elements of the set ; compare whether the sum is equal to n or not , if it is equal to n print the numbers ; find previous number stored in the array ; store all the numbers recursively into the arr [ ] ; function to find all the distinct combinations of n ; 
def findCombinationsUtil(arr, index, n, red_num):
    s = set()
    sum = 0
    if (red_num < 0):
        return
    if (red_num == 0):
        for i in range(index):
            s .add(arr[i])
        for itr in s:
            sum = sum + (itr)
        if (sum == n):
            for i in s:
                print(i, end=" ")
            print("",  end="")
            return
    if (index == 0):
        prev = 1
    else:
        prev = arr[index - 1]
    for k in range(prev, n + 1, 1):
        arr[index] = k
        findCombinationsUtil(arr, index + 1, n, red_num - k)


def findCombinations(n):
    a = [0 for i in range(n + 1)]
    findCombinationsUtil(a, 0, n, n)


if __name__ == '__main__':
    n = 7
    findCombinations(n)

# Expected output:
1 2 4 1 6 2 5 3 4 7
# check whether an array can be made strictly increasing by modifying atmost one element | function that returns true if arr [ ] can be made strictly increasing after modifying at most one element ; to store the number of modifications required to make the array strictly increasing ; check whether the first element needs to be modify or not ; loop from 2 nd element to the 2 nd last element ; check whether arr [ i ] needs to be modified ; modifying arr [ i ] ; check if arr [ i ] is equal to any of arr [ i - 1 ] or arr [ i + 1 ] ; check whether the last element needs to be modify or not ; if more than 1 modification is required ; 
def check(arr, n):
    modify = 0
    if (arr[0] > arr[1]):
        arr[0] = arr[1] // 2
        modify += 1
    for i in range(1, n - 1):
        if ((arr[i - 1] < arr[i] and arr[i + 1] < arr[i])
                or (arr[i - 1] > arr[i] and arr[i + 1] > arr[i])):
            arr[i] = (arr[i - 1] + arr[i + 1]) // 2
            if (arr[i] == arr[i - 1] or arr[i] == arr[i + 1]):
                return False
            modify += 1
    if (arr[n - 1] < arr[n - 2]):
        modify += 1
    if (modify > 1):
        return False
    return True


if __name__ == "__main__":
    arr = [2, 4, 8, 6, 9, 12]
    n = len(arr)
    if (check(arr, n)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# check if the given matrix is increasing row and column wise | python3 implementation of the approach ; function that returns true if the matrix is strictly increasing ; check if the matrix is strictly increasing ; out of bound condition ; out of bound condition ; 

N, M = 2, 2


def isMatrixInc(a):
    for i in range(N):
        for j in range(M):
            if (i - 1 >= 0):
                if (a[i][j] <= a[i - 1][j]):
                    return False
            if (j - 1 >= 0):
                if (a[i][j] <= a[i][j - 1]):
                    return False
    return True


if __name__ == "__main__":
    a = [[2, 10], [11, 20]]
    if (isMatrixInc(a)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# sort an array according to count of set bits | set 2 | function to sort the array according to the number of set bits in elements ; counting no of setbits in arr [ i ] ; the count is subtracted from 32 because the result needs to be in descending order ; printing the numbers in descending order of set bit count ; 
def sortArr(arr, n):
    mp = []
    for i in range(n):
        count = 0
        k = arr[i]
        while (k):
            k = k & k - 1
            count += 1
        mp .append((32 - count, arr[i]))
    mp .sort(key=lambda x: x[0])
    for it in mp:
        print(it[1], end=" ")


if __name__ == "__main__":
    arr = [5, 2, 3, 9, 4, 6, 7, 15, 32]
    n = len(arr)
    sortArr(arr, n)

# Expected output:
15 7 5 3 9 6 2 4 32
# smallest multiple of n formed using the given set of digits | function to return the required number ; initialize both vectors with their initial values ; sort the vector of digits ; initialize the queue ; if modulus value is not present in the queue ; compute digits modulus given number and update the queue and vectors ; while queue is not empty ; remove the first element of the queue ; compute new modulus values by using old queue values and each digit of the set ; if value is not present in the queue ; if required condition can 't be satisfied  ; initialize new vector ; constructing the solution by backtracking ; reverse the vector ; return the required number ; 
def findSmallestNumber(arr, n):
    dp = [float('inf')] * n
    result = [(-1, 0)] * n
    arr .sort()
    q = []
    for i in arr:
        if i != 0:
            if dp[i % n] > 10 ** 9:
                q .append(i % n)
                dp[i % n] = 1
                result[i % n] = -1, i
    while len(q) > 0:
        u = q .pop(0)
        for i in arr:
            v = (u * 10 + i) % n
            if dp[u] + 1 < dp[v]:
                dp[v] = dp[u] + 1
                q .append(v)
                result[v] = u, i
    if dp[0] > 10 ** 9:
        return -1
    else:
        ans = []
        u = 0
        while u != -1:
            ans .append(result[u][1])
            u = result[u][0]
        ans = ans[::-1]
        for i in ans:
            return i


if __name__ == "__main__":
    arr = [5, 2, 3]
    n = 12
    print(findSmallestNumber(arr, n))

# Expected output:
2
# count number of subsets whose median is also present in the same subset | python 3 implementation of the approach ; function to return the factorial of a number ; function to return the value of ncr ; function to return ' a ' raised to the power n with complexity o ( log ( n ) ) ; function to return the number of sub - sets whose median is also present in the set ; number of odd length sub - sets ; sort the array ; checking each element for leftmost middle element while they are equal ; calculate the number of elements in right of rightmost middle element ; calculate the number of elements in left of leftmost middle element ; add selected even length subsets to the answer ; 

mod = 1000000007


def fact(n):
    res = 1
    for i in range(2, n + 1):
        res = res * i
    return res


def nCr(n, r):
    return int(fact(n) / (fact(r) * fact(n - r)))


def powmod(a, n):
    if (n == 0):
        return 1
    pt = powmod(a, int(n / 2))
    pt = (pt * pt) % mod
    if (n % 2):
        return (pt * a) % mod
    else:
        return pt


def CountSubset(arr, n):
    ans = powmod(2, n - 1)
    arr .sort(reverse=False)
    for i in range(n):
        j = i + 1
        while (j < n and arr[j] == arr[i]):
            r = n - 1 - j
            l = i
            ans = (ans + nCr(l + r, l)) % mod
            j += 1
    return ans


if __name__ == '__main__':
    arr = [2, 3, 2]
    n = len(arr)
    print(CountSubset(arr, n))

# Expected output:
5
# reorder the position of the words in alphabetical order | function to print the ordering of words ; creating list of words and assigning them index numbers ; sort the list of words lexicographically ; print the ordering ; 
def reArrange(words, n):
    mp = {}
    for i in range(n):
        mp[words[i]] = i + 1
    words .sort()
    for i in range(n):
        print(mp[words[i]], end=" ")


words = ["live", "place", "travel", "word", "sky"]
n = len(words)
reArrange(words, n)

# Expected output:
1 2 5 3 4
# sum of elements in 1 st array such that number of elements less than or equal to them in 2 nd array is maximum | python 3 implementation of the approach ; function to return the required sum ; creating hash array initially filled with zero ; calculate the frequency of elements of arr2 [ ] ; running sum of hash array such that hash [ i ] will give count of elements less than or equal to i in arr2 [ ] ; to store the maximum value of the number of elements in arr2 [ ] which are smaller than or equal to some element of arr1 [ ] ; calculate the sum of elements from arr1 [ ] corresponding to maximum frequency ; return the required sum ; 

MAX = 100000


def findSumofEle(arr1, m, arr2, n):
    hash = [0 for i in range(MAX)]
    for i in range(n):
        hash[arr2[i]] += 1
    for i in range(1, MAX, 1):
        hash[i] = hash[i] + hash[i - 1]
    maximumFreq = 0
    for i in range(m):
        maximumFreq = max(maximumFreq, hash[arr1[i]])
    sumOfElements = 0
    for i in range(m):
        if (maximumFreq == hash[arr1[i]]):
            sumOfElements += arr1[i]
    return sumOfElements


if __name__ == '__main__':
    arr1 = [2, 5, 6, 8]
    arr2 = [4, 10]
    m = len(arr1)
    n = len(arr2)
    print(findSumofEle(arr1, m, arr2, n))

# Expected output:
19
# print 2 | function to print the coordinates along with their frequency in ascending order ; map to store the pairs and their frequencies ; store the coordinates along with their frequencies ; 
def Print(x, y, n):
    m = dict()
    for i in range(n):
        m[(x[i], y[i])] = m .get((x[i], y[i]), 0) + 1
    e = sorted(m)
    for i in e:
        print(i[0], i[1], m[i])


x = [1, 2, 1, 1, 1]
y = [1, 1, 3, 1, 3]
n = len(x)
Print(x, y, n)

# Expected output:
1 1 2
1 3 2
2 1 1
# split the array elements into strictly increasing and decreasing sequence | function to print both the arrays ; store both arrays ; used for hashing ; iterate for every element ; increase the count ; if first occurrence ; if second occurrence ; if occurs more than 2 times ; sort in increasing order ; print the increasing array ; sort in reverse order ; print the decreasing array ; 
def PrintBothArrays(a, n):
    v1, v2 = [], []
    mpp = dict .fromkeys(a, 0)
    for i in range(n):
        mpp[a[i]] += 1
        if (mpp[a[i]] == 1):
            v1 .append(a[i])
        elif (mpp[a[i]] == 2):
            v2 .append(a[i])
        else:
            print("Not possible")
            return
    v1 .sort()
    print("Strictly increasing array is:")
    for it in v1:
        print(it, end=" ")
    v2 .sort(reverse=True)
    print("Strictlydecreasingarrayis:")
    for it in v2:
        print(it, end=" ")


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

# Expected output:
Strictly increasing array is:
1 2 3 4 7 Strictlydecreasingarrayis:
7 3
# sort ugly numbers in an array at their relative positions | function that returns true if n is an ugly number ; while divisible by 2 , keep dividing ; while divisible by 3 , keep dividing ; while divisible by 5 , keep dividing ; n must be 1 if it was ugly ; function to sort ugly numbers in their relative positions ; to store the ugly numbers from the array ; if current element is an ugly number ; add it to the arraylist and set arr [ i ] to - 1 ; sort the ugly numbers ; position of an ugly number ; 
def isUgly(n):
    while n % 2 == 0:
        n = n // 2
    while n % 3 == 0:
        n = n // 3
    while n % 5 == 0:
        n = n // 5
    if n == 1:
        return True
    return False


def sortUglyNumbers(arr, n):
    list = []
    for i in range(0, n):
        if isUgly(arr[i]):
            list .append(arr[i])
            arr[i] = -1
    list .sort()
    j = 0
    for i in range(0, n):
        if arr[i] == -1:
            print(list[j], end=" ")
            j += 1
        else:
            print(arr[i], end=" ")


if __name__ == "__main__":
    arr = [1, 2, 3, 7, 12, 10]
    n = len(arr)
    sortUglyNumbers(arr, n)

# Expected output:
1 2 3 7 10 12
# sort elements of the array that occurs in between multiples of k | utility function to print the contents of an array ; function to sort elements in between multiples of k ; to store the index of previous multiple of k ; if it is not the first multiple of k ; sort the elements in between the previous and the current multiple of k ; update previous to be current ; print the updated array ; 
def printArr(arr, n):
    for i in range(n):
        print(arr[i], end=" ")


def sortArr(arr, n, k):
    prev = -1
    for i in range(n):
        if (arr[i] % k == 0):
            if (prev != -1):
                temp = arr[prev + 1:i]
                temp .sort()
                arr = arr[:prev + 1] + temp + arr[i:]
            prev = i
    printArr(arr, n)


if __name__ == "__main__":
    arr = [2, 1, 13, 3, 7, 8, 21, 13, 12]
    n = len(arr)
    k = 2
    sortArr(arr, n, k)

# Expected output:
2 1 3 7 13 8 13 21 12
# find a and b from list of divisors | function to print a and b all of whose divisors are present in the given array ; sort the array ; a is the largest element from the array ; iterate from the second largest element ; if current element is not a divisor of a then it must be b ; if current element occurs more than once ; print a and b ; 
def printNumbers(arr, n):
    arr .sort()
    A, B = arr[n - 1], -1
    for i in range(n - 2, -1, -1):
        if A % arr[i] != 0:
            B = arr[i]
            break
        if i - 1 >= 0 and arr[i] == arr[i - 1]:
            B = arr[i]
            break
    print("A =", A, ", B =", B)


if __name__ == "__main__":
    arr = [1, 2, 4, 8, 16, 1, 2, 4]
    n = len(arr)
    printNumbers(arr, n)

# Expected output:
A = 16 , B = 4
# find the modified array after performing k operations of given type | utility function to print the contents of an array ; function to remove the minimum value of the array from every element of the array ; get the minimum value from the array ; remove the minimum value from every element of the array ; function to remove every element of the array from the maximum value of the array ; get the maximum value from the array ; remove every element of the array from the maximum value of the array ; function to print the modified array after k operations ; if k is odd then remove the minimum element of the array from every element of the array ; else remove every element of the array from the maximum value from the array ; print the modified array ; 
def printArray(arr, n):
    for i in range(n):
        print(arr[i], end=" ")


def removeMin(arr, n):
    minVal = arr[0]
    for i in range(1, n):
        minVal = min(minVal, arr[i])
    for i in range(n):
        arr[i] = arr[i] - minVal


def removeFromMax(arr, n):
    maxVal = arr[0]
    for i in range(1, n):
        maxVal = max(maxVal, arr[i])
    for i in range(n):
        arr[i] = maxVal - arr[i]


def modifyArray(arr, n, k):
    if (k % 2 == 0):
        removeMin(arr, n)
    else:
        removeFromMax(arr, n)
    printArray(arr, n)


if __name__ == "__main__":
    arr = [4, 8, 12, 16]
    n = len(arr)
    k = 2
    modifyArray(arr, n, k)

# Expected output:
0 4 8 12
# maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k | function to return the maximum product value ; to store the product ; sort the array ; efficiently finding product including every element once ; storing values in hash map ; including the greater repeating values so that product can be maximized ; 
def maxProd(arr, n, k):
    product = 1
    s = dict .fromkeys(arr, 0)
    arr .sort()
    for i in range(n):
        if (s[arr[i]] == 0):
            product = product * arr[i]
        s[arr[i]] = s[arr[i]] + 1
    j = n - 1
    while (j >= 0 and k > 0):
        if ((k > (s[arr[j]] - 1)) and ((s[arr[j]] - 1) > 0)):
            product *= pow(arr[j], s[arr[j]] - 1)
            k = k - s[arr[j]] + 1
            s[arr[j]] = 0
        if (k <= (s[arr[j]] - 1) and ((s[arr[j]] - 1) > 0)):
            product *= pow(arr[j], k)
            break
        j -= 1
    return product


if __name__ == "__main__":
    arr = [5, 6, 7, 8, 2, 5, 6, 8]
    n = len(arr)
    k = 2
    print(maxProd(arr, n, k))

# Expected output:
161280
# merge k sorted arrays of different sizes | ( divide and conquer approach ) | function to merge two arrays ; array to store the result after merging l and r ; variables to store the current pointers for l and r ; loop to merge l and r using two pointer ; function to merge all the arrays ; 2d - array to store the results of a step temporarily ; loop to make pairs of arrays and merge them ; to clear the data of previous steps ; returning the required output array ; 
def mergeTwoArrays(l, r):
    ret = []
    l_in, r_in = 0, 0
    while l_in + r_in < len(l) + len(r):
        if (l_in != len(l) and (r_in == len(r) or l[l_in] < r[r_in])):
            ret .append(l[l_in])
            l_in += 1
        else:
            ret .append(r[r_in])
            r_in += 1
    return ret


def mergeArrays(arr):
    arr_s = []
    while len(arr) != 1:
        arr_s[:] = []
        for i in range(0, len(arr), 2):
            if i == len(arr) - 1:
                arr_s .append(arr[i])
            else:
                arr_s .append(mergeTwoArrays(arr[i], arr[i + 1]))
        arr = arr_s[:]
    return arr[0]


if __name__ == "__main__":
    arr = [[3, 13], [8, 10, 11], [9, 15]]
    output = mergeArrays(arr)
    for i in range(0, len(output)):
        print(output[i], end=" ")

# Expected output:
3 8 9 10 11 13 15
# minimize the sum of the squares of the sum of elements of each group the array is divided into | function to return the minimized sum ; sort the array to pair the elements ; variable to hold the answer ; pair smallest with largest , second smallest with second largest , and so on ; 
def findAnswer(n, arr):
    arr .sort(reverse=False)
    sum = 0
    for i in range(int(n / 2)):
        sum += ((arr[i] + arr[n - i - 1]) * (arr[i] + arr[n - i - 1]))
    return sum


if __name__ == '__main__':
    arr = [53, 28, 143, 5]
    n = len(arr)
    print(findAnswer(n, arr))

# Expected output:
28465
# merge k sorted arrays | set 3 ( using divide and conquer approach ) | python3 program to merge k sorted arrays ; function to perform merge operation ; to store the starting point of left and right array ; to store the size of left and right array ; array to temporarily store left and right array ; storing data in left array ; storing data in right array ; to store the current index of temporary left and right array ; to store the current index for output array ; two pointer merge for two sorted arrays ; code to drive merge - sort and create recursion tree ; base step to initialize the output array before performing merge operation ; to sort left half ; to sort right half ; merge the left and right half ; 

n = 4


def merge(l, r, output):
    l_in = l * n
    r_in = ((l + r) // 2 + 1) * n
    l_c = ((l + r) // 2 - l + 1) * n
    r_c = (r - (l + r) // 2) * n
    l_arr = [0] * l_c
    r_arr = [0] * r_c
    for i in range(l_c):
        l_arr[i] = output[l_in + i]
    for i in range(r_c):
        r_arr[i] = output[r_in + i]
    l_curr = 0
    r_curr = 0
    in1 = l_in
    while (l_curr + r_curr < l_c + r_c):
        if (r_curr == r_c or (
                l_curr != l_c and l_arr[l_curr] < r_arr[r_curr])):
            output[in1] = l_arr[l_curr]
            l_curr += 1
            in1 += 1
        else:
            output[in1] = r_arr[r_curr]
            r_curr += 1
            in1 += 1


def divide(l, r, output, arr):
    if (l == r):
        for i in range(n):
            output[l * n + i] = arr[l][i]
        return
    divide(l, (l + r) // 2, output, arr)
    divide((l + r) // 2 + 1, r, output, arr)
    merge(l, r, output)


if __name__ == "__main__":
    arr = [[5, 7, 15, 18], [1, 8, 9, 17], [1, 4, 7, 7]]
    k = len(arr)
    output = [0] * (n * k)
    divide(0, k - 1, output, arr)
    for i in range(n * k):
        print(output[i], end=" ")

# Expected output:
1 1 4 5 7 7 7 8 9 15 17 18
# make palindromic string non | function to print the non - palindromic string if it exists , otherwise prints - 1 ; if all characters are not same , set flag to 1 ; update frequency of the current character ; if all characters are same ; print characters in sorted manner ; 
def findNonPalinString(s):
    freq = [0] * (26)
    flag = 0
    for i in range(0, len(s)):
        if s[i] != s[0]:
            flag = 1
        freq[ord(s[i]) - ord('a')] += 1
    if not flag:
        print("-1")
    else:
        for i in range(0, 26):
            for j in range(0, freq[i]):
                print(chr(ord('a') + i), end="")


if __name__ == "__main__":
    s = "abba"
    findNonPalinString(s)

# Expected output:
aabb