codeparrot/xlcost-text-to-code huggingface dataset:
Smallest value of N such that the sum of all natural numbers from K to N is at least X | Function to check if the sum of natural numbers from K to N is >= X ; Function to find the minimum value of N such that the sum of natural numbers from K to N is at least X ; If K is greater than X ; Perform the Binary Search ; If the sum of the natural numbers from K to mid is atleast X ; Update res ; Update high ; Otherwise , update low ; Print the value of res as the answer ; Driver Code
def isGreaterEqual ( N , K , X ) : NEW_LINE INDENT return ( ( ( N * ( N + 1 ) // 2 ) - ( ( K - 1 ) * K // 2 ) ) >= X ) NEW_LINE DEDENT def minimumNumber ( K , X ) : NEW_LINE INDENT if ( K > X ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT low = K NEW_LINE high = X NEW_LINE res = - 1 NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = low + ( ( high - low ) // 2 ) NEW_LINE if ( isGreaterEqual ( mid , K , X ) ) : NEW_LINE INDENT res = mid NEW_LINE high = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT print ( res ) NEW_LINE DEDENT K = 5 NEW_LINE X = 13 NEW_LINE minimumNumber ( K , X ) NEW_LINE
Count pairs with product of indices equal to the product of elements present at those indices | Function to count the number of pairs having product of indices equal to the product of elements at that indices ; Stores the count of valid pairs ; Generate all possible pairs ; If the condition is satisfied ; Increment the count ; Return the total count ; Driver Code
def CountPairs ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( ( i * j ) == ( arr [ i ] * arr [ j ] ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 0 , 3 , 2 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE print ( CountPairs ( arr , N ) ) NEW_LINE DEDENT
Maximize remainder of sum of a pair of array elements with different parity modulo K | Python3 program for the above approach ; Function to find the maximum remainder of sum of a pair of array elements modulo K ; Stores all even numbers ; Stores all odd numbers ; Segregate remainders of even and odd numbers in respective sets ; Stores the maximum remainder obtained ; Find the complement of remainder of each even number in odd set ; Find the complement of remainder x ; Print the answer ; Driver code ; Given array ; Size of the array ; Given value of K
from bisect import bisect_left NEW_LINE def maxRemainder ( A , N , K ) : NEW_LINE INDENT even = { } NEW_LINE odd = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT num = A [ i ] NEW_LINE if ( num % 2 == 0 ) : NEW_LINE INDENT even [ num % K ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT odd [ num % K ] = 1 NEW_LINE DEDENT DEDENT max_rem = 0 NEW_LINE for x in even : NEW_LINE INDENT y = K - 1 - x NEW_LINE od = list ( odd . keys ( ) ) NEW_LINE it = bisect_left ( od , y ) NEW_LINE if ( it != 0 ) : NEW_LINE INDENT max_rem = max ( max_rem , x + od [ it ] ) NEW_LINE DEDENT DEDENT print ( max_rem ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 4 , 11 , 6 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE K = 7 NEW_LINE maxRemainder ( arr , N , K ) NEW_LINE DEDENT
Length of the longest path ending at vertex V in a Graph | Python3 program for the above approach ; Function to perform DFS Traversal from source node to the deepest node and update maximum distance to the deepest node ; Mark source as visited ; Update the maximum distance ; Traverse the adjacency list of the current source node ; Recursively call for the child node ; Backtracking step ; Function to calculate maximum length of the path ending at vertex V from any source node ; Stores the maximum length of the path ending at vertex V ; Stores the size of the matrix ; Stores the adjacency list of the given graph ; Traverse the matrix to create adjacency list ; Perform DFS Traversal to update the maximum distance ; Driver Code
visited = [ False for i in range ( 4 ) ] NEW_LINE def dfs ( src , Adj , level , distance ) : NEW_LINE INDENT global visited NEW_LINE visited [ src ] = True NEW_LINE distance = max ( distance , level ) NEW_LINE for child in Adj [ src ] : NEW_LINE INDENT if ( child != src and visited [ child ] == False ) : NEW_LINE INDENT dfs ( child , Adj , level + 1 , distance ) NEW_LINE DEDENT DEDENT visited [ src ] = False NEW_LINE DEDENT def maximumLength ( mat , V ) : NEW_LINE INDENT distance = 0 NEW_LINE N = len ( mat ) NEW_LINE Adj = [ [ ] for i in range ( N ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT if ( mat [ i ] [ j ] == 1 ) : NEW_LINE INDENT Adj [ i ] . append ( j ) NEW_LINE DEDENT DEDENT DEDENT dfs ( V , Adj , 0 , distance ) NEW_LINE return distance + 2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 0 , 1 , 0 , 0 ] , [ 1 , 0 , 1 , 1 ] , [ 0 , 1 , 0 , 0 ] , [ 0 , 1 , 0 , 0 ] ] NEW_LINE V = 2 NEW_LINE print ( maximumLength ( mat , V ) ) NEW_LINE DEDENT
Minimum removals required such that given string consists only of a pair of alternating characters | Function to find the maximum length of alternating occurrences of a pair of characters in a string s ; Stores the next character for alternating sequence ; Stores the length of alternating occurrences of a pair of characters ; Traverse the given string ; If current character is same as the required character ; Increase length by 1 ; Reset required character ; Return the length ; Function to find minimum characters required to be deleted from S to obtain an alternating sequence ; Stores maximum length of alternating sequence of two characters ; Stores length of the string ; Generate every pair of English alphabets ; Function call to find length of alternating sequence for current pair of characters ; Update len to store the maximum of len and newLen in len ; Return n - len as the final result ; Driver Code ; Given Input ; Function call to find minimum characters required to be removed from S to make it an alternating sequence of a pair of characters
def findLength ( s , i , j ) : NEW_LINE INDENT required = i NEW_LINE length = 0 NEW_LINE for curr in s : NEW_LINE INDENT if ( curr == required ) : NEW_LINE INDENT length += 1 NEW_LINE if ( required == i ) : NEW_LINE INDENT required = j NEW_LINE DEDENT else : NEW_LINE INDENT required = i NEW_LINE DEDENT DEDENT DEDENT return length NEW_LINE DEDENT def minimumDeletions ( S ) : NEW_LINE INDENT len1 = 0 NEW_LINE n = len ( S ) NEW_LINE for i in range ( 0 , 26 , 1 ) : NEW_LINE INDENT for j in range ( i + 1 , 26 , 1 ) : NEW_LINE INDENT newLen = findLength ( S , chr ( i + 97 ) , chr ( j + 97 ) ) NEW_LINE len1 = max ( len1 , newLen ) NEW_LINE DEDENT DEDENT return n - len1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " adebbeeaebd " NEW_LINE print ( minimumDeletions ( S ) ) NEW_LINE DEDENT
Smallest value of X satisfying the condition X % A [ i ] = B [ i ] for two given arrays | Python3 program for the above approach ; Function to calculate modulo inverse of a w . r . t m using Extended Euclid Algorithm ; Base Case ; Perform extended euclid algorithm ; q is quotient ; m is remainder now , process same as euclid 's algorithm ; If x1 is negative ; Make x1 positive ; Function to implement Chinese Remainder Theorem to find X ; Stores the product of array elements ; Traverse the array ; Update product ; Initialize the result ; Apply the above formula ; Function to calculate the product of all elements of the array a [ ] ; Stores product of all array elements ; Traverse the array ; Return the product ; Function to find the value of X that satisfies the given condition ; Stores the required smallest value using Chinese Remainder Theorem ; Stores the product of all array elements ; The equation is Y + K * M >= P Therefore , calculate K = ceil ( ( P - Y ) / M ) ; So , X = Y + K * M ; Print the resultant value of X ; Driver Code
import math NEW_LINE def inv ( a , m ) : NEW_LINE INDENT m0 = m NEW_LINE x0 = 0 NEW_LINE x1 = 1 NEW_LINE if ( m == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT while ( a > 1 ) : NEW_LINE INDENT q = a // m NEW_LINE t = m NEW_LINE m = a % m NEW_LINE a = t NEW_LINE t = x0 NEW_LINE x0 = x1 - q * x0 NEW_LINE x1 = t NEW_LINE DEDENT if ( x1 < 0 ) : NEW_LINE INDENT x1 += m0 NEW_LINE DEDENT return x1 NEW_LINE DEDENT def findMinX ( A , B , N ) : NEW_LINE INDENT prod = 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT prod *= A [ i ] NEW_LINE DEDENT result = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT pp = prod // A [ i ] NEW_LINE result += B [ i ] * inv ( pp , A [ i ] ) * pp NEW_LINE DEDENT return result % prod NEW_LINE DEDENT def product ( a , n ) : NEW_LINE INDENT ans = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans *= a [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT def findSmallestInteger ( A , B , P , n ) : NEW_LINE INDENT Y = findMinX ( A , B , n ) NEW_LINE M = product ( A , n ) NEW_LINE K = math . ceil ( ( P - Y ) / M ) NEW_LINE X = Y + K * M NEW_LINE print ( X ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 3 , 4 , 5 ] NEW_LINE B = [ 2 , 3 , 1 ] NEW_LINE n = len ( A ) NEW_LINE P = 72 NEW_LINE findSmallestInteger ( A , B , P , n ) NEW_LINE DEDENT
Minimum number of jumps required to sort the given array in ascending order | Function to count minimum number of jumps required to sort the array ; Stores minimum number of jumps ; Stores distances of jumps ; Stores the array elements with their starting indices ; Push the pairs { arr [ i ] , i + 1 } into the vector of pairs vect ; Update vect ; Populate the array temp [ ] ; Update temp [ arr [ i ] ] ; Sort the vector in the ascending order ; Jump till the previous index <= current index ; Update vect [ i ] ; Increment the number of jumps ; Print the minimum number of jumps required ; Driver Code ; Input ; Size of the array
def minJumps ( arr , jump , N ) : NEW_LINE INDENT jumps = 0 NEW_LINE temp = [ 0 for i in range ( 1000 ) ] NEW_LINE vect = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT vect . append ( [ arr [ i ] , i + 1 ] ) NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT temp [ arr [ i ] ] = jump [ i ] NEW_LINE DEDENT vect . sort ( reverse = False ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT while ( vect [ i ] [ 1 ] <= vect [ i - 1 ] [ 1 ] ) : NEW_LINE INDENT vect [ i ] = [ vect [ i ] [ 0 ] , vect [ i ] [ 1 ] + temp [ vect [ i ] [ 0 ] ] ] NEW_LINE jumps += 1 NEW_LINE DEDENT DEDENT print ( jumps ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 1 ] NEW_LINE jump = [ 1 , 1 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE minJumps ( arr , jump , N ) NEW_LINE DEDENT
Minimum number of basic logic gates required to realize given Boolean expression | Function to count the total number of gates required to realize the boolean expression S ; Length of the string ; Stores the count of total gates ; Traverse the string ; AND , OR and NOT Gate ; Print the count of gates required ; Driver Code ; Input ; Function call to count the total number of gates required
def numberOfGates ( s ) : NEW_LINE INDENT N = len ( s ) NEW_LINE ans = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == ' . ' or s [ i ] == ' + ' or s [ i ] == '1' ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT print ( ans , end = " " ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " ( 1 - A ) . B + C " NEW_LINE numberOfGates ( S ) NEW_LINE DEDENT
Count pairs ( i , j ) from an array such that | arr [ i ] | and | arr [ j ] | both lies between | arr [ i ] | Function to find pairs ( i , j ) such that | arr [ i ] | and | arr [ j ] | lies in between | arr [ i ] - arr [ j ] | and | arr [ i ] + arr [ j ] | ; Calculate absolute value of all array elements ; Sort the array ; Stores the count of pairs ; Traverse the array ; Increment left ; Add to the current count of pairs ; Print the answer ; Driver Code
def findPairs ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT arr [ i ] = abs ( arr [ i ] ) NEW_LINE DEDENT arr . sort ( ) NEW_LINE left = 0 NEW_LINE ans = 0 NEW_LINE for right in range ( N ) : NEW_LINE INDENT while ( 2 * arr [ left ] < arr [ right ] ) : NEW_LINE INDENT left += 1 NEW_LINE DEDENT ans += ( right - left ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 3 , 5 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE findPairs ( arr , N ) NEW_LINE DEDENT
Calculate absolute difference between minimum and maximum sum of pairs in an array | Python 3 program for the above approach ; Function to find the difference between the maximum and minimum sum of a pair ( arr [ i ] , arr [ j ] ) from the array such that i < j and arr [ i ] < arr [ j ] ; Stores the maximum from the suffix of the array ; Set the last element ; Traverse the remaining array ; Update the maximum from suffix for the remaining indices ; Stores the maximum sum of any pair ; Calculate the maximum sum ; Stores the maximum sum of any pair ; Stores the minimum of suffixes from the given array ; Set the last element ; Traverse the remaining array ; Update the maximum from suffix for the remaining indices ; Calculate the minimum sum ; Return the resultant difference ; Driver Code ; Function Call
import sys NEW_LINE def GetDiff ( A , N ) : NEW_LINE INDENT SuffMaxArr = [ 0 for i in range ( N ) ] NEW_LINE SuffMaxArr [ N - 1 ] = A [ N - 1 ] NEW_LINE i = N - 2 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT SuffMaxArr [ i ] = max ( SuffMaxArr [ i + 1 ] , A [ i + 1 ] ) NEW_LINE i -= 1 NEW_LINE DEDENT MaximumSum = - sys . maxsize - 1 NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT if ( A [ i ] < SuffMaxArr [ i ] ) : NEW_LINE INDENT MaximumSum = max ( MaximumSum , A [ i ] + SuffMaxArr [ i ] ) NEW_LINE DEDENT DEDENT MinimumSum = sys . maxsize NEW_LINE SuffMinArr = [ 0 for i in range ( N ) ] NEW_LINE SuffMinArr [ N - 1 ] = sys . maxsize NEW_LINE i = N - 2 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT SuffMinArr [ i ] = min ( SuffMinArr [ i + 1 ] , A [ i + 1 ] ) NEW_LINE i -= 1 NEW_LINE DEDENT for i in range ( N - 1 ) : NEW_LINE INDENT if ( A [ i ] < SuffMinArr [ i ] ) : NEW_LINE INDENT MinimumSum = min ( MinimumSum , A [ i ] + SuffMinArr [ i ] ) NEW_LINE DEDENT DEDENT return abs ( MaximumSum - MinimumSum ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 1 , 3 , 7 , 5 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE print ( GetDiff ( arr , N ) ) NEW_LINE DEDENT
Minimum number of swaps required to make parity of array elements same as their indices | Function to count the minimum number of swaps required to make the parity of array elements same as their indices ; Stores count of even and odd array elements ; Traverse the array ; Check if indices and array elements are not of the same parity ; If index is even ; Update even ; Update odd ; Condition for not possible ; Otherwise ; Driver Code
def minimumSwaps ( arr , N ) : NEW_LINE INDENT even , odd = 0 , 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] % 2 != i % 2 ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT even += 1 NEW_LINE DEDENT else : NEW_LINE INDENT odd += 1 NEW_LINE DEDENT DEDENT DEDENT if ( even != odd ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( even ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 7 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE minimumSwaps ( arr , N ) NEW_LINE DEDENT
Smallest substring occurring only once in a given string | Python3 program of the above approach ; Function to find the smallest substring occurring only once ; Stores all occurences ; Generate all the substrings ; Avoid multiple occurences ; Append all substrings ; Take into account all the substrings ; Iterate over all unique substrings ; If frequency is 1 ; Append into fresh list ; Initialize a dictionary ; Append the keys ; Traverse the dictionary ; Print the minimum of dictionary ; Driver Code
from collections import Counter NEW_LINE def smallestSubstring ( a ) : NEW_LINE INDENT a1 = [ ] NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT for j in range ( i + 1 , len ( a ) ) : NEW_LINE INDENT if i != j : NEW_LINE INDENT a1 . append ( a [ i : j + 1 ] ) NEW_LINE DEDENT DEDENT DEDENT a2 = Counter ( a1 ) NEW_LINE freshlist = [ ] NEW_LINE for i in a2 : NEW_LINE INDENT if a2 [ i ] == 1 : NEW_LINE INDENT freshlist . append ( i ) NEW_LINE DEDENT DEDENT dictionary = dict ( ) NEW_LINE for i in range ( len ( freshlist ) ) : NEW_LINE INDENT dictionary [ freshlist [ i ] ] = len ( freshlist [ i ] ) NEW_LINE DEDENT newlist = [ ] NEW_LINE for i in dictionary : NEW_LINE INDENT newlist . append ( dictionary [ i ] ) NEW_LINE DEDENT return ( min ( newlist ) ) NEW_LINE DEDENT S = " ababaabba " NEW_LINE print ( smallestSubstring ( S ) ) NEW_LINE
Minimize insertions required to make ratio of maximum and minimum of all pairs of adjacent array elements at most K | Function to count the minimum number of insertions required ; Stores the number of insertions required ; Traverse the array ; Store the minimum array element ; Store the maximum array element ; Checking condition ; Increase current count ; Return the count of insertions ; Driver Code
def mininsert ( arr , K , N ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT a = min ( arr [ i ] , arr [ i + 1 ] ) NEW_LINE b = max ( arr [ i ] , arr [ i + 1 ] ) NEW_LINE while ( K * a < b ) : NEW_LINE INDENT a *= K NEW_LINE ans += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 2 , 10 , 25 , 21 ] NEW_LINE K = 2 NEW_LINE N = len ( arr ) NEW_LINE print ( mininsert ( arr , K , N ) ) NEW_LINE
Sum of numbers formed by consecutive digits present in a given string | Function to calculate the sum of numbers formed by consecutive sequences of digits present in the string ; Stores consecutive digits present in the string ; Stores the sum ; Iterate over characters of the input string ; If current character is a digit ; Append current digit to curr ; Add curr to sum ; Reset curr ; Driver Code
def sumOfDigits ( s ) : NEW_LINE INDENT curr = 0 NEW_LINE ret = 0 NEW_LINE for ch in s : NEW_LINE INDENT if ( ord ( ch ) >= 48 and ord ( ch ) <= 57 ) : NEW_LINE INDENT curr = curr * 10 + ord ( ch ) - ord ( '0' ) NEW_LINE DEDENT else : NEW_LINE INDENT ret += curr NEW_LINE curr = 0 NEW_LINE DEDENT DEDENT ret += curr NEW_LINE return ret NEW_LINE DEDENT S = "11aa32bbb5" NEW_LINE print ( sumOfDigits ( S ) ) NEW_LINE
Find a triplet ( i , j , k ) from an array such that i < j < k and arr [ i ] < arr [ j ] > arr [ k ] | Function to find a triplet such that i < j < k and arr [ i ] < arr [ j ] and arr [ j ] > arr [ k ] ; Traverse the array ; Condition to satisfy for the resultant triplet ; Otherwise , triplet doesn 't exist ; Driver Code
def print_triplet ( arr , n ) : NEW_LINE INDENT for i in range ( 1 , n - 1 ) : NEW_LINE INDENT if ( arr [ i - 1 ] < arr [ i ] and arr [ i ] > arr [ i + 1 ] ) : NEW_LINE INDENT print ( i - 1 , i , i + 1 ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( - 1 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 3 , 5 , 2 , 1 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE print_triplet ( arr , N ) NEW_LINE DEDENT
Check if an array element is concatenation of two elements from another array | Python3 program for the above approach ; Function to find elements present in the array b [ ] which are concatenation of any pair of elements in the array a [ ] ; Stores if there doesn 't any such  element in the array brr[] ; Stored the size of both the arrays ; Store the presence of an element of array a [ ] ; Traverse the array a [ ] ; Traverse the array b [ ] ; Traverse over all possible concatenations of b [ i ] ; Update right and left parts ; Check if both left and right parts are present in a [ ] ; Driver Code
from collections import defaultdict NEW_LINE def findConcatenatedNumbers ( a , b ) : NEW_LINE INDENT ans = True NEW_LINE n1 = len ( a ) NEW_LINE n2 = len ( b ) NEW_LINE cnt = defaultdict ( int ) NEW_LINE for i in range ( n1 ) : NEW_LINE INDENT cnt [ a [ i ] ] = 1 NEW_LINE DEDENT for i in range ( n2 ) : NEW_LINE INDENT left = b [ i ] NEW_LINE right = 0 NEW_LINE mul = 1 NEW_LINE while ( left > 9 ) : NEW_LINE INDENT right += ( left % 10 ) * mul NEW_LINE left //= 10 NEW_LINE mul *= 10 NEW_LINE if ( cnt [ left ] == 1 and cnt [ right ] == 1 ) : NEW_LINE INDENT ans = False NEW_LINE print ( b [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if ( ans ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 2 , 34 , 4 , 5 ] NEW_LINE b = [ 26 , 24 , 345 , 4 , 22 ] NEW_LINE findConcatenatedNumbers ( a , b ) NEW_LINE DEDENT
Number of subarrays having even product | Function to count subarrays with even product ; Stores count of subarrays with even product ; Traverse the array ; Initialize product ; Update product of the subarray ; Print total count of subarrays ; Driver Code ; Input ; Length of an array ; Function call to count subarrays with even product
def evenproduct ( arr , length ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( length + 1 ) : NEW_LINE INDENT product = 1 ; NEW_LINE for j in range ( i , length + 1 ) : NEW_LINE product *= arr [ j ] ; NEW_LINE if ( product % 2 == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT print ( count ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 7 , 5 , 4 , 9 ] ; NEW_LINE length = len ( arr ) - 1 ; NEW_LINE evenproduct ( arr , length ) ; NEW_LINE DEDENT
Count pairs of nodes having minimum distance between them equal to the difference of their distances from root | Stores the count of pairs ; Store the adjacency list of the connecting vertex ; Function for theto perform DFS traversal of the given tree ; Traverse the adjacency list of the current node u ; If the current node is the parent node ; Add number of ancestors , which is same as depth of the node ; Function for DFS traversal of the given tree ; Print result ; Function to find the count of pairs such that the minimum distance between them is equal to the difference between distance of the nodes from root node ; Add edges to adj [ ] ; Driver Code
ans = 0 NEW_LINE adj = [ [ ] for i in range ( 10 ** 5 + 1 ) ] NEW_LINE def dfsUtil ( u , par , depth ) : NEW_LINE INDENT global adj , ans NEW_LINE for it in adj [ u ] : NEW_LINE INDENT if ( it != par ) : NEW_LINE INDENT dfsUtil ( it , u , depth + 1 ) NEW_LINE DEDENT DEDENT ans += depth NEW_LINE DEDENT def dfs ( u , par , depth ) : NEW_LINE INDENT global ans NEW_LINE dfsUtil ( u , par , depth ) NEW_LINE print ( ans ) NEW_LINE DEDENT def countPairs ( edges ) : NEW_LINE INDENT global adj NEW_LINE for i in range ( len ( edges ) ) : NEW_LINE INDENT u = edges [ i ] [ 0 ] NEW_LINE v = edges [ i ] [ 1 ] NEW_LINE adj [ u ] . append ( v ) NEW_LINE adj [ v ] . append ( u ) NEW_LINE DEDENT dfs ( 1 , 1 , 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT edges = [ [ 1 , 2 ] , [ 1 , 3 ] , [ 2 , 4 ] ] NEW_LINE countPairs ( edges ) NEW_LINE DEDENT