giulio98/xlcost-single-prompt huggingface dataset:
# cost of creating smallest subsequence with sum of difference between adjacent elements maximum |  ; initializing cost = 0 ; to store the removed element ; this will store the sum of the subsequence ; checking all the element of the vector ; storing the value of arr [ i ] in temp variable ; if the situation like arr [ i - 1 ] < arr [ i ] < arr [ i + 1 ] or arr [ i - 1 ] > arr [ i ] > arr [ i + 1 ] occur remove arr [ i ] i . e , temp from sequence ; insert the element in the set removedelements ; storing the value of arr [ i ] in temp ; taking the element not in removedelements ; adding the value of elements of subsequence ; if we have to remove the element then we need to add the cost associated with the element ; printing the sum of the subsequence with minimum length possible ; printing the cost incurred in creating subsequence ; 
def costOfSubsequence(N, arr, costArray):
    i, temp, cost = 0, 0, 0
    removedElements = {''}
    ans = 0
    for i in range(1, N - 1):
        temp = arr[i]
        if (((arr[i - 1] < temp) and (temp < arr[i + 1]))
                or ((arr[i - 1] > temp) and (temp > arr[i + 1]))):
            removedElements .add(temp)
    for i in range(0, N):
        temp = arr[i]
        if (temp not in removedElements):
            ans = ans + arr[i]
        else:
            cost += costArray[i]
    print(ans, end=", ")
    print(cost)


N = 4
arr = [1, 3, 4, 2]
costArray = [0, 1, 0, 0]
costOfSubsequence(N, arr, costArray)

# Expected output:
7, 1
# xor of a subarray ( range of elements ) | set 2 | function to find xor in a range from l to r ; compute xor from arr [ 0 ] to arr [ i ] ; process every query in constant time ; if l == 0 ; 
def find_Xor(arr, query, N, Q):
    for i in range(1, N):
        arr[i] = arr[i] ^ arr[i - 1]
    ans = 0
    for i in range(Q):
        if query[i][0] == 0:
            ans = arr[query[i][1]]
        else:
            ans = (arr[query[i][0] - 1] ^ arr[query[i][1]])
        print(ans)


def main():
    arr = [3, 2, 4, 5, 1, 1, 5, 3]
    N = 8
    Q = 2
    query = [[1, 4], [3, 7]]
    find_Xor(arr, query, N, Q)


main()

# Expected output:
2
3
# generate an alternate increasing and decreasing array | function that returns generated array ; dynamically allocate array ; start , end = 0 , n ; iterate over array ; if str [ i ] = = ' i ' assign arr [ i ] as start and increment start ; if str [ i ] = = ' d ' assign arr [ i ] as end and decrement end ; assign a [ n ] as start ; return starting address of array a ; 
def DiStirngMatch(Str):
    N = len(Str)
    arr = (N + 1) * [0]
    START, END = 0, N
    for i in range(N):
        if (Str[i] == 'I'):
            arr[i] = START
            START += 1
        if (Str[i] == 'D'):
            arr[i] = END
            END -= 1
    arr[N] = START
    return arr


if __name__ == "__main__":
    Str = "IDID"
    N = len(Str)
    ptr = DiStirngMatch(Str)
    for i in range(N + 1):
        print(ptr[i], end=" ")

# Expected output:
0 4 1 3 2
# generate array whose difference of each element with its left yields the given array | function to find the sequence ; initializing 1 st element ; creating sequence in terms of x ; finding min element ; finding value of x ; creating original sequence ; output original sequence ; 
def find_seq(arr, m, n):
    b = []
    x = 0
    b .append(x)
    for i in range(n - 1):
        b .append(x + arr[i] + b[i])
    mn = n
    for i in range(n):
        mn = min(mn, b[i])
    x = 1 - mn
    for i in range(n):
        b[i] += x
    for i in range(n):
        print(b[i], end=' ')
    print()


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

# Expected output:
3 1 2
# check whether count of odd and even factors of a number are equal | python3 code for the naive approach ; function to check condition ; initialize even_count and od_count ; loop runs till square root ; 

import math


def isEqualFactors(N):
    ev_count = 0
    od_count = 0
    for i in range(1, (int)(math .sqrt(N)) + 2):
        if (N % i == 0):
            if (i == N // i):
                if (i % 2 == 0):
                    ev_count += 1
                else:
                    od_count += 1
            else:
                if (i % 2 == 0):
                    ev_count += 1
                else:
                    od_count += 1
                if ((N // i) % 2 == 0):
                    ev_count += 1
                else:
                    od_count += 1
    if (ev_count == od_count):
        print("YES")
    else:
        print("NO")


N = 10
isEqualFactors(N)

# Expected output:
YES
# check whether count of odd and even factors of a number are equal | function to check condition ; 
def isEqualFactors(N):
    if ((N % 2 == 0) and (N % 4 != 0)):
        print("YES")
    else:
        print("NO")


N = 10
isEqualFactors(N)
N = 125
isEqualFactors(N)

# Expected output:
YES
NO
# count of all values of n in [ l , r ] such that count of primes upto n is also prime | function to count the number of crazy primes in the given range [ l , r ] ; stores all primes ; stores count of primes ; stores if frequency of primes is a prime or not upto each index ; sieve of eratosthenes ; count primes ; if i is a prime ; stores frequency of primes ; if the frequency of primes is a prime ; increase count of required numbers ; return the required count ; 
def count_crazy_primes(L, R):
    prime = [0] * (R + 1)
    countPrime = [0] * (R + 1)
    freqPrime = [0] * (R + 1)
    prime[0] = prime[1] = 1
    p = 2
    while p * p <= R:
        if (prime[p] == 0):
            for i in range(p * p, R + 1, p):
                prime[i] = 1
        p += 1
    for i in range(1, R + 1):
        countPrime[i] = countPrime[i - 1]
        if (not prime[i]):
            countPrime[i] += 1
    for i in range(1, R + 1):
        freqPrime[i] = freqPrime[i - 1]
        if (not prime[countPrime[i]]):
            freqPrime[i] += 1
    return (freqPrime[R] - freqPrime[L - 1])


if __name__ == "__main__":
    L = 4
    R = 12
    print(count_crazy_primes(L, R))

# Expected output:
5
# largest n digit number in base b | function to print the largest n - digit numbers of base b ; find the largest n digit number in base b using the formula b ^ n - 1 ; print the largest number ; 
def findNumbers(n, b):
    largest = pow(b, n) - 1
    print(largest)


N, B = 2, 5
findNumbers(N, B)

# Expected output:
24
# generate an array such with elements maximized through swapping bits | function to generate the maximized array elements ; traverse the array ; iterate to count set and unset bits ; count of unset bits ; count of set bits ; bitwise right shift ; shifting all 1 ' s ▁ to ▁ msb ▁ ▁ and ▁ 0' s to lsb ; 
def maximizedArray(arr, N):
    i = 0
    while (N > 0):
        num = arr[i]
        one = 0
        zero = 0
        while (num):
            if (num % 2 == 0):
                zero += 1
            else:
                one += 1
            num = num >> 1
        for j in range(zero, (one + zero)):
            num += (1 << j)
        print(num, end="")
        i += 1
        if (N > 0):
            print(", ", end="")
        N -= 1


if __name__ == "__main__":
    arr = [8, 15, 9, 10, 14]
    N = len(arr)
    maximizedArray(arr, N)

# Expected output:
8, 15, 12, 12, 14,
# find the ratio of lcm to gcd of a given array | python3 program to implement above approach ; function to calculate and return gcd of the given array ; initialise gcd ; once gcd is 1 , it will always be 1 with all other elements ; return gcd ; function to calculate and return lcm of the given array ; initialise lcm ; lcm of two numbers is evaluated as [ ( a * b ) / gcd ( a , b ) ] ; return lcm ; function to print the ratio of lcm to gcd of the given array ; 

import math


def findGCD(arr, n):
    gcd = arr[0]
    for i in range(1, n):
        gcd = int(math .gcd(arr[i], gcd))
        if (gcd == 1):
            return 1
    return gcd


def findLCM(arr, n):
    lcm = arr[0]
    for i in range(1, n):
        lcm = int((((arr[i] * lcm)) / (math .gcd(arr[i], lcm))))
    return lcm


def findRatio(arr, n):
    gcd = findGCD(arr, n)
    lcm = findLCM(arr, n)
    print(int(lcm / gcd), ":", "1")


arr = [6, 12, 36]
N = len(arr)
findRatio(arr, N)

# Expected output:
6 : 1
# minimum operations required to make all array elements divisible by k | python3 implementation to find the minimum number of moves required to update the array such that each of its element is divisible by k ; function to find the minimum number of moves required to update the array such that each of its element is divisible by k ; initialize map data structure ; iterate for all the elements of the array ; calculate the value to be added ; check if the value equals to 0 then simply continue ; check if the value to be added is present in the map ; update the answer ; print the required result we need to add 1 to maxx because we cant ignore the first move where initially x = 0 and we need to increase it by 1 to make some changes in array ; 

from collections import defaultdict


def compute(a, N, K):
    eqVal = defaultdict(int)
    maxX = 0
    for i in range(N):
        val = a[i] % K
        if (val != 0):
            val = K - val
        if (val == 0):
            continue
        if (val in eqVal):
            numVal = eqVal[val]
            maxX = max(maxX, val + (K * numVal))
            eqVal[val] += 1
        else:
            eqVal[val] += 1
            maxX = max(maxX, val)
    if maxX == 0:
        print(0)
    else:
        print(maxX + 1)


if __name__ == "__main__":
    K = 3
    a = [1, 2, 2, 18]
    N = len(a)
    compute(a, N, K)

# Expected output:
5
# array sum after replacing all occurrences of x by y for q queries | function that print the sum of the array for q queries ; stores the frequencies of array elements ; calculate the sum of the initial array and store the frequency of each element in map ; iterate for all the queries ; store query values ; decrement the sum accordingly ; increment the sum accordingly ; set count of y [ i ] ; reset count of x [ i ] ; print the sum ; 
def sumOfTheArrayForQuery(A, N, X, Y, Q):
    sum = 0
    count = {}
    for i in range(N):
        sum += A[i]
        if A[i] in count:
            count[A[i]] += 1
        else:
            count[A[i]] = 1
    for i in range(Q):
        x = X[i]
        y = Y[i]
        if X[i]not in count:
            count[X[i]] = 0
        if Y[i]not in count:
            count[Y[i]] = 0
        sum -= (count[X[i]] * X[i])
        sum += count[X[i]] * Y[i]
        count[Y[i]] += count[X[i]]
        count[X[i]] = 0
        print(sum, end=" ")


arr = [1, 2, 1, 3, 2, ]
X = [2, 3, 5]
Y = [3, 1, 2]
N = len(arr)
Q = len(X)
sumOfTheArrayForQuery(arr, N, X, Y, Q)

# Expected output:
11 5 5
# maximum or value of a pair in an array | set 2 | function to return the maximum bitwise or for any pair of the given array in o ( n ) time complexity . ; find the maximum element in ; stores the maximum or value the array ; traverse the array and perform bitwise or between every array element with the maximum element ; 
def maxOR(arr, n):
    max_value = max(arr)
    ans = 0
    for i in range(n):
        ans = max(ans, (max_value & arr[i]))
    return ans


if __name__ == "__main__":
    arr = [3, 6, 8, 16]
    n = len(arr)
    print(maxOR(arr, n))

# Expected output:
16
# count of elements on the left which are divisible by current element | set 2 | utility function to prthe elements of the array ; function to increment the count for each factor of given val ; function to generate and print the required array ; find max element of arr ; create count array of maxi size ; for every element of the array ; count [ a [ i ] ] denotes how many previous elements are there whose factor is the current element . ; increment in count array for factors of a [ i ] ; print the generated array ; 
def printArr(arr, n):
    for i in range(n):
        print(arr[i], end=" ")


def IncrementFactors(count, val):
    i = 1
    while (i * i <= val):
        if (val % i == 0):
            if (i == val // i):
                count[i] += 1
            else:
                count[i] += 1
                count[val // i] += 1
        i += 1


def generateArr(A, n):
    B = [0] * n
    maxi = max(A)
    count = [0] * (maxi + 1)
    for i in range(n):
        B[i] = count[A[i]]
        IncrementFactors(count, A[i])
    printArr(B, n)


arr = [8, 1, 28, 4, 2, 6, 7]
n = len(arr)
generateArr(arr, n)

# Expected output:
0 1 0 2 3 0 1
# minimum changes required to make all array elements prime | function to generate all primes ; if p is a prime ; mark all its multiples as non - prime ; store all prime numbers ; return the list of primes ; function to calculate the minimum increments to convert every array elements to a prime ; extract maximum element of the given array ; extract the index which has the next greater prime ; store the difference between the prime and the array element ; 
def SieveOfEratosthenes(n):
    prime = [True for i in range(2 * n + 1)]
    p = 2
    while (p * p <= 2 * n):
        if (prime[p]):
            i = p * p
            while (i <= n * 2):
                prime[i] = False
                i += p
        p += 1
    primes = []
    for p in range(2, (2 * n) + 1):
        if (prime[p]):
            primes .append(p)
    return primes


def minChanges(arr):
    n = len(arr)
    ans = 0
    maxi = max(arr)
    primes = SieveOfEratosthenes(maxi)
    for i in range(n):
        x = -1
        for j in range(len(primes)):
            if (arr[i] == primes[j]):
                x = j
                break
            elif (arr[i] < primes[j]):
                x = j
                break
        minm = abs(primes[x] - arr[i])
        if (x > 1):
            minm = min(minm, abs(primes[x - 1] - arr[i]))
        ans += minm
    return ans


arr = [4, 25, 13, 6, 20]
print(minChanges(arr))

# Expected output:
5
# sum of multiples of array elements within a given range [ l , r ] | function to find the sum of all multiples of n up to k ; calculate the sum ; return the sum ; function to find the total sum ; if l is divisible by a [ i ] ; otherwise ; return the final sum ; 
def calcSum(k, n):
    value = (k * n * (n + 1)) // 2
    return value


def findSum(a, n, L, R):
    sum = 0
    for i in range(n):
        if (L % a[i] == 0 and L != 0):
            sum += (calcSum(a[i], R // a[i]) - calcSum(a[i], (L - 1) // a[i]))
        else:
            sum += (calcSum(a[i], R // a[i]) - calcSum(a[i], L // a[i]))
    return sum


if __name__ == "__main__":
    arr = [2, 7, 3, 8]
    N = len(arr)
    L = 7
    R = 20
    print(findSum(arr, N, L, R))

# Expected output:
197
# count all prime numbers in a given range whose sum of digits is also prime | python3 program for the above approach ; create an array for storing primes ; create a prefix array that will contain whether sum is prime or not ; function to find primes in the range and check whether the sum of digits of a prime number is prime or not ; initialise prime array arr [ ] ; since 0 and 1 are not prime numbers we mark them as '0 ; using sieve of eratosthenes ; if the number is prime ; mark all the multiples of i starting from square of i with '0' ie . composite ; '0' represents not prime ; initialise a sum variable as 0 ; check if the number is prime ; a temporary variable to store the number ; loop to calculate the sum of digits ; check if the sum of prime number is prime ; if prime mark 1 ; if not prime mark 0 ; computing prefix array ; function to count the prime numbers in the range [ l , r ] ; function call to find primes ; print the result ; 

maxN = 1000000
arr = [0] * (1000001)
prefix = [0] * (1000001)


def findPrimes():
    for i in range(1, maxN + 1):
        arr[i] = 1
    arr[0] = 0
    arr[1] = 0
    i = 2
    while i * i <= maxN:
        if (arr[i] == 1):
            for j in range(i * i, maxN, i):
                arr[j] = 0

        i += 1
    sum = 0
    prefix[0] = 0

    for i in range(1, maxN + 1):
        if (arr[i] == 1):
            temp = i
            sum = 0
            while (temp > 0):
                x = temp % 10
                sum += x
                temp = temp // 10
                if (arr[sum] == 1):
                    prefix[i] = 1

                else:
                    prefix[i] = 0
    for i in range(1, maxN + 1):
        prefix[i] += prefix[i - 1]


def countNumbersInRange(l, r):
    findPrimes()
    result = (prefix[r] - prefix[l - 1])
    print(result)


if __name__ == "__main__":
    l = 5
    r = 20
    countNumbersInRange(l, r)

# Expected output:
3
# find the concentration of a solution using given mass and volume | function to calculate concentration from the given mass of solute and volume of a solution ; 
def get_concentration(mass, volume):
    if (volume == 0):
        return -1
    else:
        return (mass / volume) * 1000


mass = 100.00
volume = 500.00
print(get_concentration(mass, volume))

# Expected output:
200.0
# practical numbers | python3 program to check if a number is practical or not . ; returns true if there is a subset of set [ ] with sun equal to given sum ; the value of subset [ i ] [ j ] will be true if there is a subset of set [ 0. . j - 1 ] with sum equal to i ; if sum is 0 , then answer is true ; if sum is not 0 and set is empty , then answer is false ; fill the subset table in bottom up manner ; function to store divisors of n in a vector ; find all divisors which divides 'num ; if ' i ' is divisor of 'n ; if both divisors are same then store it once else store both divisors ; returns true if num is practical ; vector to store all divisors of n ; to check all numbers from 1 to < n ; 

import math


def isSubsetSum(Set, n, Sum):
    subset = [[False for i in range(Sum + 1)]
              for j in range(n + 1)]
    for i in range(n + 1):
        subset[i][0] = True
    for i in range(1, Sum + 1):
        subset[0][i] = False
    for i in range(1, n + 1):
        for j in range(1, Sum + 1):
            if (j < Set[i - 1]):
                subset[i][j] = subset[i - 1][j]
            if (j >= Set[i - 1]):
                subset[i][j] = (subset[i - 1][j] or
                                subset[i - 1][j - Set[i - 1]])

    return subset[n][Sum]


def storeDivisors(n, div):
    for i in range(1, int(math.sqrt(n)) + 1):
        if (n % i == 0):
            if (i == int(n / i)):
                div.append(i)
            else:
                div.append(i)
                div.append(int(n / i))


def isPractical(N):
    div = []
    storeDivisors(N, div)
    for i in range(1, N):
        if (not isSubsetSum(div, len(div), i)):
            return False

    return True


N = 18

if (isPractical(N)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# super niven numbers | checks if sums of all subsets of digits array divides the number n ; to calculate length of array arr ; there are totoal 2 ^ n subsets ; consider all numbers from 0 to 2 ^ n - 1 ; consider binary representation of current i to decide which elements to pick . ; check sum of picked elements . ; function to check if a number is a super - niven number ; to store digits of n ; 
def isDivBySubsetSums(arr, num):
    n = len(arr)
    total = 1 << n
    i = 0
    while i < total:
        sum = 0
        j = 0
        while j < n:
            if (i & (1 << j)):
                sum += arr[j]
            j += 1
        if (sum != 0) and (num % sum != 0):
            return False
        i += 1
    return True


def isSuperNivenNum(n):
    temp = n
    digits = []
    while (n > 1):
        digit = int(n) % 10
        digits .append(digit)
        n = n / 10
    return isDivBySubsetSums(digits, temp)


if __name__ == '__main__':
    n = 500
    if isSuperNivenNum(n):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# highly composite numbers | function to count the number of divisors of the n ; sieve method for prime calculation ; traversing through all prime numbers ; calculate number of divisor with formula total div = ( p1 + 1 ) * ( p2 + 1 ) * ... . . * ( pn + 1 ) where n = ( a1 ^ p1 ) * ( a2 ^ p2 ) . ... * ( an ^ pn ) ai being prime divisor for n and pi are their respective power in factorization ; function to check if a number is a highly composite number ; count number of factors of n ; loop to count number of factors of every number less than n ; if any number less than n has more factors than n , then return false ; 
def divCount(n):
    Hash = [True for i in range(n + 1)]
    p = 2
    while ((p * p) < n):
        if bool(Hash[p]):
            i = p * 2
            while i < n:
                Hash[i] = False
                i += p
        p += 1
    total = 1
    for P in range(2, n + 1):
        if (bool(Hash[P])):
            count = 0
            if (n % P == 0):
                while (n % P == 0):
                    n = n // P
                    count += 1
                total = total * (count + 1)
    return total


def isHighlyCompositeNumber(N):
    NdivCount = divCount(N)
    for i in range(N):
        idivCount = divCount(i)
        if (idivCount >= NdivCount):
            return bool(False)
    return bool(True)


N = 12
if (bool(isHighlyCompositeNumber(N))):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# pentacontahenagon number | function to find the n - th pentacontahenagon number ; 
def PentacontahenagonNum(N):
    return (49 * N * N - 47 * N) // 2


N = 3
print("3rd Pentacontahenagon Number is", PentacontahenagonNum(N))

# Expected output:
3rd Pentacontahenagon Number is 150
# saint | function to check if a number is a saint - exupery number ; considering triplets in sorted order . the value of first element in sorted triplet can be at - most n / 3. ; the value of second element must be less than equal to n / 2 ; 
def isSaintExuperyNum(n):
    for i in range(1, (n // 3) + 1):
        for j in range(i + 1, (n // 2) + 1):
            k = n / i / j
            if i * i + j * j == k * k:
                if i * j * k == n:
                    return True
    return False


N = 60
if isSaintExuperyNum(N):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# repdigit numbers | function to check if a number is a repdigit number ; to store previous digit ( assigning initial value which is less than any digit ) ; traverse all digits from right to left and check if any digit is smaller than previous . ; 
def isRepdigit(num, b):
    prev = -1
    while (num):
        digit = num % b
        num //= b
        if (prev != -1 and digit != prev):
            return False
        prev = digit
    return True


num = 2000
base = 7
if (isRepdigit(num, base)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# enlightened numbers | python3 implementation of the above approach ; function to check if n is a composite number ; corner cases ; this is checked so that we can skip middle five numbers in below loop ; function to return concatenation of distinct prime factors of a given number n ; handle prime factor 2 explicitly so that can optimally handle other prime factors . ; n must be odd at this point . so we can skip one element ( note i = i + 2 ) ; while i divides n , print i and divide n ; this condition is to handle the case when n is a prime number greater than 2 ; function to check if a number is is an enlightened number ; number should not be prime ; converting n to string ; function call ; 

import math


def isComposite(n):
    if n <= 3:
        return False
    if (n % 2 == 0 or n % 3 == 0):
        return True
    i = 5
    while (i * i <= n):
        if (n % i == 0 or n % (i + 2) == 0):
            return True
        i = i + 6
    return False


def concatenatePrimeFactors(n):
    concatenate = ""
    if (n % 2 == 0):
        concatenate += "2"
        while (n % 2 == 0):
            n = int(n / 2)
    i = 3
    while (i <= int(math .sqrt(n))):
        if (n % i == 0):
            concatenate += str(i)
            while (n % i == 0):
                n = int(n / i)
        i = i + 2
    if (n > 2):
        concatenate += str(n)
    return concatenate


def isEnlightened(N):
    if (not isComposite(N)):
        return False
    num = str(N)
    prefixConc = concatenatePrimeFactors(N)
    return int(prefixConc)


if __name__ == "__main__":
    n = 250
    if (isEnlightened(n)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes