giulio98/xlcost-single-prompt huggingface dataset:
# number of subarrays having sum of the form k ^ m , m >= 0 | python3 implementation of the above approach ; function to count number of sub - arrays whose sum is k ^ p where p >= 0 ; if m [ a + b ] = c , then add c to the current sum . ; increase count of prefix sum . ; if m [ a + b ] = c , then add c to the current sum . ; increase count of prefix sum . ; b = k ^ p , p >= 0 ; k ^ m can be maximum equal to 10 ^ 14. ; if m [ a + b ] = c , then add c to the current sum . ; increase count of prefix sum . ; 

from collections import defaultdict
MAX = 100005


def partial_sum(prefix_sum, arr, n):
    for i in range(1, n + 1):
        prefix_sum[i] = (prefix_sum[i - 1] + arr[i - 1])
    return prefix_sum


def countSubarrays(arr, n, k):
    prefix_sum = [0] * MAX
    prefix_sum[0] = 0
    prefix_sum = partial_sum(prefix_sum, arr, n)
    if (k == 1):
        sum = 0
        m = defaultdict(int)
        for i in range(n, -1, -1):
            if ((prefix_sum[i] + 1) in m):
                sum += m[prefix_sum[i] + 1]
            m[prefix_sum[i]] += 1
        return sum
    if (k == -1):
        sum = 0
        m = defaultdict(int)
        for i in range(n, -1, -1):
            if ((prefix_sum[i] + 1) in m):
                sum += m[prefix_sum[i] + 1]
            if ((prefix_sum[i] - 1) in m):
                sum += m[prefix_sum[i] - 1]
            m[prefix_sum[i]] += 1
        return sum
    sum = 0
    m = defaultdict(int)
    for i in range(n, -1, -1):
        b = 1
        while (True):
            if (b > 100000000000000):
                break
            if ((prefix_sum[i] + b) in m):
                sum += m[prefix_sum[i] + b]
            b *= k
        m[prefix_sum[i]] += 1
    return sum


if __name__ == "__main__":
    arr = [2, 2, 2, 2]
    n = len(arr)
    k = 2
    print(countSubarrays(arr, n, k))

# Expected output:
8
# given two binary strings perform operation until b > 0 and print the result | python 3 implementation of the approach ; function to return the required result ; reverse the strings ; count the number of set bits in b ; to store the powers of 2 ; power [ i ] = pow ( 2 , i ) % mod ; to store the final answer ; add power [ i ] to the ans after multiplying it with the number of set bits in b ; divide by 2 means right shift b >> 1 if b has 1 at right most side than number of set bits will get decreased ; if no more set bits in b i . e . b = 0 ; return the required answer ; 

mod = 1000000007


def BitOperations(a, n, b, m):
    a = a[::-1]
    b = b[::-1]
    c = 0
    for i in range(m):
        if (b[i] == '1'):
            c += 1
    power = [None] * n
    power[0] = 1
    for i in range(1, n):
        power[i] = (power[i - 1] * 2) % mod
    ans = 0
    for i in range(0, n):
        if (a[i] == '1'):
            ans += c * power[i]
            if (ans >= mod):
                ans %= mod
        if (b[i] == '1'):
            c -= 1
        if (c == 0):
            break
    return ans


if __name__ == '__main__':
    a = "1001"
    b = "10101"
    n = len(a)
    m = len(b)
    print(BitOperations(a, n, b, m))

# Expected output:
11
# print prime numbers with prime sum of digits in an array | python3 implementation of the above approach ; function to store the primes ; function to return the sum of digits ; function to print additive primes ; if the number is prime ; check if it 's digit sum is prime ; 

from math import sqrt


def sieve(maxEle, prime):
    prime[0], prime[1] = 1, 1
    for i in range(2, int(sqrt(maxEle)) + 1):
        if (not prime[i]):
            for j in range(2 * i, maxEle + 1, i):
                prime[j] = 1


def digitSum(n):
    sum = 0
    while (n):
        sum += n % 10
        n = n // 10
    return sum


def printAdditivePrime(arr, n):
    maxEle = max(arr)
    prime = [0] * (maxEle + 1)
    sieve(maxEle, prime)
    for i in range(n):
        if (prime[arr[i]] == 0):
            sum = digitSum(arr[i])
            if (prime[sum] == 0):
                print(arr[i], end=" ")


if __name__ == "__main__":
    a = [2, 4, 6, 11, 12, 18, 7]
    n = len(a)
    printAdditivePrime(a, n)

# Expected output:
2 11 7
# find the nth term of the series 9 , 45 , 243 , 1377 | function to return the nth term of the given series ; nth term ; 
def nthterm(n):
    An = (1 ** n + 2 ** n) * (3 ** n)
    return An


n = 3
print(nthterm(n))

# Expected output:
243
# count the numbers < n which have equal number of divisors as k | function to return the count of the divisors of a number ; count the number of 2 s that divide n ; n must be odd at this point . so we can skip one element ; while i divides n ; this condition is to handle the case when n is a prime number > 2 ; count the total elements that have divisors exactly equal to as that of k 's ; exclude k from the result if it is smaller than n . ; 
def countDivisors(n):
    x, ans = 0, 1
    while (n % 2 == 0):
        x += 1
        n = n / 2
    ans = ans * (x + 1)
    for i in range(3, int(n ** 1 / 2) + 1, 2):
        x = 0
        while (n % i == 0):
            x += 1
            n = n / i
        ans = ans * (x + 1)
    if (n > 2):
        ans = ans * 2
    return ans


def getTotalCount(n, k):
    k_count = countDivisors(k)
    count = 0
    for i in range(1, n):
        if (k_count == countDivisors(i)):
            count += 1
    if (k < n):
        count = count - 1
    return count


if __name__ == '__main__':
    n, k = 500, 6
    print(getTotalCount(n, k))

# Expected output:
148
# find the nth term of the series 0 , 8 , 64 , 216 , 512 , . . . | function to return the nth term of the given series ; common difference ; first term ; nth term ; nth term of the given series ; 
def term(n):
    d = 2
    a1 = 0
    An = a1 + (n - 1) * d
    An = An ** 3
    return An


n = 5
print(term(n))

# Expected output:
512
# count the number of intervals in which a given value lies | python3 program to count the number of intervals in which a given value lies ; function to count the number of intervals in which a given value lies ; variables to store overall minimumimum and maximumimum of the intervals ; frequency array to keep track of how many of the given intervals an element lies in ; constructing the frequency array ; 

MAX_VAL = 200000


def countIntervals(arr, V, N):
    minimum = float("inf")
    maximum = 0
    freq = [0] * (MAX_VAL)
    for i in range(0, N):
        li = arr[i][0]
        freq[li] = freq[li] + 1
        ri = arr[i][1]
        freq[ri + 1] = freq[ri + 1] - 1
        if li < minimum:
            minimum = li
        if ri > maximum:
            maximum = ri
    for i in range(minimum, maximum + 1):
        freq[i] = freq[i] + freq[i - 1]
    return freq[V]


if __name__ == "__main__":
    arr = [[1, 10], [5, 10], [15, 25], [7, 12], [20, 25]]
    V = 7
    N = len(arr)
    print(countIntervals(arr, V, N))

# Expected output:
3
# split n ^ 2 numbers into n groups of equal sum | function to print n groups of equal sum ; no . of groups ; n / 2 pairs ; 
def printGroups(n):
    x = 1
    y = n * n
    for i in range(1, n + 1):
        for j in range(1, n // 2 + 1):
            print("{", x, ",", y, "}", end=" ")
            x += 1
            y -= 1
        print()


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

# Expected output:
{ 1 , 16 } { 2 , 15 } 
{ 3 , 14 } { 4 , 13 } 
{ 5 , 12 } { 6 , 11 } 
{ 7 , 10 } { 8 , 9 }
# minimize the value of n by applying the given operations | function to return the product of distinct prime factors of a numberdef minsteps ( str ) : ; find distinct prime ; 
def minimum(n):
    product = 1
    i = 2
    while i * i <= n:
        if n % i == 0:
            while n % i == 0:
                n = n / i
            product = product * i
        i = i + 1
    if n >= 2:
        product = product * n
    return product


n = 20
print(minimum(n))

# Expected output:
10.0
# n digit numbers divisible by 5 formed from the m digits | function to find the count of all possible n digit numbers which are divisible by 5 formed from m digits ; if it is not possible to form n digit number from the given m digits without repetition ; if both zero and five exists ; remaining n - 1 iterations ; remaining n - 1 iterations ; 
def numbers(n, arr, m):
    isZero = 0
    isFive = 0
    result = 0
    if (m < n):
        return -1
    for i in range(m):
        if (arr[i] == 0):
            isZero = 1
        if (arr[i] == 5):
            isFive = 1
    if (isZero and isFive):
        result = 2
        for i in range(n - 1):
            m -= 1
            result = result * (m)
    elif (isZero or isFive):
        result = 1
        for i in range(n - 1):
            m -= 1
            result = result * (m)
    else:
        result = -1
    return result


if __name__ == "__main__":
    n = 3
    m = 6
    arr = [2, 3, 5, 6, 7, 9]
    print(numbers(n, arr, m))

# Expected output:
20
# find subsequences with maximum bitwise and and bitwise or | function to find the maximum sum ; maximum and is maximum element ; maximum or is bitwise or of all . ; 
def maxSum(a, n):
    maxAnd = max(a)
    maxOR = 0
    for i in range(n):
        maxOR |= a[i]
    print(maxAnd + maxOR)


n = 4
a = [3, 5, 6, 1]
maxSum(a, n)

# Expected output:
13
# count magic squares in a grid | python3 program to count magic squares ; function to check is subgrid is magic square ; elements of grid must contain all numbers from 1 to 9 , sum of all rows , columns and diagonals must be same , i . e . , 15. ; function to count total magic square subgrids ; if condition true skip check ; check for magic square subgrid ; return total magic square ; 

R = 3
C = 4


def magic(a, b, c, d, e, f, g, h, i):
    s1 = set([a, b, c, d, e, f, g, h, i])
    s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
    if (
        s1 == s2 and (
            a +
            b +
            c) == 15 and (
            d +
            e +
            f) == 15 and (
                g +
                h +
                i) == 15 and (
                    a +
                    d +
                    g) == 15 and (
                        b +
                        e +
                        h) == 15 and (
                            c +
                            f +
                            i) == 15 and (
                                a +
                                e +
                                i) == 15 and (
                                    c +
                                    e +
            g) == 15):
        return True
    return false


def CountMagicSquare(Grid):
    ans = 0
    for i in range(0, R - 2):
        for j in range(0, C - 2):
            if Grid[i + 1][j + 1] != 5:
                continue
            if (magic(Grid[i][j],
                      Grid[i][j + 1],
                Grid[i][j + 2],
                Grid[i + 1][j],
                Grid[i + 1][j + 1],
                Grid[i + 1][j + 2],
                Grid[i + 2][j],
                Grid[i + 2][j + 1],
                    Grid[i + 2][j + 2])):
                ans += 1
    return ans


if __name__ == "__main__":
    G = [[4, 3, 8, 4], [9, 5, 1, 9], [2, 7, 6, 2]]
    print(CountMagicSquare(G))

# Expected output:
1
# sum of minimum elements of all subarrays | function to return required minimum sum ; getting number of element strictly larger than a [ i ] on left . ; get elements from stack until element greater than a [ i ] found ; getting number of element larger than a [ i ] on right . ; get elements from stack until element greater or equal to a [ i ] found ; calculating required resultult ; 
def sumSubarrayMins(A, n):
    left, right = [None] * n, [None] * n
    s1, s2 = [], []
    for i in range(0, n):
        cnt = 1
        while len(s1) > 0 and s1[-1][0] > A[i]:
            cnt += s1[-1][1]
            s1 .pop()
        s1 .append([A[i], cnt])
        left[i] = cnt
    for i in range(n - 1, -1, -1):
        cnt = 1
        while len(s2) > 0 and s2[-1][0] >= A[i]:
            cnt += s2[-1][1]
            s2 .pop()
        s2 .append([A[i], cnt])
        right[i] = cnt
    result = 0
    for i in range(0, n):
        result += A[i] * left[i] * right[i]
    return result


if __name__ == "__main__":
    A = [3, 1, 2, 4]
    n = len(A)
    print(sumSubarrayMins(A, n))

# Expected output:
17
# print a number containing k digits with digital root d | function to find a number ; if d is 0 , k has to be 1 ; print k - 1 zeroes ; 
def printNumberWithDR(k, d):
    if d == 0 and k != 1:
        print(-1, end="")
    else:
        print(d, end="")
        k -= 1
        while k:
            print(0, end="")
            k -= 1


if __name__ == "__main__":
    k, d = 4, 4
    printNumberWithDR(k, d)

# Expected output:
4000
# count number of integers less than or equal to n which has exactly 9 divisors | function to count numbers having exactly 9 divisors ; sieve array , initially prime [ i ] = i ; use sieve concept to store the first prime factor of every number ; mark all factors of i ; check for all numbers if they can be expressed in form p * q ; p prime factor ; q prime factor ; if both prime factors are different if p * q <= n and q != ; check if it can be expressed as p ^ 8 ; 
def countNumbers(n):
    c = 0
    limit = int(n ** (0.5))
    prime = [i for i in range(limit + 1)]
    i = 2
    while i * i <= limit:
        if prime[i] == i:
            for j in range(i * i, limit + 1, i):
                if prime[j] == j:
                    prime[j] = i
        i += 1
    for i in range(2, limit + 1):
        p = prime[i]
        q = prime[i // prime[i]]
        if p * q == i and q != 1 and p != q:
            c += 1
        elif prime[i] == i:
            if i ** 8 <= n:
                c += 1
    return c


if __name__ == "__main__":
    n = 1000
    print(countNumbers(n))

# Expected output:
8
# interprime | utility function to check if a number is prime or not ; corner cases ; this is checked so that we can skip middle five numbers in below loop ; function to check if the given number is interprime or not ; smallest interprime is 4 so the number less than 4 can not be a interprime ; calculate first prime number < n ; calculate first prime number > n ; check if next_prime and prev_prime have the same average ; 
def isPrime(n):
    if (n <= 1):
        return False
    if (n <= 3):
        return True
    if (n % 2 == 0 or n % 3 == 0):
        return False
    i = 5
    while (i * i <= n):
        if (n % i == 0 or n % (i + 2) == 0):
            return False
        i = i + 6
    return True


def isInterprime(n):
    if (n < 4):
        return False
    prev_prime = n
    next_prime = n
    while (isPrime(prev_prime) == 0):
        prev_prime = prev_prime - 1
    while (isPrime(next_prime) == 0):
        next_prime = next_prime + 1
    if ((prev_prime + next_prime) == 2 * n):
        return True
    else:
        return False


n = 9
if (isInterprime(n)):
    print("YES")
else:
    print("NO")

# Expected output:
YES
# find the unit place digit of sum of n factorials | function to find the unit 's place digit ; let us write for cases when n is smaller than or equal to 4. ; we know following ( 1 ! + 2 ! + 3 ! + 4 ! ) % 10 = 3 ; 
def get_unit_digit(N):
    if (N == 0 or N == 1):
        return 1
    elif (N == 2):
        return 3
    elif (N == 3):
        return 9
    else:
        return 3


N = 1
for N in range(11):
    print("For N = ", N, ":", get_unit_digit(N), sep=' ')

# Expected output:
For N =  0 : 1
For N =  1 : 1
For N =  2 : 3
For N =  3 : 9
For N =  4 : 3
For N =  5 : 3
For N =  6 : 3
For N =  7 : 3
For N =  8 : 3
For N =  9 : 3
For N =  10 : 3
# sum of squares of fibonacci numbers | python3 program to find sum of squares of fibonacci numbers in o ( log n ) time . ; create an array for memoization ; returns n 'th fibonacci number using table f[] ; base cases ; if fib ( n ) is already computed ; applying above formula [ note value n & 1 is 1 if n is odd , else 0 ] . ; function to calculate sum of squares of fibonacci numbers ; 

MAX = 1000
f = [0 for i in range(MAX)]


def fib(n):
    if n == 0:
        return 0
    if n == 1 or n == 2:
        return 1
    if f[n]:
        return f[n]
    if n & 1:
        k = (n + 1) // 2
    else:
        k = n // 2
    if n & 1:
        f[n] = (fib(k) * fib(k) + fib(k - 1) * fib(k - 1))
    else:
        f[n] = ((2 * fib(k - 1) + fib(k)) * fib(k))
    return f[n]


def calculateSumOfSquares(n):
    return fib(n) * fib(n + 1)


n = 6
print("Sum of Squares of " "Fibonacci numbers is :", calculateSumOfSquares(n))

# Expected output:
Sum of Squares of Fibonacci numbers is : 104
# number of solutions for the equation x + y + z <= n | function to find the number of solutions for the equation x + y + z <= n , such that 0 <= x <= x , 0 <= y <= y , 0 <= z <= z . ; to store answer ; for values of x ; for values of y ; maximum possible value of z ; if z value greater than equals to 0 then only it is valid ; find minimum of temp and z ; return required answer ; 
def NumberOfSolutions(x, y, z, n):
    ans = 0
    for i in range(x + 1):
        for j in range(y + 1):
            temp = n - i - j
            if temp >= 0:
                temp = min(temp, z)
                ans += temp + 1
    return ans


if __name__ == "__main__":
    x, y, z, n = 1, 2, 3, 4
    print(NumberOfSolutions(x, y, z, n))

# Expected output:
20
# program to find the nth term of series 5 , 12 , 21 , 32 , 45. ... . . | calculate nth term of series ; 
def nthTerm(n):
    return n ** 2 + 4 * n


N = 4
print(nthTerm(N))

# Expected output:
32
# numbers less than n which are product of exactly two distinct prime numbers | python 3 program to find numbers that are product of exactly two distinct prime numbers ; function to check whether a number is a perfectsquare or not ; function to check if a number is a product of exactly two distinct primes ; function to find numbers that are product of exactly two distinct prime numbers . ; vector to store such numbers ; insert in the vector ; print all numbers till n from the vector ; 

import math


def isPerfectSquare(x):
    sr = math .sqrt(x)
    return ((sr - math .floor(sr)) == 0)


def isProduct(num):
    cnt = 0
    i = 2
    while cnt < 2 and i * i <= num:
        while (num % i == 0):
            num //= i
            cnt += 1
        i += 1
    if (num > 1):
        cnt += 1
    return cnt == 2


def findNumbers(N):
    vec = []
    for i in range(1, N + 1):
        if (isProduct(i) and not isPerfectSquare(i)):
            vec .append(i)
    for i in range(len(vec)):
        print(vec[i], end=" ")


if __name__ == "__main__":
    N = 30
    findNumbers(N)

# Expected output:
6 10 14 15 21 22 26
# program to find the nth term of the series 3 , 20 , 63 , 144 , 230 , â €¦ â €¦ | calculate nth term of series ; return final sum ; 
def nthTerm(n):
    return 2 * pow(n, 3) + pow(n, 2)


if __name__ == "__main__":
    N = 3
    print(nthTerm(N))

# Expected output:
63
# find nth number of the series 1 , 6 , 15 , 28 , 45 , ... . . | function for calculating nth term of series ; 
def NthTerm(N):
    x = (2 * N*N) % 1000000009
    return ((x - N + 1000000009) % 1000000009)


if __name__ == "__main__":
    N = 4
    print(NthTerm(N))

# Expected output:
28
# program to print the sum of series | calculate nth term of series ; 
def findSum(N):
    return ((N * (N + 1) * (2 * N - 5) + 4 * N) / 2)


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

# Expected output:
12.0
# program to find the nth term of series | python3 program to find n - th term of the series : 9 , 23 , 45 , 75 , 113 , 159. . ... . ; calculate nth term of series ; 
def nthTerm(N):
    return ((3 * N * N) - (6 * N) + 2)


if __name__ == '__main__':
    n = 3
    print(nthTerm(n))

# Expected output:
11
# Program to find Sum of the series 1 * 3 + 3 * 5 + ... . | Python program to find sum of first n terms ; Sn = n * ( 4 * n * n + 6 * n - 1 ) / 3 ; number of terms to be included in the sum ; find the Sn
def calculateSum(n):
    return (n * (4 * n * n + 6 * n - 1) / 3)


n = 4
print("Sum =", calculateSum(n))

# Expected output:
Sum = 116.0
# triplet with no element divisible by 3 and sum n | function to print a , b and c ; first loop ; check for 1 st number ; second loop ; check for 2 nd number ; third loop ; check for 3 rd number ; 
def printCombination(n):
    for i in range(1, n):
        if (i % 3 != 0):
            for j in range(1, n):
                if (j % 3 != 0):
                    for k in range(1, n):
                        if (k % 3 != 0 and (i + j + k) == n):
                            print(i, j, k)
                            return


n = 233
printCombination(n)

# Expected output:
1 2 230
# program to find the percentage of difference between two numbers | function to calculate the percentage ; 
def percent(a, b):
    result = int(((b - a) * 100) / a)
    return result


if __name__ == "__main__":
    a, b = 20, 25
    print(percent(a, b), "%")

# Expected output:
25 %
# count numbers which can be represented as sum of same parity primes |  ; function to calculate count ; 
def calculate(array, size):
    count = 0
    for i in range(size):
        if (array[i] % 2 == 0 and array[i] != 0 and array[i] != 2):
            count += 1
    return count


if __name__ == "__main__":
    a = [1, 3, 4, 6]
    size = len(a)
    print(calculate(a, size))

# Expected output:
2
# program to find hcf ( highest common factor ) of 2 numbers | recursive function to return gcd of a and b ; 
def gcd(a, b):
    if (b == 0):
        return a
    return gcd(b, a % b)


a = 98
b = 56
if (gcd(a, b)):
    print('GCD of', a, 'and', b, 'is', gcd(a, b))
else:
    print('not found')

# Expected output:
GCD of 98 and 56 is 14
# egg dropping puzzle with 2 eggs and k floors | python3 program to find optimal number of trials for k floors and 2 eggs . ; 

import math as mt


def twoEggDrop(k):
    return mt .ceil((-1.0 + mt .sqrt(1 + 8 * k)) / 2)


k = 100
print(twoEggDrop(k))

# Expected output:
14
# Total number of ways to place X and Y at n places such that no two X are together | Function to return number of ways ; for n = 1 ; for n = 2 ; iterate to find Fibonacci term ; total number of places
def ways(n):
    first = 2
    second = 3
    res = 0
    for i in range(3, n + 1):
        res = first + second
        first = second
        second = res
    return res


n = 7
print("Total ways are: ", ways(n))

# Expected output:
Total ways are:  34
# number of digits in n factorial to the power n | python3 program to find count of digits in n factorial raised to n ; we take sum of logarithms as explained in the approach ; multiply the result with n ; 

import math


def countDigits(n):
    ans = 0
    for i in range(1, n + 1):
        ans += math .log10(i)
    ans = ans * n
    return 1 + math .floor(ans)


if __name__ == "__main__":
    n = 4
    print(countDigits(n))

# Expected output:
6
# check if two people starting from different points ever meet | python3 program to find if two people starting from different positions ever meet or not . ; if speed of a person at a position before other person is smaller , then return false . ; making sure that x1 is greater ; checking if relative speed is a factor of relative distance or not ;  ; 
def everMeet(x1, x2, v1, v2):
    if (x1 < x2 and v1 <= v2):
        return False
        if (x1 > x2 and v1 >= v2):
            return False
            if (x1 < x2):
                swap(x1, x2)
                swap(v1, v2)
                return ((x1 - x2) % (v1 - v2) == 0)

                def swap(a, b):
                    t = a
                    a = b
                    b = t


def main():
    x1, v1, x2, v2 = 5, 8, 4, 7
    if (everMeet(x1, x2, v1, v2)):
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    main()

# Expected output:
No
# sum of range in a series of first odd then even natural numbers | python 3 program to find sum in the given range in the sequence 1 3 5 7 ... . . n 2 4 6. . . n - 1 ; function that returns sum in the range 1 to x in the sequence 1 3 5 7. ... . n 2 4 6. . . n - 1 ; number of odd numbers ; number of extra even numbers required ; 

import math


def sumTillX(x, n):
    odd = math .ceil(n / 2.0)
    if (x <= odd):
        return x * x
    even = x - odd
    return ((odd * odd) + (even * even) + even)


def rangeSum(N, L, R):
    return (sumTillX(R, N) - sumTillX(L - 1, N))


N = 10
L = 1
R = 6
print(rangeSum(N, L, R))

# Expected output:
27