giulio98/xlcost-single-prompt huggingface dataset:
# longest common subsequence of two arrays out of which one array consists of distinct elements only | python3 program to to implement the above approach ; function to find the longest common subsequence between the two arrays ; maps elements of firstarr [ ] to their respective indices ; traverse the array firstarr [ ] ; stores the indices of common elements between firstarr [ ] and secondarr [ ] ; traverse the array secondarr [ ] ; if current element exists in the map ; stores lis from temparr [ ] ; 

from bisect import bisect_left


def LCS(firstArr, secondArr):
    mp = {}
    for i in range(len(firstArr)):
        mp[firstArr[i]] = i + 1
    tempArr = []
    for i in range(len(secondArr)):
        if (secondArr[i] in mp):
            tempArr .append(mp[secondArr[i]])
    tail = []
    tail .append(tempArr[0])
    for i in range(1, len(tempArr)):
        if (tempArr[i] > tail[-1]):
            tail .append(tempArr[i])
        elif (tempArr[i] < tail[0]):
            tail[0] = tempArr[i]
        else:
            it = bisect_left(tail, tempArr[i])
            it = tempArr[i]
    return len(tail)


if __name__ == '__main__':
    firstArr = [3, 5, 1, 8]
    secondArr = [3, 3, 5, 3, 8]
    print(LCS(firstArr, secondArr))

# Expected output:
3
# count non | function to find the total count of triplets ( i , j , k ) such that i < j < k and ( j - i ) != ( k - j ) ; stores indices of 0 s ; stores indices of 1 s ; stores indices of 2 s ; traverse the array ; if current array element is 0 ; if current array element is 1 ; if current array element is 2 ; total count of triplets ; traverse the array zero_i [ ] ; traverse the array one_i [ ] ; stores index of 0 s ; stores index of 1 s ; stores third element of triplets that does not satisfy the condition ; if r present in the map ; update r ; if r present in the map ; update r ; if r present in the map and equidistant ; print the obtained count ; 
def countTriplets(arr, N):
    zero_i = []
    one_i = []
    mp = {}
    for i in range(N):
        if (arr[i] == 0):
            zero_i .append(i + 1)
        elif (arr[i] == 1):
            one_i .append(i + 1)
        else:
            mp[i + 1] = 1
    total = len(zero_i) * len(one_i) * len(mp)
    for i in range(len(zero_i)):
        for j in range(len(one_i)):
            p = zero_i[i]
            q = one_i[j]
            r = 2 * p - q
            if (r in mp):
                total -= 1
            r = 2 * q - p
            if (r in mp):
                total -= 1
            r = (p + q) // 2
            if ((r in mp) and abs(r - p) == abs(r - q)):
                total -= 1
    print(total)


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

# Expected output:
1
# print prime factors of a given integer in decreasing order using stack | function to print prime factors of n in decreasing order ; stores prime factors of n in decreasing order ; insert i into stack ; update n ; update i ; print value of stack st ; 
def PrimeFactors(N):
    st = []
    i = 2
    while (N != 1):
        if (N % i == 0):
            st .append(i)
            while (N % i == 0):
                N = N // i
        i += 1
    while (len(st) != 0):
        print(st[-1])
        st .pop()


if __name__ == "__main__":
    N = 8
    PrimeFactors(N)

# Expected output:
2
# count subarrays consisting of first k natural numbers in descending order | function to count subarray having the decreasing sequence k to 1 ; traverse the array ; check if required sequence is present or not ; reset temp to k ; return the count ; 
def CountSubarray(arr, n, k):
    temp = k
    count = 0
    for i in range(n):
        if (arr[i] == temp):
            if (temp == 1):
                count += 1
                temp = k
            else:
                temp -= 1
        else:
            temp = k
            if (arr[i] == k):
                i -= 1
    return count


if __name__ == "__main__":
    arr = [1, 2, 3, 7, 9, 3, 2, 1, 8, 3, 2, 1]
    N = len(arr)
    K = 3
    print(CountSubarray(arr, N, K))

# Expected output:
2
# smallest number not less than n which is divisible by all digits of n | python3 program for the above approach ; function to calculate the lcm ; function to find the smallest number satisfying given constraints ; lcm value is 1 initially ; finding the lcm of all non zero digits ; update the value lcm ; stores ceil value ; print the answer ; 

import math


def LCM(A, B):
    return (A * B // math .gcd(A, B))


def findSmallestNumber(X):
    lcm = 1
    temp = X
    while (temp):
        last = temp % 10
        temp //= 10
        if (not last):
            continue
        lcm = LCM(lcm, last)
    answer = ((X + lcm - 1) // lcm) * lcm
    print(answer)


if __name__ == "__main__":
    X = 280
    findSmallestNumber(X)

# Expected output:
280
# minimum sum of two integers whose product is strictly greater than n | function to find the minimum sum of two integers such that their product is strictly greater than n ; initialise low as 0 and high as 1e9 ; iterate to find the first number ; find the middle value ; if mid ^ 2 is greater than equal to a , then update high to mid ; otherwise update low ; store the first number ; again , set low as 0 and high as 1e9 ; iterate to find the second number ; find the middle value ; if first number * mid is greater than n then update high to mid ; else , update low to mid ; store the second number ; print the result ; 
def minSum(N):
    low = 0
    high = 1000000000
    while (low + 1 < high):
        mid = low + (high - low) / 2
        if (mid * mid >= N):
            high = mid
        else:
            low = mid
    first = high
    low = 0
    high = 1000000000
    while (low + 1 < high):
        mid = low + (high - low) / 2
        if (first * mid > N):
            high = mid
        else:
            low = mid
    second = high
    print(round(first + second))


N = 10
minSum(N)

# Expected output:
7
# minimum sum of two integers whose product is strictly greater than n | python3 program for the above approach ; function to find the minimum sum of two integers such that their product is strictly greater than n ; store the answer using the ap - gp inequality ; print the result ; 

import math


def minSum(N):
    ans = math .ceil(2 * math .sqrt(N + 1))
    print(math .trunc(ans))


N = 10
minSum(N)

# Expected output:
7
# query to find length of the longest subarray consisting only of 1 s | python 3 program for the above approach ; arrays to store prefix sums , suffix and max 's node respectively ; function to construct segment tree ; max array for node max ; array for prefix sum node ; array for suffix sum node ; calculate max node ; function to update segment tree ; update at position ; update sums from bottom to the top of segment tree ; update max tree ; update pref tree ; update suf tree ; function to perform given queries ; stores count of queries ; traverse each query ; print longest length of subarray in [ 1 , n ] ; flip the character ; 

INF = 1000000
pref = [0 for i in range(500005)]
suf = [0 for i in range(500005)]
MAX = [0 for i in range(500005)]


def build(a, tl, tr, v):
    global MAX
    global pref
    global suf
    if (tl == tr):
        MAX[v] = a[tl]
        pref[v] = a[tl]
        suf[v] = a[tl]
    else:
        tm = (tl + tr) // 2
        build(a, tl, tm, v * 2)
        build(a, tm + 1, tr, v * 2 + 1)
        MAX[v] = max(MAX[v * 2], max(MAX[v * 2 + 1],
                     suf[v * 2] + pref[v * 2 + 1]))
        pref[v] = max(pref[v * 2], pref[2 * v] +
                      (pref[2 * v] == tm - tl + 1) * pref[v * 2 + 1])
        suf[v] = max(suf[v * 2 + 1], suf[2 * v + 1] +
                     suf[v * 2] * (suf[2 * v + 1] == tr - tm))


def update(a, pos, tl, tr, v):
    global pref
    global suf
    global MAX
    if (tl > pos or tr < pos):
        return
    if (tl == tr and tl == pos):
        MAX[v] = a[pos]
        pref[v] = a[pos]
        suf[v] = a[pos]
    else:
        tm = (tl + tr) // 2
        update(a, pos, tl, tm, v * 2)
        update(a, pos, tm + 1, tr, v * 2 + 1)
        MAX[v] = max(MAX[v * 2], max(MAX[v * 2 + 1],
                     suf[v * 2] + pref[v * 2 + 1]))
        pref[v] = max(pref[v * 2], pref[2 * v] +
                      (pref[2 * v] == tm - tl + 1) * pref[v * 2 + 1])
        suf[v] = max(suf[v * 2 + 1], suf[2 * v + 1] +
                     (suf[2 * v + 1] == tr - tm) * suf[v * 2])


def solveQueries(arr, n, Q, k):
    global MAX
    cntQuery = len(Q)
    build(arr, 0, n - 1, 1)
    for i in range(cntQuery):
        if (Q[i][0] == 1):
            print(MAX[1], end=" ")
        else:
            arr[Q[i][1] - 1] ^= 1
            update(arr, Q[i][1] - 1, 0, n - 1, 1)


if __name__ == '__main__':
    N = 10
    arr = [1, 1, 0, 1, 1, 1, 0, 0, 1, 1]
    Q = [[1], [2, 3], [1]]
    K = 3
    solveQueries(arr, N, Q, K)

# Expected output:
3 6
# find the array element having maximum frequency of the digit k | function to find the count of digits , k in the given number n ; stores count of occurrence of digit k in n ; iterate over digits of n ; if current digit is k ; update count ; update n ; utility function to find an array element having maximum frequency of digit k ; stores frequency of digit k in arr [ i ] ; stores maximum frequency of digit k in the array ; stores an array element having maximum frequency of digit k ; traverse the array ; count the frequency of digit k in arr [ i ] ; update max with maximum frequency found so far ; update ele ; if there is no array element having digit k in it ; function to find an array element having maximum frequency of digit k ; stores an array element having maximum frequency of digit k ; if there is no element found having digit k in it ; print element having max frequency of digit k ; 
def countFreq(N, K):
    count = 0
    while (N > 0):
        if (N % 10 == K):
            count += 1
        N = N // 10
    return count


def findElementUtil(arr, N, K):
    c = 0
    max = 0
    ele = 0
    for i in range(N):
        c = countFreq(arr[i], K)
        if (c > max):
            max = c
            ele = arr[i]
    if (max == 0):
        return -1
    else:
        return ele


def findElement(arr, N, K):
    ele = findElementUtil(arr, N, K)
    if (ele == -1):
        print("-1", end="")
    else:
        print(ele)


if __name__ == '__main__':
    K = 3
    arr = [3, 77, 343, 456]
    N = len(arr)
    findElement(arr, K, N)

# Expected output:
343
# minimum removals required to make any interval equal to the union of the given set | function to count minimum number of removals required to make an interval equal to the union of the given set ; stores the minimum number of removals ; traverse the set ; left boundary ; right boundary ; stores count of intervals lying within current interval ; traverse over all remaining intervals ; check if interval lies within the current interval ; increase count ; update minimum removals required ; 
def findMinDeletions(v, n):
    minDel = 10 ** 18
    for i in range(n):
        L = v[i][0]
        R = v[i][1]
        Count = 0
        for j in range(n):
            if (v[j][1] >= L and v[j][0] <= R):
                Count += 1
        minDel = min(minDel, n - Count)
    return minDel


if __name__ == '__main__':
    v = []
    v .append([1, 3])
    v .append([4, 12])
    v .append([5, 8])
    v .append([13, 2])
    N = len(v)
    print(findMinDeletions(v, N))

# Expected output:
2
# maximum even numbers present in any subarray of size k | function to find the maximum count of even numbers from all the subarrays of size m ; stores the count of even numbers in a subarray of size m ; calculate the count of even numbers in the current subarray ; if current element is an even number ; stores the maximum count of even numbers from all the subarrays of size m ; traverse remaining subarrays of size m using sliding window technique ; if the first element of the subarray is even ; update curr ; if i - th element is even increment the count ; update the answer ; return answer ; 
def maxEvenIntegers(arr, N, M):
    curr = 0
    for i in range(0, M):
        if (arr[i] % 2 == 0):
            curr += 1
    ans = curr
    for i in range(M, N):
        if (arr[i - M] % 2 == 0):
            curr -= 1
        if (arr[i] % 2 == 0):
            curr += 1
            ans = max(curr, ans)
    return ans


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

# Expected output:
2
# count numbers up to n having digit d in its octal representation | function to count the numbers in given range whose octal representation contains atleast digit , d ; store count of numbers up to n whose octal representation contains digit d ; iterate over the range [ 1 , n ] ; calculate digit of i in octal representation ; check if octal representation of x contains digit d ; update total ; update x ; prthe answer ; 
def countNumbers(n, d):
    total = 0
    for i in range(1, n + 1):
        x = i
        while (x > 0):
            if (x % 8 == d):
                total += 1
                break
            x = x // 8
    print(total)


if __name__ == '__main__':
    n, d = 20, 7
    countNumbers(n, d)

# Expected output:
2
# count number of triplets ( a , b , c ) from first n natural numbers such that a * b + c = n | function to find the count of triplets ( a , b , c ) with a * b + c = n ; stores count of triplets of 1 st n natural numbers which are of the form a * b + c = n ; iterate over the range [ 1 , n ] ; if n is divisible by i ; update cnttriplet ; update cnttriplet ; 
def findCntTriplet(N):
    cntTriplet = 0
    for i in range(1, N):
        if (N % i != 0):
            cntTriplet += N // i
        else:
            cntTriplet += (N // i) - 1
    return cntTriplet


if __name__ == '__main__':
    N = 3
    print(findCntTriplet(N))

# Expected output:
3
# count divisors which generates same quotient and remainder | python3 program of the above approach ; function to calculate the count of numbers such that it gives same quotient and remainder ; stores divisor of number n . ; iterate through numbers from 2 to sqrt ( n ) and store divisors of n ; as n is also divisor of itself ; iterate through divisors ; checking whether x satisfies the required condition ; print the count ; 

from math import floor, sqrt


def countDivisors(n):
    count = 0
    divisor = []
    for i in range(2, floor(sqrt(n))):
        if (n % i == 0):
            if (n // i == i):
                divisor .append(i)
            else:
                divisor .append(i)
                divisor .append(n // i)
    divisor .append(n)
    for x in divisor:
        x -= 1
        if ((n // x) == (n % x)):
            count += 1
    print(count)


if __name__ == '__main__':
    N = 1000000000000
    countDivisors(N)

# Expected output:
84
# smallest indexed array element required to be flipped to make sum of array equal to 0 | function to find the smallest indexed array element required to be flipped to make sum of the given array equal to 0 ; stores the required index ; traverse the given array ; flip the sign of current element ; stores the sum of array elements ; find the sum of the array ; update sum ; if sum is equal to 0 ; update pos ; reset the current element to its original value ; reset the value of arr [ i ] ; 
def smallestIndexArrayElementsFlip(arr, N):
    pos = -1
    for i in range(N):
        arr[i] *= -1
        sum = 0
        for j in range(N):
            sum += arr[j]
        if (sum == 0):
            pos = i
            break
        else:
            arr[i] *= -1
    return pos


if __name__ == '__main__':
    arr = [1, 3, -5, 3, 4]
    N = len(arr)
    print(smallestIndexArrayElementsFlip(arr, N))

# Expected output:
1
# smallest indexed array element required to be flipped to make sum of array equal to 0 | function to find the smallest indexed array element required to be flipped to make sum of the given array equal to 0 ; stores the required index ; stores the sum of the array ; traverse the given array ; update arrsum ; traverse the given array ; if sum of array elements double the value of the current element ; update pos ; 
def smallestIndexArrayElementsFlip(arr, N):
    pos = -1
    ArrSum = 0
    for i in range(N):
        ArrSum += arr[i]
    for i in range(N):
        if (2 * arr[i] == ArrSum):
            pos = i
            break
    return pos


arr = [1, 3, -5, 3, 4]
N = len(arr)
print(smallestIndexArrayElementsFlip(arr, N))

# Expected output:
1
# check if a string can be converted to another given string by removal of a substring | function to check if s can be converted to t by removing at most one substring from s ; check if s can be converted to t by removing at most one substring from s ; stores length of string t ; stores length of string s ; iterate over the range [ 0 , m - 1 ] ; stores length of the substring s [ 0 ] , ... , s [ i ] ; stores length of the substring s [ 0 ] , ... , s [ i ] ; stores prefix substring ; stores suffix substring ; checking if prefix + suffix == t ; 
def make_string_S_to_T(S, T):
    possible = False
    M = len(T)
    N = len(S)
    for i in range(0, M + 1):
        prefix_length = i
        suffix_length = M - i
        prefix = S[:prefix_length]
        suffix = S[N - suffix_length:N]
        if (prefix + suffix == T):
            possible = True
            break
    if (possible):
        return "YES"
    else:
        return "NO"


S = "ababcdcd"
T = "abcd"
print(make_string_S_to_T(S, T))

# Expected output:
YES
# sum of maximum of all subarrays by adding even frequent maximum twice | function to calculate sum of maximum of all subarrays ; stores the sum of maximums ; traverse the array ; store the frequency of the maximum element in subarray ; finding maximum ; increment frequency by 1 ; if new maximum is obtained ; if frequency of maximum is even , then add 2 * maxnumber . otherwise , add maxnumber ; print the sum obtained ; 
def findSum(a):
    ans = 0
    for low in range(0, len(a)):
        for high in range(low, len(a)):
            count = 0
            maxNumber = 0
            for i in range(low, high + 1):
                if (a[i] == maxNumber):
                    count += 1
                elif (a[i] > maxNumber):
                    maxNumber = a[i]
                    count = 1
            if count % 2:
                ans += maxNumber
            else:
                ans += maxNumber * 2
    print(ans)


arr = [2, 1, 4, 4, 2]
findSum(arr)

# Expected output:
75
# count pairs of indices having equal prefix and suffix sums | function to find the count of index pairs having equal prefix and suffix sums ; maps indices with prefix sums ; traverse the array ; update prefix sum ; update frequency in map ; traverse the array in reverse ; update suffix sum ; check if any prefix sum of equal value exists or not ; print the obtained count ; 
def countPairs(arr, n):
    mp1 = {}
    sum = 0
    for i in range(n):
        sum += arr[i]
        mp1[sum] = mp1 .get(sum, 0) + 1
    sum = 0
    ans = 0
    for i in range(n - 1, -1, -1):
        sum += arr[i]
        if (sum in mp1):
            ans += mp1[sum]
    print(ans)


if __name__ == '__main__':
    arr = [1, 2, 1, 1]
    n = len(arr)
    countPairs(arr, n)

# Expected output:
3
# queries to count composite magic numbers from a given range [ l , r ] | check if a number is magic number or not ; check number is composite number or not ; corner cases ; check if the number is a multiple of 2 or 3 ; check for multiples of remaining primes ; function to find composite magic numbers in given ranges ; dp [ i ] : stores the count of composite magic numbers up to i ; traverse in the range [ 1 , 1e5 ) ; check if number is composite number as well as a magic number or not ; print results for each query ; 
def isMagic(num):
    return (num % 9 == 1)


def isComposite(n):
    if (n <= 1):
        return False
    if (n <= 3):
        return False
    if (n % 2 == 0 or n % 3 == 0):
        return True
    for i in range(5, n + 1, 6):
        if i * i > n + 1:
            break
        if (n % i == 0 or n % (i + 2) == 0):
            return True
    return False


def find(L, R, q):
    dp = [0] * 1000005
    dp[0] = 0
    dp[1] = 0
    for i in range(1, 1000005):
        if (isComposite(i) and isMagic(i)):
            dp[i] = dp[i - 1] + 1
        else:
            dp[i] = dp[i - 1]
    for i in range(q):
        print(dp[R[i]] - dp[L[i] - 1])


if __name__ == '__main__':
    L = [10, 3]
    R = [100, 2279]
    Q = 2
    find(L, R, Q)

# Expected output:
8
198
# smallest positive integer that divides all array elements to generate quotients with sum not exceeding k | function to find the smallest positive integer that divides array elements to get the sum <= k ; stores minimum possible of the smallest positive integer satisfying the condition ; stores maximum possible of the smallest positive integer satisfying the condition ; apply binary search over the range [ left , right ] ; stores middle element of left and right ; stores sum of array elements ; traverse the array ; update sum ; if sum is greater than k ; update left ; update right ; 
def findSmallestInteger(arr, N, K):
    left = 1
    right = max(arr)
    while (left < right):
        mid = (left + right) // 2
        sum = 0
        for i in range(N):
            sum += (arr[i] + mid - 1) // mid
        if (sum > K):
            left = mid + 1
        else:
            right = mid
    return left


if __name__ == '__main__':
    arr = [1, 2, 5, 9]
    N = len(arr)
    K = 6
    print(findSmallestInteger(arr, N, K))

# Expected output:
5
# minimum deci | function to find the count of minimum deci - binary numbers required to obtain s ; stores the minimum count ; iterate over the string s ; convert the char to its equivalent integer ; if current character is the maximum so far ; update the maximum digit ; prthe required result ; 
def minimum_deci_binary_number(s):
    m = -10 ** 19
    for i in range(len(s)):
        temp = ord(s[i]) - ord('0')
        if (temp > m):
            m = temp
    return m


if __name__ == '__main__':
    S = "31"
    print(minimum_deci_binary_number(S))

# Expected output:
3
# make all the elements of array odd by incrementing odd | function to count minimum subarrays whose odd - indexed elements need to be incremented to make array odd ; stores the minimum number of operations required ; iterate over even - indices ; check if the current element is odd ; if true , continue ; otherwise , mark the starting of the subarray and iterate until i < n and arr [ i ] is even ; increment number of operations ; iterate over odd indexed positions of arr [ ] ; check if the current element is odd ; if true , continue ; otherwise , mark the starting of the subarray and iterate until i < n and arr [ i ] is even ; increment the number of operations ; print the number of operations ; 
def minOperations(arr, n):
    flips = 0
    i = 0
    while i < n:
        if (arr[i] % 2 == 1):
            i += 2
            continue
        while (i < n and arr[i] % 2 == 0):
            i += 2
        flips += 1
        i += 2
    i = 1
    while i < n:
        if (arr[i] % 2 == 1):
            i += 2
            continue
        while (i < n and arr[i] % 2 == 0):
            i += 2
        flips += 1
        i += 2
    print(flips)


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

# Expected output:
2