codeparrot/xlcost-text-to-code huggingface dataset:
Program to convert a given number to words | A function that prints given number in words ; Get number of digits in given number ; Base cases ; The first string is not used , it is to make array indexing simple ; The first string is not used , it is to make array indexing simple ; The first two string are not used , they are to make array indexing simple ; Used for debugging purpose only ; For single digit number ; Iterate while num is not '\0 ; Code path for first 2 digits ; here len can be 3 or 4 ; Code path for last 2 digits ; Need to explicitly handle 10 - 19. Sum of the two digits is used as index of " two _ digits " array of strings ; Need to explicitely handle 20 ; Rest of the two digit numbers i . e . , 21 to 99
def convert_to_words ( num ) : NEW_LINE INDENT l = len ( num ) NEW_LINE if ( l == 0 ) : NEW_LINE INDENT print ( " empty ▁ string " ) NEW_LINE return NEW_LINE DEDENT if ( l > 4 ) : NEW_LINE INDENT print ( " Length ▁ more ▁ than ▁ 4 ▁ is ▁ not ▁ supported " ) NEW_LINE return NEW_LINE DEDENT single_digits = [ " zero " , " one " , " two " , " three " , " four " , " five " , " six " , " seven " , " eight " , " nine " ] NEW_LINE two_digits = [ " " , " ten " , " eleven " , " twelve " , " thirteen " , " fourteen " , " fifteen " , " sixteen " , " seventeen " , " eighteen " , " nineteen " ] NEW_LINE tens_multiple = [ " " , " " , " twenty " , " thirty " , " forty " , " fifty " , " sixty " , " seventy " , " eighty " , " ninety " ] NEW_LINE tens_power = [ " hundred " , " thousand " ] NEW_LINE print ( num , " : " , end = " ▁ " ) NEW_LINE if ( l == 1 ) : NEW_LINE INDENT print ( single_digits [ ord ( num [ 0 ] ) - 48 ] ) NEW_LINE return NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT x = 0 NEW_LINE while ( x < len ( num ) ) : NEW_LINE INDENT if ( l >= 3 ) : NEW_LINE INDENT if ( ord ( num [ x ] ) - 48 != 0 ) : NEW_LINE INDENT print ( single_digits [ ord ( num [ x ] ) - 48 ] , end = " ▁ " ) NEW_LINE print ( tens_power [ l - 3 ] , end = " ▁ " ) NEW_LINE DEDENT l -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( ord ( num [ x ] ) - 48 == 1 ) : NEW_LINE INDENT sum = ( ord ( num [ x ] ) - 48 + ord ( num [ x + 1 ] ) - 48 ) NEW_LINE print ( two_digits [ sum ] ) NEW_LINE return NEW_LINE DEDENT elif ( ord ( num [ x ] ) - 48 == 2 and ord ( num [ x + 1 ] ) - 48 == 0 ) : NEW_LINE INDENT print ( " twenty " ) NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT i = ord ( num [ x ] ) - 48 NEW_LINE if ( i > 0 ) : NEW_LINE INDENT print ( tens_multiple [ i ] , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " " , end = " " ) NEW_LINE DEDENT x += 1 NEW_LINE if ( ord ( num [ x ] ) - 48 != 0 ) : NEW_LINE INDENT print ( single_digits [ ord ( num [ x ] ) - 48 ] ) NEW_LINE DEDENT DEDENT DEDENT x += 1 NEW_LINE DEDENT DEDENT
Check if a number is multiple of 5 without using / and % operators | Assuming that integer takes 4 bytes , there can be maximum 10 digits in a integer ; Check the last character of string ; Driver Code
MAX = 11 ; NEW_LINE def isMultipleof5 ( n ) : NEW_LINE INDENT s = str ( n ) ; NEW_LINE l = len ( s ) ; NEW_LINE if ( s [ l - 1 ] == '5' or s [ l - 1 ] == '0' ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT n = 19 ; NEW_LINE if ( isMultipleof5 ( n ) == True ) : NEW_LINE INDENT print ( n , " is ▁ multiple ▁ of ▁ 5" ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( n , " is ▁ not ▁ a ▁ multiple ▁ of ▁ 5" ) ; NEW_LINE DEDENT
Check if there are T number of continuous of blocks of 0 s or not in given Binary Matrix | python program for the above approach ; Stores the moves in the matrix ; Function to find if the current cell lies in the matrix or not ; Function to perform the DFS Traversal ; Iterate over the direction vector ; DFS Call ; Function to check if it satisfy the given criteria or not ; Keeps the count of cell having value as 0 ; If condition doesn 't satisfy ; Keeps the track of unvisted cell having values 0 ; Increasing count of black_spot ; Find the GCD of N and M ; Print the result ; Driver Code
import math NEW_LINE M = 1000000000 + 7 NEW_LINE directions = [ [ 0 , 1 ] , [ - 1 , 0 ] , [ 0 , - 1 ] , [ 1 , 0 ] , [ 1 , 1 ] , [ - 1 , - 1 ] , [ - 1 , 1 ] , [ 1 , - 1 ] ] NEW_LINE def isInside ( i , j , N , M ) : NEW_LINE INDENT if ( i >= 0 and i < N and j >= 0 and j < M ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def DFS ( mat , N , M , i , j , visited ) : NEW_LINE INDENT if ( visited [ i ] [ j ] == True ) : NEW_LINE INDENT return NEW_LINE DEDENT visited [ i ] [ j ] = True NEW_LINE for it in directions : NEW_LINE INDENT I = i + it [ 0 ] NEW_LINE J = j + it [ 1 ] NEW_LINE if ( isInside ( I , J , N , M ) ) : NEW_LINE INDENT if ( mat [ I ] [ J ] == 0 ) : NEW_LINE INDENT DFS ( mat , N , M , I , J , visited ) NEW_LINE DEDENT DEDENT DEDENT DEDENT def check ( N , M , mat ) : NEW_LINE INDENT black = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( 0 , M ) : NEW_LINE INDENT if ( mat [ i ] [ j ] == 0 ) : NEW_LINE INDENT black += 1 NEW_LINE DEDENT DEDENT DEDENT if ( black < 2 * ( max ( N , M ) ) ) : NEW_LINE INDENT print ( " NO " ) NEW_LINE return NEW_LINE DEDENT visited = [ ] NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT temp = [ ] NEW_LINE for j in range ( 0 , M ) : NEW_LINE INDENT temp . append ( False ) NEW_LINE DEDENT visited . append ( temp ) NEW_LINE DEDENT black_spots = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( 0 , M ) : NEW_LINE INDENT if ( visited [ i ] [ j ] == False and mat [ i ] [ j ] == 0 ) : NEW_LINE INDENT black_spots += 1 NEW_LINE DFS ( mat , N , M , i , j , visited ) NEW_LINE DEDENT DEDENT DEDENT T = math . gcd ( N , M ) NEW_LINE if black_spots >= T : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE M = 3 NEW_LINE mat = [ [ 0 , 0 , 1 ] , [ 1 , 1 , 1 ] , [ 0 , 0 , 1 ] ] NEW_LINE check ( M , N , mat ) NEW_LINE DEDENT
Count subsequences having odd Bitwise OR values in an array | Function to count the subsequences having odd bitwise OR value ; Stores count of odd elements ; Stores count of even elements ; Traverse the array arr [ ] ; If element is odd ; Return the final answer ; Driver Code ; Given array arr [ ]
def countSubsequences ( arr ) : NEW_LINE INDENT odd = 0 ; NEW_LINE even = 0 ; NEW_LINE for x in arr : NEW_LINE INDENT if ( x & 1 ) : NEW_LINE INDENT odd += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT even += 1 ; NEW_LINE DEDENT DEDENT return ( ( 1 << odd ) - 1 ) * ( 1 << even ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 4 , 1 ] ; NEW_LINE print ( countSubsequences ( arr ) ) ; NEW_LINE DEDENT
Compress a Binary Tree from top to bottom with overlapping condition | Structure of a node of th tree ; Function to compress all the nodes on the same vertical line ; Stores node by compressing all nodes on the current vertical line ; Check if i - th bit of current bit set or not ; Iterate over the range [ 0 , 31 ] ; Stores count of set bits at i - th positions ; Stores count of clear bits at i - th positions ; Traverse the array ; If i - th bit of current element is set ; Update S ; Update NS ; If count of set bits at i - th position is greater than count of clear bits ; Update ans ; Update getBit ; Function to compress all the nodes on the same vertical line with a single node that satisfies the condition ; Map all the nodes on the same vertical line ; Function to traverse the tree and map all the nodes of same vertical line to vertical distance ; Storing the values in the map ; Recursive calls on left and right subtree ; Getting the range of horizontal distances ; Driver Code ; Function Call
class TreeNode : NEW_LINE INDENT def __init__ ( self , val = ' ' , left = None , right = None ) : NEW_LINE INDENT self . val = val NEW_LINE self . left = left NEW_LINE self . right = right NEW_LINE DEDENT DEDENT def evalComp ( arr ) : NEW_LINE INDENT ans = 0 NEW_LINE getBit = 1 NEW_LINE for i in range ( 32 ) : NEW_LINE INDENT S = 0 NEW_LINE NS = 0 NEW_LINE for j in arr : NEW_LINE INDENT if getBit & j : NEW_LINE INDENT S += 1 NEW_LINE DEDENT else : NEW_LINE INDENT NS += 1 NEW_LINE DEDENT DEDENT if S > NS : NEW_LINE INDENT ans += 2 ** i NEW_LINE DEDENT getBit <<= 1 NEW_LINE DEDENT print ( ans , end = " ▁ " ) NEW_LINE DEDENT def compressTree ( root ) : NEW_LINE INDENT mp = { } NEW_LINE def Trav ( root , hd ) : NEW_LINE INDENT if not root : NEW_LINE INDENT return NEW_LINE DEDENT if hd not in mp : NEW_LINE INDENT mp [ hd ] = [ root . val ] NEW_LINE DEDENT else : NEW_LINE INDENT mp [ hd ] . append ( root . val ) NEW_LINE DEDENT Trav ( root . left , hd - 1 ) NEW_LINE Trav ( root . right , hd + 1 ) NEW_LINE DEDENT Trav ( root , 0 ) NEW_LINE lower = min ( mp . keys ( ) ) NEW_LINE upper = max ( mp . keys ( ) ) NEW_LINE for i in range ( lower , upper + 1 ) : NEW_LINE INDENT evalComp ( mp [ i ] ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = TreeNode ( 5 ) NEW_LINE root . left = TreeNode ( 3 ) NEW_LINE root . right = TreeNode ( 2 ) NEW_LINE root . left . left = TreeNode ( 1 ) NEW_LINE root . left . right = TreeNode ( 4 ) NEW_LINE root . right . left = TreeNode ( 1 ) NEW_LINE root . right . right = TreeNode ( 2 ) NEW_LINE compressTree ( root ) NEW_LINE DEDENT
Bitwise OR of Bitwise AND of all subsets of an Array for Q queries | Function to find the OR of AND of all subsets of the array for each query ; An array to store the bits ; Itearte for all the bits ; Iterate over all the numbers and store the bits in bits [ ] array ; Itearte over all the queries ; Replace the bits of the value at arr [ queries [ p ] [ 0 ] ] with the bits of queries [ p ] [ 1 ] ; Substitute the value in the array ; Find OR of the bits [ ] array ; Print the answer ; Given Input ; Function Call
def Or_of_Ands_for_each_query ( arr , n , queries , q ) : NEW_LINE INDENT bits = [ 0 for x in range ( 32 ) ] NEW_LINE for i in range ( 0 , 32 ) : NEW_LINE INDENT for j in range ( 0 , n ) : NEW_LINE INDENT if ( ( 1 << i ) & arr [ j ] ) : NEW_LINE INDENT bits [ i ] += 1 NEW_LINE DEDENT DEDENT DEDENT for p in range ( 0 , q ) : NEW_LINE INDENT for i in range ( 0 , 32 ) : NEW_LINE INDENT if ( ( 1 << i ) & arr [ queries [ p ] [ 0 ] ] ) : NEW_LINE INDENT bits [ i ] -= 1 NEW_LINE DEDENT if ( queries [ p ] [ 1 ] & ( 1 << i ) ) : NEW_LINE INDENT bits [ i ] += 1 NEW_LINE DEDENT DEDENT arr [ queries [ p ] [ 0 ] ] = queries [ p ] [ 1 ] NEW_LINE ans = 0 NEW_LINE for i in range ( 0 , 32 ) : NEW_LINE INDENT if ( bits [ i ] != 0 ) : NEW_LINE INDENT ans |= ( 1 << i ) NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT DEDENT n = 3 NEW_LINE q = 2 NEW_LINE arr = [ 3 , 5 , 7 ] NEW_LINE queries = [ [ 1 , 2 ] , [ 2 , 1 ] ] NEW_LINE Or_of_Ands_for_each_query ( arr , n , queries , q ) NEW_LINE
Find XOR sum of Bitwise AND of all pairs from given two Arrays | Function to calculate the XOR sum of all ANDS of all pairs on A and B ; variable to store anshu ; when there has been no AND of pairs before this ; Driver code ; Input ; Function call
def XorSum ( A , B , N , M ) : NEW_LINE INDENT ans = - 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if ( ans == - 1 ) : NEW_LINE INDENT ans = ( A [ i ] & B [ j ] ) NEW_LINE DEDENT else : NEW_LINE INDENT ans ^= ( A [ i ] & B [ j ] ) NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 3 , 5 ] NEW_LINE B = [ 2 , 3 ] NEW_LINE N = len ( A ) NEW_LINE M = len ( B ) NEW_LINE print ( XorSum ( A , B , N , M ) ) NEW_LINE DEDENT
Shortest path length between two given nodes such that adjacent nodes are at bit difference 2 | Function to count set bits in a number ; Stores count of set bits in xo ; Iterate over each bits of xo ; If current bit of xo is 1 ; Update count ; Update xo ; Function to find length of shortest path between the nodes a and b ; Stores XOR of a and b ; Stores the count of set bits in xorVal ; If cnt is an even number ; Driver Code ; Given N ; Given a and b ; Function call
def countbitdiff ( xo ) : NEW_LINE INDENT count = 0 NEW_LINE while ( xo ) : NEW_LINE INDENT if ( xo % 2 == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT xo = xo // 2 NEW_LINE DEDENT return count NEW_LINE DEDENT def shortestPath ( n , a , b ) : NEW_LINE INDENT xorVal = a ^ b NEW_LINE cnt = countbitdiff ( xorVal ) NEW_LINE if ( cnt % 2 == 0 ) : NEW_LINE INDENT print ( cnt // 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 15 NEW_LINE a , b = 15 , 3 NEW_LINE shortestPath ( n , a , b ) NEW_LINE DEDENT
Modify a matrix by converting each element to XOR of its digits | Python3 program for the above approach ; Function to calculate Bitwise XOR of digits present in X ; Stores the Bitwise XOR ; While X is true ; Update Bitwise XOR of its digits ; Return the result ; Function to print matrix after converting each matrix element to XOR of its digits ; Traverse each row of arr [ ] [ ] ; Traverse each column of arr [ ] [ ] ; Function to convert the given matrix to required XOR matrix ; Traverse each row of arr [ ] [ ] ; Traverse each column of arr [ ] [ ] ; Store the current matrix element ; Find the xor of digits present in X ; Stores the XOR value ; Print resultant matrix ; Driver Code
M = 3 NEW_LINE N = 3 NEW_LINE def findXOR ( X ) : NEW_LINE INDENT ans = 0 NEW_LINE while ( X ) : NEW_LINE INDENT ans ^= ( X % 10 ) NEW_LINE X //= 10 NEW_LINE DEDENT return ans NEW_LINE DEDENT def printXORmatrix ( arr ) : NEW_LINE INDENT for i in range ( 3 ) : NEW_LINE INDENT for j in range ( 3 ) : NEW_LINE INDENT print ( arr [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT def convertXOR ( arr ) : NEW_LINE INDENT for i in range ( 3 ) : NEW_LINE INDENT for j in range ( 3 ) : NEW_LINE INDENT X = arr [ i ] [ j ] NEW_LINE temp = findXOR ( X ) NEW_LINE arr [ i ] [ j ] = temp NEW_LINE DEDENT DEDENT printXORmatrix ( arr ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 27 , 173 , 5 ] , [ 21 , 6 , 624 ] , [ 5 , 321 , 49 ] ] NEW_LINE convertXOR ( arr ) NEW_LINE DEDENT
Sum of Bitwise AND of sum of pairs and their Bitwise AND from a given array | Python 3 program for the above approach ; Function to find the sum of Bitwise AND of sum of pairs and their Bitwise AND from a given array ; Stores the total sum ; Check if jth bit is set ; Stores the right shifted element by ( i + 1 ) ; Update the value of X ; Push in vector vec ; Sort the vector in ascending order ; Traverse the vector vec ; Stores the value 2 ^ ( i + 1 ) - 2 ^ ( i ) - vec [ j ] ; Stores count of numbers whose value > Y ; Update the ans ; Return the ans ; Driver Code
M = 1000000007 NEW_LINE from bisect import bisect_left NEW_LINE def findSum ( A , N ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 30 ) : NEW_LINE INDENT vec = [ ] NEW_LINE for j in range ( N ) : NEW_LINE INDENT if ( ( A [ j ] >> i ) & 1 ) : NEW_LINE INDENT X = ( A [ j ] >> ( i + 1 ) ) NEW_LINE X = X * ( 1 << ( i + 1 ) ) NEW_LINE X %= M NEW_LINE vec . append ( A [ j ] - X ) NEW_LINE DEDENT DEDENT vec . sort ( reverse = False ) NEW_LINE for j in range ( len ( vec ) ) : NEW_LINE INDENT Y = ( 1 << ( i + 1 ) ) + ( 1 << i ) - vec [ j ] NEW_LINE temp = vec [ j + 1 : ] NEW_LINE idx = int ( bisect_left ( temp , Y ) ) NEW_LINE ans += ( ( len ( vec ) - idx ) * ( 1 << i ) ) NEW_LINE ans %= M NEW_LINE DEDENT DEDENT ans /= 7 NEW_LINE print ( int ( ans % M ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE findSum ( arr , N ) NEW_LINE DEDENT
Count subarrays made up of elements having exactly K set bits | Function to count the number of set bits in an integer N ; Stores the count of set bits ; While N is non - zero ; If the LSB is 1 , then increment ans by 1 ; Return the total set bits ; Function to count the number of subarrays having made up of elements having K set bits ; Stores the total count of resultant subarrays ; Traverse the given array ; If the current element has K set bits ; Otherwise ; Increment count of subarrays ; Return total count of subarrays ; Driver Code ; Function Call
def countSet ( N ) : NEW_LINE INDENT ans = 0 NEW_LINE while N : NEW_LINE INDENT ans += N & 1 NEW_LINE N >>= 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT def countSub ( arr , k ) : NEW_LINE INDENT ans = 0 NEW_LINE setK = 0 NEW_LINE for i in arr : NEW_LINE INDENT if countSet ( i ) == k : NEW_LINE INDENT setK += 1 NEW_LINE DEDENT else : NEW_LINE INDENT setK = 0 NEW_LINE DEDENT ans += setK NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 4 , 2 , 1 , 5 , 6 ] NEW_LINE K = 2 NEW_LINE print ( countSub ( arr , K ) ) NEW_LINE
Count subarrays having odd Bitwise XOR | Function to count the number of subarrays of the given array having odd Bitwise XOR ; Stores number of odd numbers upto i - th index ; Stores number of required subarrays starting from i - th index ; Store the required result ; Find the number of subarrays having odd Bitwise XOR values starting at 0 - th index ; Check if current element is odd ; If the current value of odd is not zero , increment c_odd by 1 ; Find the number of subarrays having odd bitwise XOR value starting at ith index and add to result ; Add c_odd to result ; Print the result ; Driver Code ; Given array ; Stores the size of the array
def oddXorSubarray ( a , n ) : NEW_LINE INDENT odd = 0 NEW_LINE c_odd = 0 NEW_LINE result = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] & 1 ) : NEW_LINE INDENT odd = not odd NEW_LINE DEDENT if ( odd ) : NEW_LINE INDENT c_odd += 1 NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT result += c_odd NEW_LINE if ( a [ i ] & 1 ) : NEW_LINE INDENT c_odd = ( n - i - c_odd ) NEW_LINE DEDENT DEDENT print ( result ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 4 , 7 , 9 , 10 ] NEW_LINE N = len ( arr ) NEW_LINE oddXorSubarray ( arr , N ) NEW_LINE DEDENT
Sum of Bitwise OR of every array element paired with all other array elements | Function to print required sum for every valid index i ; Store the required sum for current array element ; Generate all possible pairs ( arr [ i ] , arr [ j ] ) ; Update the value of req_sum ; Print required sum ; Given array ; Size of array ; Function call
def printORSumforEachElement ( arr , N ) : NEW_LINE INDENT for i in range ( 0 , N ) : NEW_LINE INDENT req_sum = 0 NEW_LINE for j in range ( 0 , N ) : NEW_LINE INDENT req_sum += ( arr [ i ] arr [ j ] ) NEW_LINE DEDENT print ( req_sum , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE printORSumforEachElement ( arr , N ) NEW_LINE
Sum of Bitwise OR of each array element of an array with all elements of another array | Function to compute sum of Bitwise OR of each element in arr1 [ ] with all elements of the array arr2 [ ] ; Declaring an array of size 32 to store the count of each bit ; Traverse the array arr1 [ ] ; Current bit position ; While num exceeds 0 ; Checks if i - th bit is set or not ; Increment the count at bit_position by one ; Increment bit_position ; Right shift the num by one ; Traverse in the arr2 [ ] ; Store the ith bit value ; Total required sum ; Traverse in the range [ 0 , 31 ] ; Check if current bit is set ; Increment the Bitwise sum by N * ( 2 ^ i ) ; Right shift num by one ; Left shift valee_at_that_bit by one ; Print the sum obtained for ith number in arr1 [ ] ; Given arr1 [ ] ; Given arr2 [ ] ; Size of arr1 [ ] ; Size of arr2 [ ] ; Function Call
def Bitwise_OR_sum_i ( arr1 , arr2 , M , N ) : NEW_LINE INDENT frequency = [ 0 ] * 32 NEW_LINE for i in range ( N ) : NEW_LINE INDENT bit_position = 0 NEW_LINE num = arr1 [ i ] NEW_LINE while ( num ) : NEW_LINE INDENT if ( num & 1 != 0 ) : NEW_LINE INDENT frequency [ bit_position ] += 1 NEW_LINE DEDENT bit_position += 1 NEW_LINE num >>= 1 NEW_LINE DEDENT DEDENT for i in range ( M ) : NEW_LINE INDENT num = arr2 [ i ] NEW_LINE value_at_that_bit = 1 NEW_LINE bitwise_OR_sum = 0 NEW_LINE for bit_position in range ( 32 ) : NEW_LINE INDENT if ( num & 1 != 0 ) : NEW_LINE INDENT bitwise_OR_sum += N * value_at_that_bit NEW_LINE DEDENT else : NEW_LINE INDENT bitwise_OR_sum += ( frequency [ bit_position ] * value_at_that_bit ) NEW_LINE DEDENT num >>= 1 NEW_LINE value_at_that_bit <<= 1 NEW_LINE DEDENT print ( bitwise_OR_sum , end = " ▁ " ) NEW_LINE DEDENT return NEW_LINE DEDENT arr1 = [ 1 , 2 , 3 ] NEW_LINE arr2 = [ 1 , 2 , 3 ] NEW_LINE N = len ( arr1 ) NEW_LINE M = len ( arr2 ) NEW_LINE Bitwise_OR_sum_i ( arr1 , arr2 , M , N ) NEW_LINE
Count pairs from given array with Bitwise OR equal to K | Function that counts the pairs from the array whose Bitwise OR is K ; Stores the required count of pairs ; Generate all possible pairs ; Perform OR operation ; If Bitwise OR is equal to K , increment count ; Print the total count ; Driver Code ; Function Call
def countPairs ( arr , k , size ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( size - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , size ) : NEW_LINE INDENT x = arr [ i ] | arr [ j ] NEW_LINE if ( x == k ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT print ( count ) NEW_LINE DEDENT arr = [ 2 , 38 , 44 , 29 , 62 ] NEW_LINE K = 46 NEW_LINE N = len ( arr ) NEW_LINE countPairs ( arr , K , N ) NEW_LINE
Count nodes having Bitwise XOR of all edges in their path from the root equal to K | Initialize the adjacency list to represent the tree ; Marks visited / unvisited vertices ; Stores the required count of nodes ; DFS to visit each vertex ; Mark the current node as visited ; Update the counter xor is K ; Visit adjacent nodes ; Calculate Bitwise XOR of edges in the path ; Recursive call to dfs function ; Function to construct the tree and prrequired count of nodes ; Add edges ; Print answer ; Driver Code ; Given K and R ; Given edges ; Number of vertices ; Function call
adj = [ [ ] for i in range ( 100005 ) ] NEW_LINE visited = [ 0 ] * 100005 NEW_LINE ans = 0 NEW_LINE def dfs ( node , xorr , k ) : NEW_LINE INDENT global ans NEW_LINE visited [ node ] = 1 NEW_LINE if ( node != 1 and xorr == k ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT for x in adj [ node ] : NEW_LINE INDENT if ( not visited [ x [ 0 ] ] ) : NEW_LINE INDENT xorr1 = xorr ^ x [ 1 ] NEW_LINE dfs ( x [ 0 ] , xorr1 , k ) NEW_LINE DEDENT DEDENT DEDENT def countNodes ( N , K , R , edges ) : NEW_LINE INDENT for i in range ( N - 1 ) : NEW_LINE INDENT u = edges [ i ] [ 0 ] NEW_LINE v = edges [ i ] [ 1 ] NEW_LINE w = edges [ i ] [ 2 ] NEW_LINE adj [ u ] . append ( [ v , w ] ) NEW_LINE adj [ v ] . append ( [ u , w ] ) NEW_LINE DEDENT dfs ( R , 0 , K ) NEW_LINE print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT K = 0 NEW_LINE R = 1 NEW_LINE edges = [ [ 1 , 2 , 3 ] , [ 1 , 3 , 1 ] , [ 2 , 4 , 3 ] , [ 2 , 5 , 4 ] , [ 3 , 6 , 1 ] , [ 3 , 7 , 2 ] ] NEW_LINE N = len ( edges ) NEW_LINE countNodes ( N , K , R , edges ) NEW_LINE DEDENT
Bitwise operations on Subarrays of size K | Function to find the minimum XOR of the subarray of size K ; K must be smaller than or equal to n ; Initialize the beginning index of result ; Compute XOR sum of first subarray of size K ; Initialize minimum XOR sum as current xor ; Traverse from ( k + 1 ) ' th ▁ ▁ element ▁ to ▁ n ' th element ; XOR with current item and first item of previous subarray ; Update result if needed ; Print the minimum XOR ; Driver Code ; Given array arr ; Given subarray size K ; Function call
def findMinXORSubarray ( arr , n , k ) : NEW_LINE INDENT if ( n < k ) : NEW_LINE INDENT return ; NEW_LINE DEDENT res_index = 0 ; NEW_LINE curr_xor = 0 ; NEW_LINE for i in range ( k ) : NEW_LINE INDENT curr_xor ^= arr [ i ] ; NEW_LINE DEDENT min_xor = curr_xor ; NEW_LINE for i in range ( k , n ) : NEW_LINE INDENT curr_xor ^= ( arr [ i ] ^ arr [ i - k ] ) ; NEW_LINE if ( curr_xor < min_xor ) : NEW_LINE INDENT min_xor = curr_xor ; NEW_LINE res_index = ( i - k + 1 ) ; NEW_LINE DEDENT DEDENT print ( min_xor ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 7 , 90 , 20 , 10 , 50 , 40 ] ; NEW_LINE k = 3 ; NEW_LINE n = len ( arr ) ; NEW_LINE findMinXORSubarray ( arr , n , k ) ; NEW_LINE DEDENT