codeparrot/xlcost-text-to-code huggingface dataset:
Maximum number of consecutive 1 s after flipping all 0 s in a K length subarray | Function to find the maximum number of consecutive 1 's after flipping all zero in a K length subarray ; Initialize variable ; Iterate unil n - k + 1 as we have to go till i + k ; Iterate in the array in left direction till you get 1 else break ; Iterate in the array in right direction till you get 1 else break ; Compute the maximum length ; Return the length ; Driver code ; Array initialization ; Size of array
def findmax ( arr , n , k ) : NEW_LINE INDENT trav , i = 0 , 0 NEW_LINE c = 0 NEW_LINE maximum = 0 NEW_LINE while i < n - k + 1 : NEW_LINE INDENT trav = i - 1 NEW_LINE c = 0 NEW_LINE while trav >= 0 and arr [ trav ] == 1 : NEW_LINE INDENT trav -= 1 NEW_LINE c += 1 NEW_LINE DEDENT trav = i + k NEW_LINE while ( trav < n and arr [ trav ] == 1 ) : NEW_LINE INDENT trav += 1 NEW_LINE c += 1 NEW_LINE DEDENT c += k NEW_LINE if ( c > maximum ) : NEW_LINE INDENT maximum = c NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return maximum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT k = 3 NEW_LINE arr = [ 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 ] NEW_LINE n = len ( arr ) NEW_LINE ans = findmax ( arr , n , k ) NEW_LINE print ( ans ) NEW_LINE DEDENT
Count of elements which cannot form any pair whose sum is power of 2 | Python3 program to count of array elements which do not form a pair with sum equal to a power of 2 with any other array element ; Function to calculate and return the count of elements ; Stores the frequencies of every array element ; Stores the count of removals ; For every element , check if it can form a sum equal to any power of 2 with any other element ; Store pow ( 2 , j ) - a [ i ] ; Check if s is present in the array ; If frequency of s exceeds 1 ; If s has frequency 1 but is different from a [ i ] ; Pair possible ; If no pair possible for the current element ; Return the answer ; Driver Code
from collections import defaultdict NEW_LINE def powerOfTwo ( a , n ) : NEW_LINE INDENT mp = defaultdict ( int ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ a [ i ] ] += 1 NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT f = False NEW_LINE for j in range ( 31 ) : NEW_LINE INDENT s = ( 1 << j ) - a [ i ] NEW_LINE if ( s in mp and ( mp [ s ] > 1 or mp [ s ] == 1 and s != a [ i ] ) ) : NEW_LINE INDENT f = True NEW_LINE DEDENT DEDENT if ( f == False ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 6 , 2 , 11 ] NEW_LINE n = len ( a ) NEW_LINE print ( powerOfTwo ( a , n ) ) NEW_LINE DEDENT
Construct the Array using given bitwise AND , OR and XOR | Python3 program for the above approach ; Function to find the array ; Loop through all bits in number ; If bit is set in AND then set it in every element of the array ; If bit is not set in AND ; But set in b ( OR ) ; Set bit position in first element ; If bit is not set in c then set it in second element to keep xor as zero for bit position ; Calculate AND , OR and XOR of array ; Check if values are equal or not ; If not , then array is not possible ; Driver Code ; Given Bitwise AND , OR , and XOR ; Function Call
import sys NEW_LINE def findArray ( n , a , b , c ) : NEW_LINE INDENT arr = [ 0 ] * ( n + 1 ) NEW_LINE for bit in range ( 30 , - 1 , - 1 ) : NEW_LINE INDENT set = a & ( 1 << bit ) NEW_LINE if ( set ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT arr [ i ] |= set NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( b & ( 1 << bit ) ) : NEW_LINE INDENT arr [ 0 ] |= ( 1 << bit ) NEW_LINE if ( not ( c & ( 1 << bit ) ) ) : NEW_LINE INDENT arr [ 1 ] |= ( 1 << bit ) NEW_LINE DEDENT DEDENT DEDENT DEDENT aa = sys . maxsize NEW_LINE bb = 0 NEW_LINE cc = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT aa &= arr [ i ] NEW_LINE bb |= arr [ i ] NEW_LINE cc ^= arr [ i ] NEW_LINE DEDENT if ( a == aa and b == bb and c == cc ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE a = 4 NEW_LINE b = 6 NEW_LINE c = 6 NEW_LINE findArray ( n , a , b , c ) NEW_LINE DEDENT
Minimum XOR of OR and AND of any pair in the Array | Function to find the minimum value of XOR of AND and OR of any pair in the given array ; Sort the array ; Traverse the array arr [ ] ; Compare and Find the minimum XOR value of an array . ; Return the final answer ; Driver Code ; Given array ; Function Call
def maxAndXor ( arr , n ) : NEW_LINE INDENT ans = float ( ' inf ' ) NEW_LINE arr . sort ( ) NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT ans = min ( ans , arr [ i ] ^ arr [ i + 1 ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE print ( maxAndXor ( arr , N ) ) NEW_LINE DEDENT
Count of subarrays of size K with elements having even frequencies | Function to return count of required subarrays ; If K is odd ; Not possible to have any such subarrays ; Stores the starting index of every subarrays ; Stores the count of required subarrays ; Stores Xor of the current subarray . ; Xor of first subarray of size K ; If all elements appear even number of times , increase the count of such subarrays ; Remove the starting element from the current subarray ; Traverse the array for the remaining subarrays ; Update Xor by adding the last element of the current subarray ; Increment i ; If currXor becomes 0 , then increment count ; Update currXor by removing the starting element of the current subarray ; Return count ; Driver Code
def countSubarray ( arr , K , N ) : NEW_LINE INDENT if ( K % 2 != 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( N < K ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT start = 0 NEW_LINE i = 0 NEW_LINE count = 0 NEW_LINE currXor = arr [ i ] NEW_LINE i += 1 NEW_LINE while ( i < K ) : NEW_LINE INDENT currXor ^= arr [ i ] NEW_LINE i += 1 NEW_LINE DEDENT if ( currXor == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT currXor ^= arr [ start ] NEW_LINE start += 1 NEW_LINE while ( i < N ) : NEW_LINE INDENT currXor ^= arr [ i ] NEW_LINE i += 1 NEW_LINE if ( currXor == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT currXor ^= arr [ start ] NEW_LINE start += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 4 , 2 , 2 , 4 ] NEW_LINE K = 4 NEW_LINE N = len ( arr ) NEW_LINE print ( countSubarray ( arr , K , N ) ) NEW_LINE DEDENT
Count of even and odd set bit Array elements after XOR with K for Q queries | Python3 program to count number of even and odd set bits elements after XOR with a given element ; Store the count of set bits ; Brian Kernighan 's algorithm ; Function to solve Q queries ; Store set bits in X ; Count set bits of X ; Driver code
even = 0 NEW_LINE odd = 0 NEW_LINE def keep_count ( arr , N ) : NEW_LINE INDENT global even NEW_LINE global odd NEW_LINE for i in range ( N ) : NEW_LINE INDENT count = 0 NEW_LINE while ( arr [ i ] != 0 ) : NEW_LINE INDENT arr [ i ] = ( arr [ i ] - 1 ) & arr [ i ] NEW_LINE count += 1 NEW_LINE DEDENT if ( count % 2 == 0 ) : NEW_LINE INDENT even += 1 NEW_LINE DEDENT else : NEW_LINE INDENT odd += 1 NEW_LINE DEDENT DEDENT return NEW_LINE DEDENT def solveQueries ( arr , n , q , m ) : NEW_LINE INDENT global even NEW_LINE global odd NEW_LINE keep_count ( arr , n ) NEW_LINE for i in range ( m ) : NEW_LINE INDENT X = q [ i ] NEW_LINE count = 0 NEW_LINE while ( X != 0 ) : NEW_LINE INDENT X = ( X - 1 ) & X NEW_LINE count += 1 NEW_LINE DEDENT if ( count % 2 == 0 ) : NEW_LINE INDENT print ( even , odd ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( odd , even ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 7 , 4 , 5 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE q = [ 3 , 4 , 12 , 6 ] NEW_LINE m = len ( q ) NEW_LINE solveQueries ( arr , n , q , m ) NEW_LINE DEDENT
Largest number in the Array having frequency same as value | Function to find the largest number whose frequency is equal to itself . ; Find the maximum element in the array ; Driver code
def findLargestNumber ( arr ) : NEW_LINE INDENT k = max ( arr ) ; NEW_LINE m = [ 0 ] * ( k + 1 ) ; NEW_LINE for n in arr : NEW_LINE INDENT m [ n ] += 1 ; NEW_LINE DEDENT for n in range ( len ( arr ) - 1 , 0 , - 1 ) : NEW_LINE INDENT if ( n == m [ n ] ) : NEW_LINE INDENT return n ; NEW_LINE DEDENT DEDENT return - 1 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 5 , 2 , 4 , 5 ] ; NEW_LINE print ( findLargestNumber ( arr ) ) ; NEW_LINE DEDENT
Largest number in the Array having frequency same as value | Function to find the largest number whose frequency is equal to itself . ; Adding 65536 to keep the count of the current number ; Right shifting by 16 bits to find the count of the number i ; Driver code
def findLargestNumber ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT arr [ i ] &= 0xFFFF ; NEW_LINE if ( arr [ i ] <= n ) : NEW_LINE INDENT arr [ i ] += 0x10000 ; NEW_LINE DEDENT DEDENT for i in range ( n - 1 , 0 , - 1 ) : NEW_LINE INDENT if ( ( arr [ i ] >> 16 ) == i ) : NEW_LINE INDENT return i + 1 ; NEW_LINE DEDENT DEDENT return - 1 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 5 , 5 , 2 , 4 , 5 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( findLargestNumber ( arr , n ) ) ; NEW_LINE DEDENT
Convert numbers into binary representation and add them without carry | Function returns sum of both the binary number without carry ; XOR of N and M ; Driver code
def NoCarrySum ( N , M ) : NEW_LINE INDENT return N ^ M NEW_LINE DEDENT N = 37 NEW_LINE M = 12 NEW_LINE print ( NoCarrySum ( N , M ) ) NEW_LINE
Check if all the set bits of the binary representation of N are at least K places away | Python3 program to check if all the set bits of the binary representation of N are at least K places away . ; Initialize check and count with 0 ; The i - th bit is a set bit ; This is the first set bit so , start calculating all the distances between consecutive bits from here ; If count is less than K return false ; Adding the count as the number of zeroes increase between set bits ; Driver code
def CheckBits ( N , K ) : NEW_LINE INDENT check = 0 NEW_LINE count = 0 NEW_LINE for i in range ( 31 , - 1 , - 1 ) : NEW_LINE INDENT if ( ( 1 << i ) & N ) : NEW_LINE INDENT if ( check == 0 ) : NEW_LINE INDENT check = 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( count < K ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT count = 0 NEW_LINE DEDENT else : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE K = 1 NEW_LINE if ( CheckBits ( N , K ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Minimize bits to be flipped in X and Y such that their Bitwise OR is equal to Z | This function returns minimum number of bits to be flipped in X and Y to make X | Y = Z ; If current bit in Z is set and is also set in either of X or Y or both ; If current bit in Z is set and is unset in both X and Y ; Set that bit in either X or Y ; If current bit in Z is unset and is set in both X and Y ; Unset the bit in both X and Y ; If current bit in Z is unset and is set in either X or Y ; Unset that set bit ; Driver Code
def minimumFlips ( X , Y , Z ) : NEW_LINE INDENT res = 0 NEW_LINE while ( X > 0 or Y > 0 or Z > 0 ) : NEW_LINE INDENT if ( ( ( X & 1 ) or ( Y & 1 ) ) and ( Z & 1 ) ) : NEW_LINE INDENT X = X >> 1 NEW_LINE Y = Y >> 1 NEW_LINE Z = Z >> 1 NEW_LINE continue NEW_LINE DEDENT elif ( not ( X & 1 ) and not ( Y & 1 ) and ( Z & 1 ) ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT elif ( ( X & 1 ) or ( Y & 1 ) == 1 ) : NEW_LINE INDENT if ( ( X & 1 ) and ( Y & 1 ) and not ( Z & 1 ) ) : NEW_LINE INDENT res += 2 NEW_LINE DEDENT elif ( ( ( X & 1 ) or ( Y & 1 ) ) and not ( Z & 1 ) ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT X = X >> 1 NEW_LINE Y = Y >> 1 NEW_LINE Z = Z >> 1 NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = 5 NEW_LINE Y = 8 NEW_LINE Z = 6 NEW_LINE print ( minimumFlips ( X , Y , Z ) ) NEW_LINE DEDENT
Count subsequences with same values of Bitwise AND , OR and XOR | ''function for finding count of possible subsequence ; '' creating a map to count the frequency of each element ; store frequency of each element ; iterate through the map ; add all possible combination for key equal zero ; add all ( odd number of elements ) possible combination for key other than zero ; Driver function
def countSubseq ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE mp = { } NEW_LINE for x in arr : NEW_LINE INDENT if x in mp . keys ( ) : NEW_LINE INDENT mp [ x ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ x ] = 1 NEW_LINE DEDENT DEDENT for i in mp . keys ( ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT count += pow ( 2 , mp [ i ] ) - 1 NEW_LINE DEDENT else : NEW_LINE INDENT count += pow ( 2 , mp [ i ] - 1 ) NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 2 , 2 , 2 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countSubseq ( arr , n ) ) NEW_LINE
Choose an integer K such that maximum of the xor values of K with all Array elements is minimized | Function to calculate Minimum possible value of the Maximum XOR in an array ; base case ; Divide elements into two sections ; Traverse all elements of current section and divide in two groups ; Check if one of the sections is empty ; explore both the possibilities using recursion ; Function to calculate minimum XOR value ; Start recursion from the most significant pos position ; Driver code
def calculate ( section , pos ) : NEW_LINE INDENT if ( pos < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT on_section = [ ] NEW_LINE off_section = [ ] NEW_LINE for el in section : NEW_LINE INDENT if ( ( ( el >> pos ) & 1 ) == 0 ) : NEW_LINE INDENT off_section . append ( el ) NEW_LINE DEDENT else : NEW_LINE INDENT on_section . append ( el ) NEW_LINE DEDENT DEDENT if ( len ( off_section ) == 0 ) : NEW_LINE INDENT return calculate ( on_section , pos - 1 ) NEW_LINE DEDENT if ( len ( on_section ) == 0 ) : NEW_LINE INDENT return calculate ( off_section , pos - 1 ) NEW_LINE DEDENT return min ( calculate ( off_section , pos - 1 ) , calculate ( on_section , pos - 1 ) ) + ( 1 << pos ) NEW_LINE DEDENT def minXorValue ( a , n ) : NEW_LINE INDENT section = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT section . append ( a [ i ] ) ; NEW_LINE DEDENT return calculate ( section , 30 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE A = [ 3 , 2 , 5 , 6 ] NEW_LINE print ( minXorValue ( A , N ) ) NEW_LINE DEDENT
Program to find the Nth natural number with exactly two bits set | Function to find the Nth number with exactly two bits set ; Keep incrementing until we reach the partition of ' N ' stored in bit_L ; set the rightmost bit based on bit_R ; Driver code
def findNthNum ( N ) : NEW_LINE INDENT bit_L = 1 ; NEW_LINE last_num = 0 ; NEW_LINE while ( bit_L * ( bit_L + 1 ) / 2 < N ) : NEW_LINE INDENT last_num = last_num + bit_L ; NEW_LINE bit_L += 1 ; NEW_LINE DEDENT bit_R = N - last_num - 1 ; NEW_LINE print ( ( 1 << bit_L ) + ( 1 << bit_R ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 13 ; NEW_LINE findNthNum ( N ) ; NEW_LINE DEDENT
XOR of all Prime numbers in an Array at positions divisible by K | Python3 program to find XOR of all Prime numbers in an Array at positions divisible by K ; 0 and 1 are not prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to find the required XOR ; To store XOR of the primes ; Traverse the array ; If the number is a prime ; If index is divisible by k ; Print the xor ; Driver code ; Function call
MAX = 1000005 NEW_LINE def SieveOfEratosthenes ( prime ) : NEW_LINE INDENT prime [ 1 ] = False ; NEW_LINE prime [ 0 ] = False ; NEW_LINE for p in range ( 2 , int ( MAX ** ( 1 / 2 ) ) ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * 2 , MAX , p ) : NEW_LINE INDENT prime [ i ] = False ; NEW_LINE DEDENT DEDENT DEDENT DEDENT def prime_xor ( arr , n , k ) : NEW_LINE INDENT prime = [ True ] * MAX ; NEW_LINE SieveOfEratosthenes ( prime ) ; NEW_LINE ans = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( prime [ arr [ i ] ] ) : NEW_LINE INDENT if ( ( i + 1 ) % k == 0 ) : NEW_LINE INDENT ans ^= arr [ i ] ; NEW_LINE DEDENT DEDENT DEDENT print ( ans ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 5 , 7 , 11 , 8 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE K = 2 ; NEW_LINE prime_xor ( arr , n , K ) ; NEW_LINE DEDENT
Find XOR of all elements in an Array | Function to find the XOR of all elements in the array ; Resultant variable ; Iterating through every element in the array ; Find XOR with the result ; Return the XOR ; Driver Code ; Function call
def xorOfArray ( arr , n ) : NEW_LINE INDENT xor_arr = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT xor_arr = xor_arr ^ arr [ i ] NEW_LINE DEDENT return xor_arr NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 9 , 12 , 13 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE print ( xorOfArray ( arr , n ) ) NEW_LINE DEDENT
Count of even set bits between XOR of two arrays | Function that count the XOR of B with all the element in A having even set bit ; Count the set bits in A [ i ] ; check for even or Odd ; To store the count of element for B such that XOR with all the element in A having even set bit ; Count set bit for B [ i ] ; check for Even or Odd ; Driver Code
def countEvenBit ( A , B , n , m ) : NEW_LINE INDENT i , j , cntOdd = 0 , 0 , 0 NEW_LINE cntEven = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT x = bin ( A [ i ] ) [ 2 : ] . count ( '1' ) NEW_LINE if ( x & 1 ) : NEW_LINE INDENT cntEven += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cntOdd += 1 NEW_LINE DEDENT DEDENT CountB = [ 0 ] * m NEW_LINE for i in range ( m ) : NEW_LINE INDENT x = bin ( B [ i ] ) [ 2 : ] . count ( '1' ) NEW_LINE if ( x & 1 ) : NEW_LINE INDENT CountB [ i ] = cntEven NEW_LINE DEDENT else : NEW_LINE INDENT CountB [ i ] = cntOdd NEW_LINE DEDENT DEDENT for i in range ( m ) : NEW_LINE INDENT print ( CountB [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 4 , 2 , 15 , 9 , 8 , 8 ] NEW_LINE B = [ 3 , 4 , 22 ] NEW_LINE countEvenBit ( A , B , 6 , 3 ) NEW_LINE DEDENT
Check if a Number is Odd or Even using Bitwise Operators | Returns true if n is even , else odd ; n ^ 1 is n + 1 , then even , else odd ; Driver code
def isEven ( n ) : NEW_LINE INDENT if ( n ^ 1 == n + 1 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 100 ; NEW_LINE print ( " Even " ) if isEven ( n ) else print ( " Odd " ) ; NEW_LINE DEDENT
Bitwise OR ( | ) of all even number from 1 to N | Function to return the bitwise OR of all the even numbers upto N ; Initialize result as 2 ; Driver code
def bitwiseOrTillN ( n ) : NEW_LINE INDENT result = 2 ; NEW_LINE for i in range ( 4 , n + 1 , 2 ) : NEW_LINE INDENT result = result | i NEW_LINE DEDENT return result NEW_LINE DEDENT n = 10 ; NEW_LINE print ( bitwiseOrTillN ( n ) ) ; NEW_LINE
Bitwise OR ( | ) of all even number from 1 to N | Python3 implementation of the above approach ; Function to return the bitwise OR of all even numbers upto N ; For value less than 2 ; Count total number of bits in bitwise or all bits will be set except last bit ; Compute 2 to the power bitCount and subtract 2 ; Driver code
from math import log2 NEW_LINE def bitwiseOrTillN ( n ) : NEW_LINE INDENT if ( n < 2 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT bitCount = int ( log2 ( n ) ) + 1 ; NEW_LINE return pow ( 2 , bitCount ) - 2 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 ; NEW_LINE print ( bitwiseOrTillN ( n ) ) ; NEW_LINE DEDENT
Generating N | Function to Generating N - bit Gray Code starting from K ; Generate gray code of corresponding binary number of integer i . ; Driver code
def genSequence ( n , val ) : NEW_LINE INDENT for i in range ( 1 << n ) : NEW_LINE INDENT x = i ^ ( i >> 1 ) ^ val ; NEW_LINE print ( x , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 ; k = 2 ; NEW_LINE genSequence ( n , k ) ; NEW_LINE DEDENT
Winner in the Rock | Function to return the winner of the game ; Both the players chose to play the same move ; Player A wins the game ; Function to perform the queries ; Driver code
def winner ( moves ) : NEW_LINE INDENT data = dict ( ) NEW_LINE data [ ' R ' ] = 0 NEW_LINE data [ ' P ' ] = 1 NEW_LINE data [ ' S ' ] = 2 NEW_LINE if ( moves [ 0 ] == moves [ 1 ] ) : NEW_LINE INDENT return " Draw " NEW_LINE DEDENT if ( ( ( data [ moves [ 0 ] ] | 1 << ( 2 ) ) - ( data [ moves [ 1 ] ] | 0 << ( 2 ) ) ) % 3 ) : NEW_LINE INDENT return " A " NEW_LINE DEDENT return " B " NEW_LINE DEDENT def performQueries ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( winner ( arr [ i ] ) ) NEW_LINE DEDENT DEDENT arr = [ " RS " , " SR " , " SP " , " PP " ] NEW_LINE n = len ( arr ) NEW_LINE performQueries ( arr , n ) NEW_LINE
Find the minimum spanning tree with alternating colored edges | Python implementation of the approach ; Initializing dp of size = ( 2 ^ 18 ) * 18 * 2. ; Recursive Function to calculate Minimum Cost with alternate colour edges ; Base case ; If already calculated ; Masking previous edges as explained in above formula . ; Function to Adjacency List Representation of a Graph ; Function to getCost for the Minimum Spanning Tree Formed ; Assigning maximum possible value . ; Driver Code
graph = [ [ [ 0 , 0 ] for i in range ( 18 ) ] for j in range ( 18 ) ] NEW_LINE dp = [ [ [ 0 , 0 ] for i in range ( 18 ) ] for j in range ( 1 << 15 ) ] NEW_LINE def minCost ( n : int , m : int , mask : int , prev : int , col : int ) -> int : NEW_LINE INDENT global dp NEW_LINE if mask == ( ( 1 << n ) - 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if dp [ mask ] [ prev ] [ col == 1 ] != 0 : NEW_LINE INDENT return dp [ mask ] [ prev ] [ col == 1 ] NEW_LINE DEDENT ans = int ( 1e9 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( 2 ) : NEW_LINE INDENT if graph [ prev ] [ i ] [ j ] and not ( mask & ( 1 << i ) ) and ( j != col ) : NEW_LINE INDENT z = graph [ prev ] [ i ] [ j ] + minCost ( n , m , mask | ( 1 << i ) , i , j ) NEW_LINE ans = min ( z , ans ) NEW_LINE DEDENT DEDENT DEDENT dp [ mask ] [ prev ] [ col == 1 ] = ans NEW_LINE return dp [ mask ] [ prev ] [ col == 1 ] NEW_LINE DEDENT def makeGraph ( vp : list , m : int ) : NEW_LINE INDENT global graph NEW_LINE for i in range ( m ) : NEW_LINE INDENT a = vp [ i ] [ 0 ] [ 0 ] - 1 NEW_LINE b = vp [ i ] [ 0 ] [ 1 ] - 1 NEW_LINE cost = vp [ i ] [ 1 ] [ 0 ] NEW_LINE color = vp [ i ] [ 1 ] [ 1 ] NEW_LINE graph [ a ] [ b ] [ color == ' W ' ] = cost NEW_LINE graph [ b ] [ a ] [ color == ' W ' ] = cost NEW_LINE DEDENT DEDENT def getCost ( n : int , m : int ) -> int : NEW_LINE INDENT ans = int ( 1e9 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans = min ( ans , minCost ( n , m , 1 << i , i , 2 ) ) NEW_LINE DEDENT if ans != int ( 1e9 ) : NEW_LINE INDENT return ans NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE m = 4 NEW_LINE vp = [ [ [ 1 , 2 ] , [ 2 , ' B ' ] ] , [ [ 1 , 2 ] , [ 3 , ' W ' ] ] , [ [ 2 , 3 ] , [ 4 , ' W ' ] ] , [ [ 2 , 3 ] , [ 5 , ' B ' ] ] ] NEW_LINE makeGraph ( vp , m ) NEW_LINE print ( getCost ( n , m ) ) NEW_LINE DEDENT