codeparrot/xlcost-text-to-code huggingface dataset:
Find number of subarrays with even sum | Python 3 program to count number of sub - arrays with even sum using an efficient algorithm Time Complexity - O ( N ) Space Complexity - O ( 1 ) ; result may be large enough not to fit in int ; ; to keep track of subarrays with even sum starting from index i ; if a [ i ] is odd then all subarrays starting from index i + 1 which was odd becomes even when a [ i ] gets added to it . ; if a [ i ] is even then all subarrays starting from index i + 1 which was even remains even and one extra a [ i ] even subarray gets added to it . ; Driver code
def countEvenSum ( arr , n ) : NEW_LINE INDENT res = 0 NEW_LINE s = 0 NEW_LINE for i in reversed ( range ( n ) ) : NEW_LINE INDENT if arr [ i ] % 2 == 1 : NEW_LINE INDENT s = n - i - 1 - s NEW_LINE DEDENT else : NEW_LINE INDENT s = s + 1 NEW_LINE DEDENT res = res + s NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 2 , 3 , 4 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " The ▁ Number ▁ of ▁ Subarrays ▁ with ▁ even " " ▁ sum ▁ is " , countEvenSum ( arr , n ) ) NEW_LINE DEDENT
Linear Diophantine Equations | Python 3 program to check for solutions of diophantine equations ; This function checks if integral solutions are possible ; Driver Code ; First example ; Second example ; Third example
from math import gcd NEW_LINE def isPossible ( a , b , c ) : NEW_LINE INDENT return ( c % gcd ( a , b ) == 0 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 3 NEW_LINE b = 6 NEW_LINE c = 9 NEW_LINE if ( isPossible ( a , b , c ) ) : NEW_LINE INDENT print ( " Possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE DEDENT a = 3 NEW_LINE b = 6 NEW_LINE c = 8 NEW_LINE if ( isPossible ( a , b , c ) ) : NEW_LINE INDENT print ( " Possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE DEDENT a = 2 NEW_LINE b = 5 NEW_LINE c = 1 NEW_LINE if ( isPossible ( a , b , c ) ) : NEW_LINE INDENT print ( " Possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE DEDENT DEDENT
Farey Sequence | Efficient Python3 program to print Farey Sequence of order n ; Optimized function to print Farey sequence of order n ; We know first two terms are 0 / 1 and 1 / n ; For next terms to be evaluated ; Using recurrence relation to find the next term ; Print next term ; Update x1 , y1 , x2 and y2 for next iteration ; Driver Code
import math NEW_LINE def farey ( n ) : NEW_LINE INDENT x1 = 0 ; NEW_LINE y1 = 1 ; NEW_LINE x2 = 1 ; NEW_LINE y2 = n ; NEW_LINE print ( x1 , end = " " ) NEW_LINE print ( " / " , end = " " ) NEW_LINE print ( y1 , x2 , end = " " ) NEW_LINE print ( " / " , end = " " ) NEW_LINE print ( y2 , end = " ▁ " ) ; NEW_LINE x = 0 ; NEW_LINE y = 0 ; NEW_LINE while ( y != 1.0 ) : NEW_LINE INDENT x = math . floor ( ( y1 + n ) / y2 ) * x2 - x1 ; NEW_LINE y = math . floor ( ( y1 + n ) / y2 ) * y2 - y1 ; NEW_LINE print ( x , end = " " ) NEW_LINE print ( " / " , end = " " ) NEW_LINE print ( y , end = " ▁ " ) ; NEW_LINE x1 = x2 ; NEW_LINE x2 = x ; NEW_LINE y1 = y2 ; NEW_LINE y2 = y ; NEW_LINE DEDENT DEDENT n = 7 ; NEW_LINE print ( " Farey ▁ Sequence ▁ of ▁ order " , n , " is " ) ; NEW_LINE farey ( n ) ; NEW_LINE
Find smallest values of x and y such that ax | To find GCD using Eculcid 's algorithm ; Prints smallest values of x and y that satisfy " ax ▁ - ▁ by ▁ = ▁ 0" ; Find LCM ; Driver code
def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return ( gcd ( b , a % b ) ) NEW_LINE DEDENT def findSmallest ( a , b ) : NEW_LINE INDENT lcm = ( a * b ) / gcd ( a , b ) NEW_LINE print ( " x ▁ = " , lcm / a ,   " y =   " , lcm / b ) NEW_LINE DEDENT a = 25 NEW_LINE b = 35 NEW_LINE findSmallest ( a , b ) NEW_LINE
Compute n ! under modulo p | Utility function to do modular exponentiation . It returns ( x ^ y ) % p ; res = 1 ; Initialize result x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Function to find modular inverse of a under modulo p using Fermat 's method. Assumption: p is prime ; Returns n ! % p using Wilson 's Theorem ; n ! % p is 0 if n >= p ; Initialize result as ( p - 1 ) ! which is - 1 or ( p - 1 ) ; Multiply modulo inverse of all numbers from ( n + 1 ) to p ; Driver code
def power ( x , y , p ) : NEW_LINE INDENT while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % p ; NEW_LINE DEDENT x = ( x * x ) % p ; NEW_LINE DEDENT return res NEW_LINE DEDENT def modInverse ( a , p ) : NEW_LINE INDENT return power ( a , p - 2 , p ) NEW_LINE DEDENT def modFact ( n , p ) : NEW_LINE INDENT if ( p <= n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT res = ( p - 1 ) NEW_LINE for i in range ( n + 1 , p ) : NEW_LINE INDENT res = ( res * modInverse ( i , p ) ) % p NEW_LINE DEDENT return res NEW_LINE DEDENT n = 25 NEW_LINE p = 29 NEW_LINE print ( modFact ( n , p ) ) NEW_LINE
Count number of ways to divide a number in 4 parts | Returns count of ways ; Generate all possible quadruplet and increment counter when sum of a quadruplet is equal to n ; Driver Code
def countWays ( n ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT for k in range ( j , n ) : NEW_LINE INDENT for l in range ( k , n ) : NEW_LINE INDENT if ( i + j + k + l == n ) : NEW_LINE INDENT counter += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT return counter NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 8 NEW_LINE print ( countWays ( n ) ) NEW_LINE DEDENT
Sum of Bitwise And of all pairs in a given array | Returns value of " arr [ 0 ] ▁ & ▁ arr [ 1 ] ▁ + ▁ arr [ 0 ] ▁ & ▁ arr [ 2 ] ▁ + ▁ . . . ▁ arr [ i ] ▁ & ▁ arr [ j ] ▁ + ▁ . . . . . ▁ arr [ n - 2 ] ▁ & ▁ arr [ n - 1 ] " ; Consider all pairs ( arr [ i ] , arr [ j ) such that i < j ; Driver program to test above function
def pairAndSum ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( ( i + 1 ) , n ) : NEW_LINE INDENT ans = ans + arr [ i ] & arr [ j ] NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 5 , 10 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE print ( pairAndSum ( arr , n ) ) NEW_LINE
Sum of Bitwise And of all pairs in a given array | Returns value of " arr [ 0 ] ▁ & ▁ arr [ 1 ] ▁ + ▁ arr [ 0 ] ▁ & ▁ arr [ 2 ] ▁ + ▁ . . . ▁ arr [ i ] ▁ & ▁ arr [ j ] ▁ + ▁ . . . . . ▁ arr [ n - 2 ] ▁ & ▁ arr [ n - 1 ] " ; Traverse over all bits ; Count number of elements with i 'th bit set  Initialize the count ; There are k set bits , means k ( k - 1 ) / 2 pairs . Every pair adds 2 ^ i to the answer . Therefore , we add "2 ^ i ▁ * ▁ [ k * ( k - 1 ) / 2 ] " to the answer . ; Driver program to test above function
def pairAndSum ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , 32 ) : NEW_LINE INDENT k = 0 NEW_LINE for j in range ( 0 , n ) : NEW_LINE INDENT if ( ( arr [ j ] & ( 1 << i ) ) ) : NEW_LINE INDENT k = k + 1 NEW_LINE DEDENT DEDENT ans = ans + ( 1 << i ) * ( k * ( k - 1 ) // 2 ) NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 5 , 10 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE print ( pairAndSum ( arr , n ) ) NEW_LINE
Primality Test | Set 1 ( Introduction and School Method ) | A school method based Python3 program to check if a number is prime ; Corner case ; Check from 2 to n - 1 ; Driver Program to test above function
def isPrime ( n ) : NEW_LINE INDENT if n <= 1 : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , n ) : NEW_LINE INDENT if n % i == 0 : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT print ( " true " ) if isPrime ( 11 ) else print ( " false " ) NEW_LINE print ( " true " ) if isPrime ( 14 ) else print ( " false " ) NEW_LINE
Euler 's Totient function for all numbers smaller than or equal to n | Computes and prints totient of all numbers smaller than or equal to n . ; Create and initialize an array to store phi or totient values ; Compute other Phi values ; If phi [ p ] is not computed already , then number p is prime ; Phi of a prime number p is always equal to p - 1. ; Update phi values of all multiples of p ; Add contribution of p to its multiple i by multiplying with ( 1 - 1 / p ) ; Print precomputed phi values ; Driver code
def computeTotient ( n ) : NEW_LINE INDENT phi = [ ] NEW_LINE for i in range ( n + 2 ) : NEW_LINE INDENT phi . append ( 0 ) NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE for p in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( phi [ p ] == p ) : NEW_LINE INDENT phi [ p ] = p - 1 NEW_LINE for i in range ( 2 * p , n + 1 , p ) : NEW_LINE INDENT phi [ i ] = ( phi [ i ] // p ) * ( p - 1 ) NEW_LINE DEDENT DEDENT DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( " Totient ▁ of ▁ " , i , " ▁ is ▁ " , phi [ i ] ) NEW_LINE DEDENT DEDENT n = 12 NEW_LINE computeTotient ( n ) NEW_LINE
Euler 's Totient function for all numbers smaller than or equal to n | python program for the above approach ; Driver code
import math NEW_LINE def Euler_totient_function ( n ) : NEW_LINE INDENT result = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT c = 0 NEW_LINE if n % i == 0 : NEW_LINE INDENT while ( n % i == 0 ) : NEW_LINE INDENT c += 1 NEW_LINE n //= i NEW_LINE DEDENT DEDENT if ( c > 0 ) : NEW_LINE INDENT power = math . pow ( i , c - 1 ) NEW_LINE m = math . pow ( i , c - 1 ) * ( i - 1 ) NEW_LINE result *= m NEW_LINE DEDENT DEDENT if ( n > 1 ) : NEW_LINE INDENT result *= ( n - 1 ) NEW_LINE DEDENT return int ( result ) NEW_LINE DEDENT for i in range ( 1 , 13 ) : NEW_LINE INDENT print ( " Euler _ totient _ function ( " , i , " ) : ▁ " , end = " " ) NEW_LINE print ( Euler_totient_function ( i ) ) NEW_LINE DEDENT
Check if a number can be expressed as x ^ y ( x raised to power y ) | Python3 program to check if a given number can be expressed as power ; Returns true if n can be written as x ^ y ; Driver code
import math NEW_LINE def isPower ( n ) : NEW_LINE INDENT p = 0 NEW_LINE if ( n <= 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT for i in range ( 2 , ( int ) ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT p = math . log2 ( n ) / math . log2 ( i ) NEW_LINE if ( ( math . ceil ( p ) == math . floor ( p ) ) and p > 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT return 0 NEW_LINE DEDENT for i in range ( 2 , 100 ) : NEW_LINE INDENT if isPower ( i ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Sum of all elements between k1 ' th ▁ and ▁ k2' th smallest elements | Returns sum between two kth smallest element of array ; Sort the given array ; Below code is equivalent to ; Driver code
def sumBetweenTwoKth ( arr , n , k1 , k2 ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE result = 0 NEW_LINE for i in range ( k1 , k2 - 1 ) : NEW_LINE INDENT result += arr [ i ] NEW_LINE DEDENT return result NEW_LINE DEDENT arr = [ 20 , 8 , 22 , 4 , 12 , 10 , 14 ] NEW_LINE k1 = 3 ; k2 = 6 NEW_LINE n = len ( arr ) NEW_LINE print ( sumBetweenTwoKth ( arr , n , k1 , k2 ) ) NEW_LINE
Puzzle | Program to find number of squares in a chessboard | Function to return count of squares ; ; better way to write n * ( n + 1 ) * ( 2 n + 1 ) / 6 ; Driver code
def countSquares ( n ) : NEW_LINE INDENT return ( ( n * ( n + 1 ) / 2 ) * ( 2 * n + 1 ) / 3 ) NEW_LINE DEDENT n = 4 NEW_LINE print ( " Count ▁ of ▁ squares ▁ is ▁ " , countSquares ( n ) ) NEW_LINE
Find nth Magic Number | Function to find nth magic number ; Go through every bit of n ; If last bit of n is set ; proceed to next bit n >>= 1 or n = n / 2 ; Driver program to test above function
def nthMagicNo ( n ) : NEW_LINE INDENT pow = 1 NEW_LINE answer = 0 NEW_LINE while ( n ) : NEW_LINE INDENT pow = pow * 5 NEW_LINE if ( n & 1 ) : NEW_LINE INDENT answer += pow NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT n = 5 NEW_LINE print ( " nth ▁ magic ▁ number ▁ is " , nthMagicNo ( n ) ) NEW_LINE
Given a number n , count all multiples of 3 and / or 5 in set { 1 , 2 , 3 , ... n } | python program to find count of multiples of 3 and 5 in { 1 , 2 , 3 , . . n } ; Add multiples of 3 and 5. Since common multiples are counted twice in n / 3 + n / 15 , subtract common multiples ; Driver program to test above function
def countOfMultiples ( n ) : NEW_LINE INDENT return ( int ( n / 3 ) + int ( n / 5 ) - int ( n / 15 ) ) ; NEW_LINE DEDENT print ( countOfMultiples ( 6 ) ) NEW_LINE print ( countOfMultiples ( 16 ) ) NEW_LINE
Program to find GCD or HCF of two numbers | Recursive function to return gcd of a and b ; Everything divides 0 ; base case ; a is greater ; Driver program to test above function
def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT if ( a == b ) : NEW_LINE INDENT return a NEW_LINE DEDENT if ( a > b ) : NEW_LINE INDENT return gcd ( a - b , b ) NEW_LINE DEDENT return gcd ( a , b - a ) NEW_LINE DEDENT a = 98 NEW_LINE b = 56 NEW_LINE if ( gcd ( a , b ) ) : NEW_LINE INDENT print ( ' GCD ▁ of ' , a , ' and ' , b , ' is ' , gcd ( a , b ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' not ▁ found ' ) NEW_LINE DEDENT
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 xor sums of first array and second array respectively . ; Xor sum of first array ; Xor sum of second array ; required answer ; Driver code ; Input ; Function call
def XorSum ( A , B , N , M ) : NEW_LINE INDENT ans1 = 0 NEW_LINE ans2 = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT ans1 = ans1 ^ A [ i ] NEW_LINE DEDENT for i in range ( M ) : NEW_LINE INDENT ans2 = ans2 ^ B [ i ] NEW_LINE DEDENT return ( ans1 & ans2 ) 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
Count levels in a Binary Tree consisting of node values having set bits at different positions | Structure of a node in the binary tree ; Function to find total unique levels ; Stores count of levels , where the set bits of all the nodes are at different positions ; Store nodes at each level of the tree using BFS ; Performing level order traversal ; Stores count of nodes at current level ; Stores prefix XOR of all the nodes at current level ; Stores prefix OR of all the nodes at current level ; Check if set bit of all the nodes at current level is at different positions or not ; Traverse nodes at current level ; Stores front element of the que ; Update prefix_OR ; Update prefix_XOR ; If left subtree not NULL ; If right subtree not NULL ; If bitwise AND is zero ; Driver Code ; Function Call
class TreeNode : NEW_LINE INDENT def __init__ ( self , val = 0 , 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 uniqueLevels ( root ) : NEW_LINE INDENT uniqueLevels = 0 NEW_LINE que = [ root ] NEW_LINE while len ( que ) : NEW_LINE INDENT length = len ( que ) NEW_LINE prefix_XOR = 0 ; NEW_LINE prefix_OR = 0 NEW_LINE flag = True NEW_LINE while length : NEW_LINE INDENT temp = que . pop ( 0 ) NEW_LINE prefix_OR |= temp . val NEW_LINE prefix_XOR ^= temp . val NEW_LINE if prefix_XOR != prefix_OR : NEW_LINE INDENT flag = False NEW_LINE DEDENT if temp . left : NEW_LINE INDENT que . append ( temp . left ) NEW_LINE DEDENT if temp . right : NEW_LINE INDENT que . append ( temp . right ) NEW_LINE DEDENT length -= 1 NEW_LINE DEDENT if flag : NEW_LINE INDENT uniqueLevels += 1 NEW_LINE DEDENT DEDENT print ( uniqueLevels ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = TreeNode ( 5 ) NEW_LINE root . left = TreeNode ( 6 ) NEW_LINE root . right = TreeNode ( 9 ) NEW_LINE root . left . left = TreeNode ( 1 ) NEW_LINE root . left . right = TreeNode ( 4 ) NEW_LINE root . right . right = TreeNode ( 7 ) NEW_LINE uniqueLevels ( root ) NEW_LINE DEDENT
Sum of all elements between k1 ' th ▁ and ▁ k2' th smallest elements | Python 3 implementation of above approach ; Driver Code ; decreasing value by 1 because we want min heapifying k times and it starts from 0 so we have to decrease it 1 time ; Step 1 : Do extract minimum k1 times ( This step takes O ( K1 Log n ) time ) ; Step 2 : Do extract minimum k2 k1 1 times and sum all extracted elements . ( This step takes O ( ( K2 k1 ) * Log n ) time )
n = 7 NEW_LINE def minheapify ( a , index ) : NEW_LINE INDENT small = index NEW_LINE l = 2 * index + 1 NEW_LINE r = 2 * index + 2 NEW_LINE if ( l < n and a [ l ] < a [ small ] ) : NEW_LINE INDENT small = l NEW_LINE DEDENT if ( r < n and a [ r ] < a [ small ] ) : NEW_LINE INDENT small = r NEW_LINE DEDENT if ( small != index ) : NEW_LINE INDENT ( a [ small ] , a [ index ] ) = ( a [ index ] , a [ small ] ) NEW_LINE minheapify ( a , small ) NEW_LINE DEDENT DEDENT i = 0 NEW_LINE k1 = 3 NEW_LINE k2 = 6 NEW_LINE a = [ 20 , 8 , 22 , 4 , 12 , 10 , 14 ] NEW_LINE ans = 0 NEW_LINE for i in range ( ( n // 2 ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT minheapify ( a , i ) NEW_LINE DEDENT k1 -= 1 NEW_LINE k2 -= 1 NEW_LINE for i in range ( 0 , k1 + 1 ) : NEW_LINE INDENT a [ 0 ] = a [ n - 1 ] NEW_LINE n -= 1 NEW_LINE minheapify ( a , 0 ) NEW_LINE DEDENT for i in range ( k1 + 1 , k2 ) : NEW_LINE INDENT ans += a [ 0 ] NEW_LINE a [ 0 ] = a [ n - 1 ] NEW_LINE n -= 1 NEW_LINE minheapify ( a , 0 ) NEW_LINE DEDENT print ( ans ) NEW_LINE
Construct a List using the given Q XOR queries | Function to return required list after performing all the queries ; Store cumulative Bitwise XOR ; Initialize final list to return ; Perform each query ; The initial value of 0 ; Sort the list ; Return final list ; Given Queries ; Function call
def ConstructList ( Q ) : NEW_LINE INDENT xor = 0 NEW_LINE ans = [ ] NEW_LINE for i in range ( len ( Q ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( Q [ i ] [ 0 ] == 0 ) : NEW_LINE INDENT ans . append ( Q [ i ] [ 1 ] ^ xor ) NEW_LINE DEDENT else : NEW_LINE INDENT xor ^= Q [ i ] [ 1 ] NEW_LINE DEDENT DEDENT ans . append ( xor ) NEW_LINE ans . sort ( ) NEW_LINE return ans NEW_LINE DEDENT Q = [ [ 0 , 6 ] , [ 0 , 3 ] , [ 0 , 2 ] , [ 1 , 4 ] , [ 1 , 5 ] ] NEW_LINE print ( ConstructList ( Q ) ) NEW_LINE
Bitwise operations on Subarrays of size K | Function to convert bit array to decimal number ; Function to find maximum values of each bitwise OR operation on element of subarray of size K ; Maintain an integer array bit of size 32 all initialized to 0 ; Create a sliding window of size k ; Function call ; Perform operation for removed element ; Perform operation for added_element ; Taking maximum value ; Return the result ; Driver Code ; Given array arr ; Given subarray size K ; Function call
def build_num ( bit ) : NEW_LINE INDENT ans = 0 ; NEW_LINE for i in range ( 32 ) : NEW_LINE INDENT if ( bit [ i ] > 0 ) : NEW_LINE INDENT ans += ( 1 << i ) ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT def maximumOR ( arr , n , k ) : NEW_LINE INDENT bit = [ 0 ] * 32 ; NEW_LINE for i in range ( k ) : NEW_LINE INDENT for j in range ( 32 ) : NEW_LINE INDENT if ( ( arr [ i ] & ( 1 << j ) ) > 0 ) : NEW_LINE INDENT bit [ j ] += 1 ; NEW_LINE DEDENT DEDENT DEDENT max_or = build_num ( bit ) ; NEW_LINE for i in range ( k , n ) : NEW_LINE INDENT for j in range ( 32 ) : NEW_LINE INDENT if ( ( arr [ i - k ] & ( 1 << j ) ) > 0 ) : NEW_LINE INDENT bit [ j ] -= 1 ; NEW_LINE DEDENT DEDENT for j in range ( 32 ) : NEW_LINE INDENT if ( ( arr [ i ] & ( 1 << j ) ) > 0 ) : NEW_LINE INDENT bit [ j ] += 1 ; NEW_LINE DEDENT DEDENT max_or = max ( build_num ( bit ) , max_or ) ; NEW_LINE DEDENT return max_or ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 5 , 3 , 6 , 11 , 13 ] ; NEW_LINE k = 3 ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( maximumOR ( arr , n , k ) ) ; NEW_LINE DEDENT
Maximum XOR path of a Binary Tree | Binary tree node ; Function to create a new node ; Function calculate the value of max - xor ; Updating the xor value with the xor of the path from root to the node ; Check if node is leaf node ; Check if the left node exist in the tree ; Check if the right node exist in the tree ; Function to find the required count ; Recursively traverse the tree and compute the max_xor ; Return the result ; Create the binary tree
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def Solve ( root , xr , max_xor ) : NEW_LINE INDENT xr = xr ^ root . data NEW_LINE if ( root . left == None and root . right == None ) : NEW_LINE INDENT max_xor [ 0 ] = max ( max_xor [ 0 ] , xr ) NEW_LINE DEDENT if root . left != None : NEW_LINE INDENT Solve ( root . left , xr , max_xor ) NEW_LINE DEDENT if root . right != None : NEW_LINE INDENT Solve ( root . right , xr , max_xor ) NEW_LINE DEDENT return NEW_LINE DEDENT def findMaxXor ( root ) : NEW_LINE INDENT xr , max_xor = 0 , [ 0 ] NEW_LINE Solve ( root , xr , max_xor ) NEW_LINE return max_xor [ 0 ] NEW_LINE DEDENT root = Node ( 2 ) NEW_LINE root . left = Node ( 1 ) NEW_LINE root . right = Node ( 4 ) NEW_LINE root . left . left = Node ( 10 ) NEW_LINE root . left . right = Node ( 8 ) NEW_LINE root . right . left = Node ( 5 ) NEW_LINE root . right . right = Node ( 10 ) NEW_LINE print ( findMaxXor ( root ) ) NEW_LINE
XOR of every element of an Array with a given number K | Function to construct new array ; Traverse the array and compute XOR with K ; Print new array ; Driver code
def constructXORArray ( A , n , K ) : NEW_LINE INDENT B = [ 0 ] * n ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT B [ i ] = A [ i ] ^ K ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( B [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT print ( ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 2 , 4 , 1 , 3 , 5 ] ; NEW_LINE K = 5 ; NEW_LINE n = len ( A ) ; NEW_LINE constructXORArray ( A , n , K ) ; NEW_LINE B = [ 4 , 75 , 45 , 42 ] ; NEW_LINE K = 2 ; NEW_LINE n = len ( B ) ; NEW_LINE constructXORArray ( B , n , K ) ; NEW_LINE DEDENT
Bitwise AND of all the elements of array | Function to calculate bitwise AND ; Initialise ans variable is arr [ 0 ] ; Traverse the array compute AND ; Return ans ; Driver Code ; Function Call to find AND
def find_and ( arr ) : NEW_LINE INDENT ans = arr [ 0 ] NEW_LINE for i in range ( 1 , len ( arr ) ) : NEW_LINE INDENT ans = ans & arr [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 5 , 9 , 11 ] NEW_LINE print ( find_and ( arr ) ) NEW_LINE DEDENT