codeparrot/xlcost-text-to-code huggingface dataset:
Delete Edge to minimize subtree sum difference | DFS method to traverse through edges , calculating subtree Sum at each node and updating the difference between subtrees ; loop for all neighbors except parent and aggregate Sum over all subtrees ; store Sum in current node 's  subtree index ; at one side subtree Sum is ' Sum ' and other side subtree Sum is ' totalSum ▁ - ▁ Sum ' so their difference will be totalSum - 2 * Sum , by which we 'll update res ; Method returns minimum subtree Sum difference ; Calculating total Sum of tree and initializing subtree Sum 's  by vertex values ; filling edge data structure ; calling DFS method at node 0 , with parent as - 1 ; Driver Code
def dfs ( u , parent , totalSum , edge , subtree , res ) : NEW_LINE INDENT Sum = subtree [ u ] NEW_LINE for i in range ( len ( edge [ u ] ) ) : NEW_LINE INDENT v = edge [ u ] [ i ] NEW_LINE if ( v != parent ) : NEW_LINE INDENT dfs ( v , u , totalSum , edge , subtree , res ) NEW_LINE Sum += subtree [ v ] NEW_LINE DEDENT DEDENT subtree [ u ] = Sum NEW_LINE if ( u != 0 and abs ( totalSum - 2 * Sum ) < res [ 0 ] ) : NEW_LINE INDENT res [ 0 ] = abs ( totalSum - 2 * Sum ) NEW_LINE DEDENT DEDENT def getMinSubtreeSumDifference ( vertex , edges , N ) : NEW_LINE INDENT totalSum = 0 NEW_LINE subtree = [ None ] * N NEW_LINE for i in range ( N ) : NEW_LINE INDENT subtree [ i ] = vertex [ i ] NEW_LINE totalSum += vertex [ i ] NEW_LINE DEDENT edge = [ [ ] for i in range ( N ) ] NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT edge [ edges [ i ] [ 0 ] ] . append ( edges [ i ] [ 1 ] ) NEW_LINE edge [ edges [ i ] [ 1 ] ] . append ( edges [ i ] [ 0 ] ) NEW_LINE DEDENT res = [ 999999999999 ] NEW_LINE dfs ( 0 , - 1 , totalSum , edge , subtree , res ) NEW_LINE return res [ 0 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT vertex = [ 4 , 2 , 1 , 6 , 3 , 5 , 2 ] NEW_LINE edges = [ [ 0 , 1 ] , [ 0 , 2 ] , [ 0 , 3 ] , [ 2 , 4 ] , [ 2 , 5 ] , [ 3 , 6 ] ] NEW_LINE N = len ( vertex ) NEW_LINE print ( getMinSubtreeSumDifference ( vertex , edges , N ) ) NEW_LINE DEDENT
Minimum operations required to make every element greater than or equal to K | Function to calculate gcd of two numbers ; function to get minimum operation needed ; The priority queue holds a minimum element in the top position ; push value one by one from the given array ; store count of minimum operation needed ; All elements are now >= k ; It is impossible to make as there are no sufficient elements available ; Take two smallest elements and replace them by their LCM first smallest element ; Second smallest element ; Increment the count ; Driver code
def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE return b NEW_LINE return gcd ( b % a , a ) NEW_LINE DEDENT def FindMinOperation ( a , n , k ) : NEW_LINE INDENT Q = [ ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE Q . append ( a [ i ] ) NEW_LINE Q . sort ( ) NEW_LINE ans = 0 NEW_LINE while ( True ) : NEW_LINE if ( Q [ 0 ] >= k ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( len ( Q ) < 2 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT x = Q [ 0 ] NEW_LINE Q . pop ( 0 ) NEW_LINE y = Q [ 0 ] NEW_LINE Q . pop ( 0 ) NEW_LINE z = ( x * y ) // gcd ( x , y ) NEW_LINE Q . append ( z ) NEW_LINE Q . sort ( ) NEW_LINE ans += 1 NEW_LINE return ans NEW_LINE DEDENT a = [ 3 , 5 , 7 , 6 , 8 ] NEW_LINE k = 8 NEW_LINE n = len ( a ) NEW_LINE print ( FindMinOperation ( a , n , k ) ) NEW_LINE
Find the lexicographically smallest string which satisfies the given condition | Function to return the required string ; First character will always be 'a ; To store the resultant string ; Since length of the string should be greater than 0 and first element of array should be 1 ; Check one by one all element of given prefix array ; If the difference between any two consecutive elements of the prefix array is greater than 1 then there will be no such string possible that satisfies the given array . Also , string cannot have more than 26 distinct characters ; If difference is 0 then the ( i + 1 ) th character will be same as the ith character ; If difference is 1 then the ( i + 1 ) th character will be different from the ith character ; Return the resultant string ; Driver code
def smallestString ( N , A ) : NEW_LINE ' NEW_LINE INDENT ch = ' a ' NEW_LINE S = " " NEW_LINE if ( N < 1 or A [ 0 ] != 1 ) : NEW_LINE INDENT S = " - 1" NEW_LINE return S NEW_LINE DEDENT S += str ( ch ) NEW_LINE ch = chr ( ord ( ch ) + 1 ) NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT diff = A [ i ] - A [ i - 1 ] NEW_LINE if ( diff > 1 or diff < 0 or A [ i ] > 26 ) : NEW_LINE INDENT S = " - 1" NEW_LINE return S NEW_LINE DEDENT elif ( diff == 0 ) : NEW_LINE INDENT S += ' a ' NEW_LINE DEDENT else : NEW_LINE INDENT S += ch NEW_LINE ch = chr ( ord ( ch ) + 1 ) NEW_LINE DEDENT DEDENT return S NEW_LINE DEDENT arr = [ 1 , 1 , 2 , 3 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( smallestString ( n , arr ) ) NEW_LINE
Count of alphabets whose ASCII values can be formed with the digits of N | Python3 implementation of the approach ; Function that returns true if num can be formed with the digits in digits [ ] array ; Copy of the digits array ; Get last digit ; If digit array doesn 't contain  current digit ; One occurrence is used ; Remove the last digit ; Function to return the count of required alphabets ; To store the occurrences of digits ( 0 - 9 ) ; Get last digit ; Update the occurrence of the digit ; Remove the last digit ; If any lowercase character can be picked from the current digits ; If any uppercase character can be picked from the current digits ; Return the required count of alphabets ; Driver code
import math NEW_LINE def canBePicked ( digits , num ) : NEW_LINE INDENT copyDigits = [ ] ; NEW_LINE for i in range ( len ( digits ) ) : NEW_LINE INDENT copyDigits . append ( digits [ i ] ) ; NEW_LINE DEDENT while ( num > 0 ) : NEW_LINE INDENT digit = num % 10 ; NEW_LINE if ( copyDigits [ digit ] == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT else : NEW_LINE INDENT copyDigits [ digit ] -= 1 ; NEW_LINE DEDENT num = math . floor ( num / 10 ) ; NEW_LINE DEDENT return True ; NEW_LINE DEDENT def countAlphabets ( n ) : NEW_LINE INDENT count = 0 ; NEW_LINE digits = [ 0 ] * 10 ; NEW_LINE while ( n > 0 ) : NEW_LINE INDENT digit = n % 10 ; NEW_LINE digits [ digit ] += 1 ; NEW_LINE n = math . floor ( n / 10 ) ; NEW_LINE DEDENT for i in range ( ord ( ' a ' ) , ord ( ' z ' ) + 1 ) : NEW_LINE INDENT if ( canBePicked ( digits , i ) ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT for i in range ( ord ( ' A ' ) , ord ( ' Z ' ) + 1 ) : NEW_LINE INDENT if ( canBePicked ( digits , i ) ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT n = 1623455078 ; NEW_LINE print ( countAlphabets ( n ) ) ; NEW_LINE
Largest number less than X having at most K set bits | Function to return the greatest number <= X having at most K set bits . ; Remove rightmost set bits one by one until we count becomes k ; Return the required number ; Driver code
def greatestKBits ( X , K ) : NEW_LINE INDENT set_bit_count = bin ( X ) . count ( '1' ) NEW_LINE if ( set_bit_count <= K ) : NEW_LINE INDENT return X NEW_LINE DEDENT diff = set_bit_count - K NEW_LINE for i in range ( 0 , diff , 1 ) : NEW_LINE INDENT X &= ( X - 1 ) NEW_LINE DEDENT return X NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 21 NEW_LINE K = 2 NEW_LINE print ( greatestKBits ( X , K ) ) NEW_LINE DEDENT
Find two numbers whose sum and GCD are given | Python 3 program to find two numbers whose sum and GCD is given ; Function to find two numbers whose sum and gcd is given ; sum != gcd checks that both the numbers are positive or not ; Driver code
from math import gcd as __gcd NEW_LINE def findTwoNumbers ( sum , gcd ) : NEW_LINE INDENT if ( __gcd ( gcd , sum - gcd ) == gcd and sum != gcd ) : NEW_LINE INDENT print ( " a ▁ = " , min ( gcd , sum - gcd ) , " , ▁ b ▁ = " , sum - min ( gcd , sum - gcd ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT sum = 8 NEW_LINE gcd = 2 NEW_LINE findTwoNumbers ( sum , gcd ) NEW_LINE DEDENT
Find maximum distance between any city and station | Function to calculate the maximum distance between any city and its nearest station ; Initialize boolean list ; Assign True to cities containing station ;
def findMaxDistance ( numOfCities , station ) : NEW_LINE INDENT hasStation = [ False ] * numOfCities NEW_LINE for city in station : NEW_LINE INDENT hasStation [ city ] = True NEW_LINE DEDENT dist , maxDist = 0 , min ( station ) NEW_LINE for city in range ( numOfCities ) : NEW_LINE INDENT if hasStation [ city ] == True : NEW_LINE INDENT maxDist = max ( ( dist + 1 ) // 2 , maxDist ) NEW_LINE dist = 0 NEW_LINE DEDENT else : NEW_LINE INDENT dist += 1 NEW_LINE DEDENT DEDENT return max ( maxDist , dist ) NEW_LINE DEDENT / * Driver code * / NEW_LINE numOfCities = 6 NEW_LINE station = [ 3 , 1 ] NEW_LINE print ( " Max ▁ Distance : " , findMaxDistance ( numOfCities , station ) ) NEW_LINE
Split the number into N parts such that difference between the smallest and the largest part is minimum | Function that prints the required sequence ; If we cannot split the number into exactly ' N ' parts ; If x % n == 0 then the minimum difference is 0 and all numbers are x / n ; upto n - ( x % n ) the values will be x / n after that the values will be x / n + 1 ; Driver code
def split ( x , n ) : NEW_LINE INDENT if ( x < n ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT elif ( x % n == 0 ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( x // n , end = " ▁ " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT zp = n - ( x % n ) NEW_LINE pp = x // n NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i >= zp ) : NEW_LINE INDENT print ( pp + 1 , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( pp , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT x = 5 NEW_LINE n = 3 NEW_LINE split ( x , n ) NEW_LINE
Minimum time to reach a point with + t and | returns the minimum time required to reach 'X ; Stores the minimum time ; increment ' t ' by 1 ; update the sum ; Driver code
' NEW_LINE def cal_minimum_time ( X ) : NEW_LINE INDENT t = 0 NEW_LINE sum = 0 NEW_LINE while ( sum < X ) : NEW_LINE INDENT t = t + 1 NEW_LINE sum = sum + t ; NEW_LINE DEDENT return t ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 6 NEW_LINE ans = cal_minimum_time ( n ) NEW_LINE print ( " The ▁ minimum ▁ time ▁ required ▁ is ▁ : " , ans ) NEW_LINE DEDENT
Check if a string can be rearranged to form special palindrome | Driver code ; creating a list which stores the frequency of each character ; Checking if a character is uppercase or not ; Increasing by 1 if uppercase ; Decreasing by 1 if lower case ; Storing the sum of positive numbers in the frequency array ; Storing the sum of negative numbers in the frequency array ; If all character balances out then its Yes ; If there is only 1 character which does not balances then also it is Yes
s = " ABCdcba " NEW_LINE u = [ 0 ] * 26 NEW_LINE n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] . isupper ( ) ) : NEW_LINE INDENT u [ ord ( s [ i ] ) - 65 ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT u [ ord ( s [ i ] ) - 97 ] -= 1 NEW_LINE DEDENT DEDENT fl = True NEW_LINE po = 0 NEW_LINE ne = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( u [ i ] > 0 ) : NEW_LINE INDENT po += u [ i ] NEW_LINE DEDENT if ( u [ i ] < 0 ) : NEW_LINE INDENT ne += u [ i ] NEW_LINE DEDENT DEDENT if ( po == 0 and ne == 0 ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT elif ( po == 1 and ne == 0 ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT elif ( po == 0 and ne == - 1 ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Sum of minimum difference between consecutive elements of an array | Utility pair ; function to find minimum sum of difference of consecutive element ; ul to store upper limit ll to store lower limit ; storethe lower range in ll and upper range in ul ; initialize the answer with 0 ; iterate for all ranges ; case 1 , in this case the difference will be 0 ; change upper limit and lower limit ; case 2 ; store the difference ; case 3 ; store the difference ; array of range
class pair : NEW_LINE INDENT first = 0 NEW_LINE second = 0 NEW_LINE def __init__ ( self , a , b ) : NEW_LINE INDENT self . first = a NEW_LINE self . second = b NEW_LINE DEDENT DEDENT def solve ( v , n ) : NEW_LINE INDENT ans = 0 ; ul = 0 ; ll = 0 ; NEW_LINE ll = v [ 0 ] . first NEW_LINE ul = v [ 0 ] . second NEW_LINE ans = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( v [ i ] . first <= ul and v [ i ] . first >= ll ) or ( v [ i ] . second >= ll and v [ i ] . second <= ul ) : NEW_LINE INDENT if v [ i ] . first > ll : NEW_LINE INDENT ll = v [ i ] . first NEW_LINE DEDENT if v [ i ] . second < ul : NEW_LINE INDENT ul = v [ i ] . second ; NEW_LINE DEDENT DEDENT elif v [ i ] . first > ul : NEW_LINE INDENT ans += abs ( ul - v [ i ] . first ) NEW_LINE ul = v [ i ] . first NEW_LINE ll = v [ i ] . first NEW_LINE DEDENT elif v [ i ] . second < ll : NEW_LINE INDENT ans += abs ( ll - v [ i ] . second ) ; NEW_LINE ul = v [ i ] . second ; NEW_LINE ll = v [ i ] . second ; NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT v = [ pair ( 1 , 3 ) , pair ( 2 , 5 ) , pair ( 6 , 8 ) , pair ( 1 , 2 ) , pair ( 2 , 3 ) ] NEW_LINE n = len ( v ) NEW_LINE print ( solve ( v , n ) ) NEW_LINE
Find the Largest Cube formed by Deleting minimum Digits from a number | Python3 code to implement maximum perfect cube formed after deleting minimum digits ; Returns vector of Pre Processed perfect cubes ; convert the cube to string and push into preProcessedCubes vector ; Utility function for findLargestCube ( ) . Returns the Largest cube number that can be formed ; reverse the preProcessed cubes so that we have the largest cube in the beginning of the vector ; iterate over all cubes ; check if the current digit of the cube matches with that of the number num ; if control reaches here , the its not possible to form a perfect cube ; wrapper for findLargestCubeUtil ( ) ; pre process perfect cubes ; convert number n to String ; Driver Code
import math as mt NEW_LINE def preProcess ( n ) : NEW_LINE INDENT preProcessedCubes = list ( ) NEW_LINE for i in range ( 1 , mt . ceil ( n ** ( 1. / 3. ) ) ) : NEW_LINE INDENT iThCube = i ** 3 NEW_LINE cubeString = str ( iThCube ) NEW_LINE preProcessedCubes . append ( cubeString ) NEW_LINE DEDENT return preProcessedCubes NEW_LINE DEDENT def findLargestCubeUtil ( num , preProcessedCubes ) : NEW_LINE INDENT preProcessedCubes = preProcessedCubes [ : : - 1 ] NEW_LINE totalCubes = len ( preProcessedCubes ) NEW_LINE for i in range ( totalCubes ) : NEW_LINE INDENT currCube = preProcessedCubes [ i ] NEW_LINE digitsInCube = len ( currCube ) NEW_LINE index = 0 NEW_LINE digitsInNumber = len ( num ) NEW_LINE for j in range ( digitsInNumber ) : NEW_LINE INDENT if ( num [ j ] == currCube [ index ] ) : NEW_LINE INDENT index += 1 NEW_LINE DEDENT if ( digitsInCube == index ) : NEW_LINE INDENT return currCube NEW_LINE DEDENT DEDENT DEDENT return " Not ▁ Possible " NEW_LINE DEDENT def findLargestCube ( n ) : NEW_LINE INDENT preProcessedCubes = preProcess ( n ) NEW_LINE num = str ( n ) NEW_LINE ans = findLargestCubeUtil ( num , preProcessedCubes ) NEW_LINE print ( " Largest ▁ Cube ▁ that ▁ can ▁ be ▁ formed ▁ from " , n , " is " , ans ) NEW_LINE DEDENT n = 4125 NEW_LINE findLargestCube ( n ) NEW_LINE n = 876 NEW_LINE findLargestCube ( n ) NEW_LINE
Water Connection Problem | number of houses and number of pipes ; Array rd stores the ending vertex of pipe ; Array wd stores the value of diameters between two pipes ; Array cd stores the starting end of pipe ; List a , b , c are used to store the final output ; Function performing calculations . ; If a pipe has no ending vertex but has starting vertex i . e is an outgoing pipe then we need to start DFS with this vertex . ; We put the details of component in final output array ; main function ; set the value of the araray to zero
n = 0 NEW_LINE p = 0 NEW_LINE rd = [ 0 ] * 1100 NEW_LINE wt = [ 0 ] * 1100 NEW_LINE cd = [ 0 ] * 1100 NEW_LINE a = [ ] NEW_LINE b = [ ] NEW_LINE c = [ ] NEW_LINE ans = 0 NEW_LINE def dfs ( w ) : NEW_LINE INDENT global ans NEW_LINE if ( cd [ w ] == 0 ) : NEW_LINE INDENT return w NEW_LINE DEDENT if ( wt [ w ] < ans ) : NEW_LINE INDENT ans = wt [ w ] NEW_LINE DEDENT return dfs ( cd [ w ] ) NEW_LINE DEDENT def solve ( arr ) : NEW_LINE INDENT global ans NEW_LINE i = 0 NEW_LINE while ( i < p ) : NEW_LINE INDENT q = arr [ i ] [ 0 ] NEW_LINE h = arr [ i ] [ 1 ] NEW_LINE t = arr [ i ] [ 2 ] NEW_LINE cd [ q ] = h NEW_LINE wt [ q ] = t NEW_LINE rd [ h ] = q NEW_LINE i += 1 NEW_LINE DEDENT a = [ ] NEW_LINE b = [ ] NEW_LINE c = [ ] NEW_LINE for j in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( rd [ j ] == 0 and cd [ j ] ) : NEW_LINE INDENT ans = 1000000000 NEW_LINE w = dfs ( j ) NEW_LINE a . append ( j ) NEW_LINE b . append ( w ) NEW_LINE c . append ( ans ) NEW_LINE DEDENT DEDENT print ( len ( a ) ) NEW_LINE for j in range ( len ( a ) ) : NEW_LINE INDENT print ( a [ j ] , b [ j ] , c [ j ] ) NEW_LINE DEDENT DEDENT n = 9 NEW_LINE p = 6 NEW_LINE arr = [ [ 7 , 4 , 98 ] , [ 5 , 9 , 72 ] , [ 4 , 6 , 10 ] , [ 2 , 8 , 22 ] , [ 9 , 7 , 17 ] , [ 3 , 1 , 66 ] ] NEW_LINE solve ( arr ) NEW_LINE
Print a closest string that does not contain adjacent duplicates | Function to print simple string ; If any two adjacent characters are equal ; Initialize it to ' a ' ; Traverse the loop until it is different from the left and right letter . ; Driver Function
def noAdjacentDup ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( s [ i ] == s [ i - 1 ] ) : NEW_LINE INDENT s [ i ] = " a " NEW_LINE while ( s [ i ] == s [ i - 1 ] or ( i + 1 < n and s [ i ] == s [ i + 1 ] ) ) : NEW_LINE INDENT s [ i ] += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT return s NEW_LINE DEDENT s = list ( " geeksforgeeks " ) NEW_LINE print ( " " . join ( noAdjacentDup ( s ) ) ) NEW_LINE
Array element moved by k using single moves | Python3 code to find winner of game ; if the number of steps is more then n - 1 ; initially the best is 0 and no of wins is 0. ; traverse through all the numbers ; if the value of array is more then that of previous best ; best is replaced by a [ i ] ; if not the first index ; no of wins is 1 now ; if any position has more then k wins then return ; Maximum element will be winner because we move smaller element at end and repeat the process . ; driver code
def winner ( a , n , k ) : NEW_LINE INDENT if k >= n - 1 : NEW_LINE INDENT return n NEW_LINE DEDENT best = 0 NEW_LINE times = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if a [ i ] > best : NEW_LINE INDENT best = a [ i ] NEW_LINE if i == True : NEW_LINE INDENT times = 1 NEW_LINE DEDENT DEDENT else : NEW_LINE if times >= k : NEW_LINE INDENT return best NEW_LINE DEDENT DEDENT return best NEW_LINE DEDENT a = [ 2 , 1 , 3 , 4 , 5 ] NEW_LINE n = len ( a ) NEW_LINE k = 2 NEW_LINE print ( winner ( a , n , k ) ) NEW_LINE
Paper Cut into Minimum Number of Squares | Returns min number of squares needed ; swap if a is small size side . ; Iterate until small size side is greater then 0 ; Update result ; Driver code
def minimumSquare ( a , b ) : NEW_LINE INDENT result = 0 NEW_LINE rem = 0 NEW_LINE if ( a < b ) : NEW_LINE INDENT a , b = b , a NEW_LINE DEDENT while ( b > 0 ) : NEW_LINE INDENT result += int ( a / b ) NEW_LINE rem = int ( a % b ) NEW_LINE a = b NEW_LINE b = rem NEW_LINE DEDENT return result NEW_LINE DEDENT n = 13 NEW_LINE m = 29 NEW_LINE print ( minimumSquare ( n , m ) ) NEW_LINE
Count of non decreasing Arrays with ith element in range [ A [ i ] , B [ i ] ] | Function to count the total number of possible valid arrays ; Make a 2D DP table ; Make a 2D prefix sum table ;  ; Base Case ; Initialize the prefix values ; Iterate over the range and update the dp table accordingly ; Add the dp values to the prefix sum ; Update the prefix sum table ; Find the result count of arrays formed ; Return the total count of arrays ; Driver Code
def totalValidArrays ( a , b , N ) : NEW_LINE INDENT dp = [ [ 0 for _ in range ( b [ N - 1 ] + 1 ) ] for _ in range ( N + 1 ) ] NEW_LINE pref = [ [ 0 for _ in range ( b [ N - 1 ] + 1 ) ] for _ in range ( N + 1 ) ] NEW_LINE DEDENT / * Initialize all values to 0 * / NEW_LINE INDENT dp [ 0 ] [ 0 ] = 1 NEW_LINE for i in range ( 0 , b [ N - 1 ] + 1 ) : NEW_LINE INDENT pref [ 0 ] [ i ] = 1 NEW_LINE DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( a [ i - 1 ] , b [ i - 1 ] + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] += pref [ i - 1 ] [ j ] NEW_LINE pref [ i ] [ j ] += dp [ i ] [ j ] NEW_LINE DEDENT for j in range ( 0 , b [ N - 1 ] + 1 ) : NEW_LINE INDENT if ( j > 0 ) : NEW_LINE INDENT pref [ i ] [ j ] += pref [ i ] [ j - 1 ] NEW_LINE DEDENT DEDENT DEDENT ans = 0 NEW_LINE for i in range ( a [ N - 1 ] , b [ N - 1 ] + 1 ) : NEW_LINE INDENT ans += dp [ N ] [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 1 ] NEW_LINE B = [ 2 , 3 ] NEW_LINE N = len ( A ) NEW_LINE print ( totalValidArrays ( A , B , N ) ) NEW_LINE DEDENT
Count of integers in range [ L , R ] having even frequency of each digit | Stores the upper limit of the range ; Stores the overlapping states ; Recursive Function to calculate the count of valid integers in the range [ 1 , s ] using memoization ; Base Case ; If current integer has even count of digits and is not repeated ; If current state is already considered ; Stores the maximum valid digit at the current index ; Stores the count of valid integers ; If the current digit is not the most significant digit , i . e , the integer is already started ; Iterate through all valid digits ; Recursive call for ith digit at the current index ; Recursive call for integers having leading zeroes in the beginning ; Iterate through all valid digits as most significant digits ; Recursive call for ith digit at the current index ; Return answer ; Function to calculate valid number in the range [ 1 , X ] ; Initialize dp array with - 1 ; Store the range in form of string ; Return Count ; Function to find the count of integers in the range [ L , R ] such that the frequency of each digit is even ; Driver Code
s = " " NEW_LINE dp = [ [ [ [ 0 for _ in range ( 2 ) ] for _ in range ( 2 ) ] for _ in range ( 10 ) ] for _ in range ( 1024 ) ] NEW_LINE def calcCnt ( mask , sz , smaller , started ) : NEW_LINE INDENT if ( sz == len ( s ) ) : NEW_LINE INDENT return ( mask == 0 and started ) NEW_LINE DEDENT if ( dp [ mask ] [ sz ] [ smaller ] [ started ] != - 1 ) : NEW_LINE INDENT return dp [ mask ] [ sz ] [ smaller ] [ started ] NEW_LINE DEDENT mx = 9 NEW_LINE if ( not smaller ) : NEW_LINE INDENT mx = ord ( s [ sz ] ) - ord ( '0' ) NEW_LINE DEDENT ans = 0 NEW_LINE if ( started ) : NEW_LINE INDENT for i in range ( 0 , mx + 1 ) : NEW_LINE INDENT ans += calcCnt ( mask ^ ( 1 << i ) , sz + 1 , smaller or ( i < ord ( s [ sz ] ) - ord ( '0' ) ) , 1 ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT ans = calcCnt ( mask , sz + 1 , 1 , 0 ) NEW_LINE for i in range ( 1 , mx + 1 ) : NEW_LINE INDENT ans += calcCnt ( mask ^ ( 1 << i ) , sz + 1 , smaller or ( i < ord ( s [ sz ] ) - ord ( '0' ) ) , 1 ) NEW_LINE DEDENT DEDENT dp [ mask ] [ sz ] [ smaller ] [ started ] = ans NEW_LINE return dp [ mask ] [ sz ] [ smaller ] [ started ] NEW_LINE DEDENT def countInt ( x ) : NEW_LINE INDENT global dp NEW_LINE dp = [ [ [ [ - 1 for _ in range ( 2 ) ] for _ in range ( 2 ) ] for _ in range ( 10 ) ] for _ in range ( 1024 ) ] NEW_LINE global s NEW_LINE s = str ( x ) NEW_LINE return calcCnt ( 0 , 0 , 0 , 0 ) NEW_LINE DEDENT def countIntInRange ( L , R ) : NEW_LINE INDENT return countInt ( R ) - countInt ( L - 1 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT L = 32 NEW_LINE R = 1010 NEW_LINE print ( countIntInRange ( L , R ) ) NEW_LINE DEDENT