codeparrot/xlcost-text-to-code huggingface dataset:
Count ways to remove pairs from a matrix such that remaining elements can be grouped in vertical or horizontal pairs | Function to count ways to remove pairs such that the remaining elements can be arranged in pairs vertically or horizontally ; Store the size of matrix ; If N is odd , then no such pair exists ; Store the number of required pairs ; Initialize an auxiliary matrix and fill it with 0 s ; Traverse the matrix v [ ] [ ] ; Check if i + j is odd or even ; Increment the value dp [ v [ i ] [ j ] - 1 ] [ 0 ] by 1 ; Increment the value dp [ v [ i ] [ j ] - 1 ] [ 1 ] by 1 ; Iterate in range [ 0 , k - 1 ] using i ; Iterate in range [ i + 1 , k - 1 ] using j ; Update the ans ; Print the answer ; Driver code
def numberofpairs ( v , k ) : NEW_LINE INDENT n = len ( v ) NEW_LINE if ( n % 2 == 1 ) : NEW_LINE INDENT print ( 0 ) NEW_LINE return NEW_LINE DEDENT ans = 0 NEW_LINE dp = [ [ 0 for i in range ( 2 ) ] for j in range ( k ) ] NEW_LINE for i in range ( k ) : NEW_LINE INDENT for j in range ( 2 ) : NEW_LINE INDENT dp [ i ] [ j ] = 0 NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( ( i + j ) % 2 == 0 ) : NEW_LINE INDENT dp [ v [ i ] [ j ] - 1 ] [ 0 ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT dp [ v [ i ] [ j ] - 1 ] [ 1 ] += 1 NEW_LINE DEDENT DEDENT DEDENT for i in range ( k ) : NEW_LINE INDENT for j in range ( i + 1 , k ) : NEW_LINE INDENT ans += dp [ i ] [ 0 ] * dp [ j ] [ 1 ] NEW_LINE ans += dp [ i ] [ 1 ] * dp [ j ] [ 0 ] NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT mat = [ [ 1 , 2 ] , [ 3 , 4 ] ] NEW_LINE K = 4 NEW_LINE numberofpairs ( mat , K ) NEW_LINE
Maximize sum by selecting X different | Python3 program for the above approach ; Store overlapping subproblems of the recurrence relation ; Function to find maximum sum of at most N with different index array elements such that at most X are from A [ ] , Y are from B [ ] and Z are from C [ ] ; Base Cases ; If the subproblem already computed ; Selecting i - th element from A [ ] ; Selecting i - th element from B [ ] ; Selecting i - th element from C [ ] ; i - th elements not selected from any of the arrays ; Select the maximum sum from all the possible calls ; Driver Code ; Given X , Y and Z ; Given A [ ] ; Given B [ ] ; Given C [ ] ; Given Size ; Function Call
import sys NEW_LINE dp = [ [ [ [ - 1 for i in range ( 50 ) ] for j in range ( 50 ) ] for k in range ( 50 ) ] for l in range ( 50 ) ] NEW_LINE def FindMaxS ( X , Y , Z , n , A , B , C ) : NEW_LINE INDENT if ( X < 0 or Y < 0 or Z < 0 ) : NEW_LINE INDENT return - sys . maxsize - 1 NEW_LINE DEDENT if ( n < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ n ] [ X ] [ Y ] [ Z ] != - 1 ) : NEW_LINE INDENT return dp [ n ] [ X ] [ Y ] [ Z ] NEW_LINE DEDENT ch = A [ n ] + FindMaxS ( X - 1 , Y , Z , n - 1 , A , B , C ) NEW_LINE ca = B [ n ] + FindMaxS ( X , Y - 1 , Z , n - 1 , A , B , C ) NEW_LINE co = C [ n ] + FindMaxS ( X , Y , Z - 1 , n - 1 , A , B , C ) NEW_LINE no = FindMaxS ( X , Y , Z , n - 1 , A , B , C ) NEW_LINE maximum = max ( ch , max ( ca , max ( co , no ) ) ) NEW_LINE dp [ n ] [ X ] [ Y ] [ Z ] = maximum NEW_LINE return dp [ n ] [ X ] [ Y ] [ Z ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 1 NEW_LINE Y = 1 NEW_LINE Z = 1 NEW_LINE A = [ 10 , 0 , 5 ] NEW_LINE B = [ 5 , 10 , 0 ] NEW_LINE C = [ 0 , 5 , 10 ] NEW_LINE n = len ( B ) NEW_LINE print ( FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ) NEW_LINE DEDENT
Minimize swaps of adjacent characters to sort every possible rearrangement of given Binary String | Python3 program for the above approach ; Precalculate the values of power of 2 ; Function to calculate 2 ^ N % mod ; Function to find sum of inversions ; Initialise a list of 0 s and ? s ; Traverse the string in the reversed manner ; If the current character is 1 ; Effectively calculate a * b ^ ( b - 1 ) ; If the current character is 0 ; Increment count of zeroes ; Double count the zeroes ; Find b * 2 ^ ( b - 1 ) ; Increment count of questions ; Return the final count ; Driver Code ; Given string S ; Function Call
MOD = 10 ** 9 + 7 NEW_LINE MEM = [ 1 , 2 , 4 , 8 , 16 , 32 , 64 , 128 , 256 , 512 , 1024 , 2048 , 4096 ] NEW_LINE def mod_pow2 ( n ) : NEW_LINE INDENT while n >= len ( MEM ) : NEW_LINE INDENT MEM . append ( ( MEM [ - 1 ] * 2 ) % MOD ) NEW_LINE DEDENT return MEM [ n ] NEW_LINE DEDENT def inversions ( bstr ) : NEW_LINE INDENT total , zeros , questions = ( 0 , ) * 3 NEW_LINE for x in reversed ( bstr ) : NEW_LINE INDENT if x == '1' : NEW_LINE INDENT z = zeros * mod_pow2 ( questions ) NEW_LINE if questions == 0 : NEW_LINE INDENT q = 0 NEW_LINE DEDENT else : NEW_LINE INDENT q = questions * mod_pow2 ( questions - 1 ) NEW_LINE DEDENT total = ( total + z + q ) % MOD NEW_LINE DEDENT elif x == '0' : NEW_LINE INDENT zeros += 1 NEW_LINE DEDENT else : NEW_LINE INDENT total *= 2 NEW_LINE z = zeros * mod_pow2 ( questions ) NEW_LINE if questions == 0 : NEW_LINE INDENT q = 0 NEW_LINE DEDENT else : NEW_LINE INDENT q = questions * mod_pow2 ( questions - 1 ) NEW_LINE DEDENT total = ( total + z + q ) % MOD NEW_LINE questions += 1 NEW_LINE DEDENT DEDENT return total NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT S = " ? 0 ? " NEW_LINE print ( inversions ( S ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT main ( ) NEW_LINE DEDENT
Minimum removals required to convert given array to a Mountain Array | Utility function to count array elements required to be removed to make array a mountain array ; Stores length of increasing subsequence from [ 0 , i - 1 ] ; Stores length of increasing subsequence from [ i + 1 , n - 1 ] ; Iterate for each position up to N - 1 to find the length of subsequence ; If j is less than i , then i - th position has leftIncreasing [ j ] + 1 lesser elements including itself ; Check if it is the maximum obtained so far ; Search for increasing subsequence from right ; Find the position following the peak condition and have maximum leftIncreasing [ i ] + rightIncreasing [ i ] ; Function to count elements to be removed to make array a mountain array ; Print the answer ; Driver Code ; Given array ; Function Call
def minRemovalsUtil ( arr ) : NEW_LINE INDENT result = 0 NEW_LINE if ( len ( arr ) < 3 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT leftIncreasing = [ 0 ] * len ( arr ) NEW_LINE rightIncreasing = [ 0 ] * len ( arr ) NEW_LINE for i in range ( 1 , len ( arr ) ) : NEW_LINE INDENT for j in range ( i ) : NEW_LINE INDENT if ( arr [ j ] < arr [ i ] ) : NEW_LINE INDENT leftIncreasing [ i ] = max ( leftIncreasing [ i ] , leftIncreasing [ j ] + 1 ) ; NEW_LINE DEDENT DEDENT DEDENT for i in range ( len ( arr ) - 2 , - 1 , - 1 ) : NEW_LINE INDENT j = len ( arr ) - 1 NEW_LINE while j > i : NEW_LINE INDENT if ( arr [ j ] < arr [ i ] ) : NEW_LINE INDENT rightIncreasing [ i ] = max ( rightIncreasing [ i ] , rightIncreasing [ j ] + 1 ) NEW_LINE DEDENT j -= 1 NEW_LINE DEDENT DEDENT for i in range ( len ( arr ) ) : NEW_LINE INDENT if ( leftIncreasing [ i ] != 0 and rightIncreasing [ i ] != 0 ) : NEW_LINE INDENT result = max ( result , leftIncreasing [ i ] + rightIncreasing [ i ] ) ; NEW_LINE DEDENT DEDENT return len ( arr ) - ( result + 1 ) NEW_LINE DEDENT def minRemovals ( arr ) : NEW_LINE INDENT ans = minRemovalsUtil ( arr ) NEW_LINE print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 1 , 1 , 5 , 6 , 2 , 3 , 1 ] NEW_LINE minRemovals ( arr ) NEW_LINE DEDENT
Minimum number of jumps to obtain an element of opposite parity | Bfs for odd numbers are source ; Initialize queue ; Stores for each node , the nodes visited and their distances ; If parity is 0 -> odd Otherwise -> even ; Perform multi - source bfs ; Extract the front element of the queue ; Traverse nodes connected to the current node ; If u is not visited ; If element with opposite parity is obtained ; Store its distance from source in ans [ ] ; Push the current neighbour to the queue ; Function to find the minimum jumps required by each index to reach element of opposite parity ; Initialise Inverse Graph ; Stores the result for each index ; For the jumped index ; If the ind is valid then add reverse directed edge ; Multi - source bfs with odd numbers as source by passing 0 ; Multi - source bfs with even numbers as source by passing 1 ; Print the answer ; Driver Code
def bfs ( n , a , invGr , ans , parity ) : NEW_LINE INDENT q = [ ] NEW_LINE vis = [ 0 for i in range ( n + 1 ) ] NEW_LINE dist = [ 0 for i in range ( n + 1 ) ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( ( a [ i ] + parity ) & 1 ) : NEW_LINE INDENT q . append ( i ) NEW_LINE vis [ i ] = 1 NEW_LINE DEDENT DEDENT while ( len ( q ) != 0 ) : NEW_LINE INDENT v = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE for u in invGr [ v ] : NEW_LINE INDENT if ( not vis [ u ] ) : NEW_LINE INDENT dist [ u ] = dist [ v ] + 1 NEW_LINE vis [ u ] = 1 NEW_LINE if ( ( a [ u ] + parity ) % 2 == 0 ) : NEW_LINE INDENT if ( ans [ u ] == - 1 ) : NEW_LINE INDENT ans [ u ] = dist [ u ] NEW_LINE DEDENT DEDENT q . append ( u ) NEW_LINE DEDENT DEDENT DEDENT DEDENT def minJumps ( a , jump , n ) : NEW_LINE INDENT invGr = [ [ ] for i in range ( n + 1 ) ] NEW_LINE ans = [ - 1 for i in range ( n + 1 ) ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for ind in [ i + jump [ i ] , i - jump [ i ] ] : NEW_LINE INDENT if ( ind >= 1 and ind <= n ) : NEW_LINE INDENT invGr [ ind ] . append ( i ) NEW_LINE DEDENT DEDENT DEDENT bfs ( n , a , invGr , ans , 0 ) NEW_LINE bfs ( n , a , invGr , ans , 1 ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( str ( ans [ i ] ) , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 4 , 2 , 5 , 2 , 1 ] NEW_LINE jump = [ 0 , 1 , 2 , 3 , 1 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE minJumps ( arr , jump , N - 1 ) NEW_LINE DEDENT
Longest subsequence having maximum GCD between any pair of distinct elements | Python3 program for the above approach ; Function to find GCD of pair with maximum GCD ; Stores maximum element of arr [ ] ; Find the maximum element ; Maintain a count array ; Store the frequency of arr [ ] ; Stores the multiples of a number ; Iterate over the range [ MAX , 1 ] ; Iterate from current potential GCD till it is less than MAX ; A multiple found ; Increment potential GCD by itself io check i , 2 i , 3 i ... ; If 2 multiples found max GCD found ; Function to find longest subsequence such that GCD of any two distinct integers is maximum ; Base Cases ; Compare current GCD to the maximum GCD ; If true increment and move the pointer ; Return max of either subsequences ; Drivers Code ; Sorted array ; Function call to calculate GCD of pair with maximum GCD ; Print the result
import math NEW_LINE def findMaxGCD ( arr , N ) : NEW_LINE INDENT high = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT high = max ( high , arr [ i ] ) NEW_LINE DEDENT count = [ 0 ] * ( high + 1 ) NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT count [ arr [ i ] ] += 1 NEW_LINE DEDENT counter = 0 NEW_LINE for i in range ( high , 0 , - 1 ) : NEW_LINE INDENT j = i NEW_LINE while ( j <= high ) : NEW_LINE INDENT if ( count [ j ] > 0 ) : NEW_LINE INDENT counter += count [ j ] NEW_LINE DEDENT j += i NEW_LINE if ( counter == 2 ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT counter = 0 NEW_LINE DEDENT DEDENT def maxlen ( i , j ) : NEW_LINE INDENT a = 0 NEW_LINE if i >= N or j >= N : NEW_LINE INDENT return 0 NEW_LINE DEDENT if math . gcd ( arr [ i ] , arr1 [ j ] ) == maxgcd and arr [ i ] != arr1 [ j ] : NEW_LINE INDENT a = max ( a , 1 + maxlen ( i , j + 1 ) ) NEW_LINE return a NEW_LINE DEDENT return max ( maxlen ( i + 1 , j ) , maxlen ( i , j + 1 ) ) NEW_LINE DEDENT arr = [ 1 , 2 , 8 , 5 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE arr1 = sorted ( arr ) NEW_LINE maxgcd = findMaxGCD ( arr , N ) NEW_LINE print ( maxlen ( 0 , 0 ) ) NEW_LINE
Count unique paths is a matrix whose product of elements contains odd number of divisors | Python3 program for the above approach ; Stores the results ; Count of unique product paths ; Function to check whether number is perfect square or not ; If square root is an integer ; Function to calculate and store all the paths product in vector ; Store the value a [ 0 ] [ 0 ] ; Initialize first row of dp ; Find prefix product ; Initialize first column of dp ; Find the prefix product ; Iterate over range ( 1 , 1 ) to ( N , M ) ; Copy dp [ i - 1 ] [ j ] in top [ ] ; Copy dp [ i ] [ j - 1 ] into left [ ] ; Compute the values of current state and store it in curr [ ] ; Find the product of a [ i ] [ j ] with elements at top [ ] ; Find the product of a [ i ] [ j ] with elements at left [ ] ; Update the current state ; Traverse dp [ m - 1 ] [ n - 1 ] ; Check if perfect square ; Driver Code ; Given matrix mat [ ] [ ] ; Function Call ; Print the final count
from math import sqrt , floor NEW_LINE dp = [ [ [ ] for j in range ( 105 ) ] for k in range ( 105 ) ] NEW_LINE countPaths = 0 NEW_LINE def isPerfectSquare ( n ) : NEW_LINE INDENT sr = sqrt ( n ) NEW_LINE return ( ( sr - floor ( sr ) ) == 0 ) NEW_LINE DEDENT def countUniquePaths ( a , m , n , ans ) : NEW_LINE INDENT global dp NEW_LINE global countPaths NEW_LINE dp [ 0 ] [ 0 ] . append ( a [ 0 ] [ 0 ] ) NEW_LINE for i in range ( 1 , m ) : NEW_LINE INDENT a [ i ] [ 0 ] *= a [ i - 1 ] [ 0 ] NEW_LINE dp [ i ] [ 0 ] . append ( a [ i ] [ 0 ] ) NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT a [ 0 ] [ i ] *= a [ 0 ] [ i - 1 ] NEW_LINE dp [ 0 ] [ i ] . append ( a [ 0 ] [ i ] ) NEW_LINE DEDENT for i in range ( 1 , m ) : NEW_LINE INDENT for j in range ( 1 , n ) : NEW_LINE INDENT top = dp [ i - 1 ] [ j ] NEW_LINE left = dp [ i ] [ j - 1 ] NEW_LINE curr = [ ] NEW_LINE for k in range ( len ( top ) ) : NEW_LINE INDENT curr . append ( top [ k ] * a [ i ] [ j ] ) NEW_LINE DEDENT for k in range ( len ( left ) ) : NEW_LINE INDENT curr . append ( left [ k ] * a [ i ] [ j ] ) NEW_LINE DEDENT dp [ i ] [ j ] = curr NEW_LINE DEDENT DEDENT for i in dp [ m - 1 ] [ n - 1 ] : NEW_LINE INDENT if ( isPerfectSquare ( i ) ) : NEW_LINE INDENT countPaths += 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT M = 3 NEW_LINE N = 4 NEW_LINE mat = [ [ 1 , 2 , 3 , 1 ] , [ 3 , 1 , 2 , 4 ] , [ 2 , 3 , 1 , 1 ] ] NEW_LINE countUniquePaths ( mat , M , N , 1 ) NEW_LINE print ( countPaths ) NEW_LINE DEDENT
Generate a combination of minimum coins that sums to a given value | Python3 program for the above approach ; dp array to memoize the results ; List to store the result ; Function to find the minimum number of coins to make the sum equals to X ; Base case ; If previously computed subproblem occurred ; Initialize result ; Try every coin that has smaller value than n ; Check for INT_MAX to avoid overflow and see if result . an be minimized ; Memoizing value of current state ; Function to find the possible combination of coins to make the sum equal to X ; Base Case ; Print Solutions ; Try every coin that has value smaller than n ; Add current denominations ; Backtrack ; Function to find the minimum combinations of coins for value X ; Min coins ; If no solution exists ; Backtrack to find the solution ; Driver code ; Set of possible denominations ; Function call
import sys NEW_LINE MAX = 100000 NEW_LINE dp = [ - 1 ] * ( MAX + 1 ) NEW_LINE denomination = [ ] NEW_LINE def countMinCoins ( n , C , m ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT dp [ 0 ] = 0 NEW_LINE return 0 NEW_LINE DEDENT if ( dp [ n ] != - 1 ) : NEW_LINE INDENT return dp [ n ] NEW_LINE DEDENT ret = sys . maxsize NEW_LINE for i in range ( m ) : NEW_LINE INDENT if ( C [ i ] <= n ) : NEW_LINE INDENT x = countMinCoins ( n - C [ i ] , C , m ) NEW_LINE if ( x != sys . maxsize ) : NEW_LINE INDENT ret = min ( ret , 1 + x ) NEW_LINE DEDENT DEDENT DEDENT dp [ n ] = ret NEW_LINE return ret NEW_LINE DEDENT def findSolution ( n , C , m ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT for it in denomination : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT return NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT if ( n - C [ i ] >= 0 and dp [ n - C [ i ] ] + 1 == dp [ n ] ) : NEW_LINE INDENT denomination . append ( C [ i ] ) NEW_LINE findSolution ( n - C [ i ] , C , m ) NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT def countMinCoinsUtil ( X , C , N ) : NEW_LINE INDENT isPossible = countMinCoins ( X , C , N ) NEW_LINE if ( isPossible == sys . maxsize ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT findSolution ( X , C , N ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 21 NEW_LINE arr = [ 2 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE countMinCoinsUtil ( X , arr , N ) NEW_LINE DEDENT
Count all possible paths from top left to bottom right of a Matrix without crossing the diagonal | Function to calculate Binomial Coefficient C ( n , r ) ; C ( n , r ) = C ( n , n - r ) ; [ n * ( n - 1 ) * -- - * ( n - r + 1 ) ] / [ r * ( r - 1 ) * -- -- * 1 ] ; Function to calculate the total possible paths ; Update n to n - 1 ; Stores 2 nCn ; Stores Nth Catalan number ; Stores the required answer ; Driver Code
def binCoff ( n , r ) : NEW_LINE INDENT val = 1 NEW_LINE if ( r > ( n - r ) ) : NEW_LINE INDENT r = ( n - r ) NEW_LINE DEDENT for i in range ( r ) : NEW_LINE INDENT val *= ( n - i ) NEW_LINE val //= ( i + 1 ) NEW_LINE DEDENT return val NEW_LINE DEDENT def findWays ( n ) : NEW_LINE INDENT n = n - 1 NEW_LINE a = binCoff ( 2 * n , n ) NEW_LINE b = a // ( n + 1 ) NEW_LINE ans = b NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE print ( findWays ( n ) ) NEW_LINE DEDENT
Minimum operations to transform given string to another by moving characters to front or end | Python3 program for the above approach ; Function that finds the minimum number of steps to find the minimum characters must be moved to convert string s to t ; r = maximum value over all dp [ i ] [ j ] computed so far ; dp [ i ] [ j ] stores the longest contiguous suffix of T [ 0. . j ] that is subsequence of S [ 0. . i ] ; Update the maximum length ; Return the resulting length ; Given string s , t ; Function call
dp = [ [ 0 ] * 1010 ] * 1010 NEW_LINE def solve ( s , t ) : NEW_LINE INDENT n = len ( s ) NEW_LINE r = 0 NEW_LINE for j in range ( 0 , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT dp [ i ] [ j ] = 0 NEW_LINE if ( i > 0 ) : NEW_LINE INDENT dp [ i ] [ j ] = max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j ] ) NEW_LINE DEDENT if ( s [ i ] == t [ j ] ) : NEW_LINE INDENT ans = 1 NEW_LINE if ( i > 0 and j > 0 ) : NEW_LINE INDENT ans = 1 + dp [ i - 1 ] [ j - 1 ] NEW_LINE DEDENT dp [ i ] [ j ] = max ( dp [ i ] [ j ] , ans ) NEW_LINE r = max ( r , dp [ i ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT return ( n - r ) NEW_LINE DEDENT s = " abcde " NEW_LINE t = " edacb " NEW_LINE print ( solve ( s , t ) ) NEW_LINE
Longest substring whose characters can be rearranged to form a Palindrome | Function to get the length of longest substring whose characters can be arranged to form a palindromic string ; To keep track of the last index of each xor ; Initialize answer with 0 ; Now iterate through each character of the string ; Convert the character from [ a , z ] to [ 0 , 25 ] ; Turn the temp - th bit on if character occurs odd number of times and turn off the temp - th bit off if the character occurs ever number of times ; If a mask is present in the index Therefore a palindrome is found from index [ mask ] to i ; If x is not found then add its position in the index dict . ; Check for the palindrome of odd length ; We cancel the occurrence of a character if it occurs odd number times ; Given String ; Length of given string ; Function call
def longestSubstring ( s : str , n : int ) : NEW_LINE INDENT index = dict ( ) NEW_LINE answer = 0 NEW_LINE mask = 0 NEW_LINE index [ mask ] = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT temp = ord ( s [ i ] ) - 97 NEW_LINE mask ^= ( 1 << temp ) NEW_LINE if mask in index . keys ( ) : NEW_LINE INDENT answer = max ( answer , i - index [ mask ] ) NEW_LINE DEDENT else : NEW_LINE INDENT index [ mask ] = i NEW_LINE DEDENT for j in range ( 26 ) : NEW_LINE INDENT mask2 = mask ^ ( 1 << j ) NEW_LINE if mask2 in index . keys ( ) : NEW_LINE INDENT answer = max ( answer , i - index [ mask2 ] ) NEW_LINE DEDENT DEDENT DEDENT return answer NEW_LINE DEDENT s = " adbabd " NEW_LINE n = len ( s ) NEW_LINE print ( longestSubstring ( s , n ) ) NEW_LINE
Count of N | Function to find count of N - digit numbers with single digit XOR ; dp [ i ] [ j ] stores the number of i - digit numbers with XOR equal to j ; For 1 - 9 store the value ; Iterate till N ; Calculate XOR ; Store in DP table ; Initialize count ; Print answer ; Driver Code ; Given number N ; Function Call
def countNums ( N ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( 16 ) ] for j in range ( N ) ] ; NEW_LINE for i in range ( 1 , 10 ) : NEW_LINE INDENT dp [ 0 ] [ i ] = 1 ; NEW_LINE DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT for j in range ( 0 , 10 ) : NEW_LINE INDENT for k in range ( 0 , 16 ) : NEW_LINE INDENT xor = j ^ k ; NEW_LINE dp [ i ] [ xor ] += dp [ i - 1 ] [ k ] ; NEW_LINE DEDENT DEDENT DEDENT count = 0 ; NEW_LINE for i in range ( 0 , 10 ) : NEW_LINE INDENT count += dp [ N - 1 ] [ i ] ; NEW_LINE DEDENT print ( count ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 1 ; NEW_LINE countNums ( N ) ; NEW_LINE DEDENT
Count of numbers upto M divisible by given Prime Numbers | Function to count the numbers that are divisible by the numbers in the array from range 1 to M ; Initialize the count variable ; Iterate over [ 1 , M ] ; Iterate over array elements arr [ ] ; Check if i is divisible by a [ j ] ; Increment the count ; Return the answer ; Given list lst ; Given number M ; Function call
def count ( a , M , N ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( 1 , M + 1 ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT if ( i % a [ j ] == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT lst = [ 2 , 3 , 5 , 7 ] NEW_LINE m = 100 NEW_LINE n = len ( lst ) NEW_LINE print ( count ( lst , m , n ) ) NEW_LINE
Count of numbers upto N digits formed using digits 0 to K | Python3 implementation to count the numbers upto N digits such that no two zeros are adjacent ; Function to count the numbers upto N digits such that no two zeros are adjacent ; Condition to check if only one element remains ; If last element is non zero , return K - 1 ; If last element is 0 ; Condition to check if value calculated already ; If last element is non zero , then two cases arise , current element can be either zero or non zero ; Memoize this case ; If last is 0 , then current can only be non zero ; Memoize and return ; Given N and K ; Function call
dp = [ [ 0 ] * 10 for j in range ( 15 ) ] NEW_LINE def solve ( n , last , k ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT if ( last == k ) : NEW_LINE INDENT return ( k - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT if ( dp [ n ] [ last ] ) : NEW_LINE INDENT return dp [ n ] [ last ] NEW_LINE DEDENT if ( last == k ) : NEW_LINE INDENT dp [ n ] [ last ] = ( ( k - 1 ) * solve ( n - 1 , k , k ) + ( k - 1 ) * solve ( n - 1 , 1 , k ) ) NEW_LINE return dp [ n ] [ last ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ n ] [ last ] = solve ( n - 1 , k , k ) NEW_LINE return dp [ n ] [ last ] NEW_LINE DEDENT DEDENT n = 2 NEW_LINE k = 3 NEW_LINE x = solve ( n , k , k ) + solve ( n , 1 , k ) NEW_LINE print ( x ) NEW_LINE