giulio98/xlcost-single-prompt huggingface dataset:
# length of longest subsequence consisting of non | python3 program for the above approach ; function to check if n is a deficient number or not ; stores sum of divisors ; iterate over the range [ 1 , sqrt ( n ) ] ; if n is divisible by i ; if divisors are equal , add only one of them ; otherwise add both ; function to print the longest subsequence which does not contain any deficient numbers ; stores the count of array elements which are non - deficient ; traverse the array ; if element is non - deficient ; return the answer ; 

import math


def isNonDeficient(n):
    sum = 0
    for i in range(1, int(math .sqrt(n)) + 1):
        if (n % i == 0):
            if (n // i == i):
                sum = sum + i
            else:
                sum = sum + i
                sum = sum + (n // i)
    return sum >= 2 * n


def LongestNonDeficientSubsequence(arr, n):
    res = 0
    for i in range(n):
        if (isNonDeficient(arr[i])):
            res += 1
    return res


if __name__ == "__main__":
    arr = [13, 55, 240, 32, 24, 27, 56, 80, 100, 330, 89]
    N = len(arr)
    print(LongestNonDeficientSubsequence(arr, N))

# Expected output:
6
# number of subarrays consisting only of pronic numbers | function to check if a number is pronic number or not ; iterate over the range [ 1 , sqrt ( n ) ] ; return true if n is pronic ; otherwise , return false ; function to count the number of subarrays consisting of pronic numbers ; stores the count of subarrays ; stores the number of consecutive array elements which are pronic ; traverse the array ; if i is pronic ; return the total count ; 
def isPronic(n):
    for i in range(int(n ** (1 / 2)) + 1):
        if i * (i + 1) == n:
            return True
    return False


def countSub(arr):
    ans = 0
    ispro = 0
    for i in arr:
        if isPronic(i):
            ispro += 1
        else:
            ispro = 0
        ans += ispro
    return ans


arr = [5, 6, 12, 3, 4]
print(countSub(arr))

# Expected output:
3
# replace all array elements with the nearest power of its previous element | python3 program for the above approach ; function to calculate log x for given base ; function to replace all array elements with the nearest power of previous adjacent nelement ; for the first element , set the last array element to its previous element ; traverse the array ; find k for which x ^ k is nearest to arr [ i ] ; find the power to x nearest to arr [ i ] ; update x ; return the array ; 

import math


def LOG(x, base):
    return int(math .log(x) / math .log(base))


def repbyNP(arr):
    x = arr[-1]
    for i in range(len(arr)):
        k = LOG(arr[i], x)
        temp = arr[i]
        if abs(x ** k - arr[i]) < abs(x ** (k + 1) - arr[i]):
            arr[i] = x ** k
        else:
            arr[i] = x ** (k + 1)
        x = temp
    return arr


arr = [2, 4, 6, 3, 11]
print(repbyNP(arr))

# Expected output:
[1, 4, 4, 1, 9]
# remaining array element after repeated removal of last element and subtraction of each element from next adjacent element | function to find the last remaining array element after performing the given operations repeatedly ; stores the resultant sum ; traverse the array ; increment sum by arr [ i ] * coefficient of i - th term in ( x - y ) ^ ( n - 1 ) ; update multiplier ; return the resultant sum ; 
def lastElement(arr, n):
    sum = 0
    if n % 2 == 0:
        multiplier = -1
    else:
        multiplier = 1
    for i in range(n):
        sum += arr[i] * multiplier
        multiplier = multiplier * (n - 1 - i) / (i + 1) * (-1)
    return sum


if __name__ == '__main__':
    arr = [3, 4, 2, 1]
    N = len(arr)
    print(int(lastElement(arr, N)))

# Expected output:
4
# maximize count of pairs whose bitwise xor is even by replacing such pairs with their bitwise xor | function to maximize the count of pairs with even xor possible in an array by given operations ; stores count of odd array elements ; traverse the array ; if arr [ i ] is odd ; stores the total number of even pairs ; 
def countPairs(arr, N):
    odd = 0
    for i in range(N):
        if (arr[i] & 1):
            odd += 1
    ans = (N - odd + odd // 2 - 1) + odd // 2
    return ans


if __name__ == '__main__':
    arr = [4, 6, 1, 3]
    N = len(arr)
    print(countPairs(arr, N))

# Expected output:
3
# maximize count of pairs whose bitwise and exceeds bitwise xor by replacing such pairs with their bitwise and | python3 program for the above approach ; function to count the number of pairs whose bitwise and is greater than the bitwise xor ; stores the frequency of msb of array elements ; traverse the array ; increment count of numbers having msb at log ( arr [ i ] ) ; stores total number of pairs ; traverse the map ; return total count of pairs ; 

from math import log2


def countPairs(arr, N):
    freq = {}
    for i in range(N):
        x = int(log2(arr[i]))
        freq[x] = freq .get(x, 0) + 1
    pairs = 0
    for i in freq:
        pairs += freq[i] - 1
    return pairs


if __name__ == '__main__':
    arr = [12, 9, 15, 7]
    N = len(arr)
    print(countPairs(arr, N))

# Expected output:
2
# maximize difference between odd and even indexed array elements by swapping unequal adjacent bits in their binary representations | python program for the above approach ; function to count total number of bits present in a number ; function to count total set bits in a number ; stores the count of set bits ; right shift by 1 ; return the final count ; function to find maximum number by shifting two unequal bits ; count set bits in number n ; iterate the string bits ; function to find minimum number by shifting two unequal bits ; iterate the set bit ; function to find the maximum difference ; stores the maximum difference ; stores the sum of elements placed at odd positions ; stores the sum of elements placed at even positions ; traverse the array ; update caseone ; stores the maximum difference ; assign value o ; traverse the array ; update casetwo ; return maximum of caseone and casetwo ; 

import math


def countBit(n):
    return int(math .log(n, 2)) + 1


def countSetBit(n):
    ans = 0
    while n:
        ans += n & 1
        n >>= 1
    return ans


def maximize(n):
    bits = countBit(n)
    setBits = countSetBit(n)
    ans = 0
    for i in range(bits):
        if i < setBits:
            ans |= 1
        if i != setBits - 1:
            ans <<= 1
    return ans


def minimize(n):
    setBits = countSetBit(n)
    ans = 0
    for i in range(setBits):
        ans |= 1
        if i != setBits - 1:
            ans <<= 1
    return ans


def maxDiff(arr):
    caseOne = 0
    SumOfOdd = 0
    SumOfeven = 0
    for i in range(len(arr)):
        if i % 2:
            SumOfOdd += minimize(arr[i])
        else:
            SumOfeven += maximize(arr[i])
    caseOne = abs(SumOfOdd - SumOfeven)
    caseTwo = 0
    SumOfOdd = 0
    SumOfeven = 0
    for i in range(len(arr)):
        if i % 2:
            SumOfOdd += maximize(arr[i])
        else:
            SumOfeven += minimize(arr[i])
    caseTwo = abs(SumOfOdd - SumOfeven)
    return max(caseOne, caseTwo)


arr = [54, 32, 11, 23]
print(maxDiff(arr))

# Expected output:
58
# sum of numbers obtained by the count of set and non | functino to find the number after processing the diagonal elements ; store the required number ; checking for each position ; store the number of set bits & non - set bits at position i ; traverse the diagonal elements ; update count of s if current element is set at position i ; else update ns ; if number of set bits is > number of non - set bits , add set bits value to the ans ; return the answer ; function to find the sum of the numbers generated after processing both the diagonals of the matrix ; store the primary diagonal elements ; store the secondary diagonal elements ; function call to get the required numbers and return their sum ; 
def processDiagonal(arr):
    ans = 0

    getBit = 1

    for i in range(32):
        S = 0
        NS = 0
        for j in arr:
            if getBit & j:
                S += 1
            else:
                NS += 1
        if S > NS:
            ans += 2**i
        getBit <<= 1
    return ans


def findSum(mat):
    i = 0
    j = 0
    priDiag = []

    while i < len(mat):
        priDiag.append(mat[i][j])
        i += 1
        j += 1

    i = 0
    j = len(mat)-1
    secDiag = []
    while i < len(mat):
        secDiag.append(mat[i][j])
        i += 1
        j -= 1
    return processDiagonal(priDiag) + processDiagonal(secDiag)


mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(findSum(mat))

# Expected output:
8
# minimize difference between two sequences obtained by splitting first n powers of 2 | function to partition first n powers of 2 into two subsequences with minimum difference between their sum ; largest element in the first part ; place n / 2 - 1 smallest elements in the first sequence ; place remaining n / 2 elements in the second sequence ; print the minimum difference ; 
def minimumDifference(N):
    sum1 = (1 << N)
    sum2 = 0
    for i in range(1, N // 2):
        sum1 += (1 << i)
    for i in range(N // 2, N):
        sum2 += (1 << i)
    print(sum1 - sum2)


N = 4
minimumDifference(N)

# Expected output:
6
# check whether n can be a perfect cube after adding or subtracting k | function to check if a number is a perfect cube or not ; 
def isPerfectCube(x):
    cr = int(x ** (1 / 3))
    return (cr * cr * cr == x)


def canBePerfectCube(N, K):
    if (isPerfectCube(N + K) or isPerfectCube(N - K)):
        print("Yes")
    else:
        print("No")


if __name__ == "__main__":
    N = 7
    K = 1
    canBePerfectCube(N, K)
    N = 5
    K = 4
    canBePerfectCube(N, K)
    N = 7
    K = 2
    canBePerfectCube(N, K)

# Expected output:
Yes
Yes
No
# floor square root without using sqrt ( ) function : recursive | function to find the square root of the number n using bs ; if the range is still valid ; find the mid - value of the range ; base case ; condition to check if the left search space is useless ; 
def sqrtSearch(low, high, N):
    if (low <= high):
        mid = (low + high) // 2
        if ((mid * mid <= N) and ((mid + 1) * (mid + 1) > N)):
            return mid
        elif (mid * mid < N):
            return sqrtSearch(mid + 1, high, N)
        else:
            return sqrtSearch(low, mid - 1, N)
    return low


if __name__ == "__main__":
    N = 25
    print(sqrtSearch(0, N, N))

# Expected output:
5
# common divisors of n numbers | function to calculate gcd of two numbers ; function to prall the common divisors ; variable to find the gcd of n numbers ; set to store all the common divisors ; finding gcd of the given n numbers ; finding divisors of the hcf of n numbers ; prall the divisors ; 
def gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)


def printAllDivisors(arr, N):
    g = arr[0]
    divisors = dict()
    for i in range(1, N):
        g = gcd(arr[i], g)
    for i in range(1, g + 1):
        if i * i > g:
            break
        if (g % i == 0):
            divisors[i] = 1
            if (g // i != i):
                divisors[g // i] = 1
    for it in sorted(divisors):
        print(it, end=" ")


if __name__ == '__main__':
    arr = [6, 90, 12, 18, 30, 24]
    n = len(arr)
    printAllDivisors(arr, n)

# Expected output:
1 2 3 6
# recursive program to print multiplication table of a number | function that print the table of a 
def mul_table(N, i):
    if (i > 10):
        return
    print(N, "*", i, "=", N * i)
    return mul_table(N, i + 1)


N = 8
mul_table(N, 1)

# Expected output:
8 * 1 = 8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80
# check if n and m can be made equal by increasing n by a and decreasing m by b | function to whether the numbers can be made equal or not ; check whether the numbers can reach an equal point or not ; m and n cannot be made equal by increasing m and decreasing n when m is already greater than n ; 
def checkEqualNo(m, n, a, b):
    if (m <= n):
        if ((n - m) % (a + b) == 0):
            return True
        else:
            return False
    else:
        return False


if __name__ == "__main__":
    M = 2
    N = 8
    A = 3
    B = 3
    if (checkEqualNo(M, N, A, B)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# xor and or of all n | function to check if a number is palindrome or not ; convert the num n to string ; iterate over string to check whether it is palindromic or not ; function to find xor of all n - digits palindrome number ; to store the xor and or of all palindromic number ; starting n - digit palindromic number ; ending n - digit palindromic number ; iterate over starting and ending number ; to check if i is palindromic or not ; print the xor and or of all palindromic number ; 
def ispalin(num):
    s = str(num)
    st = 0
    ed = len(s) - 1
    while (st <= ed):
        if (s[st] != s[ed]):
            return False
        st += 1
        ed -= 1
    return True


def CalculateXORandOR(n):
    CalculateXOR = 0
    CalculateOR = 0
    start = 10 ** (n - 1)
    end = (10 ** n) - 1
    for i in range(start, end + 1):
        if (ispalin(i)):
            CalculateXOR = CalculateXOR ^ i
            CalculateOR = CalculateOR | i
    print("XOR =", CalculateXOR, end=" ")
    print("OR = ", CalculateOR)


if __name__ == "__main__":
    n = 4
    CalculateXORandOR(n)

# Expected output:
XOR = 4606 OR =  16383
# Sum of all palindromic numbers lying in the range [ L , R ] for Q queries | pref [ ] array to precompute the sum of all palindromic number ; Function that return number num if num is palindromic else return 0 ; Convert num to string ; Function to precompute the sum of all palindrome numbers upto 100000 ; checkPalindrome ( ) return the number i if i is palindromic else return 0 ; Function to print the sum for each query ; Function to prsum of all palindromic numbers between [ L , R ] ; Function that pre computes the sum of all palindromic numbers ; Iterate over all Queries to print the sum ; Queries ; Function that print the the sum of all palindromic number in Range [ L , R ]

pref = [0] * 100001


def checkPalindrome(num):
    strr = str(num)
    l = 0
    r = len(strr) - 1
    while (l < r):
        if (strr[l] != strr[r]):
            return 0
        l += 1
        r -= 1
    return num


def preCompute():
    for i in range(1, 100001):
        pref[i] = pref[i - 1] + checkPalindrome(i)


def printSum(L, R):
    print(pref[R] - pref[L - 1])


def printSumPalindromic(arr, Q):
    preCompute()
    for i in range(Q):
        printSum(arr[i][0], arr[i][1])


Q = 2
arr = [[10, 13], [12, 21]]
printSumPalindromic(arr, Q)

# Expected output:
11
0
# sum of alternating sign squares of first n natural numbers | function to calculate the alternating sign sum ; variable to store the sum ; loop to iterate each number from 1 to n ; the alternating sign is put by checking if the number is even or odd ; add the square with the sign ; add the square with the sign ; 
def summation(n):
    sum = 0
    for i in range(1, n + 1):
        if (i % 2 == 1):
            sum += (i * i)
        else:
            sum -= (i * i)
    return sum


if __name__ == "__main__":
    N = 2
    print(summation(N))

# Expected output:
-3
# sum of alternating sign squares of first n natural numbers | function to calculate the alternating sign sum ; variable to store the absolute sum ; variable to store the sign ; variable to store the resultant sum ; 
def summation(n):
    abs_sum = n * (n + 1) // 2
    sign = 1 if ((n + 1) % 2 == 0)else -1
    result_sum = sign * abs_sum
    return result_sum


if __name__ == "__main__":
    N = 2
    print(summation(N))

# Expected output:
-3
# count pairs from 1 to n such that their sum is divisible by their xor | function to count pairs ; variable to store count ; generate all possible pairs such that 1 <= x < y < n ; 
def countPairs(n):
    count = 0
    for x in range(1, n):
        for y in range(x + 1, n + 1):
            if ((y + x) % (y ^ x) == 0):
                count += 1
    return count


if __name__ == "__main__":
    n = 6
    print(countPairs(n))

# Expected output:
11
# count pairs in array such that one element is power of another | python3 program to count pairs in array such that one element is power of another ; function to check if given number number y is power of x ; log function to calculate value ; compare to the result1 or result2 both are equal ; function to find pairs from array ; iterate through all pairs ; increment count if one is the power of other ; 

from math import log


def isPower(x, y):
    res1 = log(y) // log(x)
    res2 = log(y) / log(x)
    return (res1 == res2)


def countPower(arr, n):
    res = 0
    for i in range(n):
        for j in range(i + 1, n):
            if isPower(arr[i], arr[j]) or isPower(arr[j], arr[i]):
                res += 1
    return res


if __name__ == "__main__":
    a = [16, 2, 3, 9]
    n = len(a)
    print(countPower(a, n))

# Expected output:
2
# find the original array using xor values of all adjacent elements | xor of all elements from 1 to n ; function to find the array from the xor array ; take a vector to store the permutation ; xor of n natural numbers ; loop to find the xor of adjacent elements of the xor array ; loop to find the other elements of the permutation ; finding the next and next elements ; 
def xor_all_elements(n):
    if n & 3 == 0:
        return n
    elif n & 3 == 1:
        return 1
    elif n & 3 == 2:
        return n + 1
    else:
        return 0


def findArray(xorr, n):
    arr = []
    xor_all = xor_all_elements(n)
    xor_adjacent = 0
    for i in range(0, n - 1, 2):
        xor_adjacent = xor_adjacent ^ xorr[i]
    last_element = xor_all ^ xor_adjacent
    arr .append(last_element)
    for i in range(n - 2, -1, -1):
        last_element = xorr[i] ^ last_element
        arr .append(last_element)
    return arr


xorr = [7, 5, 3, 7]
n = 5
arr = findArray(xorr, n)
for i in range(n - 1, -1, -1):
    print(arr[i], end=" ")

# Expected output:
3 4 1 2 5
# sum of non | function to return a vector which consists the sum of four portions of the matrix ; iterating through the matrix ; condition for selecting all values before the second diagonal of metrics ; top portion of the matrix ; left portion of the matrix ; bottom portion of the matrix ; right portion of the matrix ; adding all the four portions into a vecto ; 
def sumOfParts(arr, N):
    sum_part1, sum_part2, sum_part3, sum_part4 = 0, 0, 0, 0
    totalsum = 0
    for i in range(N):
        for j in range(N):
            if i + j < N - 1:
                if (i < j and i != j and i + j):
                    sum_part1 += arr[i][j]
                elif i != j:
                    sum_part2 += arr[i][j]
            else:
                if i > j and i + j != N - 1:
                    sum_part3 += arr[i][j]
                else:
                    if i + j != N - 1 and i != j:
                        sum_part4 += arr[i][j]
    return sum_part1 + sum_part2 + sum_part3 + sum_part4


N = 4
arr = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
print(sumOfParts(arr, N))

# Expected output:
68
# check if x and y can be made zero by using given operation any number of times | function to check if x and y can be made equal to zero by using given operation any number of times ; check for the two conditions ; 
def ifPossible(X, Y):
    if (X > Y):
        X, Y = Y, X
    if ((X + Y) % 5 == 0 and 3 * X >= 2 * Y):
        print("Yes")
    else:
        print("No")


X = 33
Y = 27
ifPossible(X, Y)

# Expected output:
Yes
# largest even and odd n | function to print the largest n - digit even and odd numbers in hexadecimal number system ; append ' f ' ( n - 1 ) times ; append ' e ' for an even number ; append ' f ' for an odd number ; 
def findNumbers(n):
    ans = 'F' * (n - 1)
    even = ans + 'E'
    odd = ans + 'F'
    print("Even: ", even)
    print("Odd: ", odd)


if __name__ == "__main__":
    n = 2
    findNumbers(n)

# Expected output:
Even:  FE
Odd:  FF
# program to compute log a to any base b ( logb a ) | python3 program to find log ( a ) on any base b ; 

from math import log


def log_a_to_base_b(a, b):
    return log(a) // log(b)


if __name__ == "__main__":
    a = 3
    b = 2
    print(log_a_to_base_b(a, b))
    a = 256
    b = 4
    print(log_a_to_base_b(a, b))

# Expected output:
1.0
4.0
# program to compute log a to any base b ( logb a ) | recursive function to compute log a to the base b ; 
def log_a_to_base_b(a, b):
    rslt = (1 + log_a_to_base_b(a // b, b))if (a > (b - 1))else 0
    return rslt


if __name__ == "__main__":
    a = 3
    b = 2
    print(log_a_to_base_b(a, b))
    a = 256
    b = 4
    print(log_a_to_base_b(a, b))

# Expected output:
1
4
# largest and smallest n | function to return the largest n - digit number in hexa - decimal number system ; append ' f ' n times ; function to return the smallest n - digit number in hexa - decimal number system ; append '0' ( n - 1 ) times to 1 ; function to print the largest and smallest n - digit hexa - decimal number ; 
def findLargest(N):
    largest = 'F' * N
    return largest


def findSmallest(N):
    smallest = '1' + '0' * (N - 1)
    return smallest


def printAns(largest):
    print("Largest: ", findLargest(largest))
    print("Smallest: ", findSmallest(largest))


if __name__ == "__main__":
    N = 4
    printAns(N)

# Expected output:
Largest:  FFFF
Smallest:  1000
# highest and smallest power of k less than and greater than equal to n respectively | python3 implementation of the approach ; function to return the highest power of k less than or equal to n ; function to return the smallest power of k greater than or equal to n ; function to print the result ; 

import math


def prevPowerofK(n, k):
    p = int(math .log(n) / math .log(k))
    return int(math .pow(k, p))


def nextPowerOfK(n, k):
    return prevPowerofK(n, k) * k


def printResult(n, k):
    print(prevPowerofK(n, k), nextPowerOfK(n, k))


n = 6
k = 3
printResult(n, k)

# Expected output:
3 9
# check if all elements of the given array can be made 0 by decrementing value in pairs | function to check if all the elements can be made 0 in an array ; variable to store sum and maximum element in an array ; loop to calculate the sum and max value of the given array ; if n is 1 or sum is odd or sum - max element < max then no solution ; for the remaining case , print yes ; 
def canMake(n, ar):
    sum = 0
    maxx = -1
    for i in range(n):
        sum += ar[i]
        maxx = max(maxx, ar[i])
    if (n == 1 or sum % 2 == 1 or sum - maxx < maxx):
        print("No")
    else:
        print("Yes")


if __name__ == "__main__":
    n = 6
    arr = [1, 1, 2, 3, 6, 11]
    canMake(n, arr)

# Expected output:
Yes
# program to find all factors of a number using recursion | recursive function to prfactors of a number ; checking if the number is less than n ; calling the function recursively for the next number ; 
def factors(n, i):
    if (i <= n):
        if (n % i == 0):
            print(i, end=" ")
        factors(n, i + 1)


if __name__ == '__main__':
    N = 16
    factors(N, 1)

# Expected output:
1 2 4 8 16
# maximize value of ( a + b ) such that ( a * a | function to maximize the value of ( a + b ) such that ( a * a - b * b = n ) ; 
def maxValue(n):
    return n


if __name__ == "__main__":
    n = 1
    print(maxValue(n))

# Expected output:
1
# maximum number of edges that n | function to find the maximum number of edges in a n - vertex graph . ; according to the mantel 's theorem  the maximum number of edges will be  floor of [(n^2)/4] ; 
def solve(n):
    ans = (n * n // 4)
    return ans


if __name__ == '__main__':
    n = 10
    print(solve(n))

# Expected output:
25
# multiplication table till n rows where every kth row is table of k upto kth term | function to pr the multiplication table upto k - th term ; for loop to iterate from 1 to n where i serves as the value of k ; inner loop which at every iteration goes till i ; printing the table value for i ; new line after every row ; 
def prMultiples(N):
    for i in range(1, N + 1):
        for j in range(1, i + 1):
            print((i * j), end=" ")
        print()


if __name__ == '__main__':
    N = 5
    prMultiples(N)

# Expected output:
1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25