codeparrot/xlcost-text-to-code huggingface dataset:
Count of subsets whose product is multiple of unique primes | Python program for the above approach ; Function to check number has distinct prime ; While N has factors of two ; Traversing till sqrt ( N ) ; If N has a factor of i ; While N has a factor of i ; Covering case , N is Prime ; Function to check wheather num can be added to the subset ; Recursive Function to count subset ; Base Case ; If currSubset is empty ; If Unique [ pos ] can be added to the Subset ; Function to count the subsets ; Initialize unique ; Check it is a product of distinct primes ; Count frequency of unique element ; Function Call ; Driver Code ; Given Input ; Function Call
from math import gcd , sqrt NEW_LINE def checkDistinctPrime ( n ) : NEW_LINE INDENT original = n NEW_LINE product = 1 NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT product *= 2 NEW_LINE while ( n % 2 == 0 ) : NEW_LINE INDENT n = n // 2 NEW_LINE DEDENT DEDENT for i in range ( 3 , int ( sqrt ( n ) ) , 2 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT product = product * i NEW_LINE while ( n % i == 0 ) : NEW_LINE INDENT n = n // i NEW_LINE DEDENT DEDENT DEDENT if ( n > 2 ) : NEW_LINE INDENT product = product * n NEW_LINE DEDENT return product == original NEW_LINE DEDENT def check ( pos , subset , unique ) : NEW_LINE INDENT for num in subset : NEW_LINE INDENT if gcd ( num , unique [ pos ] ) != 1 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def countPrime ( pos , currSubset , unique , frequency ) : NEW_LINE INDENT if pos == len ( unique ) : NEW_LINE INDENT if not currSubset : NEW_LINE INDENT return 0 NEW_LINE DEDENT count = 1 NEW_LINE for element in currSubset : NEW_LINE INDENT count *= frequency [ element ] NEW_LINE DEDENT return count NEW_LINE DEDENT if check ( pos , currSubset , unique ) : NEW_LINE INDENT return countPrime ( pos + 1 , currSubset ,   \ unique , frequency ) + countPrime ( pos + 1 , currSubset + [ unique [ pos ] ] ,   \ unique , frequency ) NEW_LINE DEDENT else : NEW_LINE INDENT return countPrime ( pos + 1 , currSubset ,   \ unique , frequency ) NEW_LINE DEDENT DEDENT def countSubsets ( arr , N ) : NEW_LINE INDENT unique = set ( ) NEW_LINE for element in arr : NEW_LINE INDENT if checkDistinctPrime ( element ) : NEW_LINE INDENT unique . add ( element ) NEW_LINE DEDENT DEDENT unique = list ( unique ) NEW_LINE frequency = dict ( ) NEW_LINE for element in unique : NEW_LINE INDENT frequency [ element ] = arr . count ( element ) NEW_LINE DEDENT ans = countPrime ( 0 , [ ] , unique , frequency ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 4 , 7 , 10 ] NEW_LINE N = len ( arr ) NEW_LINE ans = countSubsets ( arr , N ) NEW_LINE print ( ans ) NEW_LINE DEDENT
Lexicographically largest possible by merging two strings by adding one character at a time | Recursive function for finding the lexicographically largest string ; If either of the string length is 0 , return the other string ; If s1 is lexicographically larger than s2 ; Take first character of s1 and call the function ; Take first character of s2 and recursively call function for remaining string ; Driver code ; Given Input ; Function call
def largestMerge ( s1 , s2 ) : NEW_LINE INDENT if len ( s1 ) == 0 or len ( s2 ) == 0 : NEW_LINE INDENT return s1 + s2 NEW_LINE DEDENT if ( s1 > s2 ) : NEW_LINE INDENT return s1 [ 0 ] + largestMerge ( s1 [ 1 : ] , s2 ) NEW_LINE DEDENT return s2 [ 0 ] + largestMerge ( s1 , s2 [ 1 : ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s1 = " geeks " NEW_LINE s2 = " forgeeks " NEW_LINE print ( largestMerge ( s1 , s2 ) ) NEW_LINE DEDENT
Minimum distance between duplicates in a String | This function is used to find the required the minimum distances of repeating characters ; Define dictionary and list ; Traverse through string ; If character present in dictionary ; Difference between current position and last stored value ; Updating current position ; Storing difference in list ; If character not in dictionary assign it with initial of its position ; If no element inserted in list i . e . no repeating characters ; Return minimum distance ; Given Input ; Function Call
def shortestDistance ( S , N ) : NEW_LINE INDENT dic = { } NEW_LINE dis = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT if S [ i ] in dic : NEW_LINE INDENT var = i - dic [ S [ i ] ] NEW_LINE dic [ S [ i ] ] = i NEW_LINE dis . append ( var ) NEW_LINE DEDENT else : NEW_LINE INDENT dic [ S [ i ] ] = i NEW_LINE DEDENT DEDENT if ( len ( dis ) == 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return min ( dis ) - 1 NEW_LINE DEDENT DEDENT S = " geeksforgeeks " NEW_LINE N = 13 NEW_LINE print ( shortestDistance ( S , N ) ) NEW_LINE
Number of open doors | TCS Coding Question | Python3 code for the above approach ; Function that counts the number of doors opens after the Nth turn ; Find the number of open doors ; Return the resultant count of open doors ; Driver Code
import math NEW_LINE def countOpenDoors ( N ) : NEW_LINE INDENT doorsOpen = int ( math . sqrt ( N ) ) NEW_LINE return doorsOpen NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 100 NEW_LINE print ( countOpenDoors ( N ) ) NEW_LINE DEDENT
Minimum number of candies required to distribute among children based on given conditions | Function to count the minimum number of candies that is to be distributed ; Stores total count of candies ; Stores the amount of candies allocated to a student ; If the value of N is 1 ; Traverse the array arr [ ] ; If arr [ i + 1 ] is greater than arr [ i ] and ans [ i + 1 ] is at most ans [ i ] ; Assign ans [ i ] + 1 to ans [ i + 1 ] ; Iterate until i is atleast 0 ; If arr [ i ] is greater than arr [ i + 1 ] and ans [ i ] is at most ans [ i + 1 ] ; Assign ans [ i + 1 ] + 1 to ans [ i ] ; If arr [ i ] is equals arr [ i + 1 ] and ans [ i ] is less than ans [ i + 1 ] ; Assign ans [ i + 1 ] + 1 to ans [ i ] ; Increment the sum by ans [ i ] ; Return the resultant sum ; Driver Code
def countCandies ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE ans = [ 1 ] * n NEW_LINE if ( n == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT if ( arr [ i + 1 ] > arr [ i ] and ans [ i + 1 ] <= ans [ i ] ) : NEW_LINE INDENT ans [ i + 1 ] = ans [ i ] + 1 NEW_LINE DEDENT DEDENT for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ i ] > arr [ i + 1 ] and ans [ i ] <= ans [ i + 1 ] ) : NEW_LINE INDENT ans [ i ] = ans [ i + 1 ] + 1 NEW_LINE DEDENT elif ( arr [ i ] == arr [ i + 1 ] and ans [ i ] < ans [ i + 1 ] ) : NEW_LINE INDENT ans [ i ] = ans [ i + 1 ] + 1 NEW_LINE DEDENT sum += ans [ i ] NEW_LINE DEDENT sum += ans [ n - 1 ] NEW_LINE return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 0 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countCandies ( arr , N ) ) NEW_LINE DEDENT
Maximize profit possible by selling M products such that profit of a product is the number of products left of that supplier | Function to find the maximum profit by selling M number of products ; Initialize a Max - Heap to keep track of the maximum value ; Stores the maximum profit ; Traverse the array and push all the elements in max_heap ; Iterate a loop until M > 0 ; Decrement the value of M by 1 ; Pop the maximum element from the heap ; Update the maxProfit ; Push ( X - 1 ) to max heap ; Print the result ; Driver code
def findMaximumProfit ( arr , M , N ) : NEW_LINE INDENT max_heap = [ ] NEW_LINE maxProfit = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT max_heap . append ( arr [ i ] ) NEW_LINE DEDENT max_heap . sort ( ) NEW_LINE max_heap . reverse ( ) NEW_LINE while ( M > 0 ) : NEW_LINE INDENT M -= 1 NEW_LINE X = max_heap [ 0 ] NEW_LINE max_heap . pop ( 0 ) NEW_LINE maxProfit += X NEW_LINE max_heap . append ( X - 1 ) NEW_LINE max_heap . sort ( ) NEW_LINE max_heap . reverse ( ) NEW_LINE DEDENT print ( maxProfit ) NEW_LINE DEDENT arr = [ 4 , 6 ] NEW_LINE M = 4 NEW_LINE N = len ( arr ) NEW_LINE findMaximumProfit ( arr , M , N ) NEW_LINE
Generate an array with K positive numbers such that arr [ i ] is either | Python program for the above approach Function to generate the resultant sequence of first N natural numbers ; Initialize an array of size N ; Stores the sum of positive and negative elements ; Mark the first N - K elements as negative ; Update the value of arr [ i ] ; Update the value of sumNeg ; Mark the remaining K elements as positive ; Update the value of arr [ i ] ; Update the value of sumPos ; If the sum of the sequence is negative , then pr - 1 ; Pr the required sequence ; Driver Code
def findSequence ( n , k ) : NEW_LINE INDENT arr = [ 0 ] * n NEW_LINE sumPos = 0 NEW_LINE sumNeg = 0 NEW_LINE for i in range ( 0 , n - k ) : NEW_LINE INDENT arr [ i ] = - ( i + 1 ) NEW_LINE sumNeg += arr [ i ] NEW_LINE DEDENT for i in range ( n - k , n ) : NEW_LINE INDENT arr [ i ] = i + 1 NEW_LINE sumPos += arr [ i ] NEW_LINE DEDENT if ( abs ( sumNeg ) >= sumPos ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT N = 10 NEW_LINE K = 6 NEW_LINE findSequence ( N , K ) NEW_LINE
Print the indices for every row of a grid from which escaping from the grid is possible | Function to find the row index for every row of a matrix from which one can exit the grid after entering from left ; Iterate over the range [ 0 , M - 1 ] ; Stores the direction from which a person enters a cell ; Row index from which one enters the grid ; Column index from which one enters the grid ; Iterate until j is atleast N - 1 ; If Mat [ i ] [ j ] is equal to 1 ; If entry is from left cell ; Decrement i by 1 ; Assign ' D ' to dir ; If entry is from upper cell ; Decrement j by 1 ; Assign ' R ' to dir ; If entry is from right cell ; Increment i by 1 ; Assign ' U ' to dir ; If entry is from bottom cell ; Increment j by 1 ; Assign ' L ' to dir ; Otherwise , ; If entry is from left cell ; Increment i by 1 ; Assign ' U ' to dir ; If entry is from upper cell ; Increment j by 1 ; Assign ' L ' to dir ; If entry is from right cell ; Decrement i by 1 ; Assign ' D ' to dir ; If entry is from lower cell ; Decrement j by 1 ; Assign ' R ' to dir ; If i or j is less than 0 or i is equal to M or j is equal to N ; If j is equal to N ; Otherwise ; Input ; Function call
def findPath ( arr , M , N ) : NEW_LINE INDENT for row in range ( M ) : NEW_LINE INDENT dir = ' L ' NEW_LINE i = row NEW_LINE j = 0 NEW_LINE while ( j < N ) : NEW_LINE INDENT if ( arr [ i ] [ j ] == 1 ) : NEW_LINE INDENT if ( dir == ' L ' ) : NEW_LINE INDENT i -= 1 NEW_LINE dir = ' D ' NEW_LINE DEDENT elif ( dir == ' U ' ) : NEW_LINE INDENT j -= 1 NEW_LINE dir = ' R ' NEW_LINE DEDENT elif ( dir == ' R ' ) : NEW_LINE INDENT i += 1 NEW_LINE dir = ' U ' NEW_LINE DEDENT elif ( dir == ' D ' ) : NEW_LINE INDENT j += 1 NEW_LINE dir = ' L ' NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( dir == ' L ' ) : NEW_LINE INDENT i += 1 NEW_LINE dir = ' U ' NEW_LINE DEDENT elif ( dir == ' U ' ) : NEW_LINE INDENT j += 1 NEW_LINE dir = ' L ' NEW_LINE DEDENT elif ( dir == ' R ' ) : NEW_LINE INDENT i -= 1 NEW_LINE dir = ' D ' NEW_LINE DEDENT elif ( dir == ' D ' ) : NEW_LINE INDENT j -= 1 NEW_LINE dir = ' R ' NEW_LINE DEDENT DEDENT if ( i < 0 or i == M or j < 0 or j == N ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( j == N ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT arr = [ [ 1 , 1 , 0 , 1 ] , [ 1 , 1 , 0 , 0 ] , [ 1 , 0 , 0 , 0 ] , [ 1 , 1 , 0 , 1 ] , [ 0 , 1 , 0 , 1 ] ] NEW_LINE M = len ( arr ) NEW_LINE N = len ( arr [ 0 ] ) NEW_LINE findPath ( arr , M , N ) NEW_LINE
Maximize sum of array by repeatedly removing an element from pairs whose concatenation is a multiple of 3 | Function to calculate sum of digits of an integer ; Function to calculate maximum sum of array after removing pairs whose concatenation is divisible by 3 ; Stores the sum of digits of array element ; Find the sum of digits ; If i is divisible by 3 ; Otherwise , if i modulo 3 is 1 ; Otherwise , if i modulo 3 is 2 ; Return the resultant sum of array elements ; Driver Code
def getSum ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in str ( n ) : NEW_LINE INDENT ans += int ( i ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def getMax ( arr ) : NEW_LINE INDENT maxRem0 = 0 NEW_LINE rem1 = 0 NEW_LINE rem2 = 0 NEW_LINE for i in arr : NEW_LINE INDENT digitSum = getSum ( i ) NEW_LINE if digitSum % 3 == 0 : NEW_LINE INDENT maxRem0 = max ( maxRem0 , i ) NEW_LINE DEDENT elif digitSum % 3 == 1 : NEW_LINE INDENT rem1 += i NEW_LINE DEDENT else : NEW_LINE INDENT rem2 += i NEW_LINE DEDENT DEDENT print ( maxRem0 + max ( rem1 , rem2 ) ) NEW_LINE DEDENT arr = [ 23 , 12 , 43 , 3 , 56 ] NEW_LINE getMax ( arr ) NEW_LINE
Minimum edge reversals to make a root | Python3 program to find min edge reversal to make every node reachable from root ; Method to dfs in tree and populates disRev values ; Visit current node ; Looping over all neighbors ; Distance of v will be one more than distance of u ; Initialize back edge count same as parent node 's count ; If there is a reverse edge from u to i , then only update ; Return total reversal in subtree rooted at u ; Method prints root and minimum number of edge reversal ; Number of nodes are one more than number of edges ; Data structure to store directed tree ; disRev stores two values - distance and back edge count from root node ; Add 0 weight in direction of u to v ; Add 1 weight in reverse direction ; Initialize all variables ; dfs populates disRev data structure and store total reverse edge counts ; for ( i = 0 i < V i ++ ) { cout < < i < < " ▁ : ▁ " << disRev [ i ] [ 0 ] << " ▁ " << disRev [ i ] [ 1 ] << endl } ; Loop over all nodes to choose minimum edge reversal ; ( reversal in path to i ) + ( reversal in all other tree parts ) ; Choose minimum among all values ; Print the designated root and total edge reversal made ; Driver code
import sys NEW_LINE def dfs ( g , disRev , visit , u ) : NEW_LINE INDENT visit [ u ] = True NEW_LINE totalRev = 0 NEW_LINE for i in range ( len ( g [ u ] ) ) : NEW_LINE INDENT v = g [ u ] [ i ] [ 0 ] NEW_LINE if ( not visit [ v ] ) : NEW_LINE INDENT disRev [ v ] [ 0 ] = disRev [ u ] [ 0 ] + 1 NEW_LINE disRev [ v ] [ 1 ] = disRev [ u ] [ 1 ] NEW_LINE if ( g [ u ] [ i ] [ 1 ] ) : NEW_LINE INDENT disRev [ v ] [ 1 ] = disRev [ u ] [ 1 ] + 1 NEW_LINE totalRev += 1 NEW_LINE DEDENT totalRev += dfs ( g , disRev , visit , v ) NEW_LINE DEDENT DEDENT return totalRev NEW_LINE DEDENT def printMinEdgeReverseForRootNode ( edges , e ) : NEW_LINE INDENT V = e + 1 NEW_LINE g = [ [ ] for i in range ( V ) ] NEW_LINE disRev = [ [ 0 , 0 ] for i in range ( V ) ] NEW_LINE visit = [ False for i in range ( V ) ] NEW_LINE for i in range ( e ) : NEW_LINE INDENT u = edges [ i ] [ 0 ] NEW_LINE v = edges [ i ] [ 1 ] NEW_LINE g [ u ] . append ( [ v , 0 ] ) NEW_LINE g [ v ] . append ( [ u , 1 ] ) NEW_LINE DEDENT for i in range ( V ) : NEW_LINE INDENT visit [ i ] = False NEW_LINE disRev [ i ] [ 0 ] = disRev [ i ] [ 1 ] = 0 NEW_LINE DEDENT root = 0 NEW_LINE totalRev = dfs ( g , disRev , visit , root ) NEW_LINE res = sys . maxsize NEW_LINE for i in range ( V ) : NEW_LINE INDENT edgesToRev = ( ( totalRev - disRev [ i ] [ 1 ] ) + ( disRev [ i ] [ 0 ] - disRev [ i ] [ 1 ] ) ) NEW_LINE if ( edgesToRev < res ) : NEW_LINE INDENT res = edgesToRev NEW_LINE root = i NEW_LINE DEDENT DEDENT print ( root , res ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT edges = [ [ 0 , 1 ] , [ 2 , 1 ] , [ 3 , 2 ] , [ 3 , 4 ] , [ 5 , 4 ] , [ 5 , 6 ] , [ 7 , 6 ] ] NEW_LINE e = len ( edges ) NEW_LINE printMinEdgeReverseForRootNode ( edges , e ) NEW_LINE DEDENT
Check if 2 * K + 1 non | Function to check if the S can be obtained by ( K + 1 ) non - empty substrings whose concatenation and concatenation of the reverse of these K strings ; Stores the size of the string ; If n is less than 2 * k + 1 ; Stores the first K characters ; Stores the last K characters ; Reverse the string ; If both the strings are equal ; Driver Code
def checkString ( s , k ) : NEW_LINE INDENT n = len ( s ) NEW_LINE if ( 2 * k + 1 > n ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT a = s [ 0 : k ] NEW_LINE b = s [ n - k : n ] NEW_LINE b = b [ : : - 1 ] NEW_LINE if ( a == b ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " qwqwq " NEW_LINE K = 1 NEW_LINE checkString ( S , K ) NEW_LINE DEDENT
Minimum number of socks required to picked to have at least K pairs of the same color | Function to count the minimum number of socks to be picked ; Stores the total count of pairs of socks ; Find the total count of pairs ; If K is greater than pairs ; Otherwise ; Driver Code
def findMin ( arr , N , k ) : NEW_LINE INDENT pairs = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT pairs += arr [ i ] / 2 NEW_LINE DEDENT if ( k > pairs ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 2 * k + N - 1 NEW_LINE DEDENT DEDENT arr = [ 4 , 5 , 6 ] NEW_LINE k = 3 NEW_LINE print ( findMin ( arr , 3 , k ) ) ; NEW_LINE
Minimum number of bits required to be flipped such that Bitwise OR of A and B is equal to C | Function to count the number of bit flips required on A and B such that Bitwise OR of A and B is C ; Stores the count of flipped bit ; Iterate over the range [ 0 , 32 ] ; Check if i - th bit of A is set ; Check if i - th bit of B is set or not ; Check if i - th bit of C is set or not ; If i - th bit of C is unset ; Check if i - th bit of A is set or not ; Check if i - th bit of B is set or not ; Check if i - th bit of C is set or not ; Check if i - th bit of both A and B is set ; Return the count of bits flipped ; Driver Code
def minimumFlips ( A , B , C ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( 32 ) : NEW_LINE INDENT x , y , z = 0 , 0 , 0 NEW_LINE if ( A & ( 1 << i ) ) : NEW_LINE INDENT x = 1 NEW_LINE DEDENT if ( B & ( 1 << i ) ) : NEW_LINE INDENT y = 1 NEW_LINE DEDENT if ( C & ( 1 << i ) ) : NEW_LINE INDENT z = 1 NEW_LINE DEDENT if ( z == 0 ) : NEW_LINE INDENT if ( x ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT if ( y ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT if ( z == 1 ) : NEW_LINE INDENT if ( x == 0 and y == 0 ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A , B , C = 2 , 6 , 5 NEW_LINE print ( minimumFlips ( A , B , C ) ) NEW_LINE DEDENT
Queries to calculate Bitwise AND of an array with updates | Store the number of set bits at each position ; Function to precompute the prefix count array ; Iterate over the range [ 0 , 31 ] ; Set the bit at position i if arr [ 0 ] is set at position i ; Traverse the array and take prefix sum ; Update prefixCount [ i ] [ j ] ; Function to find the Bitwise AND of all array elements ; Stores the required result ; Iterate over the range [ 0 , 31 ] ; Stores the number of set bits at position i ; If temp is N , then set ith position in the result ; Print the result ; Function to update the prefix count array in each query ; Iterate through all the bits of the current number ; Store the bit at position i in the current value and the new value ; If bit2 is set and bit1 is unset , then increase the set bits at position i by 1 ; If bit1 is set and bit2 is unset , then decrease the set bits at position i by 1 ; Function to find the bitwise AND of the array after each query ; Fill the prefix count array ; Traverse the queries ; Store the index and the new value ; Store the current element at the index ; Update the array element ; Apply the changes to the prefix count array ; Print the bitwise AND of the modified array ; Driver Code
prefixCount = [ [ 0 for x in range ( 32 ) ] for y in range ( 10000 ) ] NEW_LINE def findPrefixCount ( arr , size ) : NEW_LINE INDENT for i in range ( 32 ) : NEW_LINE INDENT prefixCount [ i ] [ 0 ] = ( ( arr [ 0 ] >> i ) & 1 ) NEW_LINE for j in range ( 1 , size ) : NEW_LINE INDENT prefixCount [ i ] [ j ] = ( ( arr [ j ] >> i ) & 1 ) NEW_LINE prefixCount [ i ] [ j ] += prefixCount [ i ] [ j - 1 ] NEW_LINE DEDENT DEDENT DEDENT def arrayBitwiseAND ( size ) : NEW_LINE INDENT result = 0 NEW_LINE for i in range ( 32 ) : NEW_LINE INDENT temp = prefixCount [ i ] [ size - 1 ] NEW_LINE if ( temp == size ) : NEW_LINE INDENT result = ( result | ( 1 << i ) ) NEW_LINE DEDENT DEDENT print ( result , end = " ▁ " ) NEW_LINE DEDENT def applyQuery ( currentVal , newVal , size ) : NEW_LINE INDENT for i in range ( 32 ) : NEW_LINE INDENT bit1 = ( ( currentVal >> i ) & 1 ) NEW_LINE bit2 = ( ( newVal >> i ) & 1 ) NEW_LINE if ( bit2 > 0 and bit1 == 0 ) : NEW_LINE INDENT prefixCount [ i ] [ size - 1 ] += 1 NEW_LINE DEDENT elif ( bit1 > 0 and bit2 == 0 ) : NEW_LINE INDENT prefixCount [ i ] [ size - 1 ] -= 1 NEW_LINE DEDENT DEDENT DEDENT def findbitwiseAND ( queries , arr , N , M ) : NEW_LINE INDENT findPrefixCount ( arr , N ) NEW_LINE for i in range ( M ) : NEW_LINE INDENT id = queries [ i ] [ 0 ] NEW_LINE newVal = queries [ i ] [ 1 ] NEW_LINE currentVal = arr [ id ] NEW_LINE arr [ id ] = newVal NEW_LINE applyQuery ( currentVal , newVal , N ) NEW_LINE arrayBitwiseAND ( N ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE queries = [ [ 0 , 2 ] , [ 3 , 3 ] , [ 4 , 2 ] ] NEW_LINE N = len ( arr ) NEW_LINE M = len ( queries ) NEW_LINE findbitwiseAND ( queries , arr , N , M ) NEW_LINE DEDENT
Find the winner of a game of donating i candies in every i | Function to find the winning player in a game of donating i candies to opponent in i - th move ; Steps in which number of candies of player A finishes ; Steps in which number of candies of player B finishes ; If A 's candies finishes first ; Otherwise ; Candies possessed by player A ; Candies possessed by player B
def stepscount ( a , b ) : NEW_LINE INDENT chance_A = 2 * a - 1 NEW_LINE chance_B = 2 * b NEW_LINE if ( chance_A < chance_B ) : NEW_LINE INDENT return ' B ' NEW_LINE DEDENT else : NEW_LINE INDENT return " A " NEW_LINE DEDENT DEDENT A = 2 NEW_LINE B = 3 NEW_LINE print ( stepscount ( A , B ) ) NEW_LINE
Check if a point is inside , outside or on a Hyperbola | Python3 program for the above approach ; Function to check if the point ( x , y ) lies inside , on or outside the given hyperbola ; Stores the value of the equation ; Generate output based on value of p ; Driver Code
from math import pow NEW_LINE def checkpoint ( h , k , x , y , a , b ) : NEW_LINE INDENT p = ( ( pow ( ( x - h ) , 2 ) // pow ( a , 2 ) ) - ( pow ( ( y - k ) , 2 ) // pow ( b , 2 ) ) ) NEW_LINE if ( p > 1 ) : NEW_LINE INDENT print ( " Outside " ) NEW_LINE DEDENT elif ( p == 1 ) : NEW_LINE INDENT print ( " On ▁ the ▁ Hyperbola " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Inside " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT h = 0 NEW_LINE k = 0 NEW_LINE x = 2 NEW_LINE y = 1 NEW_LINE a = 4 NEW_LINE b = 5 NEW_LINE checkpoint ( h , k , x , y , a , b ) NEW_LINE DEDENT
Count subarrays having even Bitwise XOR | Function to count the number of subarrays having even Bitwise XOR ; Store the required result ; Generate subarrays with arr [ i ] as the first element ; Store XOR of current subarray ; Generate subarrays with arr [ j ] as the last element ; Calculate Bitwise XOR of the current subarray ; If XOR is even , increase ans by 1 ; Prthe result ; Driver Code ; Given array ; Stores the size of the array ; Function Call
def evenXorSubarray ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT XOR = 0 NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT XOR = XOR ^ arr [ j ] NEW_LINE if ( ( XOR & 1 ) == 0 ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE evenXorSubarray ( arr , N ) NEW_LINE DEDENT