giulio98/xlcost-single-prompt huggingface dataset:
# absolute difference between the first x and last x digits of n | function to find the number of digits in the integer ; function to find the absolute difference ; store the last x digits in last ; count the no . of digits in n ; remove the digits except the first x ; store the first x digits in first ; return the absolute difference between the first and last ; 
def digitsCount(n):
    length = 0
    while (n > 0):
        length += 1
        n //= 10
    return length


def absoluteFirstLast(n, x):
    i = 0
    mod = 1
    while (i < x):
        mod *= 10
        i += 1
    last = n % mod
    length = digitsCount(n)
    while (length != x):
        n //= 10
        length -= 1
    first = n
    return abs(first - last)


if __name__ == "__main__":
    n = 21546
    x = 2
    print(absoluteFirstLast(n, x))

# Expected output:
25
# generate minimum sum sequence of integers with even elements greater | function to print the required sequence ; arr [ ] will hold the sequence sum variable will store the sum of the sequence ; if sum of the sequence is odd ; print the sequence ; 
def make_sequence(N):
    arr = [0] * (N + 1)
    sum = 0
    for i in range(1, N + 1):
        if (i % 2 == 1):
            arr[i] = 1
        else:
            arr[i] = 2
        sum += arr[i]
    if (sum % 2 == 1):
        arr[2] = 3
    for i in range(1, N + 1):
        print(arr[i], end=" ")


if __name__ == "__main__":
    N = 9
    make_sequence(N)

# Expected output:
1 3 1 2 1 2 1 2 1
# count odd and even numbers in a range from l to r | return the number of odd numbers in the range [ l , r ] ; if either r or l is odd ; 
def countOdd(L, R):
    N = (R - L) // 2
    if (R % 2 != 0 or L % 2 != 0):
        N += 1
    return N


if __name__ == "__main__":
    L = 3
    R = 7
    odds = countOdd(L, R)
    evens = (R - L + 1) - odds
    print("Count of odd numbers is", odds)
    print("Count of even numbers is", evens)

# Expected output:
Count of odd numbers is 3
Count of even numbers is 2
# cost of painting n * m grid | function to return the minimum cost ; 
def getMinCost(n, m):
    cost = (n - 1) * m + (m - 1) * n
    return cost


if __name__ == "__main__":
    n, m = 4, 5
    print(getMinCost(n, m))

# Expected output:
31
# minimum operations required to make all the array elements equal | function to return the minimum number of given operation required to make all the array elements equal ; check if all the elements from kth index to last are equal ; finding the 1 st element which is not equal to the kth element ; 
def minOperation(n, k, a):
    for i in range(k, n):
        if (a[i] != a[k - 1]):
            return -1
    for i in range(k - 2, -1, -1):
        if (a[i] != a[k - 1]):
            return i + 1


n = 5
k = 3
a = [2, 1, 1, 1, 1]
print(minOperation(n, k, a))

# Expected output:
1
# number of simple graph with n vertices and m edges | function to return the value of binomial coefficient c ( n , k ) ; since c ( n , k ) = c ( n , n - k ) ; calculate the value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * ... * 1 ] ; 
def binomialCoeff(n, k):
    if (k > n):
        return 0
    res = 1
    if (k > n - k):
        k = n - k
    for i in range(k):
        res *= (n - i)
        res //= (i + 1)
    return res


if __name__ == "__main__":
    N = 5
    M = 1
    P = (N * (N - 1)) // 2
    print(binomialCoeff(P, M))

# Expected output:
10
# increasing sequence with given gcd | function to print the required sequence ; 
def generateSequence(n, g):
    for i in range(1, n + 1):
        print(i * g, end=" ")


if __name__ == "__main__":
    n, g = 6, 5
    generateSequence(n, g)

# Expected output:
5 10 15 20 25 30
# program to find lcm of two fibonnaci numbers | python 3 program to find lcm of fib ( a ) and fib ( b ) ; create an array for memoization ; function to return the n 'th fibonacci number using table f[]. ; base cases ; if fib ( n ) is already computed ; applying recursive formula note value n & 1 is 1 if n is odd , else 0. ; function to return gcd of a and b ; function to return the lcm of fib ( a ) and fib ( a ) ; 

MAX = 1000
f = [0] * MAX


def fib(n):
    if (n == 0):
        return 0
    if (n == 1 or n == 2):
        f[n] = 1
        return f[n]
    if (f[n]):
        return f[n]
    k = (n + 1) // 2 if (n & 1)else 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 gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)


def findLCMFibonacci(a, b):
    return (fib(a) * fib(b)) // fib(gcd(a, b))


if __name__ == "__main__":
    a = 3
    b = 12
    print(findLCMFibonacci(a, b))

# Expected output:
144
# check whether xor of all numbers in a given range is even or odd | function to check if xor of all numbers in range [ l , r ] is even or odd ; count odd numbers in range [ l , r ] ; check if count of odd numbers is even or odd ; 
def isEvenOrOdd(L, R):
    oddCount = (R - L) / 2
    if (R % 2 == 1 or L % 2 == 1):
        oddCount = oddCount + 1
    if (oddCount % 2 == 0):
        return "Even"
    else:
        return "Odd"


L = 5
R = 15
print(isEvenOrOdd(L, R))

# Expected output:
Even
# count number of trailing zeros in ( 1 ^ 1 ) * ( 2 ^ 2 ) * ( 3 ^ 3 ) * ( 4 ^ 4 ) * . . | function to return the number of trailing zeros ; to store the number of 2 s and 5 s ; if we get a factor 2 then we have i number of 2 s because the power of the number is raised to i ; if we get a factor 5 then we have i number of 5 s because the power of the number is raised to i ; take the minimum of them ; 
def trailing_zeros(N):
    count_of_two = 0
    count_of_five = 0
    for i in range(1, N + 1, 1):
        val = i
        while (val % 2 == 0 and val > 0):
            val /= 2
            count_of_two += i
        while (val % 5 == 0 and val > 0):
            val /= 5
            count_of_five += i
    ans = min(count_of_two, count_of_five)
    return ans


if __name__ == '__main__':
    N = 12
    print(trailing_zeros(N))

# Expected output:
15
# print numbers such that no two consecutive numbers are co | python3 implementation of the above approach ; function to generate sieve of eratosthenes ; if prime [ p ] is not changed , then it is a prime ; add the prime numbers to the array b ; function to return the gcd of a and b ; function to print the required sequence of integers ; including the primes in a series of primes which will be later multiplied ; this is done to mark a product as existing ; maximum number of primes that we consider ; for different interval ; for different starting index of jump ; for storing the numbers ; checking for occurrence of a product . also checking for the same prime occurring consecutively ; including the primes in a series of primes which will be later multiplied ; 

limit = 1000000000
MAX_PRIME = 2000000
MAX = 1000000
I_MAX = 50000
mp = {}
b = [0] * MAX
p = [0] * MAX
j = 0
prime = [True] * (MAX_PRIME + 1)


def SieveOfEratosthenes(n):
    global j
    p = 2
    while p * p <= n:
        if (prime[p]):
            for i in range(p * p, n + 1, p):
                prime[i] = False
        p += 1
    for p in range(2, n + 1):
        if (prime[p]):
            b[j] = p
            j += 1


def gcd(a, b):
    if (b == 0):
        return a
    return gcd(b, a % b)


def printSeries(n):
    SieveOfEratosthenes(MAX_PRIME)
    ar = [0] * (I_MAX + 2)
    for i in range(j):
        if ((b[i] * b[i + 1]) > limit):
            break
        p[i] = b[i]
        mp[b[i] * b[i + 1]] = 1
    d = 550
    flag = False
    k = 2
    while (k < d - 1) and not flag:
        m = 2
        while (m < d) and not flag:
            for l in range(m + k, d, k):
                if (((b[l] * b[l + k]) < limit) and (l + k) < d and p[i - 1] != b[l + k]
                        and p[i - 1] != b[l] and ((b[l] * b[l + k] in mp) and mp[b[l] * b[l + k]] != 1)):
                    if (mp[p[i - 1] * b[l]] != 1):
                        p[i] = b[l]
                        mp[p[i - 1] * b[l]] = 1
                        i += 1
                if (i >= I_MAX):
                    flag = True
                    break
            m += 1
        k += 1
    for i in range(n):
        ar[i] = p[i] * p[i + 1]
    for i in range(n - 1):
        print(ar[i], end=" ")
    g = gcd(ar[n - 1], ar[n - 2])
    print(g * 2)


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

# Expected output:
6 15 35 14
# print numbers such that no two consecutive numbers are co | python3 program for the above approach ; function for sieve of eratosthenes ; function to print the required sequence ; store only the required primes ; base condition ; first integer in the list ; second integer in the list ; third integer in the list ; generate ( n - 1 ) th term ; generate nth term ; modify first term ; print the sequence ; 

MAX = 620000
prime = [0] * MAX


def Sieve():
    for i in range(2, MAX):
        if (prime[i] == 0):
            for j in range(2 * i, MAX, i):
                prime[j] = 1


def printSequence(n):
    Sieve()
    v = []
    u = []
    for i in range(13, MAX):
        if (prime[i] == 0):
            v .append(i)
    if (n == 3):
        print(6, 10, 15)
        return
    k = 0
    for k in range(n - 2):
        if (k % 3 == 0):
            u .append(v[k] * 6)
        elif (k % 3 == 1):
            u .append(v[k] * 15)
        else:
            u .append(v[k] * 10)
    u .append(v[k] * 7)
    u .append(7 * 11)
    u[0] = u[0] * 11
    print(*u)


if __name__ == '__main__':
    n = 4
    printSequence(n)

# Expected output:
858 255 119 77
# Midpoint ellipse drawing algorithm | Python3 program for implementing Mid - Point Ellipse Drawing Algorithm ; Initial decision parameter of region 1 ; For region 1 ; Print points based on 4 - way symmetry ; Checking and updating value of decision parameter based on algorithm ; Decision parameter of region 2 ; Plotting points of region 2 ; printing points based on 4 - way symmetry ; Checking and updating parameter value based on algorithm ; To draw a ellipse of major and minor radius 15 , 10 centred at ( 50 , 50 )
def midptellipse(rx, ry, xc, yc):
    x = 0
    y = ry
    d1 = ((ry * ry) - (rx * rx * ry) + (0.25 * rx * rx))
    dx = 2 * ry * ry * x
    dy = 2 * rx * rx * y
    while (dx < dy):
        print("(", x + xc, ",", y + yc, ")")
        print("(", -x + xc, ",", y + yc, ")")
        print("(", x + xc, ",", -y + yc, ")")
        print("(", -x + xc, ",", -y + yc, ")")
        if (d1 < 0):
            x += 1
            dx = dx + (2 * ry * ry)
            d1 = d1 + dx + (ry * ry)
        else:
            x += 1
            y -= 1
            dx = dx + (2 * ry * ry)
            dy = dy - (2 * rx * rx)
            d1 = d1 + dx - dy + (ry * ry)
    d2 = (((ry * ry) * ((x + 0.5) * (x + 0.5))) +
          ((rx * rx) * ((y - 1) * (y - 1))) - (rx * rx * ry * ry))
    while (y >= 0):
        print("(", x + xc, ",", y + yc, ")")
        print("(", -x + xc, ",", y + yc, ")")
        print("(", x + xc, ",", -y + yc, ")")
        print("(", -x + xc, ",", -y + yc, ")")
        if (d2 > 0):
            y -= 1
            dy = dy - (2 * rx * rx)
            d2 = d2 + (rx * rx) - dy
        else:
            y -= 1
            x += 1
            dx = dx + (2 * ry * ry)
            dy = dy - (2 * rx * rx)
            d2 = d2 + dx - dy + (rx * rx)


midptellipse(10, 15, 50, 50)

# Expected output:
( 50 , 65 )
( 50 , 65 )
( 50 , 35 )
( 50 , 35 )
( 51 , 65 )
( 49 , 65 )
( 51 , 35 )
( 49 , 35 )
( 52 , 65 )
( 48 , 65 )
( 52 , 35 )
( 48 , 35 )
( 53 , 64 )
( 47 , 64 )
( 53 , 36 )
( 47 , 36 )
( 54 , 64 )
( 46 , 64 )
( 54 , 36 )
( 46 , 36 )
( 55 , 63 )
( 45 , 63 )
( 55 , 37 )
( 45 , 37 )
( 56 , 62 )
( 44 , 62 )
( 56 , 38 )
( 44 , 38 )
( 57 , 61 )
( 43 , 61 )
( 57 , 39 )
( 43 , 39 )
( 57 , 60 )
( 43 , 60 )
( 57 , 40 )
( 43 , 40 )
( 58 , 59 )
( 42 , 59 )
( 58 , 41 )
( 42 , 41 )
( 58 , 58 )
( 42 , 58 )
( 58 , 42 )
( 42 , 42 )
( 59 , 57 )
( 41 , 57 )
( 59 , 43 )
( 41 , 43 )
( 59 , 56 )
( 41 , 56 )
( 59 , 44 )
( 41 , 44 )
( 59 , 55 )
( 41 , 55 )
( 59 , 45 )
( 41 , 45 )
( 60 , 54 )
( 40 , 54 )
( 60 , 46 )
( 40 , 46 )
( 60 , 53 )
( 40 , 53 )
( 60 , 47 )
( 40 , 47 )
( 60 , 52 )
( 40 , 52 )
( 60 , 48 )
( 40 , 48 )
( 60 , 51 )
( 40 , 51 )
( 60 , 49 )
( 40 , 49 )
( 60 , 50 )
( 40 , 50 )
( 60 , 50 )
( 40 , 50 )
# program to check if a number is divisible by sum of its digits | function to check if the given number is divisible by sum of its digits ; find sum of digits ; check if sum of digits divides n ; 
def isDivisible(n):
    temp = n
    sum = 0
    while (n):
        k = n % 10
        sum += k
        n /= 10
    if (temp % sum == 0):
        return "YES"
    return "NO"


n = 123
print(isDivisible(n))

# Expected output:
NO
# program to check if a number is divisible by sum of its digits | python implementation of above approach ; converting integer to string ; initialising sum to 0 ; traversing through the string ; converting character to int ; comparing number and sum ; 
def getResult(n):
    st = str(n)
    sum = 0
    length = len(st)
    for i in st:
        sum = sum + int(i)
    if (n % sum == 0):
        return "Yes"
    else:
        return "No"


n = 123
print(getResult(n))

# Expected output:
No
# find the final x and y when they are altering under 

import math as mt


def alter(x, y):
    while (True):
        if (x == 0 or y == 0):
            break
        if (x >= 2 * y):
            x = x % (2 * y)
        elif (y >= 2 * x):
            y = y % (2 * x)
        else:
            break
    print("X =", x, ", ", "Y =", y)


x, y = 12, 5
alter(x, y)

# Expected output:
X = 0 ,  Y = 1
# print all multiplicative primes <= n | python 3 implementation of the approach ; function to return the digit product of n ; function to print all multiplicative primes <= n ; create a boolean array " prime [ 0 . . n + 1 ] " . a value in prime [ i ] will finally be false if i is not a prime , else true . ; if prime [ p ] is not changed , then it is a prime ; update all multiples of p ; if i is prime and its digit sum is also prime i . e . i is a multiplicative prime ; 

from math import sqrt


def digitProduct(n):
    prod = 1
    while (n):
        prod = prod * (n % 10)
        n = int(n / 10)
    return prod


def printMultiplicativePrimes(n):
    prime = [True for i in range(n + 1)]
    prime[0] = prime[1] = False
    for p in range(2, int(sqrt(n)) + 1, 1):
        if (prime[p]):
            for i in range(p * 2, n + 1, p):
                prime[i] = False
    for i in range(2, n + 1, 1):
        if (prime[i] and prime[digitProduct(i)]):
            print(i, end=" ")


if __name__ == '__main__':
    n = 10
    printMultiplicativePrimes(n)

# Expected output:
2 3 5 7
# largest number less than or equal to n / 2 which is coprime to n | python3 implementation of the above approacdh ; function to calculate gcd of two number ; function to check if two numbers are coprime or not ; two numbers are coprime if their gcd is 1 ; function to find largest integer less than or equal to n / 2 and coprime with n ; check one by one a numbers less than or equal to n / 2 ; 

import math as mt


def gcd(a, b):
    if (b == 0):
        return a
    else:
        return gcd(b, a % b)


def coPrime(n1, n2):
    if (gcd(n1, n2) == 1):
        return True
    else:
        return False


def largestCoprime(N):
    half = mt .floor(N / 2)
    while (coPrime(N, half) == False):
        half -= 1
    return half


n = 50
print(largestCoprime(n))

# Expected output:
23
# largest number less than or equal to n / 2 which is coprime to n | function to find largest integer less than or equal to n / 2 and is coprime with n ; handle the case for n = 6 ; 
def largestCoprime(N):
    if N == 6:
        return 1
    elif N % 4 == 0:
        return N // 2 - 1
    elif N % 2 == 0:
        return N // 2 - 2
    else:
        return (N - 1) // 2


if __name__ == "__main__":
    n = 50
    print(largestCoprime(n))

# Expected output:
23
# print all safe primes below n | python 3 implementation of the approach ; function to print first n safe primes ; initialize all entries of integer array as 1. a value in prime [ i ] will finally be 0 if i is not a prime , else 1 ; 0 and 1 are not primes ; if prime [ p ] is not changed , then it is a prime ; update all multiples of p ; if i is prime ; 2 p + 1 ; if 2 p + 1 is also a prime then set prime [ 2 p + 1 ] = 2 ; i is a safe prime ; 

from math import sqrt


def printSafePrimes(n):
    prime = [0 for i in range(n + 1)]
    for i in range(2, n + 1):
        prime[i] = 1
    prime[0] = prime[1] = 0
    for p in range(2, int(sqrt(n)) + 1, 1):
        if (prime[p] == 1):
            for i in range(p * 2, n + 1, p):
                prime[i] = 0
    for i in range(2, n + 1, 1):
        if (prime[i] != 0):
            temp = (2 * i) + 1
            if (temp <= n and prime[temp] != 0):
                prime[temp] = 2
    for i in range(5, n + 1):
        if (prime[i] == 2):
            print(i, end=" ")


if __name__ == '__main__':
    n = 20
    printSafePrimes(n)

# Expected output:
5 7 11
# minimum multiplications with { 2 , 3 , 7 } to make two numbers equal | function to find powers of 2 , 3 and 7 in x ; to keep count of each divisor ; to store the result ; count powers of 2 in x ; count powers of 3 in x ; count powers of 7 in x ; remaining number which is not divisible by 2 , 3 or 7 ; function to return the minimum number of given operations required to make a and b equal ; a = x * 2 ^ a1 * 3 ^ a2 * 7 ^ a3 va [ 0 ] = a1 va [ 1 ] = a2 va [ 2 ] = a3 va [ 3 ] = x ; similarly for b ; if a and b cannot be made equal with the given operation . note that va [ 3 ] and vb [ 3 ] contain remaining numbers after repeated divisions with 2 , 3 and 7. if remaining numbers are not same then we cannot make them equal . ; minimum number of operations required ; 
def Divisors(x):
    c = 0
    v = []
    while (x % 2 == 0):
        c += 1
        x /= 2
    v .append(c)
    c = 0
    while (x % 3 == 0):
        c += 1
        x /= 3
    v .append(c)
    c = 0
    while (x % 7 == 0):
        c += 1
        x /= 7
    v .append(c)
    v .append(x)
    return v


def MinOperations(a, b):
    va = Divisors(a)
    vb = Divisors(b)
    if (va[3] != vb[3]):
        return -1
    minOperations = abs(va[0] - vb[0]) + \
        abs(va[1] - vb[1]) + abs(va[2] - vb[2])
    return minOperations


if __name__ == '__main__':
    a = 14
    b = 28
    print(MinOperations(a, b))

# Expected output:
1
# product of n with its largest odd digit | function to return the largest odd digit in n ; if all digits are even then - 1 will be returned ; last digit from n ; if current digit is odd and > maxodd ; remove last digit ; return the maximum odd digit ; function to return the product of n with its largest odd digit ; if there are no odd digits in n ; product of n with its largest odd digit ; 
def largestOddDigit(n):
    maxOdd = -1
    while (n > 0):
        digit = n % 10
        if (digit % 2 == 1 and digit > maxOdd):
            maxOdd = digit
        n = n // 10
    return maxOdd


def getProduct(n):
    maxOdd = largestOddDigit(n)
    if (maxOdd == -1):
        return -1
    return (n * maxOdd)


if __name__ == "__main__":
    n = 12345
    print(getProduct(n))

# Expected output:
61725
# check if a number is perfect square without finding square root | program to find if x is a perfect square . ; check if mid is perfect square ; mid is small -> go right to increase mid ; mid is large -> to left to decrease mid ; 
def isPerfectSquare(x):
    left = 1
    right = x
    while (left <= right):
        mid = (left + right) >> 1
        if ((mid * mid) == x):
            return True
        if (mid * mid < x):
            left = mid + 1
        else:
            right = mid - 1
    return False


if __name__ == "__main__":
    x = 2500
    if (isPerfectSquare(x)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# find k numbers which are powers of 2 and have sum n | set 1 | function to prnumbers ; count the number of set bits ; not - possible condition ; stores the number ; get the set bits ; iterate till we get k elements ; get the topmost element ; append the elements / 2 into priority queue ; prall elements ; 
def printNum(n, k):
    x = 0
    m = n
    while (m):
        x += m & 1
        m >>= 1
    if k < x or k > n:
        print("-1")
        return
    pq = []
    two = 1
    while (n):
        if (n & 1):
            pq .append(two)
        two = two * 2
        n = n >> 1
    while (len(pq) < k):
        el = pq[-1]
        pq .pop()
        pq .append(el // 2)
        pq .append(el // 2)
    ind = 0
    pq .sort()
    while (ind < k):
        print(pq[-1], end=" ")
        pq .pop()
        ind += 1


n = 9
k = 4
printNum(n, k)

# Expected output:
4 2 2 1
# sum of lcm ( 1 , n ) , lcm ( 2 , n ) , lcm ( 3 , n ) , ... , lcm ( n , n ) | python3 implementation of the approach ; euler totient function ; function to return the required lcm sum ; summation of d * etf ( d ) where d belongs to set of divisors of n ; 

n = 100002
phi = [0] * (n + 2)
ans = [0] * (n + 2)


def ETF():
    for i in range(1, n + 1):
        phi[i] = i
    for i in range(2, n + 1):
        if (phi[i] == i):
            phi[i] = i - 1
            for j in range(2 * i, n + 1, i):
                phi[j] = (phi[j] * (i - 1)) // i


def LcmSum(m):
    ETF()
    for i in range(1, n + 1):
        for j in range(i, n + 1, i):
            ans[j] += (i * phi[i])
    answer = ans[m]
    answer = (answer + 1) * m
    answer = answer // 2
    return answer


m = 5
print(LcmSum(m))

# Expected output:
55
# permutations of string such that no two vowels are adjacent | factorial of a number ; function to find c ( n , r ) ; function to count permutations of string such that no two vowels are adjacent ; finding the frequencies of the characters ; finding the no . of vowels and consonants in given word ; finding places for the vowels ; ways to fill consonants 6 ! / 2 ! ; ways to put vowels 7 c5 x 5 ! ; 
def factorial(n):
    fact = 1
    for i in range(2, n + 1):
        fact = fact * i
    return fact


def ncr(n, r):
    return factorial(n) // (factorial(r) * factorial(n - r))


def countWays(string):
    freq = [0] * 26
    nvowels, nconsonants = 0, 0
    for i in range(len(string)):
        freq[ord(string[i]) - ord('a')] += 1
    for i in range(26):
        if (i == 0 or i == 4 or i == 8 or i == 14 or i == 20):
            nvowels += freq[i]
        else:
            nconsonants += freq[i]
    vplaces = nconsonants + 1
    cways = factorial(nconsonants)
    for i in range(26):
        if (i != 0 and i != 4 and i != 8 and i !=
                14 and i != 20 and freq[i] > 1):
            cways = cways // factorial(freq[i])
    vways = ncr(vplaces, nvowels) * factorial(nvowels)
    for i in range(26):
        if (i == 0 or i == 4 or i == 8 or i == 14 or i == 20 and freq[i] > 1):
            vways = vways // factorial(freq[i])
    return cways * vways


if __name__ == "__main__":
    string = "permutation"
    print(countWays(string))

# Expected output:
907200
# pairs from an array that satisfy the given condition | function to return the number of set bits in n ; function to return the count of required pairs ; set bits for first element of the pair ; set bits for second element of the pair ; set bits of the resultant number which is the xor of both the elements of the pair ; if the condition is satisfied ; increment the count ; return the total count ; 
def setBits(n):
    count = 0
    while (n):
        n = n & (n - 1)
        count += 1
    return count


def countPairs(a, n):
    count = 0
    for i in range(0, n - 1, 1):
        setbits_x = setBits(a[i])
        for j in range(i + 1, n, 1):
            setbits_y = setBits(a[j])
            setbits_xor_xy = setBits(a[i] ^ a[j])
            if (setbits_x + setbits_y == setbits_xor_xy):
                count += 1
    return count


if __name__ == '__main__':
    a = [2, 3, 4, 5, 6]
    n = len(a)
    print(countPairs(a, n))

# Expected output:
3