giulio98/xlcost-single-prompt huggingface dataset:
# count of subarrays with largest element at least twice the largest of remaining elements | function to find count of subarrays which have max element greater than twice maximum of all other elements ; stores the count of subarrays ; generate all possible subarrays ; stores the maximum element of the subarray ; stores the maximum of all other elements ; find the maximum element in the subarray [ i , j ] ; find the maximum of all other elements ; if the maximum of subarray is greater than twice the maximum of other elements ; print the maximum value obtained ; 
def countSubarray(arr, n):
    count = 0
    for i in range(n):
        for j in range(i, n, 1):
            mxSubarray = 0
            mxOther = 0
            for k in range(i, j + 1, 1):
                mxSubarray = max(mxSubarray, arr[k])
            for k in range(0, i, 1):
                mxOther = max(mxOther, arr[k])
            for k in range(j + 1, n, 1):
                mxOther = max(mxOther, arr[k])
            if (mxSubarray > (2 * mxOther)):
                count += 1
    print(count)


if __name__ == '__main__':
    arr = [1, 6, 10, 9, 7, 3]
    N = len(arr)
    countSubarray(arr, N)

# Expected output:
4
# count of subarrays with largest element at least twice the largest of remaining elements | function to find count of subarrays which have max element greater than twice maximum of all other elements ; stores the maximum element of the array ; traverse the given array ; if the value of 2 * arr [ i ] is greater than mx ; update the value of l and break out of loop ; if the value 2 * arr [ i ] is greater than mx ; update the value of r and break out of loop ; print the final answer ; 
def countSubarray(arr, n):
    count = 0
    L = 0
    R = 0
    mx = max(arr)
    for i in range(n):
        if (arr[i] * 2 > mx):
            L = i
            break
    i = n - 1
    while (i >= 0):
        if (arr[i] * 2 > mx):
            R = i
            break
        i -= 1
    print((L + 1) * (n - R))


if __name__ == '__main__':
    arr = [1, 6, 10, 9, 7, 3]
    N = len(arr)
    countSubarray(arr, N)

# Expected output:
4
# find prime number just less than and just greater each element of given array | python3 program for the above approach ; utility function to check for primality of a number x by checking whether x haaccs any factors other than 1 and itself . ; factor found ; function to print primes just less than and just greater than of each element in an array ; traverse the array ; traverse for finding prime just less than a [ i ] ; prime just less than a [ i ] found ; traverse for finding prime just greater than a [ i ] ; prime just greater than a [ i ] found ; 

from math import sqrt


def isPrime(X):
    for i in range(2, int(sqrt(X)) + 1, 1):
        if (X % i == 0):
            return False
    return True


def printPrimes(A, N):
    for i in range(N):
        j = A[i] - 1
        while (1):
            if (isPrime(j)):
                print(j, end=" ")
                break
            j -= 1
        j = A[i] + 1
        while (1):
            if (isPrime(j)):
                print(j, end=" ")
                break
            j += 1
        print("",  end="")


if __name__ == '__main__':
    A = [17, 28]
    N = len(A)
    printPrimes(A, N)

# Expected output:
13 19 23 29
# kth smallest element in an array that contains a [ i ] exactly b [ i ] times | function to find the kth smallest element that contains a [ i ] exactly b [ i ] times ; traverse the given array ; stores the frequency of every elements ; traverse the given array ; initialize a variable to store the prefix sums ; iterate over the range [ 0 , m ] ; increment sum by freq [ i ] ; if sum is greater than or equal to k ; return the current element as answer ; return - 1 ; 
def KthSmallest(A, B, N, K):
    M = 0
    for i in range(N):
        M = max(A[i], M)
    freq = [0] * (M + 1)
    for i in range(N):
        freq[A[i]] += B[i]
    sum = 0
    for i in range(M + 1):
        sum += freq[i]
        if (sum >= K):
            return i
    return -1


if __name__ == "__main__":
    A = [3, 4, 5]
    B = [2, 1, 3]
    N = len(A)
    K = 4
    print(KthSmallest(A, B, N, K))

# Expected output:
5
# bitwise or of bitwise and of all subarrays of an array | function to find the bitwise or of bitwise and of all subarrays ; stores the required result ; generate all the subarrays ; store the current element ; find the bitwise or ; update the result ; print the result ; 
def findbitwiseOR(a, n):
    res = 0
    for i in range(n):
        curr_sub_array = a[i]
        res = res | curr_sub_array
        for j in range(i, n):
            curr_sub_array = curr_sub_array & a[j]
            res = res | curr_sub_array
    print(res)


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

# Expected output:
3
# bitwise or of bitwise and of all subarrays of an array | function to find the bitwise or of bitwise and of all consecutive subsets of the array ; stores the required result ; traverse the given array ; print the result ; 
def findbitwiseOR(a, n):
    res = 0
    for i in range(n):
        res = res | a[i]
    print(res)


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

# Expected output:
3
# check if sum of digits of a number exceeds the product of digits of that number | function to check if the sum of the digits of n is strictly greater than the product of the digits of n or not ; stores the sum and the product of the digits of n ; stores the last digit if n ; increment the value of sumofdigits ; update the prodofdigit ; divide n by 10 ; print the result ; 
def check(n):
    sumOfDigit = 0
    prodOfDigit = 1
    while n > 0:
        rem = n % 10
        sumOfDigit += rem
        prodOfDigit *= rem
        n = n // 10
    if sumOfDigit > prodOfDigit:
        print("Yes")
    else:
        print("No")


N = 1234
check(N)

# Expected output:
No
# print all distinct even and odd prefix bitwise xors of first n natural numbers | print all distinct even & odd prefix bitwise xors from 1 to n ; print the even number ; print the odd number ; 
def evenOddBitwiseXOR(N):
    print("Even: ", 0, end=" ")
    for i in range(4, N + 1, 4):
        print(i, end=" ")
    print()
    print("Odd: ", 1, end=" ")
    for i in range(4, N + 1, 4):
        print(i - 1, end=" ")
    if (N % 4 == 2):
        print(N + 1)
    elif (N % 4 == 3):
        print(N)


N = 6
evenOddBitwiseXOR(N)

# Expected output:
Even:  0 4 
Odd:  1 3 7
# lexicographically largest permutation possible by a swap that is smaller than a given array | function to lexicographic largest permutation possible by a swap that is smaller than given array ; find the index of first element such that arr [ i ] > arr [ i + 1 ] ; if the array is sorted in increasing order ; find the index of first element which is smaller than arr [ i ] ; if arr [ j ] = = arr [ j - 1 ] ; decrement j ; swap the element ; pr the array arr [ ] ; 
def findPermutation(arr):
    N = len(arr)
    i = N - 2
    while (i >= 0 and arr[i] <= arr[i + 1]):
        i -= 1
    if (i == -1):
        print("-1")
        return
    j = N - 1
    while (j > i and arr[j] >= arr[i]):
        j -= 1
    while (j > i and arr[j] == arr[j - 1]):
        j -= 1
    temp = arr[i]
    arr[i] = arr[j]
    arr[j] = temp
    for it in arr:
        print(it, end=" ")


arr = [1, 2, 5, 3, 4, 6]
findPermutation(arr)

# Expected output:
1 2 4 3 5 6
# absolute difference between the count of odd and even factors of n | function to find the smallest prime factor of all the numbers using sieve of eratosthenes ; stores whether any number is prime or not ; initialize smallest factor as 2 for all the even numbers ; iterate over the range [ 3 , n ] ; if i is prime ; iterate all multiples of i ; i is the smallest prime factor of i * j ; function to find the absolute difference between the count of odd and even factors of n ; stores the smallest prime factor of i ; fill values in s [ ] using sieve of eratosthenes ; stores the total number of factors and the total number of odd and even factors ; store the current prime factor of the number n ; store the power of current prime factor ; loop while n is greater than 1 ; if n also has smallest prime factor as curr , then increment cnt by 1 ; update only total number of factors if curr is 2 ; update total number of factors and total number of odd factors ; update current prime factor as s [ n ] and count as 1 ; calculate the number of even factors ; print the difference ; 
def sieveOfEratosthenes(N, s):
    prime = [False] * (N + 1)
    for i in range(2, N + 1, 2):
        s[i] = 2
    for i in range(3, N, 2):
        if (prime[i] == False):
            s[i] = i
            for j in range(i, N, 2):
                if j * i > N:
                    break
                if (not prime[i * j]):
                    prime[i * j] = True
                    s[i * j] = i


def findDifference(N):
    s = [0] * (N + 1)
    sieveOfEratosthenes(N, s)
    total, odd, even = 1, 1, 0
    curr = s[N]
    cnt = 1
    while (N > 1):
        N //= s[N]
        if (curr == s[N]):
            cnt += 1
            continue
        if (curr == 2):
            total = total * (cnt + 1)
        else:
            total = total * (cnt + 1)
            odd = odd * (cnt + 1)
        curr = s[N]
        cnt = 1
    even = total - odd
    print(abs(even - odd))


if __name__ == '__main__':
    N = 12
    findDifference(N)

# Expected output:
2
# calculate median from given values of mean and mode | function to find the median of a group of data with given mean and mode ; calculate the median ; print the median ; 
def findMedian(Mean, Mode):
    Median = (2 * Mean + Mode) // 3
    print(Median)


Mode = 6
Mean = 3
findMedian(Mean, Mode)

# Expected output:
4
# program to find the magnitude of a vector | python3 program for the above approach ; function to calculate magnitude of a 3 dimensional vector ; stores the sum of squares of coordinates of a vector ; return the magnitude ; 

from math import sqrt


def vectorMagnitude(x, y, z):
    sum = x * x + y * y + z * z
    return sqrt(sum)


x = 1
y = 2
z = 3
print(vectorMagnitude(x, y, z))

# Expected output:
3.7416573867739413
# program to find the product of a number with a mersenne number | python3 program for the above approach ; function to find prodcut of a mersenne number with another number ; stores the power of 2 of integer m + 1 ; return the product ; 

import math


def multiplyByMersenne(N, M):
    x = int(math .log2(M + 1))
    return ((N << x) - N)


N = 4
M = 15
print(multiplyByMersenne(N, M))

# Expected output:
60
# nearest power of 2 of nearest perfect squares of non | python3 program for the above approach ; function to find nearest perfect square of num ; calculate square root of num ; calculate perfect square ; find the nearest perfect square ; function to find the power of 2 nearest to the number num ; calculate log base 2 of num ; highest power of 2 which is <= num ; function to find the nearest perfect square and the nearest power of 2 of every array element whose occurrence is 1 ; stores frequency of array elements ; traverse the array and update frequency of current array element ; traverse the map freq ; if the frequency is 1 ; find nearest perfect square ; print the nearest power of 2 ; if the any does not contain any non - repeating elements ; 

from math import sqrt, log2, pow


def perfectSquare(num):
    sr = int(sqrt(num))
    a = sr * sr
    b = (sr + 1) * (sr + 1)
    if ((num - a) < (b - num)):
        return a
    else:
        return b


def powerOfTwo(num):
    lg = int(log2(num))
    p = int(pow(2, lg))
    return p


def uniqueElement(arr, N):
    ans = True
    freq = {}
    for i in range(N):
        if (arr[i] in freq):
            freq[arr[i]] += 1
        else:
            freq[arr[i]] = 1
    res = []
    for key, value in freq .items():
        if (value == 1):
            ans = False
            ps = perfectSquare(key)
            res .append(powerOfTwo(ps))
    res .sort(reverse=False)
    for x in res:
        print(x, end=" ")
    if (ans):
        print("-1")


if __name__ == '__main__':
    arr = [4, 11, 4, 3, 4]
    N = len(arr)
    uniqueElement(arr, N)

# Expected output:
4 8
# partition array into two subarrays with every element in the right subarray strictly greater than every element in left subarray | python3 program for the above approach ; function to partition the array into two non - empty subarrays which satisfies the given condition ; stores the suffix min array ; stores the minimum of a suffix ; traverse the array in reverse ; update minimum ; store the minimum ; stores the maximum value of a prefix ; stores the index of the partition ; update max ; if max is less than min [ i + 1 ] ; store the index of partition ; break ; if ind is not - 1 ; print first subarray ; print second subarray ; otherwise ; 

import sys


def partitionArray(a, n):
    Min = [0] * n
    Mini = sys.maxsize
    for i in range(n - 1, -1, -1):
        Mini = min(Mini, a[i])
        Min[i] = Mini
    Maxi = -sys.maxsize - 1
    ind = -1
    for i in range(n - 1):
        Maxi = max(Maxi, a[i])
        if (Maxi < Min[i + 1]):
            ind = i
            break
    if (ind != -1):
        for i in range(ind + 1):
            print(a[i], end=" ")
        print()
        for i in range(ind + 1, n, 1):
            print(a[i], end=" ")
    else:
        print("Impossible")


arr = [5, 3, 2, 7, 9]
N = 5
partitionArray(arr, N)

# Expected output:
5 3 2 
7 9
# check if a number can be represented as sum of k positive integers out of which at least k | python3 program for the above approach ; function to count all prime factors of a given number ; count the number of 2 s that divides n ; since n is odd at this point , skip one element ; while i divides n , count i and divide n ; if n is a prime number greater than 2 ; function to find the sum of first n nearly prime numbers ; store the required sum ; add this number if it is satisfies the condition ; increment count of nearly prime numbers ; function to check if n can be represented as sum of k different positive integers out of which at least k - 1 of them are nearly prime ; store the sum of first k - 1 nearly prime numbers ; if sum is great than or equal to n ; otherwise , pryes ; 

import math


def countPrimeFactors(n):
    count = 0
    while (n % 2 == 0):
        n = n // 2
        count += 1
    for i in range(3, int(math .sqrt(n) + 1), 2):
        while (n % i == 0):
            n = n // i
            count += 1
    if (n > 2):
        count += 1
    return (count)


def findSum(n):
    sum = 0
    i = 1
    num = 2
    while (i <= n):
        if (countPrimeFactors(num) == 2):
            sum += num
            i += 1
        num += 1
    return sum


def check(n, k):
    s = findSum(k - 1)
    if (s >= n):
        print("No")
    else:
        print("Yes")


n = 100
k = 6
check(n, k)

# Expected output:
Yes
# count ways to represent an integer as an exponent | python3 program for the above approach ; function to calculate gcd of a and b using euclidean algorithm ; iterate until b is non - zero ; return the gcd ; function to count the number of ways n can be expressed as x ^ y ; base case ; stores the gcd of powers ; calculate the degree of 2 in n ; calculate the degree of prime numbers in n ; calculate the degree of prime ' i ' in n ; if n is a prime , g becomes 1. ; stores the number of ways to represent n as x ^ y ; find the number of factors of g ; update the count of ways ; iterate to find rest of the prime numbers ; find the power of i ; update the count of ways ; if g is prime ; return the total number of ways ; 

import math


def gcd(a, b):
    while (b > 0):
        rem = a % b
        a = b
        b = rem
    return a


def countNumberOfWays(n):
    if (n == 1):
        return -1
    g = 0
    power = 0
    while (n % 2 == 0):
        power += 1
        n //= 2
    g = gcd(g, power)
    for i in range(3, int(math .sqrt(g)) + 1, 2):
        power = 0
        while (n % i == 0):
            power += 1
            n //= i
        g = gcd(g, power)
    if (n > 2):
        g = gcd(g, 1)
    ways = 1
    power = 0
    while (g % 2 == 0):
        g //= 2
        power += 1
    ways *= (power + 1)
    for i in range(3, int(math .sqrt(g)) + 1, 2):
        power = 0
        while (g % i == 0):
            power += 1
            g /= i
        ways *= (power + 1)
    if (g > 2):
        ways *= 2
    return ways


N = 64
print(countNumberOfWays(N))

# Expected output:
4
# highest power of 2 less than or equal to given integer | python3 implementation of the above approach ; function to return the lowest power of 2 close to given positive number ; floor function is used to determine the value close to the number ; function to return the lowest power of 2 close to given negative number ; ceil function is used for negative numbers as - 1 > - 4. it would be opposite to positive numbers where 1 < 4 ; function to find the highest power of 2 ; to check if the given number is positive or negative ; if the number is negative , then the ceil of the positive number is calculated and negative sign is added ; 

from math import floor, ceil, log2


def powOfPositive(n):
    pos = floor(log2(n))
    return 2 ** pos


def powOfNegative(n):
    pos = ceil(log2(n))
    return (-1 * pow(2, pos))


def highestPowerOf2(n):
    if (n > 0):
        print(powOfPositive(n))
    else:
        n = -n
        print(powOfNegative(n))


if __name__ == "__main__":
    n = -24
    highestPowerOf2(n)

# Expected output:
-32
# number of cards needed build a house of cards of a given level n | function to find number of cards needed ; 
def noOfCards(n):
    return n * (3 * n + 1) // 2


n = 3
print(noOfCards(n))

# Expected output:
15
# find smallest possible number from a given large number with same count of digits | function for finding the smallest possible number after swapping the digits any number of times ; variable to store the final answer ; array to store the count of occurrence of each digit ; loop to calculate the number of occurrences of every digit ; loop to get smallest number ; returning the answer ; 
def smallestPoss(s, n):
    ans = ""
    arr = [0] * 10
    for i in range(n):
        arr[ord(s[i]) - 48] += 1
    for i in range(10):
        for j in range(arr[i]):
            ans = ans + str(i)
    return ans


if __name__ == '__main__':
    N = 15
    K = "325343273113434"
    print(smallestPoss(K, N))

# Expected output:
112233333344457
# count the subarray with sum strictly greater than the sum of remaining elements | function to count the number of sub - arrays with sum strictly greater than the remaining elements of array ; for loop for beginning point of a subarray ; for loop for ending point of the subarray ; initialise subarray_sum and remaining_sum to 0 ; for loop to calculate the sum of generated subarray ; for loop to calculate the sum remaining array element ; checking for condition when subarray sum is strictly greater than remaining sum of array element ; 
def Count_subarray(arr, n):
    subarray_sum, remaining_sum, count = 0, 0, 0
    for i in range(n):
        for j in range(i, n):
            subarray_sum = 0
            remaining_sum = 0
            for k in range(i, j + 1):
                subarray_sum += arr[k]
            for l in range(i):
                remaining_sum += arr[l]
            for l in range(j + 1, n):
                remaining_sum += arr[l]
            if (subarray_sum > remaining_sum):
                count += 1
    return count


if __name__ == '__main__':
    arr = [10, 9, 12, 6]
    n = len(arr)
    print(Count_subarray(arr, n))

# Expected output:
5
# count the subarray with sum strictly greater than the sum of remaining elements | python3 implementation of the above approach ; calculating total sum of given array ; for loop for beginning point of a subarray ; initialise subarray_sum to 0 ; for loop for calculating subarray_sum and remaining_sum ; calculating subarray_sum and corresponding remaining_sum ; checking for the condition when subarray sum is strictly greater than the remaining sum of the array element ; 
def Count_subarray(arr, n):
    total_sum = 0
    count = 0
    for i in range(n):
        total_sum += arr[i]
    for i in range(n):
        subarray_sum = 0
        for j in range(i, n):
            subarray_sum += arr[j]
            remaining_sum = total_sum - subarray_sum
            if (subarray_sum > remaining_sum):
                count += 1
    return count


if __name__ == "__main__":
    arr = [10, 9, 12, 6]
    n = len(arr)
    print(Count_subarray(arr, n))

# Expected output:
5
# remove one element to get maximum xor | function to return the maximized xor after removing an element from the array ; find xor of the complete array ; to store the final answer ; iterating through the array to find the final answer ; return the final answer ; 
def maxXOR(arr, n):
    xorArr = 0
    for i in range(n):
        xorArr ^= arr[i]
    ans = 0
    for i in range(n):
        ans = max(ans, (xorArr ^ arr[i]))
    return ans


arr = [1, 1, 3]
n = len(arr)
print(maxXOR(arr, n))

# Expected output:
2
# count of numbers from the range [ l , r ] which contains at least one digit that divides k | function that returns true if num contains at least one digit that divides k ; get the last digit ; if the digit is non - zero and it divides k ; remove the last digit ; there is no digit in num that divides k ; function to return the required count of elements from the given range which contain at least one digit that divides k ; to store the result ; for every number from the range ; if any digit of the current number divides k ; 
def digitDividesK(num, k):
    while (num):
        d = num % 10
        if (d != 0 and k % d == 0):
            return True
        num = num // 10
    return False


def findCount(l, r, k):
    count = 0
    for i in range(l, r + 1):
        if (digitDividesK(i, k)):
            count += 1
    return count


l = 20
r = 35
k = 45
print(findCount(l, r, k))

# Expected output:
10
# check if a given number is factorial of any number | function to check if the given number is a factorial of any number ; 
def isFactorial(n):
    i = 1
    while (True):
        if (n % i == 0):
            n //= i
        else:
            break
        i += 1
    if (n == 1):
        return True
    else:
        return False


if __name__ == "__main__":
    n = 24
    ans = isFactorial(n)
    if (ans == 1):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# remove an element to minimize the lcm of the given array | python3 implementation of the above approach ; function to return the lcm of two numbers ; function to return the minimum lcm after removing a single element from the given array ; prefix and suffix arrays ; single state dynamic programming relation for storing lcm of first i elements from the left in prefix [ i ] ; initializing suffix array ; single state dynamic programming relation for storing lcm of all the elements having index greater than or equal to i in suffix [ i ] ; if first or last element of the array has to be removed ; if any other element is replaced ; return the minimum lcm ; 

from math import gcd


def lcm(a, b):
    GCD = gcd(a, b)
    return (a * b) // GCD


def MinLCM(a, n):
    Prefix = [0] * (n + 2)
    Suffix = [0] * (n + 2)
    Prefix[1] = a[0]
    for i in range(2, n + 1):
        Prefix[i] = lcm(Prefix[i - 1], a[i - 1])
    Suffix[n] = a[n - 1]
    for i in range(n - 1, 0, -1):
        Suffix[i] = lcm(Suffix[i + 1], a[i - 1])
    ans = min(Suffix[2], Prefix[n - 1])
    for i in range(2, n):
        ans = min(ans, lcm(Prefix[i - 1], Suffix[i + 1]))
    return ans


if __name__ == "__main__":
    a = [5, 15, 9, 36]
    n = len(a)
    print(MinLCM(a, n))

# Expected output:
45
# number of coloured 0 's in an n | function to return the count of coloured 0 s in an n - level hexagon ; 
def count(n):
    return n * (3 * n - 1) // 2


if __name__ == "__main__":
    n = 3
    print(count(n))

# Expected output:
12