giulio98/xlcost-single-prompt huggingface dataset:
# maximize the maximum subarray sum after removing atmost one element | python3 implementation of the approach ; function to return the maximum sub - array sum ; initialized ; traverse in the array ; increase the sum ; if sub - array sum is more than the previous ; if sum is negative ; function that returns the maximum sub - array sum after removing an element from the same sub - array ; maximum sub - array sum using kadane 's algorithm ; re - apply kadane 's with minor changes ; increase the sum ; if sub - array sum is greater than the previous ; if elements are 0 , no removal ; if elements are more , then store the minimum value in the sub - array obtained till now ; if sum is negative ; re - initialize everything ; 

import sys


def maxSubArraySum(a, size):
    max_so_far = -(sys .maxsize - 1)
    max_ending_here = 0
    for i in range(size):
        max_ending_here = max_ending_here + a[i]
        if (max_so_far < max_ending_here):
            max_so_far = max_ending_here
        if (max_ending_here < 0):
            max_ending_here = 0
    return max_so_far


def maximizeSum(a, n):
    cnt = 0
    mini = sys .maxsize
    minSubarray = sys .maxsize
    sum = maxSubArraySum(a, n)
    max_so_far = -(sys .maxsize - 1)
    max_ending_here = 0
    for i in range(n):
        max_ending_here = max_ending_here + a[i]
        cnt += 1
        minSubarray = min(a[i], minSubarray)
        if (sum == max_ending_here):
            if (cnt == 1):
                mini = min(mini, 0)
            else:
                mini = min(mini, minSubarray)
        if (max_ending_here < 0):
            max_ending_here = 0
            cnt = 0
            minSubarray = sys .maxsize
    return sum - mini


if __name__ == "__main__":
    a = [1, 2, 3, -2, 3]
    n = len(a)
    print(maximizeSum(a, n))

# Expected output:
9
# 3 | function that returns true if n is an osiris number ; 3 rd digit ; 2 nd digit ; 1 st digit ; check the required condition ; 
def isOsiris(n):
    a = n % 10
    b = (n // 10) % 10
    c = n // 100
    digit_sum = a + b + c
    if (n == (2 * (digit_sum) * 11)):
        return True
    return False


if __name__ == '__main__':
    n = 132
    if isOsiris(n):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# dudeney numbers | function that returns true if n is a dudeney number ; if n is not a perfect cube ; last digit ; update the digit sum ; remove the last digit ; if cube root of n is not equal to the sum of its digits ; 
def isDudeney(n):
    cube_rt = int(round((pow(n, 1.0 / 3.0))))
    if cube_rt * cube_rt * cube_rt != n:
        return False
    dig_sum = 0
    temp = n
    while temp > 0:
        rem = temp % 10
        dig_sum += rem
        temp //= 10
    if cube_rt != dig_sum:
        return False
    return True


if __name__ == '__main__':
    n = 17576
    if isDudeney(n):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# number of triangles possible with given lengths of sticks which are powers of 2 | function to return the number of positive area triangles ; to store the count of total triangles ; to store the count of pairs of sticks with equal lengths ; back - traverse and count the number of triangles ; count the number of pairs ; if we have one remaining stick and we have a pair ; count 1 triangle ; reduce one pair ; count the remaining triangles that can be formed ; 
def countTriangles(a, n):
    cnt = 0
    pairs = 0
    for i in range(n - 1, -1, -1):
        pairs += a[i] // 2
        if (a[i] % 2 == 1 and pairs > 0):
            cnt += 1
            pairs -= 1
    cnt += (2 * pairs) // 3
    return cnt


a = [1, 2, 2, 2, 2]
n = len(a)
print(countTriangles(a, n))

# Expected output:
3
# smallest n digit number which is a multiple of 5 | function to return the smallest n digit number which is a multiple of 5 ; 
def smallestMultiple(n):
    if (n == 1):
        return 5
    return pow(10, n - 1)


n = 4
print(smallestMultiple(n))

# Expected output:
1000
# find hcf of two numbers without using recursion or euclidean algorithm | function to return the hcf of x and y ; minimum of the two numbers ; if both the numbers are divisible by the minimum of these two then the hcf is equal to the minimum ; highest number between 2 and minimum / 2 which can divide both the numbers is the required hcf ; if both the numbers are divisible by i ; 1 divides every number ; 
def getHCF(x, y):
    minimum = min(x, y)
    if (x % minimum == 0 and y % minimum == 0):
        return minimum
    for i in range(minimum // 2, 1, -1):
        if (x % i == 0 and y % i == 0):
            return i
    return 1


x, y = 16, 32
print(getHCF(x, y))

# Expected output:
16
# check if product of first n natural numbers is divisible by their sum | python 3 implementation of the approach ; function that returns true if n is prime ; corner cases ; this is checked so that we can skip middle five numbers in below loop ; function that return true if the product of the first n natural numbers is divisible by the sum of first n natural numbers ; 

from math import sqrt


def isPrime(n):
    if (n <= 1):
        return False
    if (n <= 3):
        return True
    if (n % 2 == 0 or n % 3 == 0):
        return False
    for i in range(5, int(sqrt(n)) + 1, 6):
        if (n % i == 0 or n % (i + 2) == 0):
            return False
    return True


def isDivisible(n):
    if (isPrime(n + 1)):
        return False
    return True


if __name__ == '__main__':
    n = 6
    if (isDivisible(n)):
        print("Yes")
    else:
        print("No")

# Expected output:
No
# maximum sum of cocktail glass in a 2d matrix | python 3 implementation of the approach ; function to return the maximum sum of a cocktail glass ; if no cocktail glass is possible ; initialize max_sum with the mini ; here loop runs ( r - 2 ) * ( c - 2 ) times considering different top left cells of cocktail glasses ; considering mat [ i ] [ j ] as the top left cell of the cocktail glass ; update the max_sum ; 

import sys
R = 5
C = 5


def findMaxCock(ar):
    if (R < 3 or C < 3):
        return -1
    max_sum = -sys .maxsize - 1
    for i in range(R - 2):
        for j in range(C - 2):
            sum = ((ar[i][j] + ar[i][j + 2]) + (ar[i + 1][j + 1]) +
                   (ar[i + 2][j] + ar[i + 2][j + 1] + ar[i + 2][j + 2]))
            max_sum = max(max_sum, sum)
    return max_sum


if __name__ == '__main__':
    ar = [[0, 3, 0, 6, 0], [0, 1, 1, 0, 0], [
        1, 1, 1, 0, 0], [0, 0, 2, 0, 1], [0, 2, 0, 1, 3]]
    print(findMaxCock(ar))

# Expected output:
12
# find the number of sub arrays in the permutation of first n natural numbers such that their median is m | function to return the count of sub - arrays in the given permutation of first n natural numbers such that their median is m ; if element is less than m ; if element greater than m ; if m is found ; count the answer ; increment sum ; 
def segments(n, p, m):
    c = dict()
    c[0] = 1
    has = False
    Sum = 0
    ans = 0
    for r in range(n):
        if (p[r] < m):
            Sum -= 1
        elif (p[r] > m):
            Sum += 1
        if (p[r] == m):
            has = True
        if (has):
            if (Sum in c .keys()):
                ans += c[Sum]
            if Sum - 1 in c .keys():
                ans += c[Sum - 1]
        else:
            c[Sum] = c .get(Sum, 0) + 1
    return ans


a = [2, 4, 5, 3, 1]
n = len(a)
m = 4
print(segments(n, a, m))

# Expected output:
4
# program to calculate the number of odd days in 
def oddDays(N):
    hund1 = N // 100
    hund4 = N // 400
    leap = N >> 2
    ordd = N - leap
    if (hund1):
        ordd += hund1
        leap -= hund1
    if (hund4):
        ordd -= hund4
        leap += hund4
    days = ordd + leap * 2
    odd = days % 7
    return odd


N = 100
print(oddDays(N))

# Expected output:
5
# largest ellipse that can be inscribed within a rectangle which in turn is inscribed within a semicircle | function to find the area of the biggest ellipse ; the radius cannot be negative ; area of the ellipse ; 
def ellipsearea(r):
    if (r < 0):
        return -1
    a = (3.14 * r * r) / 4
    return a


if __name__ == "__main__":
    r = 5
    print(ellipsearea(r))

# Expected output:
19.625
# find the minimum number of operations required to make all array elements equal | python3 implementation of the approach ; function to return the minimum operations required to make all array elements equal ; to store the frequency of all the array elements ; traverse through array elements and update frequencies ; to store the maximum frequency of an element from the array ; traverse through the map and find the maximum frequency for any element ; return the minimum operations required ; 

import sys


def minOperations(arr, n):
    mp = dict .fromkeys(arr, 0)
    for i in range(n):
        mp[arr[i]] += 1
    maxFreq = -(sys .maxsize - 1)
    for key in mp:
        maxFreq = max(maxFreq, mp[key])
    return (n - maxFreq)


if __name__ == "__main__":
    arr = [2, 4, 6]
    n = len(arr)
    print(minOperations(arr, n))

# Expected output:
2
# count all prefixes of the given binary array which are divisible by x | function to return the count of total binary prefix which are divisible by x ; initialize with zero ; convert all prefixes to decimal ; if number is divisible by x then increase count ; 
def CntDivbyX(arr, n, x):
    number = 0
    count = 0
    for i in range(n):
        number = number * 2 + arr[i]
        if ((number % x == 0)):
            count += 1
    return count


if __name__ == '__main__':
    arr = [1, 0, 1, 0, 1, 1, 0]
    n = len(arr)
    x = 2
    print(CntDivbyX(arr, n, x))

# Expected output:
3
# count consecutive pairs of same elements | function to return the count of consecutive elements in the array which are equal ; if consecutive elements are same ; 
def countCon(ar, n):
    cnt = 0
    for i in range(n - 1):
        if (ar[i] == ar[i + 1]):
            cnt += 1
    return cnt


ar = [1, 2, 2, 3, 4, 4, 5, 5, 5, 5]
n = len(ar)
print(countCon(ar, n))

# Expected output:
5
# reduce the fraction to its lowest form | python3 program to reduce a fraction x / y to its lowest form ; function to reduce a fraction to its lowest form ; 

from math import gcd


def reduceFraction(x, y):
    d = gcd(x, y)
    x = x // d
    y = y // d
    print("x =", x, ", y =", y)


if __name__ == "__main__":
    x = 16
    y = 10
    reduceFraction(x, y)

# Expected output:
x = 8 , y = 5
# times required by simple interest for the principal to become y times itself | function to return the no . of years ; 
def noOfYears(t1, n1, t2):
    years = (t2 - 1) * n1 / (t1 - 1)
    return years


if __name__ == "__main__":
    T1, N1, T2 = 3, 5, 6
    print(noOfYears(T1, N1, T2))

# Expected output:
12.5
# check if a given number divides the sum of the factorials of its digits | function that returns true if n divides the sum of the factorials of its digits ; to store factorials of digits ; to store sum of the factorials of the digits ; store copy of the given number ; store sum of the factorials of the digits ; if it is divisible ; 
def isPossible(n):
    fac = [0 for i in range(10)]
    fac[0] = 1
    fac[1] = 1
    for i in range(2, 10, 1):
        fac[i] = fac[i - 1] * i
    sum = 0
    x = n
    while (x):
        sum += fac[x % 10]
        x = int(x / 10)
    if (sum % n == 0):
        return True
    return False


if __name__ == '__main__':
    n = 19
    if (isPossible(n)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# find the longest subsequence of an array having lcm at most k | python3 implementation of the approach ; function to find the longest subsequence having lcm less than or equal to k ; map to store unique elements and their frequencies ; update the frequencies ; array to store the count of numbers whom 1 <= x <= k is a multiple of ; check every unique element ; find all its multiples <= k ; store its frequency ; obtain the number having maximum count ; condition to check if answer doesn 't exist ; print the answer ; 

from collections import defaultdict


def findSubsequence(arr, n, k):
    M = defaultdict(lambda: 0)
    for i in range(0, n):
        M[arr[i]] += 1
    numCount = [0] * (k + 1)
    for p in M:
        if p <= k:
            i = 1
            while p * i <= k:
                numCount[p * i] += M[p]
                i += 1
        else:
            break
    lcm, length = 0, 0
    for i in range(1, k + 1):
        if numCount[i] > length:
            length = numCount[i]
            lcm = i
    if lcm == 0:
        print(-1)
    else:
        print("LCM = {0}, Length = {1}".format(lcm, length))
        print("Indexes = ", end="")
        for i in range(0, n):
            if lcm % arr[i] == 0:
                print(i, end=" ")


if __name__ == "__main__":
    k = 14
    arr = [2, 3, 4, 5]
    n = len(arr)
    findSubsequence(arr, n, k)

# Expected output:
LCM = 12, Length = 3
Indexes = 0 1 2
# count number of binary strings of length n having only 0 ' s ▁ and ▁ 1' s | python 3 implementation of the approach ; iterative function to calculate ( x ^ y ) % p in o ( log y ) ; x = x % p update x if it is more than or equal to p ; if y is odd , multiply x with result ; y must be even now y = y >> 1 y = y / 2 ; function to count the number of binary strings of length n having only 0 ' s ▁ and ▁ 1' s ; 

mod = 1000000007


def power(x, y, p):
    res = 1

    x = x % p

    while (y > 0):
        if (y & 1):
            res = (res * x) % p
        y = y >> 1
        x = (x * x) % p

    return res


def findCount(N):
    count = power(2, N, mod)
    return count


if __name__ == '__main__':
    N = 25
    print(findCount(N))

# Expected output:
33554432
# xor of all the elements in the given range [ l , r ] | function to return the most significant bit ; function to return the required xor ; finding the msb ; value of the current bit to be added ; to store the final answer ; loop for case 1 ; edge case when both the integers lie in the same segment of continuous 1 s ; to store whether parity of count is odd ; updating the answer if parity is odd ; updating the number to be added ; case 2 ; 
def msb(x):
    ret = 0
    while ((x >> (ret + 1)) != 0):
        ret = ret + 1
    return ret


def xorRange(l, r):
    max_bit = msb(r)
    mul = 2
    ans = 0
    for i in range(1, max_bit + 1):
        if ((l // mul) * mul == (r // mul) * mul):
            if ((((l & (1 << i)) != 0) and (r - l + 1) % 2 == 1)):
                ans = ans + mul
            mul = mul * 2
            continue
        odd_c = 0
        if (((l & (1 << i)) != 0) and l % 2 == 1):
            odd_c = (odd_c ^ 1)
        if (((r & (1 << i)) != 0) and r % 2 == 0):
            odd_c = (odd_c ^ 1)
        if (odd_c):
            ans = ans + mul
        mul = mul * 2
    zero_bit_cnt = (r - l + 1) // 2
    if ((l % 2 == 1) and (r % 2 == 1)):
        zero_bit_cnt = zero_bit_cnt + 1
    if (zero_bit_cnt % 2 == 1):
        ans = ans + 1
    return ans


l = 1
r = 4
print(xorRange(l, r))

# Expected output:
4
# xor of all the elements in the given range [ l , r ] | function to return the required xor ; modulus operator are expensive on most of the computers . n & 3 will be equivalent to n % 4 n % 4 ; if n is a multiple of 4 ; if n % 4 gives remainder 1 ; if n % 4 gives remainder 2 ; if n % 4 gives remainder 3 ; 
def computeXOR(n):
    switch = {0: n, 1: 1, 2: n + 1, 3: 0, }
    return switch .get(n & 3, "")


l = 1
r = 4
print(computeXOR(r) ^ computeXOR(l - 1))

# Expected output:
4
# find the number of integers from 1 to n which contains digits 0 ' s ▁ and ▁ 1' s only | function to find the number of integers from 1 to n which contains 0 ' s ▁ and ▁ 1' s only ; if number is greater than n ; otherwise add count this number and call two functions ; 
def countNumbers(x, n):
    if x > n:
        return 0
    return (1 + countNumbers(x * 10, n) + countNumbers(x * 10 + 1, n))


if __name__ == '__main__':
    n = 120
    print(countNumbers(1, n))

# Expected output:
7
# check if factorial of n is divisible by the sum of squares of first n natural numbers | python 3 implementation of the approach ; function to count number of times prime p divide factorial n ; lengendre formula ; function to find count number of times all prime p divide summation ; formula for summation of square after removing n and constant 6 ; loop to traverse over all prime p which divide summation ; if number itself is a prime number ; 

from math import sqrt


def checkfact(N, countprime, prime):
    countfact = 0
    if (prime == 2 or prime == 3):
        countfact += 1
    divide = prime
    while (int(N / divide) != 0):
        countfact += int(N / divide)
        divide = divide * divide
    if (countfact >= countprime):
        return True
    else:
        return False


def check(N):
    sumsquares = (N + 1) * (2 * N + 1)
    countprime = 0
    for i in range(2, int(sqrt(sumsquares)) + 1, 1):
        flag = 0
        while (sumsquares % i == 0):
            flag = 1
            countprime += 1
            sumsquares /= i
        if (flag):
            if (checkfact(N - 1, countprime, i) == False):
                return False
            countprime = 0
    if (sumsquares != 1):
        if (checkfact(N - 1, 1, sumsquares) == False):
            return False
    return True


if __name__ == '__main__':
    N = 5
    if (check(N)):
        print("Yes")
    else:
        print("No")

# Expected output:
No
# count the number of non | python3 program to count number of non increasing subarrays ; initialize result ; initialize length of current non - increasing subarray ; traverse through the array ; if arr [ i + 1 ] is less than or equal to arr [ i ] , then increment length ; else update count and reset length ; if last length is more than 1 ; 
def countNonIncreasing(arr, n):
    cnt = 0
    len = 1
    for i in range(0, n - 1):
        if (arr[i + 1] <= arr[i]):
            len += 1
        else:
            cnt += (((len + 1) * len) / 2)
            len = 1
    if (len > 1):
        cnt += (((len + 1) * len) / 2)
    return int(cnt)


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

# Expected output:
10
# minimum array elements to be changed to make recaman 's sequence | python3 implementation of the approach ; first term of the sequence is always 0 ; fill remaining terms using recursive formula ; if arr [ i - 1 ] - i is negative or already exists ; function that returns minimum changes required ; set to store first n recaman numbers ; generate and store first n recaman numbers ; insert first n recaman numbers to set ; if current element of the array is present in the set ; return the remaining number of elements in the set ; 
def recamanGenerator(arr, n):
    arr[0] = 0
    for i in range(1, n):
        temp = arr[i - 1] - i
        j = 0
        for j in range(i):
            if ((arr[j] == temp) or temp < 0):
                temp = arr[i - 1] + i
                break
        arr[i] = temp


def recamanArray(arr, n):
    s = dict()
    recaman = [0 for i in range(n)]
    recamanGenerator(recaman, n)
    for i in range(n):
        s[recaman[i]] = s .get(recaman[i], 0) + 1
    for i in range(n):
        if arr[i] in s .keys():
            del s[arr[i]]
    return len(s)


arr = [7, 11, 20, 4, 2, 1, 8, 6]
n = len(arr)
print(recamanArray(arr, n))

# Expected output:
3
# check if product of array containing prime numbers is a perfect square | function that returns true if the product of all the array elements is a perfect square ; update the frequencies of all the array elements ; if frequency of some element in the array is odd ; 
def isPerfectSquare(arr, n):
    umap = dict .fromkeys(arr, n)
    for key in arr:
        umap[key] += 1
    for key in arr:
        if (umap[key] % 2 == 1):
            return False
    return True


if __name__ == "__main__":
    arr = [2, 2, 7, 7]
    n = len(arr)
    if (isPerfectSquare(arr, n)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# find the good permutation of first n natural numbers | function to print the good permutation of first n natural numbers ; if n is odd ; otherwise ; 
def printPermutation(n):
    if (n % 2 != 0):
        print(-1)
    else:
        for i in range(1, (n // 2) + 1):
            print((2 * i), (2 * i - 1), end=" ")


n = 4
printPermutation(n)

# Expected output:
2 1 4 3
# minimum number operations required to convert n to m | set | python 3 implementation of the above approach ; function to find the minimum number of steps ; if n exceeds m ; if n reaches the target ; the minimum of both the states will be the answer ; 

MAXN = 10000000


def minimumSteps(n, m, a, b):
    if (n > m):
        return MAXN
    if (n == m):
        return 0
    return min(
        1 +
        minimumSteps(
            n *
            a,
            m,
            a,
            b),
        1 +
        minimumSteps(
            n *
            b,
            m,
            a,
            b))


if __name__ == '__main__':
    n = 120
    m = 51840
    a = 2
    b = 3
    print(minimumSteps(n, m, a, b))

# Expected output:
7
# minimum number of given operation required to convert n to m | function to return the minimum operations required ; counting all 2 s ; counting all 3 s ; if q contained only 2 and 3 as the only prime factors then it must be 1 now ; 
def minOperations(n, m):
    if (m % n != 0):
        return -1
    minOperations = 0
    q = int(m / n)
    while (q % 2 == 0):
        q = int(q / 2)
        minOperations += 1
    while (q % 3 == 0):
        q = int(q / 3)
        minOperations += 1
    if (q == 1):
        return minOperations
    return -1


if __name__ == '__main__':
    n = 120
    m = 51840
    print(minOperations(n, m))

# Expected output:
7
# sum of fibonacci numbers in a range | function to return the nth fibonacci number ; function to return the required sum ; to store the sum ; calculate the sum ; 
def fib(n):
    phi = ((1 + (5 ** (1 / 2))) / 2)
    return round((phi ** n) / (5 ** (1 / 2)))


def calculateSum(l, r):
    sum = 0
    for i in range(l, r + 1):
        sum += fib(i)
    return sum


if __name__ == '__main__':
    l, r = 4, 8
    print(calculateSum(l, r))

# Expected output:
50
# largest sphere that can be inscribed within a cube which is in turn inscribed within a right circular cone | program to find the biggest sphere which is inscribed within a cube which in turn inscribed within a right circular cone ; function to find the radius of the sphere ; height and radius cannot be negative ; radius of the sphere ; 

import math


def sphereSide(h, r):
    if h < 0 and r < 0:
        return -1
    R = (((h * r * math .sqrt(2))) / (h + math .sqrt(2) * r) / 2)
    return R


h = 5
r = 6
print(sphereSide(h, r))

# Expected output:
1.5730634642982526
# find the number of ways to divide number into four parts such that a = c and b = d | function to find the number of ways to divide n into four parts such that a = c and b = d ; 
def possibleways(n):
    if (n % 2 == 1):
        return 0
    elif (n % 4 == 0):
        return n // 4 - 1
    else:
        return n // 4


n = 20
print(possibleways(n))

# Expected output:
4
# count sub | function to count sub - arrays whose product is divisible by k ; calculate the product of the current sub - array ; if product of the current sub - array is divisible by k ; 
def countSubarrays(arr, n, K):
    count = 0
    for i in range(n):
        for j in range(i, n):
            product = 1
            for x in range(i, j + 1):
                product *= arr[x]
            if (product % K == 0):
                count += 1
    return count


if __name__ == "__main__":
    arr = [6, 2, 8]
    n = len(arr)
    K = 4
    print(countSubarrays(arr, n, K))

# Expected output:
4