codeparrot/xlcost-text-to-code huggingface dataset:
Convert a number from base A to base B | Function to return ASCII value of a character ; Function to convert a number from given base to decimal number ; Stores the length of the string ; Initialize power of base ; Initialize result ; Decimal equivalent is strr [ len - 1 ] * 1 + strr [ len - 2 ] * base + strr [ len - 3 ] * ( base ^ 2 ) + ... ; A digit in input number must be less than number 's base ; Update num ; Update power ; Function to return equivalent character of a given value ; Function to convert a given decimal number to a given base ; Store the result ; Repeatedly divide inputNum by base and take remainder ; Update res ; Update inputNum ; Reverse the result ; Function to convert a given number from a base to another base ; Convert the number from base A to decimal ; Convert the number from decimal to base B ; Prthe result ; Given input ; Function Call
def val ( c ) : NEW_LINE INDENT if ( c >= '0' and c <= '9' ) : NEW_LINE INDENT return ord ( c ) - 48 NEW_LINE DEDENT else : NEW_LINE INDENT return ord ( c ) - 65 + 10 NEW_LINE DEDENT DEDENT def toDeci ( strr , base ) : NEW_LINE INDENT lenn = len ( strr ) NEW_LINE power = 1 NEW_LINE num = 0 NEW_LINE for i in range ( lenn - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( val ( strr [ i ] ) >= base ) : NEW_LINE INDENT print ( " Invalid ▁ Number " ) NEW_LINE return - 1 NEW_LINE DEDENT num += val ( strr [ i ] ) * power NEW_LINE power = power * base NEW_LINE DEDENT return num NEW_LINE DEDENT def reVal ( num ) : NEW_LINE INDENT if ( num >= 0 and num <= 9 ) : NEW_LINE INDENT return chr ( num + 48 ) NEW_LINE DEDENT else : NEW_LINE INDENT return chr ( num - 10 + 65 ) NEW_LINE DEDENT DEDENT def fromDeci ( base , inputNum ) : NEW_LINE INDENT res = " " NEW_LINE while ( inputNum > 0 ) : NEW_LINE INDENT res += reVal ( inputNum % base ) NEW_LINE inputNum //= base NEW_LINE DEDENT res = res [ : : - 1 ] NEW_LINE return res NEW_LINE DEDENT def convertBase ( s , a , b ) : NEW_LINE INDENT num = toDeci ( s , a ) NEW_LINE ans = fromDeci ( b , num ) NEW_LINE print ( ans ) NEW_LINE DEDENT s = "10B " NEW_LINE a = 16 NEW_LINE b = 10 NEW_LINE convertBase ( s , a , b ) NEW_LINE
Prefix Factorials of a Prefix Sum Array | Function to find the factorial of a number N ; Base Case ; Find the factorial recursively ; Function to find the prefix factorial array ; Find the prefix sum array ; Find the factorials of each array element ; Print the resultant array ; Driver Code
def fact ( N ) : NEW_LINE INDENT if ( N == 1 or N == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return N * fact ( N - 1 ) NEW_LINE DEDENT def prefixFactorialArray ( arr , N ) : NEW_LINE INDENT for i in range ( 1 , N ) : NEW_LINE INDENT arr [ i ] += arr [ i - 1 ] NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT arr [ i ] = fact ( arr [ i ] ) NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE prefixFactorialArray ( arr , N ) NEW_LINE DEDENT
Mean of fourth powers of first N natural numbers | Function to find the average of the fourth power of first N natural numbers ; Store the resultant average calculated using formula ; Return the average ; Driver Code
def findAverage ( N ) : NEW_LINE INDENT avg = ( ( 6 * N * N * N * N ) + ( 15 * N * N * N ) + ( 10 * N * N ) - 1 ) / 30 NEW_LINE return avg NEW_LINE DEDENT N = 3 NEW_LINE print ( round ( findAverage ( N ) , 4 ) ) NEW_LINE
Modify array by removing ( arr [ i ] + arr [ i + 1 ] ) th element exactly K times | Function to modify array by removing every K - th element from the array ; Check if current element is the k - th element ; Stores the elements after removing every kth element ; Append the current element if it is not k - th element ; Return the new array after removing every k - th element ; Function to print the array ; Traverse the array l [ ] ; Function to print the array after performing the given operations exactly k times ; Store first N natural numbers ; Iterate over the range [ 0 , k - 1 ] ; Store sums of the two consecutive terms ; Remove every p - th element from the array ; Increment x by 1 for the next iteration ; Print the resultant array ; Given arrays ; Function Call
def removeEveryKth ( l , k ) : NEW_LINE INDENT for i in range ( 0 , len ( l ) ) : NEW_LINE INDENT if i % k == 0 : NEW_LINE INDENT l [ i ] = 0 NEW_LINE DEDENT DEDENT arr = [ 0 ] NEW_LINE for i in range ( 1 , len ( l ) ) : NEW_LINE INDENT if l [ i ] != 0 : NEW_LINE INDENT arr . append ( l [ i ] ) NEW_LINE DEDENT DEDENT return arr NEW_LINE DEDENT def printArray ( l ) : NEW_LINE INDENT for i in range ( 1 , len ( l ) ) : NEW_LINE INDENT print ( l [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT def printSequence ( n , k ) : NEW_LINE INDENT l = [ int ( i ) for i in range ( 0 , n + 1 ) ] NEW_LINE x = 1 NEW_LINE for i in range ( 0 , k ) : NEW_LINE INDENT p = l [ x ] + l [ x + 1 ] NEW_LINE l = removeEveryKth ( l , p ) NEW_LINE x += 1 NEW_LINE DEDENT printArray ( l ) NEW_LINE DEDENT N = 8 NEW_LINE K = 2 NEW_LINE printSequence ( N , K ) NEW_LINE
Minimum number of bits of array elements required to be flipped to make all array elements equal | Function to count minimum number of bits required to be flipped to make all array elements equal ; Stores the count of unset bits ; Stores the count of set bits ; Traverse the array ; Traverse the bit of arr [ i ] ; If current bit is set ; Increment fre1 [ j ] ; Otherwise ; Increment fre0 [ j ] ; Right shift x by 1 ; Stores the count of total moves ; Traverse the range [ 0 , 32 ] ; Update the value of ans ; Return the minimum number of flips required ; Driver Code
def makeEqual ( arr , n ) : NEW_LINE INDENT fre0 = [ 0 ] * 33 NEW_LINE fre1 = [ 0 ] * 33 NEW_LINE for i in range ( n ) : NEW_LINE INDENT x = arr [ i ] NEW_LINE for j in range ( 33 ) : NEW_LINE INDENT if ( x & 1 ) : NEW_LINE INDENT fre1 [ j ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT fre0 [ j ] += 1 NEW_LINE DEDENT x = x >> 1 NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for i in range ( 33 ) : NEW_LINE INDENT ans += min ( fre0 [ i ] , fre1 [ i ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE print ( makeEqual ( arr , N ) ) NEW_LINE DEDENT
Sum of array elements possible by appending arr [ i ] / K to the end of the array K times for array elements divisible by K | Function to calculate sum of array elements after adding arr [ i ] / K to the end of the array if arr [ i ] is divisible by K ; Stores the sum of the array ; Traverse the array arr [ ] ; Traverse the vector ; If v [ i ] is divisible by K ; Iterate over the range [ 0 , K ] ; Update v ; Otherwise ; Traverse the vector v ; Return the sum of the updated array ; Driver Code
def summ ( arr , N , K ) : NEW_LINE INDENT sum = 4 NEW_LINE v = [ i for i in arr ] NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT if ( v [ i ] % K == 0 ) : NEW_LINE INDENT x = v [ i ] // K NEW_LINE for j in range ( K ) : NEW_LINE INDENT v . append ( x ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT for i in range ( len ( v ) ) : NEW_LINE INDENT sum = sum + v [ i ] NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 6 , 8 , 2 ] NEW_LINE K = 2 NEW_LINE N = len ( arr ) NEW_LINE print ( summ ( arr , N , K ) ) NEW_LINE DEDENT
Check if sum of arr [ i ] / j for all possible pairs ( i , j ) in an array is 0 or not | Function to check if sum of all values of ( arr [ i ] / j ) for all 0 < i <= j < ( N - 1 ) is 0 or not ; Stores the required sum ; Traverse the array ; If the sum is equal to 0 ; Otherwise ; Driver Code
def check ( arr , N ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT if ( sum == 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , - 1 , 3 , - 2 , - 1 ] NEW_LINE N = len ( arr ) NEW_LINE check ( arr , N ) NEW_LINE DEDENT
Program to calculate expected increase in price P after N consecutive days | Function to find the increased value of P after N days ; Expected value of the number P after N days ; Print the expected value ; Driver Code
def expectedValue ( P , a , b , N ) : NEW_LINE INDENT expValue = P + ( N * 0.5 * ( a + b ) ) NEW_LINE print ( int ( expValue ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT P = 3000 NEW_LINE a = 20 NEW_LINE b = 10 NEW_LINE N = 30 NEW_LINE expectedValue ( P , a , b , N ) NEW_LINE DEDENT
Find the index in a circular array from which prefix sum is always non | Function to find the starting index of the given circular array prefix sum array is non negative ; Stores the sum of the array ; Stores the starting index ; Stores the minimum prefix sum of A [ 0. . i ] ; Traverse the array ; Update the value of sum ; If sum is less than minimum ; Update the min as the value of prefix sum ; Update starting index ; Otherwise no such index is possible ; Driver code
import sys NEW_LINE def startingPoint ( A , N ) : NEW_LINE INDENT sum = 0 NEW_LINE startingindex = 0 NEW_LINE min = sys . maxsize NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT sum += A [ i ] NEW_LINE if ( sum < min ) : NEW_LINE INDENT min = sum NEW_LINE startingindex = i + 1 NEW_LINE DEDENT DEDENT if ( sum < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return startingindex % N NEW_LINE DEDENT arr = [ 3 , - 6 , 7 , - 4 , - 4 , 6 , - 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( startingPoint ( arr , N ) ) NEW_LINE
Modify array by replacing elements with the nearest power of its previous or next element | Python3 program for the above approach ; Function to calculate the power of y which is nearest to x ; Base Case ; Stores the logarithmic value of x with base y ; Function to replace each array element by the nearest power of its previous or next element ; Stores the previous and next element ; Traverse the array ; Calculate nearest power for previous and next elements ; Replacing the array values ; Print the updated array ; Given array
import math NEW_LINE def nearestPow ( x , y ) : NEW_LINE INDENT if y == 1 : NEW_LINE INDENT return 1 NEW_LINE DEDENT k = int ( math . log ( x , y ) ) NEW_LINE if abs ( y ** k - x ) < abs ( y ** ( k + 1 ) - x ) : NEW_LINE INDENT return y ** k NEW_LINE DEDENT return y ** ( k + 1 ) NEW_LINE DEDENT def replacebyNearestPower ( arr ) : NEW_LINE INDENT prev = arr [ - 1 ] NEW_LINE lastNext = arr [ 0 ] NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT temp = arr [ i ] NEW_LINE if i == len ( arr ) - 1 : NEW_LINE INDENT next = lastNext NEW_LINE DEDENT else : NEW_LINE INDENT next = arr [ ( i + 1 ) % len ( arr ) ] NEW_LINE DEDENT prevPow = nearestPow ( arr [ i ] , prev ) NEW_LINE nextPow = nearestPow ( arr [ i ] , next ) NEW_LINE if abs ( arr [ i ] - prevPow ) < abs ( arr [ i ] - nextPow ) : NEW_LINE INDENT arr [ i ] = prevPow NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] = nextPow NEW_LINE DEDENT prev = temp NEW_LINE DEDENT print ( arr ) NEW_LINE DEDENT arr = [ 2 , 3 , 4 , 1 , 2 ] NEW_LINE replacebyNearestPower ( arr ) NEW_LINE
Check if sum of array can be made equal to X by removing either the first or last digits of every array element | Utility Function to check if the sum of the array elements can be made equal to X by removing either the first or last digits of every array element ; Base Case ; Convert arr [ i ] to string ; Remove last digit ; Remove first digit ; Recursive function call ; Function to check if sum of given array can be made equal to X or not ; Driver Code ; Function Call
def makeSumX ( arr , X , S , i ) : NEW_LINE INDENT if i == len ( arr ) : NEW_LINE INDENT return S == X NEW_LINE DEDENT a = str ( arr [ i ] ) NEW_LINE l = int ( a [ : - 1 ] ) NEW_LINE r = int ( a [ 1 : ] ) NEW_LINE x = makeSumX ( arr , X , S + l , i + 1 ) NEW_LINE y = makeSumX ( arr , X , S + r , i + 1 ) NEW_LINE return x | y NEW_LINE DEDENT def Check ( arr , X ) : NEW_LINE INDENT if ( makeSumX ( arr , X , 0 , 0 ) ) : NEW_LINE print ( " Yes " ) NEW_LINE else : NEW_LINE print ( " No " ) NEW_LINE DEDENT arr = [ 545 , 433 , 654 , 23 ] NEW_LINE X = 134 NEW_LINE Check ( arr , X ) NEW_LINE
Count subarrays having product equal to the power of a given Prime Number | Python 3 program for the above approach ; Function to check if y is a power of m or not ; Calculate log y base m and store it in a variable with integer datatype ; Calculate log y base m and store it in a variable with double datatype ; If res1 and res2 are equal , return True . Otherwise , return false ; Function to count the number of subarrays having product of elements equal to a power of m , where m is a prime number ; Stores the count of subarrays required ; Stores current sequence of consecutive array elements which are a multiple of m ; Traverse the array ; If arr [ i ] is a power of M ; Increment cnt ; Update ans ; Update cnt ; Return the count of subarrays ; Driver Code ; Input
from math import log NEW_LINE def isPower ( m , y ) : NEW_LINE INDENT res1 = log ( y ) // log ( m ) NEW_LINE res2 = log ( y ) // log ( m ) NEW_LINE return ( res1 == res2 ) NEW_LINE DEDENT def numSub ( arr , n , m ) : NEW_LINE INDENT ans = 0 NEW_LINE cnt = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( isPower ( m , arr [ i ] ) ) : NEW_LINE INDENT cnt += 1 NEW_LINE ans += ( cnt * ( cnt - 1 ) ) // 2 NEW_LINE DEDENT else : NEW_LINE INDENT cnt = 0 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 1 , 3 ] NEW_LINE m = 3 NEW_LINE n = len ( arr ) NEW_LINE print ( numSub ( arr , n , m ) ) NEW_LINE DEDENT
Count pairs whose Bitwise AND exceeds Bitwise XOR from a given array | Python3 program to implement the above approach ; Function to count pairs that satisfy the above condition ; Stores the count of pairs ; Stores the count of array elements having same positions of MSB ; Traverse the array ; Stores the index of MSB of array elements ; Calculate number of pairs ; Driver Code ; Given Input ; Function call to count pairs satisfying the given condition
import math NEW_LINE def cntPairs ( arr , N ) : NEW_LINE INDENT res = 0 NEW_LINE bit = [ 0 ] * 32 NEW_LINE for i in range ( N ) : NEW_LINE INDENT pos = ( int ) ( math . log2 ( arr [ i ] ) ) NEW_LINE bit [ pos ] += 1 NEW_LINE DEDENT for i in range ( 32 ) : NEW_LINE INDENT res += ( bit [ i ] * ( bit [ i ] - 1 ) ) // 2 NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE print ( cntPairs ( arr , N ) ) NEW_LINE DEDENT
Minimum MEX from all subarrays of length K | Function to return minimum MEX from all K - length subarrays ; Stores element from [ 1 , N + 1 ] which are not present in subarray ; Store number 1 to N + 1 in set s ; Find the MEX of K - length subarray starting from index 0 ; Find the MEX of all subarrays of length K by erasing arr [ i ] and inserting arr [ i - K ] ; Store first element of set ; Updating the mex ; Print minimum MEX of all K length subarray ; Driver Code
def minimumMEX ( arr , N , K ) : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( 1 , N + 2 , 1 ) : NEW_LINE INDENT s . add ( i ) NEW_LINE DEDENT for i in range ( K ) : NEW_LINE INDENT s . remove ( arr [ i ] ) NEW_LINE DEDENT mex = list ( s ) [ 0 ] NEW_LINE for i in range ( K , N , 1 ) : NEW_LINE INDENT s . remove ( arr [ i ] ) NEW_LINE s . add ( arr [ i - K ] ) NEW_LINE firstElem = list ( s ) [ 0 ] NEW_LINE mex = min ( mex , firstElem ) NEW_LINE DEDENT print ( mex ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE K = 3 NEW_LINE N = len ( arr ) NEW_LINE minimumMEX ( arr , N , K ) NEW_LINE DEDENT
Count smaller elements present in the array for each array element | Function to count for each array element , the number of elements that are smaller than that element ; Stores the frequencies of array elements ; Traverse the array ; Update frequency of arr [ i ] ; Initialize sum with 0 ; Compute prefix sum of the array hash [ ] ; Traverse the array arr [ ] ; If current element is 0 ; Print the resultant count ; Driver Code
def smallerNumbers ( arr , N ) : NEW_LINE INDENT hash = [ 0 ] * 100000 NEW_LINE for i in range ( N ) : NEW_LINE INDENT hash [ arr [ i ] ] += 1 NEW_LINE DEDENT sum = 0 NEW_LINE for i in range ( 1 , 100000 ) : NEW_LINE INDENT hash [ i ] += hash [ i - 1 ] NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] == 0 ) : NEW_LINE INDENT print ( "0" ) NEW_LINE continue NEW_LINE DEDENT print ( hash [ arr [ i ] - 1 ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 4 , 1 , 1 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE smallerNumbers ( arr , N ) NEW_LINE DEDENT
Modulo Operations in Programming With Negative Results | Function to calculate and return the remainder of a % n ; ( a / n ) implicitly gives the truncated result ; Driver Code ; Modulo of two positive numbers ; Modulo of a negative number by a positive number ; Modulo of a positive number by a negative number ; Modulo of two negative numbers
def truncMod ( a , n ) : NEW_LINE INDENT q = a // n NEW_LINE return a - n * q NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 9 NEW_LINE b = 4 NEW_LINE print ( a , " % " , b , " = " , truncMod ( a , b ) ) NEW_LINE a = - 9 NEW_LINE b = 4 NEW_LINE print ( a , " % " , b , " = " , truncMod ( a , b ) ) NEW_LINE a = 9 NEW_LINE b = - 4 NEW_LINE print ( a , " % " , b , " = " , truncMod ( a , b ) ) NEW_LINE a = - 9 NEW_LINE b = - 4 NEW_LINE print ( a , " % " , b , " = " , truncMod ( a , b ) ) NEW_LINE DEDENT
Program for average of an array without running into overflow | Python3 program for the above approach ; Function to calculate average of an array using standard method ; Stores the sum of array ; Find the sum of the array ; Return the average ; Function to calculate average of an array using efficient method ; Store the average of the array ; Traverse the array arr [ ] ; Update avg ; Return avg ; Driver Code ; Input ; Function call
import sys NEW_LINE def average ( arr , N ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT return sum // N * 1.0 - 1 NEW_LINE DEDENT def mean ( arr , N ) : NEW_LINE INDENT avg = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT avg += ( arr [ i ] - avg ) / ( i + 1 ) NEW_LINE DEDENT return round ( avg , 7 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2147483647 , 1 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE print ( " Average ▁ by ▁ Standard ▁ method : ▁ " , " { : . 10f } " . format ( - 1.0 * average ( arr , N ) ) ) NEW_LINE print ( " Average ▁ by ▁ Efficient ▁ method : ▁ " , " { : . 10f } " . format ( mean ( arr , N ) ) ) NEW_LINE DEDENT
Count number of pairs ( i , j ) from an array such that arr [ i ] * j = arr [ j ] * i | Python3 program for the above approach ; Function to count pairs from an array satisfying given conditions ; Stores the total count of pairs ; Stores count of a [ i ] / i ; Traverse the array ; Updating count ; Update frequency in the Map ; Print count of pairs ; Driver Code ; Given array ; Size of the array ; Function call to count pairs satisfying given conditions
from collections import defaultdict NEW_LINE def countPairs ( arr , N ) : NEW_LINE INDENT count = 0 NEW_LINE mp = defaultdict ( int ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT val = 1.0 * arr [ i ] NEW_LINE idx = 1.0 * ( i + 1 ) NEW_LINE count += mp [ val / idx ] NEW_LINE mp [ val / idx ] += 1 NEW_LINE DEDENT print ( count ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 3 , 5 , 6 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE countPairs ( arr , N ) NEW_LINE DEDENT
Count ancestors with smaller value for each node of a Binary Tree | Function to add an edge between nodes u and v ; Function to perform the DFS Traversal and store parent of each node ; Store the immediate parent ; Traverse the children of the current node ; Recursively call for function dfs for the child node ; Function to count the number of ancestors with values smaller than that of the current node ; Perform the DFS Traversal ; Traverse all the nodes ; Store the number of ancestors smaller than node ; Loop until parent [ node ] != - 1 ; If the condition satisfies , increment cnt by 1 ; Print the required result for the current node ; Driver Code ; Tree Formation
def add_edge ( u , v ) : NEW_LINE INDENT global adj NEW_LINE adj [ u ] . append ( v ) NEW_LINE adj [ v ] . append ( u ) NEW_LINE DEDENT def dfs ( u , par = - 1 ) : NEW_LINE INDENT global adj , parent NEW_LINE parent [ u ] = par NEW_LINE for child in adj [ u ] : NEW_LINE INDENT if ( child != par ) : NEW_LINE INDENT dfs ( child , u ) NEW_LINE DEDENT DEDENT DEDENT def countSmallerAncestors ( n ) : NEW_LINE INDENT global parent , adj NEW_LINE dfs ( 1 ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT node = i NEW_LINE cnt = 0 NEW_LINE while ( parent [ node ] != - 1 ) : NEW_LINE INDENT if ( parent [ node ] < i ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT node = parent [ node ] NEW_LINE DEDENT print ( cnt , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 NEW_LINE adj = [ [ ] for i in range ( 10 ** 5 ) ] NEW_LINE parent = [ 0 ] * ( 10 ** 5 ) NEW_LINE add_edge ( 1 , 5 ) NEW_LINE add_edge ( 1 , 4 ) NEW_LINE add_edge ( 4 , 6 ) NEW_LINE add_edge ( 5 , 3 ) NEW_LINE add_edge ( 5 , 2 ) NEW_LINE countSmallerAncestors ( N ) NEW_LINE DEDENT
Count subsequences having odd Bitwise XOR values from an array | Function to count the subsequences having odd bitwise XOR value ; Stores count of odd elements ; Stores count of even elements ; Traverse the array A [ ] ; If el is odd ; If count of odd elements is 0 ; Driver Code ; Given array A [ ] ; Function call to count subsequences having odd bitwise XOR value
def countSubsequences ( A ) : NEW_LINE INDENT odd = 0 NEW_LINE even = 0 NEW_LINE for el in A : NEW_LINE INDENT if ( el % 2 == 1 ) : NEW_LINE INDENT odd += 1 NEW_LINE DEDENT else : NEW_LINE INDENT even += 1 NEW_LINE DEDENT DEDENT if ( odd == 0 ) : NEW_LINE INDENT print ( 0 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( 1 << len ( A ) - 1 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 3 , 4 ] NEW_LINE countSubsequences ( A ) NEW_LINE DEDENT
Maximum subarray product modulo M | Function to find maximum subarray product modulo M and minimum length of the subarray ; Stores maximum subarray product modulo M and minimum length of the subarray ; Stores the minimum length of subarray having maximum product ; Traverse the array ; Stores the product of a subarray ; Calculate Subarray whose start index is i ; Multiply product by arr [ i ] ; If product greater than ans ; Update ans ; Update length ; Print maximum subarray product mod M ; Print minimum length of subarray having maximum product ; Drivers Code
def maxModProdSubarr ( arr , n , M ) : NEW_LINE INDENT ans = 0 NEW_LINE length = n NEW_LINE for i in range ( n ) : NEW_LINE INDENT product = 1 NEW_LINE for j in range ( i , n , 1 ) : NEW_LINE INDENT product = ( product * arr [ i ] ) % M NEW_LINE if ( product > ans ) : NEW_LINE INDENT ans = product NEW_LINE if ( length > j - i + 1 ) : NEW_LINE INDENT length = j - i + 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT print ( " Maximum ▁ subarray ▁ product ▁ is " , ans ) NEW_LINE print ( " Minimum ▁ length ▁ of ▁ the ▁ maximum ▁ product ▁ subarray ▁ is " , length ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE M = 5 NEW_LINE maxModProdSubarr ( arr , N , M ) NEW_LINE DEDENT