giulio98/xlcost-single-prompt huggingface dataset:
# largest number having both positive and negative values present in the array | function to find the largest number k such that both k and - k are present in the array ; stores the resultant value of k ; sort the array arr [ ] ; initialize two variables to use two pointers technique ; iterate until the value of l is less than r ; find the value of the sum ; if the sum is 0 , then the resultant element is found ; if the sum is negative ; otherwise , decrement r ; 
def largestNum(arr):
    res = 0
    arr = sorted(arr)
    l = 0
    r = len(arr) - 1
    while (l < r):
        sum = arr[l] + arr[r]
        if (sum == 0):
            res = max(res, max(arr[l], arr[r]))
            return res
        elif (sum < 0):
            l += 1
        else:
            r -= 1
    return res


if __name__ == '__main__':
    arr = [3, 2, -2, 5, -3]
    print(largestNum(arr))

# Expected output:
3
# find elements larger than half of the elements in an array | set 2 | function to find the element that are larger than half of elements of the array ; find the value of mid ; stores the maximum element ; stores the frequency of each array element ; traverse the array in the reverse order ; decrement the value of count [ i ] and mid ; prthe current element ; check if the value of mid is equal to 0 ; 
def findLarger(arr, n):
    mid = (n + 1) // 2
    mx = max(arr)
    count = [0] * (mx + 1)
    for i in range(n):
        count[arr[i]] += 1
    for i in range(mx, -1, -1):
        while (count[i] > 0):
            count[i] -= 1
            mid -= 1
            print(i, end=" ")
            if (mid == 0):
                break
        if (mid == 0):
            break


if __name__ == '__main__':
    arr = [10, 4, 2, 8, 9]
    N = len(arr)
    findLarger(arr, N)

# Expected output:
10 9 8
# probability of obtaining pairs from two arrays such that element from the first array is smaller than that of the second array | function to find probability such that x < y and x belongs to arr1 [ ] and y belongs to arr2 [ ] ; stores the length of arr1 ; stores the length of arr2 ; stores the result ; traverse the arr1 [ ] ; stores the count of elements in arr2 that are greater than arr [ i ] ; traverse the arr2 [ ] ; if arr2 [ j ] is greater than arr1 [ i ] ; increment res by y ; update the value of res ; return resultant probability ; 
def probability(arr1, arr2):
    N = len(arr1)
    M = len(arr2)
    res = 0
    for i in range(N):
        y = 0
        for j in range(M):
            if (arr2[j] > arr1[i]):
                y += 1
        res += y
    res = res / (N * M)
    return res


if __name__ == "__main__":
    arr1 = [5, 2, 6, 1]
    arr2 = [1, 6, 10, 1]
    print(probability(arr1, arr2))

# Expected output:
0.4375
# probability of obtaining pairs from two arrays such that element from the first array is smaller than that of the second array | function to find probability such that x < y and x belongs to arr1 [ ] & y belongs to arr2 [ ] ; stores the length of arr1 ; stores the length of arr2 ; stores the result ; sort the arr2 [ ] in the ascending order ; traverse the arr1 [ ] ; stores the count of elements in arr2 that are greater than arr [ i ] ; increment res by y ; update the resultant probability ; return the result ; function to return the count of elements from the array which are greater than k ; stores the index of the leftmost element from the array which is at least k ; finds number of elements greater than k ; if mid element is at least k , then update the value of leftgreater and r ; update leftgreater ; update r ; if mid element is at most k , then update the value of l ; return the count of elements greater than k ; 
def countGreater(arr, k):
    n = len(arr)
    l = 0
    r = n - 1
    leftGreater = n
    while l <= r:
        m = (l + r) // 2
        if (arr[m] > k):
            leftGreater = m
            r = m - 1
        else:
            l = m + 1
    return n - leftGreater


def probability(arr1, arr2):
    n = len(arr1)
    m = len(arr2)
    res = 0
    arr2 .sort()
    for i in range(n):
        y = countGreater(arr2, arr1[i])
        res += y
    res /= (n * m)
    return res


if __name__ == '__main__':
    arr1 = [5, 2, 6, 1]
    arr2 = [1, 6, 10, 1]
    print(probability(arr1, arr2))

# Expected output:
0.4375
# minimize cost to cover floor using tiles of dimensions 1 * 1 and 1 * 2 | function to find the minimum cost of flooring with the given tiles ; store the size of the 2d array ; stores the minimum cost of flooring ; traverse the 2d array row - wise ; if the current character is ' * ' , then skip it ; choose the 1 * 1 tile if j is m - 1 ; if consecutive ' . ' are present , the greedily choose tile with the minimum cost ; otherwise choose the 1 * 1 tile ; print the minimum cost ; 
def minCost(arr, A, B):
    n = len(arr)
    m = len(arr[0])
    ans = 0
    for i in range(n):
        j = 0
        while j < m:
            if (arr[i][j] == '*'):
                j += 1
                continue
            if (j == m - 1):
                ans += A
            else:
                if (arr[i][j + 1] == '.'):
                    ans += min(2 * A, B)
                    j += 1
                else:
                    ans += A
            j += 1
    print(ans)


if __name__ == '__main__':
    arr = [['.', '.', '*'], ['.', '*', '*']]
    A, B = 2, 10
    minCost(arr, A, B)

# Expected output:
6
# count inversions in a permutation of first n natural numbers | python3 program for the above approach ; function to count number of inversions in a permutation of first n natural numbers ; store array elements in sorted order ; store the count of inversions ; traverse the array ; store the index of first occurrence of arr [ i ] in vector v ; add count of smaller elements than current element ; erase current element from vector and go to next index ; print the result ; 

from bisect import bisect_left


def countInversions(arr, n):
    v = []
    for i in range(1, n + 1, 1):
        v .append(i)
    ans = 0
    for i in range(n):
        itr = bisect_left(v, arr[i])
        ans += itr
        v = v[:itr] + v[itr + 1:]
    print(ans)


if __name__ == '__main__':
    arr = [2, 3, 1, 5, 4]
    n = len(arr)
    countInversions(arr, n)

# Expected output:
3
# find all possible pairs with given bitwise or and bitwise xor values | function to find pairs with xor equal to a and or equal to b ; iterate from 1 to b ; check if ( i or y ) is b ; 
def findPairs(A, B):
    for i in range(1, B + 1):
        y = A ^ i
        if (y > 0 and (i | y) == B):
            print(i, " ", y)


A = 8
B = 10
findPairs(A, B)

# Expected output:
2   10
10   2
# count distinct elements from a range of a sorted sequence from a given frequency array | function to find the first index with value is at least element ; update the value of left ; binary search for the element ; find the middle element ; check if the value lies between the elements at index mid - 1 and mid ; check in the right subarray ; update the value of left ; check in left subarray ; update the value of right ; function to count the number of distinct elements over the range [ l , r ] in the sorted sequence ; stores the count of distinct elements ; create the prefix sum array ; update the value of count ; update the value of pref [ i ] ; calculating the first index of l and r using binary search ; print the resultant count ; 
def binarysearch(array, right, element):
    left = 1
    while (left <= right):
        mid = (left + right // 2)
        if (array[mid] == element):
            return mid
        if (mid - 1 > 0 and array[mid] > element and array[mid - 1] < element):
            return mid
        elif (array[mid] < element):
            left = mid + 1
        else:
            right = mid - 1
    return 1


def countDistinct(arr, L, R):
    count = 0
    pref = [0] * (len(arr) + 1)
    for i in range(1, len(arr) + 1):
        count += arr[i - 1]
        pref[i] = count
    left = binarysearch(pref, len(arr) + 1, L)
    right = binarysearch(pref, len(arr) + 1, R)
    print(right - left + 1)


if __name__ == "__main__":
    arr = [3, 6, 7, 1, 8]
    L = 3
    R = 7
    countDistinct(arr, L, R)

# Expected output:
2
# find unique id and domain name of a website from a string | function to check if a character is alphabet or not ; function to check if a character is a numeric or not ; function to find id and domain name from a given ; stores id and the domain names ; stores the words of s ; stores the temporary word ; traverse the s ; if the current character is space ; push the curr in words ; update the curr ; otherwise ; if curr is not empty ; if length of ss is 10 ; traverse the ss ; if j is in the range [ 5 , 9 ) ; if current character is not numeric ; mark flag 1 ; otherwise ; if current character is not alphabet ; mark flag 1 ; if flag is false ; assign ss to id ; if sub formed by the first 3 character is " www " and last 3 character is " moc " ; update the domain name ; print id and domain ; 
def ischar(x):
    if ((x >= 'A' and x <= 'Z') or (x >= 'a' and x <= 'z')):
        return 1
    return 0


def isnum(x):
    if (x >= '0' and x <= '9'):
        return 1
    return 0


def findIdandDomain(S, N):
    ID, Domain = "", ""
    words = []
    curr = ""
    for i in range(N):
        if (S[i] == ' '):
            words .append(curr)
            curr = ""
        else:
            if (S[i] == '.'):
                if (i + 1 == N or (i + 1 < N and S[i + 1] == ' ')):
                    continue
            curr += S[i]
    if (len(curr)):
        words .append(curr)
    for ss in words:
        if (len(ss) == 10):
            flag = 0
            for j in range(10):
                if (j >= 5 and j < 9):
                    if (isnum(ss[j]) == 0):
                        flag = 1
                else:
                    if (ischar(ss[j]) == 0):
                        flag = 1
            if (not flag):
                ID = ss
        if (ss[0:3] == "www" and ss[len(ss) - 3:] == "com"):
            Domain = ss[4:len(ss)]
    print("ID =", ID)
    print("Domain =", Domain)


if __name__ == '__main__':
    S = "We thank ABCDE1234F for visiting us "
    N = len(S)
    findIdandDomain(S, N)

# Expected output:
ID = ABCDE1234F
Domain =
# capacity to ship packages within d days | function to check if the weights can be delivered in d days or not ; stores the count of days required to ship all the weights if the maximum capacity is mx ; traverse all the weights ; if total weight is more than the maximum capacity ; if days are more than d , then return false ; return true for the days < d ; function to find the least weight capacity of a boat to ship all the weights within d days ; stores the total weights to be shipped ; find the sum of weights ; stores the maximum weight in the array that has to be shipped ; store the ending value for the search space ; store the required result ; perform binary search ; store the middle value ; if mid can be shipped , then update the result and end value of the search space ; search for minimum value in the right part ; print the result ; 
def isValid(weight, n, D, mx):
    st = 1
    sum = 0
    for i in range(n):
        sum += weight[i]
        if (sum > mx):
            st += 1
            sum = weight[i]
        if (st > D):
            return False
    return True


def shipWithinDays(weight, D, n):
    sum = 0
    for i in range(n):
        sum += weight[i]
    s = weight[0]
    for i in range(1, n):
        s = max(s, weight[i])
    e = sum
    res = -1
    while (s <= e):
        mid = s + (e - s) // 2
        if (isValid(weight, n, D, mid)):
            res = mid
            e = mid - 1
        else:
            s = mid + 1
    print(res)


if __name__ == '__main__':
    weight = [9, 8, 10]
    D = 3
    N = len(weight)
    shipWithinDays(weight, D, N)

# Expected output:
10
# find a point whose sum of distances from all given points on a line is k | function to find the sum of distances of all points from a given point ; stores sum of distances ; traverse the array ; return the sum ; function to find such a point having sum of distances of all other points from this point equal to k ; if n is odd keep left as arr [ n / 2 ] else keep left as arr [ n / 2 - 1 ] + 1 ; ; keep right as arr [ n - 1 ] ; perform binary search in the right half ; calculate the mid index of the range ; if temp is equal to k ; print the value of mid ; if the value of k < temp ; update right to mid - 1 ; if the value of k > temp ; update left to mid + 1 ; update the value of left ; update the value of right ; perform binary search on the left half ; calculate the mid index of the range ; if temp is equal to k ; print mid ; if k > temp ; update right to mid - 1 ; if k < temp ; update left to mid + 1 ; if no such point found ; 
def findSum(arr, N, pt):
    sum = 0
    for i in range(N):
        sum += abs(arr[i] - pt)
    return sum


def findPoint(arr, N, K):
    left = 0
    if (N % 2):
        left = arr[N // 2]
    else:
        left = arr[N // 2 - 1] + 1
    right = arr[N - 1]
    while (left <= right):
        mid = (left + right) // 2
        temp = findSum(arr, N, mid)
        if (temp == K):
            print(mid)
            return
        elif (K < temp):
            right = mid - 1
        else:
            left = mid + 1
    left = arr[0]
    right = arr[N // 2] - 1
    while (left <= right):
        mid = (left + right) // 2
        temp = findSum(arr, N, mid)
        if (temp == K):
            print(mid)
            return
        elif (K > temp):
            right = mid - 1
        else:
            left = mid + 1
    print("-1")


if __name__ == '__main__':
    arr = [1, 3, 6, 7, 11]
    K = 18
    N = len(arr)
    findPoint(arr, N, K)

# Expected output:
8
# generate an array consisting of most frequent greater elements present on the right side of each array element | function to generate an array containing the most frequent greater element on the right side of each array element ; stores the generated array ; traverse the array arr [ ] ; store the result for the current index and its frequency ; iterate over the right subarray ; store the frequency of the current array element ; if the frequencies are equal ; update ans to smaller of the two elements ; if count of new element is more than count of ans ; insert answer in the array ; print the resultant array ; 
def findArray(arr, n):
    v = []
    for i in range(n):
        ans = -1
        old_c = 0
        for j in range(i + 1, n):
            if (arr[j] > arr[i]):
                curr_c = arr[j:n + 1].count(arr[j])
                if (curr_c == old_c):
                    if (arr[j] < ans):
                        ans = arr[j]
                if (curr_c > old_c):
                    ans = arr[j]
                    old_c = curr_c
        v .append(ans)
    for i in range(len(v)):
        print(v[i], end=" ")


if __name__ == '__main__':
    arr = [4, 5, 2, 25, 10, 5, 10, 3, 10, 5]
    size = len(arr)
    findArray(arr, size)

# Expected output:
5 10 10 -1 -1 10 -1 5 -1 -1
# smallest index that splits an array into two subarrays with equal product | function to find the smallest index that splits the array into two subarrays with equal product ; stores the product of the array ; traverse the given array ; stores the product of left and the right subarrays ; traverse the given array ; update the products ; check if product is equal ; print resultant index ; if no partition exists , then print - 1. ; 
def prodEquilibrium(arr, N):
    product = 1
    for i in range(N):
        product *= arr[i]
    left = 1
    right = product
    for i in range(N):
        left = left * arr[i]
        right = right // arr[i]
        if (left == right):
            print(i + 1)
            return
    print(-1)


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

# Expected output:
3
# count triplets from a sorted array having difference between adjacent elements equal to d | function to count the number of triplets having difference between adjacent elements equal to d ; stores the frequency of array elements ; stores the count of resultant triplets ; traverse the array ; check if arr [ i ] - d and arr [ i ] - 2 * d exists in the hashmap or not ; update the value of ans ; increase the frequency of the current element ; return the resultant count ; 
def countTriplets(D, arr):
    freq = {}
    ans = 0
    for i in range(len(arr)):
        if (((arr[i] - D) in freq) and (arr[i] - 2 * D) in freq):
            ans += (freq[arr[i] - D] * freq[arr[i] - 2 * D])
        freq[arr[i]] = freq .get(arr[i], 0) + 1
    return ans


if __name__ == '__main__':
    arr = [1, 2, 4, 5, 7, 8, 10]
    D = 1
    print(countTriplets(D, arr))

# Expected output:
0
# find the array element having equal sum of prime numbers on its left and right | python3 program for the above approach ; function to find an index in the array having sum of prime numbers to its left and right equal ; stores the maximum value present in the array ; stores all positive elements which are <= max_value ; if 1 is present ; remove 1 ; sieve of eratosthenes to store all prime numbers which are <= max_value in the map ; erase non - prime numbers ; stores the sum of prime numbers from left ; stores the sum of prime numbers to the left of each index ; stores the sum of prime numbers to the left of the current index ; add current value to the prime sum if the current value is prime ; stores the sum of prime numbers from right ; stores the sum of prime numbers to the right of each index ; stores the sum of prime numbers to the right of the current index ; add current value to the prime sum if the current value is prime ; traverse through the two arrays to find the index ; compare the values present at the current index ; return the index where both the values are same ; no index is found . ; 

from math import sqrt


def find_index(arr, N):
    max_value = -10 ** 9
    for i in range(N):
        max_value = max(max_value, arr[i])
    store = {}
    for i in range(1, max_value + 1):
        store[i] = store .get(i, 0) + 1
    if (1 in store):
        del store[1]
    for i in range(2, int(sqrt(max_value)) + 1):
        multiple = 2
        while ((i * multiple) <= max_value):
            if (i * multiple in store):
                del store[i * multiple]
            multiple += 1
    prime_sum_from_left = 0
    first_array = [0] * N
    for i in range(N):
        first_array[i] = prime_sum_from_left
        if arr[i] in store:
            prime_sum_from_left += arr[i]
    prime_sum_from_right = 0
    second_array = [0] * N
    for i in range(N - 1, -1, -1):
        second_array[i] = prime_sum_from_right
        if (arr[i] in store):
            prime_sum_from_right += arr[i]
    for i in range(N):
        if (first_array[i] == second_array[i]):
            return i
    return -1


if __name__ == '__main__':
    arr = [11, 4, 7, 6, 13, 1, 5]
    N = len(arr)
    print(find_index(arr, N))

# Expected output:
3
# convert an array to reduced form | set 3 ( binary search ) | function to find the reduced form of the given array arr [ ] ; stores the sorted form of the the given array arr [ ] ; sort the array brr [ ] ; traverse the given array arr [ ] ; perform the binary search ; calculate the value of mid ; prthe current index and break ; update the value of l ; update the value of r ; 
def convert(arr, n):
    brr = [i for i in arr]
    brr = sorted(brr)
    for i in range(n):
        l, r, mid = 0, n - 1, 0
        while (l <= r):
            mid = (l + r) // 2
            if (brr[mid] == arr[i]):
                print(mid, end=" ")
                break
            elif (brr[mid] < arr[i]):
                l = mid + 1
            else:
                r = mid - 1


if __name__ == '__main__':
    arr = [10, 20, 15, 12, 11, 50]
    N = len(arr)
    convert(arr, N)

# Expected output:
0 4 3 2 1 5
# find the array element having equal count of prime numbers on its left and right | python 3 program for the above approach ; function to find the index of the array such that the count of prime numbers to its either ends are same ; store the maximum value in the array ; traverse the array arr [ ] ; / stores all the numbers ; iterate over the range [ 1 , max ] ; increment the value of st [ i ] ; removes 1 from the map st ; perform sieve of prime numbers ; while i * j is less than the maxvalue ; if i * j is in map st ; erase the value ( i * j ) ; increment the value of j ; stores the count of prime from index 0 to i ; stores the count of prime numbers ; traverse the array arr [ ] ; if arr [ i ] is present in the map st ; stores the count of prime from index i to n - 1 ; stores the count of prime numbers ; iterate over the range [ 0 , n - 1 ] in reverse order ; if arr [ i ] is in map st ; iterate over the range [ 0 , n - 1 ] ; if prefix [ i ] is equal to the suffix [ i ] ; return - 1 if no such index is present ; 

from collections import defaultdict
import sys
import math


def findIndex(arr, N):
    maxValue = -sys .maxsize - 1
    for i in range(N):
        maxValue = max(maxValue, arr[i])
    St = defaultdict(int)
    for i in range(1, maxValue + 1):
        St[i] += 1
    if (1 in St):
        St .pop(1)
    for i in range(2, int(math .sqrt(maxValue)) + 1):
        j = 2
        while ((i * j) <= maxValue):
            if (i * j) in St:
                St .pop(i * j)
            j += 1
    LeftCount = 0
    Prefix = [0] * N
    for i in range(N):
        Prefix[i] = LeftCount
        if (arr[i] in St):
            LeftCount += 1
    RightCount = 0
    Suffix = [0] * N
    for i in range(N - 1, -1, -1):
        Suffix[i] = RightCount
        if arr[i] in St:
            RightCount += 1
    for i in range(N):
        if (Prefix[i] == Suffix[i]):
            return i
    return -1


if __name__ == "__main__":
    arr = [2, 3, 4, 7, 5, 10, 1, 8]
    N = len(arr)
    print(findIndex(arr, N))

# Expected output:
2
# numbers of pairs from an array whose average is also present in the array | function to count the number of pairs from the array having sum s ; stores the total count of pairs whose sum is 2 * s ; generate all possible pairs and check their sums ; if the sum is s , then increment the count ; return the total count of pairs ; function to count of pairs having whose average exists in the array ; initialize the count ; use set to remove duplicates ; add elements in the set ; for every sum , count all possible pairs ; return the total count ; 
def getCountPairs(arr, N, S):
    count = 0
    for i in range(len(arr)):
        for j in range(i + 1, len(arr)):
            if ((arr[i] + arr[j]) == S):
                count += 1
    return count


def countPairs(arr, N):
    count = 0
    S = set([])
    for i in range(N):
        S .add(arr[i])
    for ele in S:
        sum = 2 * ele
        count += getCountPairs(arr, N, sum)
    return count


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

# Expected output:
7