codeparrot/xlcost-text-to-code huggingface dataset:
Count of numbers which can be made power of 2 by given operation | Function that returns true if x is a power of 2 ; If x & ( x - 1 ) = 0 then x is a power of 2 ; Function to return the required count ; If a [ i ] or ( a [ i ] + 1 ) is a power of 2 ; Driver code
def isPowerOfTwo ( x ) : NEW_LINE INDENT if ( x == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( ( x & ( x - 1 ) ) == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def countNum ( a , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( isPowerOfTwo ( a [ i ] ) or isPowerOfTwo ( a [ i ] + 1 ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 6 , 9 , 3 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countNum ( arr , n ) ) NEW_LINE DEDENT
Sum of elements from an array having even parity | Function that returns true if x has even parity ; We basically count set bits https : www . geeksforgeeks . org / count - set - bits - in - an - integer / ; Function to return the sum of the elements from an array which have even parity ; If a [ i ] has even parity ; Driver code
def checkEvenParity ( x ) : NEW_LINE INDENT parity = 0 NEW_LINE while ( x != 0 ) : NEW_LINE INDENT x = x & ( x - 1 ) NEW_LINE parity += 1 NEW_LINE DEDENT if ( parity % 2 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def sumlist ( a , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( checkEvenParity ( a [ i ] ) ) : NEW_LINE INDENT sum += a [ i ] NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 3 , 5 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( sumlist ( arr , n ) ) NEW_LINE DEDENT
Range Sum Queries and Update with Square Root | Python program to calculate sum in an interval and update with square root ; Maximum size of input array ; structure for queries with members type , leftIndex , rightIndex of the query ; function for updating the value ; function for calculating the required sum between two indexes ; function to return answer to queries ; Declaring a Set ; inserting indexes of those numbers which are greater than 1 ; update query ; find the left index of query in the set using binary search auto it = s . lower_bound ( que [ i ] . l ) ; ; if it crosses the right index of query or end of set , then break ; update the value of arr [ i ] to itss square root ; if updated value becomes equal to 1 remove it from the set ; increment the index ; sum query ; Driver Code ; input array using 1 - based indexing ; declaring array of structure of type queries ; answer the Queries
from typing import List NEW_LINE import bisect NEW_LINE from math import sqrt , floor NEW_LINE MAX = 100 NEW_LINE BIT = [ 0 for _ in range ( MAX + 1 ) ] NEW_LINE class queries : NEW_LINE INDENT def __init__ ( self , type : int = 0 , l : int = 0 , r : int = 0 ) -> None : NEW_LINE INDENT self . type = type NEW_LINE self . l = l NEW_LINE self . r = r NEW_LINE DEDENT DEDENT def update ( x : int , val : int , n : int ) -> None : NEW_LINE INDENT a = x NEW_LINE while a <= n : NEW_LINE INDENT BIT [ a ] += val NEW_LINE a += a & - a NEW_LINE DEDENT DEDENT def sum ( x : int ) -> int : NEW_LINE INDENT s = 0 NEW_LINE a = x NEW_LINE while a : NEW_LINE INDENT s += BIT [ a ] NEW_LINE a -= a & - a NEW_LINE DEDENT return s NEW_LINE DEDENT def answerQueries ( arr : List [ int ] , que : List [ queries ] , n : int , q : int ) -> None : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] > 1 ) : NEW_LINE INDENT s . add ( i ) NEW_LINE DEDENT update ( i , arr [ i ] , n ) NEW_LINE DEDENT for i in range ( q ) : NEW_LINE INDENT if ( que [ i ] . type == 1 ) : NEW_LINE INDENT while True : NEW_LINE INDENT ss = list ( sorted ( s ) ) NEW_LINE it = bisect . bisect_left ( ss , que [ i ] . l ) NEW_LINE if it == len ( s ) or ss [ it ] > que [ i ] . r : NEW_LINE INDENT break NEW_LINE DEDENT que [ i ] . l = ss [ it ] NEW_LINE update ( ss [ it ] , floor ( sqrt ( arr [ ss [ it ] ] ) - arr [ ss [ it ] ] ) , n ) NEW_LINE arr [ ss [ it ] ] = floor ( sqrt ( arr [ ss [ it ] ] ) ) NEW_LINE if ( arr [ ss [ it ] ] == 1 ) : NEW_LINE INDENT s . remove ( ss [ it ] ) NEW_LINE DEDENT que [ i ] . l += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( sum ( que [ i ] . r ) - sum ( que [ i ] . l - 1 ) ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT q = 4 NEW_LINE arr = [ 0 , 4 , 5 , 1 , 2 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE que = [ queries ( ) for _ in range ( q + 1 ) ] NEW_LINE que [ 0 ] . type , que [ 0 ] . l , que [ 0 ] . r = 2 , 1 , 5 NEW_LINE que [ 1 ] . type , que [ 1 ] . l , que [ 1 ] . r = 1 , 1 , 2 NEW_LINE que [ 2 ] . type , que [ 2 ] . l , que [ 2 ] . r = 1 , 2 , 4 NEW_LINE que [ 3 ] . type , que [ 3 ] . l , que [ 3 ] . r = 2 , 1 , 5 NEW_LINE answerQueries ( arr , que , n , q ) NEW_LINE DEDENT
Number of pairs with Bitwise OR as Odd number | Function to count pairs with odd OR ; find OR operation check odd or odd ; return count of odd pair ; Driver Code
def findOddPair ( A , N ) : NEW_LINE INDENT oddPair = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT if ( ( A [ i ] A [ j ] ) % 2 != 0 ) : NEW_LINE INDENT oddPair += 1 NEW_LINE DEDENT DEDENT DEDENT return oddPair NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT A = [ 5 , 6 , 2 , 8 ] NEW_LINE N = len ( A ) NEW_LINE print ( findOddPair ( A , N ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT
Count pairs with Bitwise XOR as EVEN number | Function to count number of even pairs ; variable for counting even pairs ; find all pairs ; find XOR operation check even or even ; return number of even pair ; Driver Code ; calling function findevenPair and prnumber of even pair
def findevenPair ( A , N ) : NEW_LINE INDENT evenPair = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT if ( ( A [ i ] ^ A [ j ] ) % 2 == 0 ) : NEW_LINE INDENT evenPair += 1 NEW_LINE DEDENT DEDENT DEDENT return evenPair ; NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT A = [ 5 , 4 , 7 , 2 , 1 ] NEW_LINE N = len ( A ) NEW_LINE print ( findevenPair ( A , N ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT
Count pairs with Bitwise XOR as EVEN number | Function to count number of even pairs ; find all pairs ; return number of even pair ; Driver Code ; calling function findEvenPair and pr number of even pair
def findEvenPair ( A , N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( A [ i ] % 2 != 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT totalPairs = ( N * ( N - 1 ) / 2 ) NEW_LINE oddEvenPairs = count * ( N - count ) NEW_LINE return ( int ) ( totalPairs - oddEvenPairs ) NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT a = [ 5 , 4 , 7 , 2 , 1 ] NEW_LINE n = len ( a ) NEW_LINE print ( findEvenPair ( a , n ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT
Count pairs with Bitwise | Function to count number of pairs EVEN bitwise AND ; variable for counting even pairs ; find all pairs ; find AND operation to check evenpair ; return number of even pair ; Driver Code
def findevenPair ( A , N ) : NEW_LINE INDENT evenPair = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT if ( ( A [ i ] & A [ j ] ) % 2 == 0 ) : NEW_LINE INDENT evenPair += 1 NEW_LINE DEDENT DEDENT DEDENT return evenPair NEW_LINE DEDENT a = [ 5 , 1 , 3 , 2 ] NEW_LINE n = len ( a ) NEW_LINE print ( findevenPair ( a , n ) ) NEW_LINE
Count pairs with Bitwise | Function to count number of pairs with EVEN bitwise AND ; count odd numbers ; count odd pairs ; return number of even pair ; Driver Code
def findevenPair ( A , N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( A [ i ] % 2 != 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT oddCount = count * ( count - 1 ) / 2 NEW_LINE return ( int ) ( ( N * ( N - 1 ) / 2 ) - oddCount ) NEW_LINE DEDENT a = [ 5 , 1 , 3 , 2 ] NEW_LINE n = len ( a ) NEW_LINE print ( findevenPair ( a , n ) ) NEW_LINE
Find a value whose XOR with given number is maximum | Function To Calculate Answer ; Find number of bits in the given integer ; XOR the given integer with poe ( 2 , number_of_bits - 1 and print the result ; Driver Code
def calculate ( X ) : NEW_LINE INDENT number_of_bits = 8 NEW_LINE return ( ( 1 << number_of_bits ) - 1 ) ^ X NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = 4 NEW_LINE print ( " Required ▁ Number ▁ is : " , calculate ( X ) ) NEW_LINE DEDENT
XOR of all elements of array with set bits equal to K | Function to find Xor of desired elements ; Initialize vector ; push required elements ; Initialize result with first element of vector ; Driver code
def xorGivenSetBits ( arr , n , k ) : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( bin ( arr [ i ] ) . count ( '1' ) == k ) : NEW_LINE INDENT v . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT result = v [ 0 ] NEW_LINE for i in range ( 1 , len ( v ) , 1 ) : NEW_LINE INDENT result = result ^ v [ i ] NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 13 , 1 , 19 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE print ( xorGivenSetBits ( arr , n , k ) ) NEW_LINE DEDENT
Replace every element of the array with BitWise XOR of all other | Function to replace the elements ; Calculate the xor of all the elements ; Replace every element by the xor of all other elements ; Driver code ; Print the modified array .
def ReplaceElements ( arr , n ) : NEW_LINE INDENT X = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT X ^= arr [ i ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT arr [ i ] = X ^ arr [ i ] NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 3 , 5 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE ReplaceElements ( arr , n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Assign other value to a variable from two possible values | Function to alternate the values ; Driver code
def alternate ( a , b , x ) : NEW_LINE INDENT x = a ^ b ^ x NEW_LINE print ( " After ▁ exchange " ) NEW_LINE print ( " x ▁ is " , x ) NEW_LINE DEDENT a = - 10 NEW_LINE b = 15 NEW_LINE x = a NEW_LINE print ( " x ▁ is " , x ) NEW_LINE alternate ( a , b , x ) NEW_LINE
Number of leading zeros in binary representation of a given number | Function to count the no . of leading zeros ; Keep shifting x by one until leftmost bit does not become 1. ; Driver Code
def countZeros ( x ) : NEW_LINE INDENT total_bits = 32 NEW_LINE res = 0 NEW_LINE while ( ( x & ( 1 << ( total_bits - 1 ) ) ) == 0 ) : NEW_LINE INDENT x = ( x << 1 ) NEW_LINE res += 1 NEW_LINE DEDENT return res NEW_LINE DEDENT x = 101 NEW_LINE print ( countZeros ( x ) ) NEW_LINE
Number of leading zeros in binary representation of a given number | Function to count the no . of leading zeros ; Main function
def countZeros ( x ) : NEW_LINE INDENT n = 32 ; NEW_LINE y = x >> 16 ; NEW_LINE if ( y != 0 ) : NEW_LINE INDENT n = n - 16 ; NEW_LINE x = y ; NEW_LINE DEDENT y = x >> 8 ; NEW_LINE if ( y != 0 ) : NEW_LINE INDENT n = n - 8 ; NEW_LINE x = y ; NEW_LINE DEDENT y = x >> 4 ; NEW_LINE if ( y != 0 ) : NEW_LINE INDENT n = n - 4 ; NEW_LINE x = y ; NEW_LINE DEDENT y = x >> 2 ; NEW_LINE if ( y != 0 ) : NEW_LINE INDENT n = n - 2 ; NEW_LINE x = y ; NEW_LINE DEDENT y = x >> 1 ; NEW_LINE if ( y != 0 ) : NEW_LINE INDENT return n - 2 ; NEW_LINE DEDENT return n - x ; NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT x = 101 ; NEW_LINE print ( countZeros ( x ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT
Comparing leading zeros in binary representations of two numbers | Function to compare the no . of leading zeros ; if both have same no . of leading zeros ; if y has more leading zeros ; Driver Code
def LeadingZeros ( x , y ) : NEW_LINE INDENT if ( ( x ^ y ) <= ( x & y ) ) : NEW_LINE INDENT print ( " Equal " ) NEW_LINE DEDENT elif ( ( x & ( ~ y ) ) > y ) : NEW_LINE INDENT print ( y ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( x ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 10 NEW_LINE y = 16 NEW_LINE LeadingZeros ( x , y ) NEW_LINE DEDENT
Printing all subsets of { 1 , 2 , 3 , ... n } without using array or loop | This recursive function calls subset function to print the subsets one by one . numBits -- > number of bits needed to represent the number ( simply input value n ) . num -- > Initially equal to 2 ^ n - 1 and decreases by 1 every recursion until 0. ; Print the subset corresponding to binary representation of num . ; Call the function recursively to print the next subset . ; This function recursively prints the subset corresponding to the binary representation of num . nthBit -- > nth bit from right side starting from n and decreases until 0. ; Print number in given subset only if the bit corresponding to it is set in num . ; Check for the next bit ; Driver Code
def printSubsets ( numOfBits , num ) : NEW_LINE INDENT if num >= 0 : NEW_LINE INDENT print ( " { " , end = " ▁ " ) NEW_LINE subset ( numOfBits - 1 , num , numOfBits ) NEW_LINE print ( " } " ) NEW_LINE printSubsets ( numOfBits , num - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return NEW_LINE DEDENT DEDENT def subset ( nthBit , num , numOfBits ) : NEW_LINE INDENT if nthBit >= 0 : NEW_LINE INDENT if num & ( 1 << nthBit ) != 0 : NEW_LINE INDENT print ( numOfBits - nthBit , end = " ▁ " ) NEW_LINE DEDENT subset ( nthBit - 1 , num , numOfBits ) NEW_LINE DEDENT else : NEW_LINE INDENT return NEW_LINE DEDENT DEDENT n = 4 NEW_LINE printSubsets ( n , 2 ** n - 1 ) NEW_LINE
Number of mismatching bits in the binary representation of two integers | compute number of different bits ; since , the numbers are less than 2 ^ 31 run the loop from '0' to '31' only ; right shift both the numbers by ' i ' and check if the bit at the 0 th position is different ; Driver code ; find number of different bits
def solve ( A , B ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , 32 ) : NEW_LINE INDENT if ( ( ( A >> i ) & 1 ) != ( ( B >> i ) & 1 ) ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT print ( " Number ▁ of ▁ different ▁ bits ▁ : " , count ) NEW_LINE DEDENT A = 12 NEW_LINE B = 15 NEW_LINE solve ( A , B ) NEW_LINE
Set the rightmost off bit | Python3 program to set the rightmost unset bit ; If all bits are set ; Set rightmost 0 bit ; Driver code
def setRightmostUnsetBit ( n ) : NEW_LINE INDENT if n & ( n + 1 ) == 0 : NEW_LINE INDENT return n NEW_LINE DEDENT return n | ( n + 1 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 21 NEW_LINE print ( setRightmostUnsetBit ( n ) ) NEW_LINE DEDENT
Print the number of set bits in each node of a Binary Tree | Binary Tree node ; Utility function that allocates a new Node ; Function to print the number of set bits in each node of the binary tree ; Print the number of set bits of current node using count ( ) ; Traverse Left Subtree ; Traverse Right Subtree ; Driver Code
class Node : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . data = None NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def newNode ( data ) : NEW_LINE INDENT node = Node ( ) NEW_LINE node . data = data NEW_LINE node . left = None NEW_LINE node . right = None NEW_LINE return node NEW_LINE DEDENT def printSetBit ( root : Node ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return NEW_LINE DEDENT print ( " Set ▁ bits ▁ in ▁ Node ▁ % d ▁ = ▁ % d " % ( root . data , bin ( root . data ) . count ( '1' ) ) ) NEW_LINE printSetBit ( root . left ) NEW_LINE printSetBit ( root . right ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = newNode ( 16 ) NEW_LINE root . left = newNode ( 13 ) NEW_LINE root . left . left = newNode ( 14 ) NEW_LINE root . left . right = newNode ( 12 ) NEW_LINE root . right = newNode ( 11 ) NEW_LINE root . right . left = newNode ( 10 ) NEW_LINE root . right . right = newNode ( 16 ) NEW_LINE printSetBit ( root ) NEW_LINE DEDENT
Find bitwise AND ( & ) of all possible sub | function to return AND of sub - arrays ; Driver Code ; size of the array ; print and of all subarrays
def AND ( a , n ) : NEW_LINE INDENT ans = a [ 0 ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans &= a [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 3 ] NEW_LINE n = len ( a ) NEW_LINE print ( AND ( a , n ) ) NEW_LINE DEDENT
2 's complement for a givin string using XOR | Python program to find 2 's complement using XOR. ; A flag used to find if a 1 bit is seen or not . ; xor operator is used to flip the ; bits after converting in to ASCII values ; if there is no 1 in the string so just add 1 in starting of string and return ; Driver code
def TwoscomplementbyXOR ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE check_bit = 0 NEW_LINE i = n - 1 NEW_LINE s = list ( str ) NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( s [ i ] == '0' and check_bit == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT if ( check_bit == 1 ) : NEW_LINE INDENT s [ i ] = chr ( ( ord ( s [ i ] ) - 48 ) ^ 1 + 48 ) NEW_LINE DEDENT check_bit = 1 NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT str = " " . join ( s ) NEW_LINE if ( check_bit == 0 ) : NEW_LINE INDENT return "1" + str NEW_LINE DEDENT else : NEW_LINE INDENT return str NEW_LINE DEDENT DEDENT str = "101" NEW_LINE print ( TwoscomplementbyXOR ( str ) ) NEW_LINE
Check whether bits are in alternate pattern in the given range | Set | function to check whether rightmost kth bit is set or not in 'n ; function to set the rightmost kth bit in 'n ; kth bit of n is being set by this operation ; function to check if all the bits are set or not in the binary representation of 'n ; if true , then all bits are set ; else all bits are not set ; function to check if a number has bits in alternate pattern ; to check if all bits are set in 'num ; function to check whether bits are in alternate pattern in the given range ; preparing a number ' num ' and ' left _ shift ' which can be further used for the check of alternate pattern in the given range ; unset all the bits which are left to the rth bit of ( r + 1 ) th bit ; right shift ' num ' by ( l - 1 ) bits ; Driver Code
' NEW_LINE def isKthBitSet ( n , k ) : NEW_LINE INDENT if ( ( n >> ( k - 1 ) ) & 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT ' NEW_LINE def setKthBit ( n , k ) : NEW_LINE INDENT return ( ( 1 << ( k - 1 ) ) n ) NEW_LINE DEDENT ' NEW_LINE def allBitsAreSet ( n ) : NEW_LINE INDENT if ( ( ( n + 1 ) & n ) == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def bitsAreInAltOrder ( n ) : NEW_LINE INDENT num = n ^ ( n >> 1 ) NEW_LINE DEDENT ' NEW_LINE INDENT return allBitsAreSet ( num ) NEW_LINE DEDENT def bitsAreInAltPatrnInGivenRange ( n , l , r ) : NEW_LINE INDENT if ( isKthBitSet ( n , r ) ) : NEW_LINE INDENT num = n NEW_LINE left_shift = r NEW_LINE DEDENT else : NEW_LINE INDENT num = setKthBit ( n , ( r + 1 ) ) NEW_LINE left_shift = r + 1 NEW_LINE DEDENT num = num & ( ( 1 << left_shift ) - 1 ) NEW_LINE num = num >> ( l - 1 ) NEW_LINE return bitsAreInAltOrder ( num ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 18 NEW_LINE l = 1 NEW_LINE r = 3 NEW_LINE if ( bitsAreInAltPatrnInGivenRange ( n , l , r ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Increment a number without using ++ or + | function that increment the value . ; Invert bits and apply negative sign ; Driver code
def increment ( i ) : NEW_LINE INDENT i = - ( ~ i ) ; NEW_LINE return i ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 ; NEW_LINE print ( increment ( n ) ) ; NEW_LINE DEDENT
First element greater than or equal to X in prefix sum of N numbers using Binary Lifting | Python 3 program to find lower_bound of x in prefix sums array using binary lifting . ; function to make prefix sums array ; function to find lower_bound of x in prefix sums array using binary lifting . ; initialize position ; find log to the base 2 value of n . ; if x less than first number . ; starting from most significant bit . ; if value at this position less than x then updateposition Here ( 1 << i ) is similar to 2 ^ i . ; + 1 because ' pos ' will have position of largest value less than 'x ; Driver code ; given array ; value to find ; size of array ; to store prefix sum ; call for prefix sum ; function call
import math NEW_LINE def MakePreSum ( arr , presum , n ) : NEW_LINE INDENT presum [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT presum [ i ] = presum [ i - 1 ] + arr [ i ] NEW_LINE DEDENT DEDENT def BinaryLifting ( presum , n , x ) : NEW_LINE INDENT pos = 0 NEW_LINE LOGN = int ( math . log2 ( n ) ) NEW_LINE if ( x <= presum [ 0 ] ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT for i in range ( LOGN , - 1 , - 1 ) : NEW_LINE INDENT if ( pos + ( 1 << i ) < n and presum [ pos + ( 1 << i ) ] < x ) : NEW_LINE INDENT pos += ( 1 << i ) NEW_LINE DEDENT DEDENT DEDENT ' NEW_LINE INDENT return pos + 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 5 , 7 , 1 , 6 , 9 , 12 , 4 , 6 ] NEW_LINE x = 8 NEW_LINE n = len ( arr ) NEW_LINE presum = [ 0 ] * n NEW_LINE MakePreSum ( arr , presum , n ) NEW_LINE print ( BinaryLifting ( presum , n , x ) ) NEW_LINE DEDENT
Maximum sum by adding numbers with same number of set bits | count the number of bits for each element of array ; Count the number of set bits ; Function to return the the maximum sum ; Calculate the ; Assuming the number to be a maximum of 32 bits ; Add the number to the number of set bits ; Find the maximum sum ; Driver code
def bit_count ( n ) : NEW_LINE INDENT count = 0 ; NEW_LINE while ( n > 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE n = n & ( n - 1 ) ; NEW_LINE DEDENT return count ; NEW_LINE DEDENT def maxsum ( arr , n ) : NEW_LINE INDENT bits = [ 0 ] * n ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT bits [ i ] = bit_count ( arr [ i ] ) ; NEW_LINE DEDENT sum = [ 0 ] * 32 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum [ bits [ i ] ] += arr [ i ] ; NEW_LINE DEDENT maximum = 0 ; NEW_LINE for i in range ( 32 ) : NEW_LINE INDENT maximum = max ( sum [ i ] , maximum ) ; NEW_LINE DEDENT return maximum ; NEW_LINE DEDENT arr = [ 2 , 3 , 8 , 5 , 6 , 7 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( maxsum ( arr , n ) ) ; NEW_LINE
Sum of XOR of sum of all pairs in an array | Python3 program to find XOR of pair sums . ; Driver program to test the above function
def xor_pair_sum ( ar , n ) : NEW_LINE INDENT total = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT total = total ^ ar [ i ] NEW_LINE DEDENT return 2 * total NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT data = [ 1 , 2 , 3 ] NEW_LINE print ( xor_pair_sum ( data , len ( data ) ) ) NEW_LINE DEDENT
Count pairs with Bitwise OR as Even number | Python 3 program to count pairs with even OR ; Count total even numbers in array . ; return count of even pair ; Driver Code
def findEvenPair ( A , N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( not ( A [ i ] & 1 ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count * ( count - 1 ) // 2 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 5 , 6 , 2 , 8 ] NEW_LINE N = len ( A ) NEW_LINE print ( findEvenPair ( A , N ) ) NEW_LINE DEDENT