giulio98/xlcost-single-prompt huggingface dataset:
# find array elements with rightmost set bit at the position of the rightmost set bit in k | function to find the mask for finding rightmost set bit in k ; function to find all array elements with rightmost set bit same as that in k ; stores mask of k ; store position of rightmost set bit ; traverse the array ; check if rightmost set bit of current array element is same as position of rightmost set bit in k ; 
def findMask(K):
    mask = 1
    while ((K & mask) == 0):
        mask = mask << 1
    return mask


def sameRightSetBitPos(arr, N, K):
    mask = findMask(K)
    pos = (K & mask)
    for i in range(N):
        if ((arr[i] & mask) == pos):
            print(arr[i], end=" ")


if __name__ == '__main__':
    arr = [3, 4, 6, 7, 9, 12, 15]
    N = len(arr)
    K = 7
    sameRightSetBitPos(arr, N, K)

# Expected output:
3 7 9 15
# count pronic numbers from a given range | python3 program for the above approach ; function to check if x is a pronic number or not ; check for pronic number by multiplying consecutive numbers ; function to count pronic numbers in the range [ a , b ] ; initialise count ; iterate from a to b ; if i is pronic ; increment count ; prcount ; 

import math


def checkPronic(x):
    for i in range(int(math .sqrt(x)) + 1):
        if (x == i * (i + 1)):
            return True
    return False


def countPronic(A, B):
    count = 0
    for i in range(A, B + 1):
        if (checkPronic(i) != 0):
            count += 1
    print(count)


A = 3
B = 20
countPronic(A, B)

# Expected output:
3
# count pronic numbers from a given range | function to count pronic numbers in the range [ a , b ] ; check upto sqrt n ; if product of consecutive numbers are less than equal to num ; return n - 1 ; function to count pronic numbers in the range [ a , b ] ; subtract the count of pronic numbers which are <= ( a - 1 ) from the count f pronic numbers which are <= b ; 
def pronic(num):
    N = int(num ** (1 / 2))
    if (N * (N + 1) <= num):
        return N
    return N - 1


def countPronic(A, B):
    return pronic(B) - pronic(A - 1)


if __name__ == "__main__":
    A = 3
    B = 20
    print(countPronic(A, B))

# Expected output:
3
# count integers from a given range with no odd divisors | function to count integers in the range 1 to n having no odd divisors ; traverse the array ; stores the nearest power of two less than arr [ i ] ; stores the count of integers with no odd divisor for each query ; iterate until poweroftwo is less then or equal to arr [ i ] ; print the answer for the current element ; 
def oddDivisors(arr, N):
    for i in range(N):
        powerOfTwo = 2
        count = 0
        while (powerOfTwo <= arr[i]):
            count += 1
            powerOfTwo = 2 * powerOfTwo
        print(count, end=" ")


if __name__ == "__main__":
    arr = [15, 16, 20, 35]
    N = len(arr)
    oddDivisors(arr, N)

# Expected output:
3 4 4 5
# check if all array elements can be reduced to 0 by repeatedly reducing pairs of consecutive elements by their minimum | function to check if it is possible to convert the array ; traverse the array range [ 1 , n - 1 ] ; if arr [ i ] < arr [ i - 1 ] ; otherwise ; decrement arr [ i ] by arr [ i - 1 ] ; if arr [ n - 1 ] is not equal to zero ; otherwise ; 
def checkPossible(arr, n):
    for i in range(1, n):
        if (arr[i] < arr[i - 1]):
            print("No")
            return
        else:
            arr[i] -= arr[i - 1]
            arr[i - 1] = 0
    if (arr[n - 1] == 0):
        print("Yes")
    else:
        print("No")


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

# Expected output:
Yes
# nearest smaller power of 2 for every digit of a number | python 3 program for the above approach ; function to find the nearest power of two for every digit of a given number ; converting number to string ; traverse the array ; calculate log base 2 of the current digit s [ i ] ; highest power of 2 <= s [ i ] ; ascii conversion ; 

import math


def highestPowerOfTwo(num):
    s = str(num)
    for i in range(len(s)):
        if (s[i] == '0'):
            print("0")
            continue
        lg = int(math .log2(ord(s[i]) - 48))
        p = pow(2, lg)
        print(chr(p + 48), end="")


num = 4317
highestPowerOfTwo(num)

# Expected output:
4214
# maximum prefix sum after k reversals of a given array | python3 program for the above approach ; function to find the maximum prefix sum after k reversals of the array ; stores the required sum ; if k is odd , reverse the array ; store current prefix sum of array ; traverse the array arr [ ] ; add arr [ i ] to currsum ; update maximum prefix sum till now ; prthe answer ; 

import sys


def maxSumAfterKReverse(arr, K, N):
    sum = -sys .maxsize - 1
    if (K & 1):
        arr .reverse()
    currsum = 0
    for i in range(N):
        currsum += arr[i]
        sum = max(sum, currsum)
    print(sum)


arr = [1, 5, 8, 9, 11, 2]
K = 1
N = len(arr)
maxSumAfterKReverse(arr, K, N)

# Expected output:
36
# print all submasks of a given mask | function to print the submasks of n ; 
def SubMasks(N):
    S = N
    while S > 0:
        print(S, end=' ')
        S = (S - 1) & N


if __name__ == '__main__':
    N = 25
    SubMasks(N)

# Expected output:
25 24 17 16 9 8 1
# count pairs from a given range having equal bitwise or and xor values | function to calculate ( x ^ y ) % mod ; initialize result ; update x if it is more than or equal to mod ; if y is odd , multiply x with result ; y must be even now y = y / 2 ; return ( x ^ y ) % mod ; function to count total pairs ; the upper bound is 2 ^ n ; stores the count of pairs ; generate all possible pairs ; find xor of both integers ; find or of both integers ; if both are equal ; increment count ; print count % mod ; 
def power(x, y):
    MOD = 1000000007
    res = 1
    x = x % MOD
    while (y > 0):
        if (y & 1):
            res = (res * x) % MOD
        y = y >> 1
        x = (x * x) % MOD
    return res


def countPairs(N):
    MOD = 1000000007
    high = power(2, N)
    count = 0
    for i in range(high):
        for j in range(high):
            X = (i ^ j)
            Y = (i | j)
            if (X == Y):
                count += 1
    print(count % MOD)


N = 10
countPairs(N)

# Expected output:
59049
# count pairs from a given range having equal bitwise or and xor values | python program for the above approach ; function to find the value of ( x ^ y ) % mod ; initialize result ; update x if it is more than or equal to mod ; if y is odd , multiply x with result ; y must be even now , then update y / 2 ; return ( x ^ y ) % mod ; function to count total pairs ; finding 3 ^ n % 10 ^ 9 + 7 ; 

MOD = 1000000007


def power(x, y):
    res = 1
    x = x % MOD
    while (y > 0):
        if (y % 2 == 1):
            res = (res * x) % MOD
        y = y >> 1
        x = (x * x) % MOD
    return res


def countPairs(N):
    print(power(3, N))


if __name__ == '__main__':
    N = 10
    countPairs(N)

# Expected output:
59049
# minimize swaps required to place largest and smallest array elements at first and last array indices | python 3 program for the above approach ; function to find the minimum count of adjacent swaps to move largest and smallest element at the first and the last index of the array , respectively ; stores the smallest array element ; stores the smallest array element ; stores the last occurrence of the smallest array element ; stores the first occurrence of the largest array element ; traverse the array arr [ ] ; if a [ i ] is less than min_element ; update min_element ; update min_ind ; if a [ i ] is greater than max_element ; update max_element ; update max_ind ; if max_ind is equal to min_ind ; return 0 ; if max_ind is greater than min_ind ; otherwise ; 

import sys


def minimumMoves(a, n):
    min_element = sys .maxsize
    max_element = -sys .maxsize - 1
    min_ind = -1
    max_ind = -1
    for i in range(n):
        if (a[i] <= min_element):
            min_element = a[i]
            min_ind = i
        if (a[i] > max_element):
            max_element = a[i]
            max_ind = i
    if (max_ind == min_ind):
        return 0
    elif (max_ind > min_ind):
        return max_ind + (n - min_ind - 2)
    else:
        return max_ind + n - min_ind - 1


if __name__ == '__main__':
    arr = [35, 46, 17, 23]
    N = len(arr)
    print(minimumMoves(arr, N))

# Expected output:
2
# find the index of the smallest element to be removed to make sum of array divisible by k | function to find index of the smallest array element required to be removed to make sum divisible by k ; stores sum of array elements ; stores index of the smallest element removed from the array to make sum divisible by k ; stores the smallest element removed from the array to make sum divisible by k ; traverse the array , arr [ ] ; update sum ; traverse the array arr [ ] ; calculate remaining sum ; if sum is divisible by k ; if res == - 1 or mini is greater than arr [ i ] ; update res and mini ; 
def findIndex(arr, n, K):
    sum = 0
    res = -1
    mini = 1e9
    for i in range(n):
        sum += arr[i]
    for i in range(n):
        temp = sum - arr[i]
        if (temp % K == 0):
            if (res == -1 or mini > arr[i]):
                res = i + 1
                mini = arr[i]
    return res


arr = [14, 7, 8, 2, 4]
K = 7
N = len(arr)
print(findIndex(arr, N, K))

# Expected output:
2
# count subarrays having a single distinct element that can be obtained from a given array | python 3 program for the above approach ; function to count subarrays of single distinct element into which given array can be split ; stores the count ; stores frequency of array elements ; traverse the array ; traverse the map ; increase count of subarrays by ( frequency - 1 ) ; 

from collections import defaultdict


def divisionalArrays(arr, N):
    sum = N
    mp = defaultdict(int)
    for i in range(N):
        mp[arr[i]] += 1
    for x in mp:
        if (mp[x] > 1):
            sum += mp[x] - 1
    print(sum)


if __name__ == "__main__":
    arr = [1, 1, 3]
    N = len(arr)
    divisionalArrays(arr, N)

# Expected output:
4
# count inversions in a sequence generated by appending given array k times | function to count the number of inversions in k copies of given array ; stores count of inversions in the given array ; stores the count of pairs of distinct array elements ; traverse the array ; generate each pair ; check for each pair , if the condition is satisfied or not ; if pairs consist of distinct elements ; count inversiosn in the sequence ; prthe answer ; given array ; 
def totalInversions(arr, K, N):
    inv = 0
    X = 0
    for i in range(N):
        for j in range(N):
            if (arr[i] > arr[j] and i < j):
                inv += 1
            if (arr[i] > arr[j]):
                X += 1
    totalInv = X * K * (K - 1) // 2 + inv * K
    print(totalInv)


arr = [2, 1, 3]
K = 3
N = len(arr)
totalInversions(arr, K, N)

# Expected output:
12
# count ways to generate an array having distinct elements at m consecutive indices | modular function to calculate factorial ; stores factorial of n ; iterate over the range [ 1 , n ] ; update result ; function to count ways to replace array elements having 0 s with non - zero elements such that any m consecutive elements are distinct ; store m consecutive distinct elements such that arr [ i ] is equal to b [ i % m ] ; stores frequency of array elements ; traverse the array arr ; if arr [ i ] is non - zero ; if b [ i % m ] is equal to 0 ; update b [ i % m ] ; update frequency of arr [ i ] ; if a duplicate element found in m consecutive elements ; handling the case of inequality ; stores count of 0 s in b ; traverse the array , b ; if b [ i ] is 0 ; update cnt ; calculate factorial ; 
def Fact(N):
    result = 1
    for i in range(1, N + 1):
        result = (result * i)
    return result


def numberOfWays(M, arr, N):
    B = [0] * (M)
    counter = [0] * (M + 1)
    for i in range(0, N):
        if (arr[i] != 0):
            if (B[i % M] == 0):
                B[i % M] = arr[i]
                counter[arr[i]] += 1
                if (counter[arr[i]] > 1):
                    print(0)
                    return
            elif (B[i % M] != arr[i]):
                print(0)
                return
    cnt = 0
    for i in range(0, M):
        if (B[i] == 0):
            cnt += 1
    print(Fact(cnt))


if __name__ == '__main__':
    M = 4
    arr = [1, 0, 3, 0, 0]
    N = len(arr)
    numberOfWays(M, arr, N)

# Expected output:
2
# maximize sum by traversing diagonally from each cell of a given matrix | function to find the maximum sum ; loop to traverse through the upper triangular matrix and update the maximum sum to ans ; traverse through the lower triangular matrix ; 
def MaximumSum(arr, n):
    ans = 0
    for i in range(n):
        x, y, sum = 0, i, 0
        for j in range(i, n):
            sum, x, y = sum + arr[x][y], x + 1, y + 1
        if (sum > ans):
            ans = sum
    for i in range(1, n):
        x, y, sum = i, 0, 0
        for j in range(i, n):
            sum, x, y = sum + arr[x][y], x + 1, y + 1
        if (sum > ans):
            ans = sum
    return ans


if __name__ == '__main__':
    arr = [[1, 2, 3], [3, 5, 10], [1, 3, 5]]
    n = len(arr)
    print(MaximumSum(arr, n))

# Expected output:
12
# count array elements exceeding all previous elements as well as the next array element | function to count array elements satisfying the given condition ; if there is only one array element ; traverse the array ; update the maximum element encountered so far ; count the number of array elements strictly greater than all previous and immediately next elements ; print the count ; 
def numberOfIntegers(arr, N):
    cur_max = 0
    count = 0
    if (N == 1):
        count = 1
    else:
        for i in range(N - 1):
            if (arr[i] > cur_max):
                cur_max = arr[i]
                if (arr[i] > arr[i + 1]):
                    count += 1
        if (arr[N - 1] > cur_max):
            count += 1
    print(count)


arr = [1, 2, 0, 7, 2, 0, 2, 0]
N = len(arr)
numberOfIntegers(arr, N)

# Expected output:
2
# count ways to represent n as sum of powers of 2 | python3 program for above implementation ; base cases ; check if 2 ^ k can be used as one of the numbers or not ; otherwise ; count number of ways to n using 2 ^ k - 1 ; 

from math import log2


def numberOfWays(n, k):
    if (n == 0):
        return 1
    if (k == 0):
        return 1
    if (n >= pow(2, k)):
        curr_val = pow(2, k)
        return numberOfWays(n - curr_val, k) + numberOfWays(n, k - 1)
    else:
        return numberOfWays(n, k - 1)


if __name__ == '__main__':
    n = 4
    k = log2(n)
    print(numberOfWays(n, k))

# Expected output:
4
# count ways to obtain triplets with positive product consisting of at most one negative element | function to calculate possible number of triplets ; counting frequency of positive numbers in array ; if current array element is positive ; increment frequency ; select a triplet from freq elements such that i < j < k . ; 
def possibleTriplets(arr, N):
    freq = 0
    for i in range(N):
        if (arr[i] > 0):
            freq += 1
    return (freq * (freq - 1) * (freq - 2)) // 6


if __name__ == '__main__':
    arr = [2, 5, -9, -3, 6]
    N = len(arr)
    print(possibleTriplets(arr, N))

# Expected output:
1
# maximize difference between sum of even and odd | function to find the maximum possible difference between sum of even and odd indices ; convert arr [ ] o 1 - based indexing ; reverse the array ; convert arr [ ] o 1 based index ; reverse the array ; stores maximum difference between sum of even and odd indexed elements ; traverse the array ; if arr [ i ] is local maxima ; update maxdiff ; if arr [ i ] is local minima ; update maxdiff ; 
def maxPossibleDiff(arr, N):
    arr .append(-1)
    arr = arr[::-1]
    arr .append(-1)
    arr = arr[::-1]
    maxDiff = 0
    for i in range(1, N + 1):
        if (arr[i] > arr[i - 1] and arr[i] > arr[i + 1]):
            maxDiff += arr[i]
        if (arr[i] < arr[i - 1] and arr[i] < arr[i + 1]):
            maxDiff -= arr[i]
    print(maxDiff)


if __name__ == '__main__':
    arr = [3, 2, 1, 4, 5, 2, 1, 7, 8, 9]
    N = len(arr)
    maxPossibleDiff(arr, N)

# Expected output:
15
# reverse all elements of given circular array starting from index k | function to print array arr [ ] ; print the array ; function to reverse elements of given circular array starting from index k ; initialize two variables as start = k and end = k - 1 ; initialize count = n / 2 ; loop while count > 0 ; swap the elements at index ( start % n ) and ( end % n ) ; update the start and end ; if end equals to - 1 set end = n - 1 ; print the circular array ; 
def printArray(arr, N):
    for i in range(N):
        print(arr[i], end=" ")


def reverseCircularArray(arr, N, K):
    start, end = K, K - 1
    count = N // 2
    while (count):
        temp = arr[start % N]
        arr[start % N] = arr[end % N]
        arr[end % N] = temp
        start += 1
        end -= 1
        if (end == -1):
            end = N - 1
        count -= 1
    printArray(arr, N)


if __name__ == '__main__':
    arr = [3, 5, 2, 4, 1]
    K = 2
    N = len(arr)
    reverseCircularArray(arr, N, K)

# Expected output:
4 2 5 3 1
# generate a matrix such that given matrix elements are equal to bitwise or of all corresponding row and column elements of generated matrix | function to find the matrix , a [ ] [ ] satisfying the given conditions ; store the final matrix ; initialize all the elements of the matrix a with 1 ; traverse the matrix b [ ] [ ] row - wise ; if b [ i ] [ j ] is equal to 0 ; mark all the elements of ith row of a [ ] [ ] as 0 ; mark all the elements of jth column of a [ ] [ ] as 0 ; check if the matrix b [ ] [ ] can be made using matrix a [ ] [ ] ; store the bitwise or of all elements of a [ ] [ ] in ith row and jth column ; traverse through ith row ; traverse through jth column ; if b [ i ] [ j ] is not equal to c , pr " not ▁ possible " ; print the final matrix a [ ] [ ] ; 
def findOriginalMatrix(B, N, M):
    A = [[0] * M] * N
    for i in range(N):
        for j in range(M):
            A[i][j] = 1
    for i in range(N):
        for j in range(M):
            if (B[i][j] == 0):
                for k in range(M):
                    A[i][k] = 0
                for k in range(N):
                    A[k][j] = 0
    for i in range(N):
        for j in range(M):
            c = 0
            for k in range(M):
                if (c == 1):
                    break
                c += A[i][k]
            for k in range(N):
                if (c == 1):
                    break
                c += A[k][j]
            if (c != B[i][j]):
                print("Not Possible")
                return
    for i in range(N):
        for j in range(M):
            print(A[i][j], end=" ")
        print()


B = [[1, 1, 1], [1, 1, 1]]
N = len(B)
M = len(B[0])
findOriginalMatrix(B, N, M)

# Expected output:
1 1 1 
1 1 1
# minimize cost to make x and y equal by given increments | function to find gcd of x and y ; function to find lcm of x and y ; function to find minimum cost ; subtracted initial cost of x ; subtracted initial cost of y ; 
def gcd(x, y):
    if (y == 0):
        return x
    return gcd(y, x % y)


def lcm(x, y):
    return (x * y) // gcd(x, y)


def minimumCost(x, y):
    lcm_ = lcm(x, y)
    costx = (lcm_ - x) // x
    costy = (lcm_ - y) // y
    return costx + costy


if __name__ == "__main__":
    x = 5
    y = 17
    print(minimumCost(x, y))

# Expected output:
20
# find gcd between the sum of two given integers raised to the power of n and their difference | python program for the above approach ; function to find the value of ( a ^ n ) % d ; stores the value of ( a ^ n ) % d ; calculate the value of ( a ^ n ) % d ; if n is odd ; update res ; update a ; update n ; function to find the gcd of ( p ^ n + q ^ n ) and p - q mod d ; if p and q are equal ; stores gcd of ( p ^ n + q ^ n ) and ( p - q ) mod d ; stores the value of ( p - q ) ; stores square root of num ; find the divisors of num . ; if i divides num ; stores power of ( p ^ n ) mod i ; stores power of ( q ^ n ) mod i ; stores power of ( p ^ n + q ^ n ) mod i ; if ( p ^ n + q ^ n ) is divisible by i ; calculate the largest divisor . ; if i divides num , ( num / i ) also divides num . hence , calculate temp . ; if ( p ^ n + q ^ n ) is divisible by ( num / i ) ; calculate the largest divisor . ; 

import math
mod = 1000000007


def power(a, n, d):
    res = 1
    while (n != 0):
        if ((n % 2) != 0):
            res = ((res % d) * (a % d)) % d
        a = ((a % d) * (a % d)) % d
        n /= 2
    return res


def gcd(p, q, n):
    if (p == q):
        return (power(p, n, mod) + power(q, n, mod)) % mod
    candidate = 1
    num = p - q
    sq = (int)(math .sqrt(num))
    for i in range(1, sq):
        if (num % i == 0):
            X = power(p, n, i)
            Y = power(q, n, i)
            temp = (X + Y) % i
            if (temp == 0):
                candidate = max(candidate, i)
            temp = (power(p, n, num / i) + power(q, n, num / i)) % (num / i)
            if (temp == 0):
                candidate = max(candidate, num / i)
    return candidate % mod


if __name__ == '__main__':
    p = 10
    q = 6
    n = 5
    print((int)(gcd(p, q, n)))

# Expected output:
4
# split given isosceles triangle of height h into n equal parts | function to divide the isosceles triangle in equal parts by making n - 1 cuts parallel to the base ; iterate over the range [ 1 , n - 1 ] ; 
def findPoint(n, h):
    for i in range(1, n):
        print("{0:.2f}".format(((i / n)**0.5) * h), end=' ')


if __name__ == '__main__':
    n = 3
    h = 2
    findPoint(n, h)

# Expected output:
1.15 1.63
# count all possible values of k less than y such that gcd ( x , y ) = gcd ( x + k , y ) | function to find the gcd of a and b ; function to find the number of ks ; find gcd ; calculating value of totient function for n ; 
def gcd(a, b):
    if (b == 0):
        return a
    return gcd(b, a % b)


def calculateK(x, y):
    g = gcd(x, y)
    n = y // g
    res = n
    i = 2
    while i * i <= n:
        if (n % i == 0):
            res -= (res // i)
            while (n % i == 0):
                n //= i
        i += 1
    if (n != 1):
        res -= (res // n)
    return res


if __name__ == "__main__":
    x = 3
    y = 15
    print(calculateK(x, y))

# Expected output:
4
# check if a float value is equivalent to an integer value | function to check if n is equivalent to an integer ; convert float value of n to integer ; if n is not equivalent to any integer ; 
def isInteger(N):
    X = int(N)
    temp2 = N - X
    if (temp2 > 0):
        return False
    return True


if __name__ == '__main__':
    N = 1.5
    if (isInteger(N)):
        print("YES")
    else:
        print("NO")

# Expected output:
NO
# find the nearest power of 2 for every array element | python program to implement the above approach ; function to find the nearest power of two for every array element ; traverse the array ; calculate log of current array element ; find the nearest ; 

import math


def nearestPowerOfTwo(arr, N):
    for i in range(N):
        lg = (int)(math .log2(arr[i]))
        a = (int)(math .pow(2, lg))
        b = (int)(math .pow(2, lg + 1))
        if ((arr[i] - a) < (b - arr[i])):
            print(a, end=" ")
        else:
            print(b, end=" ")


arr = [5, 2, 7, 12]
N = len(arr)
nearestPowerOfTwo(arr, N)

# Expected output:
4 2 8 16
# flip bits of the sum of count of set bits of two given numbers | python3 program for the above approach ; function to count number of set bits in integer ; variable for counting set bits ; function to invert bits of a number ; calculate number of bits of n - 1 ; ; function to invert the sum of set bits in a and b ; stores sum of set bits ; 

import math


def countSetBits(n):
    count = 0
    while (n != 0):
        n &= (n - 1)
        count += 1
    return count


def invertBits(n):
    x = (int)(math .log(n) / math .log(2))
    m = 1 << x
    m = m | m - 1
    n = n ^ m
    return n


def invertSum(A, B):
    temp = countSetBits(A) + countSetBits(B)
    print(invertBits(temp))


A = 5
B = 7
invertSum(A, B)

# Expected output:
2