codeparrot/xlcost-text-to-code huggingface dataset:
Minimize cost of swapping set bits with unset bits in a given Binary string | Python program for the above approach ; Function to find the minimum cost required to swap every set bit with an unset bit ; Stores the indices of set and unset bits of the string S ; Traverse the string S ; Store the indices ; Initialize a dp table of size n1 * n2 ; Set unreachable states to INF ; Fill the dp Table according to the given recurrence relation ; Update the value of dp [ i ] [ j ] ; Return the minimum cost ; Driver Code
INF = 1000000000 ; NEW_LINE def minimumCost ( s ) : NEW_LINE INDENT N = len ( s ) ; NEW_LINE A = [ ] NEW_LINE B = [ ] NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( s [ i ] == "1" ) : NEW_LINE A . append ( i ) ; NEW_LINE else : NEW_LINE B . append ( i ) ; NEW_LINE DEDENT n1 = len ( A ) NEW_LINE n2 = len ( B ) NEW_LINE dp = [ [ 0 for i in range ( n2 + 1 ) ] for j in range ( n1 + 1 ) ] NEW_LINE for i in range ( 1 , n1 + 1 ) : NEW_LINE INDENT dp [ i ] [ 0 ] = INF NEW_LINE DEDENT for i in range ( 1 , n1 + 1 ) : NEW_LINE INDENT for j in range ( 1 , n2 + 1 ) : NEW_LINE dp [ i ] [ j ] = min ( dp [ i ] [ j - 1 ] , dp [ i - 1 ] [ j - 1 ] + abs ( A [ i - 1 ] - B [ j - 1 ] ) ) ; NEW_LINE DEDENT return dp [ n1 ] [ n2 ] ; NEW_LINE DEDENT S = "1010001" ; NEW_LINE print ( minimumCost ( S ) ) ; NEW_LINE
Queries to find minimum absolute difference between adjacent array elements in given ranges | Python3 program for the above approach ; Function to find the minimum difference between adjacent array element over the given range [ L , R ] for Q Queries ; Find the sum of all queries ; Left and right boundaries of current range ; Print the sum of the current query range ; Function to find the minimum absolute difference of adjacent array elements for the given range ; Stores the absolute difference of adjacent elements ; Find the minimum difference of adjacent elements ; Driver Code
MAX = 5000 ; NEW_LINE def minDifference ( arr , n , q , m ) : NEW_LINE INDENT for i in range ( m ) : NEW_LINE INDENT L = q [ i ] [ 0 ] ; R = q [ i ] [ 1 ] ; NEW_LINE ans = MAX ; NEW_LINE for i in range ( L , R ) : NEW_LINE INDENT ans = min ( ans , arr [ i ] ) ; NEW_LINE DEDENT print ( ans ) ; NEW_LINE DEDENT DEDENT def minimumDifference ( arr , q , N , m ) : NEW_LINE INDENT diff = [ 0 ] * N ; NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT diff [ i ] = abs ( arr [ i ] - arr [ i + 1 ] ) ; NEW_LINE DEDENT minDifference ( diff , N - 1 , q , m ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 6 , 1 , 8 , 3 , 4 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE Q = [ [ 0 , 3 ] , [ 1 , 5 ] , [ 4 , 5 ] ] ; NEW_LINE M = len ( Q ) ; NEW_LINE minimumDifference ( arr , Q , N , M ) ; NEW_LINE DEDENT
Minimum number of flips or swaps of adjacent characters required to make two strings equal | Function to count the minimum number of operations required to make strings A and B equal ; Stores all dp - states ; Iterate rate over the range [ 1 , N ] ; If A [ i - 1 ] equals to B [ i - 1 ] ; Assign Dp [ i - 1 ] to Dp [ i ] ; Otherwise ; Update dp [ i ] ; If swapping is possible ; Update dp [ i ] ; Return the minimum number of steps required ; Given Input ; Function Call
def countMinSteps ( A , B , N ) : NEW_LINE INDENT dp = [ 0 ] * ( N + 1 ) NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( A [ i - 1 ] == B [ i - 1 ] ) : NEW_LINE INDENT dp [ i ] = dp [ i - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] = dp [ i - 1 ] + 1 NEW_LINE DEDENT if ( i >= 2 and A [ i - 2 ] == B [ i - 1 ] and A [ i - 1 ] == B [ i - 2 ] ) : NEW_LINE INDENT dp [ i ] = min ( dp [ i ] , dp [ i - 2 ] + 1 ) NEW_LINE DEDENT DEDENT return dp [ N ] NEW_LINE DEDENT A = "0101" NEW_LINE B = "0011" NEW_LINE N = len ( A ) NEW_LINE print ( countMinSteps ( A , B , N ) ) NEW_LINE
Count of N | Python program for the above approach ; Function to find the number of N digit numbers such that at least one digit occurs more than once ; Base Case ; If repeated is true , then for remaining positions any digit can be placed ; If the current state has already been computed , then return it ; Stores the count of number for the current recursive calls ; If n = 1 , 0 can be also placed ; If a digit has occurred for the second time , then set repeated to 1 ; Otherwise ; For remaining positions any digit can be placed ; If a digit has occurred for the second time , then set repeated to 1 ; Return the resultant count for the current recursive call ; Function to count all the N - digit numbers having at least one digit 's occurrence more than once ; Function to count all possible number satisfying the given criteria ; Driver Code
dp = [ [ [ - 1 for i in range ( 2 ) ] for i in range ( 1 << 10 ) ] for i in range ( 50 ) ] NEW_LINE def countOfNumbers ( digit , mask , repeated , n ) : NEW_LINE INDENT global dp NEW_LINE if ( digit == n + 1 ) : NEW_LINE INDENT if ( repeated == True ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT if ( repeated == True ) : NEW_LINE INDENT return pow ( 10 , n - digit + 1 ) NEW_LINE DEDENT val = dp [ digit ] [ mask ] [ repeated ] NEW_LINE if ( val != - 1 ) : NEW_LINE INDENT return val NEW_LINE DEDENT val = 4 NEW_LINE if ( digit == 1 ) : NEW_LINE INDENT for i in range ( ( 0 if ( n == 1 ) else 1 ) , 10 ) : NEW_LINE INDENT if ( mask & ( 1 << i ) ) : NEW_LINE INDENT val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , 1 , n ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , 0 , n ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for i in range ( 10 ) : NEW_LINE INDENT if ( mask & ( 1 << i ) ) : NEW_LINE INDENT val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , 1 , n ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , 0 , n ) NEW_LINE DEDENT DEDENT dp [ digit ] [ mask ] [ repeated ] = val NEW_LINE return dp [ digit ] [ mask ] [ repeated ] NEW_LINE DEDENT def countNDigitNumber ( N ) : NEW_LINE INDENT print ( countOfNumbers ( 1 , 0 , 0 , N ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 NEW_LINE countNDigitNumber ( N ) NEW_LINE DEDENT
Construct the largest number whose sum of cost of digits is K | Function to find the maximum number among the two numbers S and T ; If "0" exists in the string S ; If "0" exists in the string T ; Else return the maximum number formed ; Recursive function to find maximum number formed such that the sum of cost of digits of formed number is K ; Base Case ; Return the stored state ; Including the digit ( idx + 1 ) ; Excluding the digit ( idx + 1 ) ; Store the result and return ; Function to find the maximum number formed such that the sum of the cost digits in the formed number is K ; Stores all Dp - states ; Recursive Call ; Return the result ; Driver Code
def getMaximum ( S , T ) : NEW_LINE INDENT if ( S . count ( "0" ) > 0 ) : NEW_LINE INDENT return T ; NEW_LINE DEDENT if ( T . count ( "0" ) > 0 ) : NEW_LINE INDENT return S ; NEW_LINE DEDENT return S if len ( S ) > len ( T ) else T ; NEW_LINE DEDENT def recursion ( arr , idx , N , K , dp ) : NEW_LINE INDENT if ( K == 0 ) : NEW_LINE INDENT return " " ; NEW_LINE DEDENT if ( K < 0 or idx == N ) : NEW_LINE INDENT return "0" ; NEW_LINE DEDENT if ( dp [ idx ] [ K ] != " - 1" ) : NEW_LINE INDENT return dp [ idx ] [ K ] ; NEW_LINE DEDENT include = str ( idx + 1 ) + recursion ( arr , 0 , N , K - arr [ idx ] , dp ) ; NEW_LINE exclude = recursion ( arr , idx + 1 , N , K , dp ) ; NEW_LINE dp [ idx ] [ K ] = getMaximum ( include , exclude ) NEW_LINE return ( dp [ idx ] [ K ] ) NEW_LINE DEDENT def largestNumber ( arr , N , K ) : NEW_LINE INDENT dp = [ [ " - 1" for i in range ( K + 1 ) ] for i in range ( N + 1 ) ] NEW_LINE ans = recursion ( arr , 0 , N , K , dp ) ; NEW_LINE return "0" if ans == " " else ans ; NEW_LINE DEDENT arr = [ 3 , 12 , 9 , 5 , 3 , 4 , 6 , 5 , 10 ] ; NEW_LINE K = 14 ; NEW_LINE N = len ( arr ) ; NEW_LINE print ( largestNumber ( arr , N , K ) ) ; NEW_LINE
Count of N | Python3 program for the above approach ; Function to calculate count of ' N ' digit numbers such that bitwise AND of adjacent digits is 0. ; If digit = n + 1 , a valid n - digit number has been formed ; If the state has already been computed ; If current position is 1 , then any digit from [ 1 - 9 ] can be placed . If n = 1 , 0 can be also placed . ; For remaining positions , any digit from [ 0 - 9 ] can be placed after checking the conditions . ; Check if bitwise AND of current digit and previous digit is 0. ; Return answer ; Driver code ; Given Input ; Function call
dp = [ [ - 1 for i in range ( 10 ) ] for j in range ( 100 ) ] NEW_LINE val = 0 NEW_LINE def countOfNumbers ( digit , prev , n ) : NEW_LINE INDENT global val NEW_LINE global dp NEW_LINE if ( digit == n + 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT val = dp [ digit ] [ prev ] NEW_LINE if ( val != - 1 ) : NEW_LINE INDENT return val NEW_LINE DEDENT val = 0 NEW_LINE if ( digit == 1 ) : NEW_LINE INDENT i = 0 if n == 1 else 1 NEW_LINE while ( i <= 9 ) : NEW_LINE INDENT val += countOfNumbers ( digit + 1 , i , n ) NEW_LINE i += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for i in range ( 10 ) : NEW_LINE INDENT if ( ( i & prev ) == 0 ) : NEW_LINE INDENT val += countOfNumbers ( digit + 1 , i , n ) NEW_LINE DEDENT DEDENT DEDENT return val NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE print ( countOfNumbers ( 1 , 0 , N ) ) NEW_LINE DEDENT
Number of ways such that only K bars are visible from the left | dp array ; Function to calculate the number of permutations of N , where only K bars are visible from the left . ; If subproblem has already been calculated , return ; Only ascending order is possible ; N is placed at the first position The nest N - 1 are arranged in ( N - 1 ) ! ways ; Recursing ; Input ; Initialize dp array ; Function call
dp = [ [ 0 for i in range ( 1005 ) ] for j in range ( 1005 ) ] NEW_LINE def KvisibleFromLeft ( N , K ) : NEW_LINE INDENT if ( dp [ N ] [ K ] != - 1 ) : NEW_LINE INDENT return dp [ N ] [ K ] NEW_LINE DEDENT if ( N == K ) : NEW_LINE INDENT dp [ N ] [ K ] = 1 NEW_LINE return dp [ N ] [ K ] NEW_LINE DEDENT if ( K == 1 ) : NEW_LINE INDENT ans = 1 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT ans *= i NEW_LINE DEDENT dp [ N ] [ K ] = ans NEW_LINE return dp [ N ] [ K ] NEW_LINE DEDENT dp [ N ] [ K ] = KvisibleFromLeft ( N - 1 , K - 1 ) + ( N - 1 ) * KvisibleFromLeft ( N - 1 , K ) NEW_LINE return dp [ N ] [ K ] NEW_LINE DEDENT N , K = 5 , 2 NEW_LINE for i in range ( 1005 ) : NEW_LINE INDENT for j in range ( 1005 ) : NEW_LINE INDENT dp [ i ] [ j ] = - 1 NEW_LINE DEDENT DEDENT print ( KvisibleFromLeft ( N , K ) ) NEW_LINE
Number of distinct words of size N with at most K contiguous vowels | Power function to calculate long powers with mod ; Function for finding number of ways to create string with length N and atmost K contiguous vowels ; Array dp to store number of ways ; dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 1 ] . . dp [ i - 1 ] [ k ] ) * 21 ; Now setting sum to be dp [ i ] [ 0 ] ; If j > i , no ways are possible to create a string with length i and vowel j ; If j = i all the character should be vowel ; dp [ i ] [ j ] relation with dp [ i - 1 ] [ j - 1 ] ; Adding dp [ i ] [ j ] in the sum ; Driver Code ; Input ; Function Call
def power ( x , y , p ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % p NEW_LINE if ( x == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % p NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % p NEW_LINE DEDENT return res NEW_LINE DEDENT def kvowelwords ( N , K ) : NEW_LINE INDENT i , j = 0 , 0 NEW_LINE MOD = 1000000007 NEW_LINE dp = [ [ 0 for i in range ( K + 1 ) ] for i in range ( N + 1 ) ] NEW_LINE sum = 1 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT dp [ i ] [ 0 ] = sum * 21 NEW_LINE dp [ i ] [ 0 ] %= MOD NEW_LINE sum = dp [ i ] [ 0 ] NEW_LINE for j in range ( 1 , K + 1 ) : NEW_LINE INDENT if ( j > i ) : NEW_LINE INDENT dp [ i ] [ j ] = 0 NEW_LINE DEDENT elif ( j == i ) : NEW_LINE INDENT dp [ i ] [ j ] = power ( 5 , i , MOD ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] * 5 NEW_LINE DEDENT dp [ i ] [ j ] %= MOD NEW_LINE sum += dp [ i ] [ j ] NEW_LINE sum %= MOD NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE K = 3 NEW_LINE print ( kvowelwords ( N , K ) ) NEW_LINE DEDENT
Maximize the length of upper boundary formed by placing given N rectangles horizontally or vertically | Function to find maximum length of the upper boundary formed by placing each of the rectangles either horizontally or vertically ; Stores the intermediate transition states ; Place the first rectangle horizontally ; Place the first rectangle vertically ; Place horizontally ; Stores the difference in height of current and previous rectangle ; Take maximum out of two options ; Place Vertically ; Stores the difference in height of current and previous rectangle ; Take maximum out two options ; Print maximum of horizontal or vertical alignment of the last rectangle ; Driver Code
def maxBoundary ( N , V ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( 2 ) ] for j in range ( N ) ] NEW_LINE dp [ 0 ] [ 0 ] = V [ 0 ] [ 0 ] NEW_LINE dp [ 0 ] [ 1 ] = V [ 0 ] [ 1 ] NEW_LINE for i in range ( 1 , N , 1 ) : NEW_LINE INDENT dp [ i ] [ 0 ] = V [ i ] [ 0 ] NEW_LINE height1 = abs ( V [ i - 1 ] [ 1 ] - V [ i ] [ 1 ] ) NEW_LINE height2 = abs ( V [ i - 1 ] [ 0 ] - V [ i ] [ 1 ] ) NEW_LINE dp [ i ] [ 0 ] += max ( height1 + dp [ i - 1 ] [ 0 ] , height2 + dp [ i - 1 ] [ 1 ] ) NEW_LINE dp [ i ] [ 1 ] = V [ i ] [ 1 ] NEW_LINE vertical1 = abs ( V [ i ] [ 0 ] - V [ i - 1 ] [ 1 ] ) ; NEW_LINE vertical2 = abs ( V [ i ] [ 0 ] - V [ i - 1 ] [ 1 ] ) ; NEW_LINE dp [ i ] [ 1 ] += max ( vertical1 + dp [ i - 1 ] [ 0 ] , vertical2 + dp [ i - 1 ] [ 1 ] ) NEW_LINE DEDENT print ( max ( dp [ N - 1 ] [ 0 ] , dp [ N - 1 ] [ 1 ] ) - 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE V = [ [ 2 , 5 ] , [ 3 , 8 ] , [ 1 , 10 ] , [ 7 , 14 ] , [ 2 , 5 ] ] NEW_LINE maxBoundary ( N , V ) NEW_LINE DEDENT
Ways to sum to N using Natural Numbers up to K with repetitions allowed | Function to find the total number of ways to represent N as the sum of integers over the range [ 1 , K ] ; Initialize a list ; Update dp [ 0 ] to 1 ; Iterate over the range [ 1 , K + 1 ] ; Iterate over the range [ 1 , N + 1 ] ; If col is greater than or equal to row ; Update current dp [ col ] state ; Return the total number of ways ; Given inputs
def NumberOfways ( N , K ) : NEW_LINE INDENT dp = [ 0 ] * ( N + 1 ) NEW_LINE dp [ 0 ] = 1 NEW_LINE for row in range ( 1 , K + 1 ) : NEW_LINE INDENT for col in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( col >= row ) : NEW_LINE INDENT dp [ col ] = dp [ col ] + dp [ col - row ] NEW_LINE DEDENT DEDENT DEDENT return ( dp [ N ] ) NEW_LINE DEDENT N = 8 NEW_LINE K = 2 NEW_LINE print ( NumberOfways ( N , K ) ) NEW_LINE
Queries to check if array elements from indices [ L , R ] forms an Arithmetic Progression or not | Function to check if the given range of queries form an AP or not in the given array arr [ ] ; Stores length of the longest subarray forming AP for every array element ; Iterate over the range [ 0 , N ] ; Stores the index of the last element of forming AP ; Iterate until the element at index ( j , j + 1 ) forms AP ; Increment j by 1 ; Traverse the current subarray over the range [ i , j - 1 ] ; Update the length of the longest subarray at index k ; Update the value of i ; Traverse the given queries ; Print the result ; Otherwise ; Driver Code
def findAPSequence ( arr , N , Q , M ) : NEW_LINE INDENT dp = [ 0 ] * ( N + 5 ) NEW_LINE i = 0 NEW_LINE while i + 1 < N : NEW_LINE INDENT j = i + 1 NEW_LINE while ( j + 1 < N and arr [ j + 1 ] - arr [ j ] == arr [ i + 1 ] - arr [ i ] ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT for k in range ( i , j ) : NEW_LINE INDENT dp [ k ] = j - k NEW_LINE DEDENT i = j NEW_LINE DEDENT for i in range ( M ) : NEW_LINE INDENT if ( dp [ Q [ i ] [ 0 ] ] >= Q [ i ] [ 1 ] - Q [ i ] [ 0 ] ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 3 , 5 , 7 , 6 , 5 , 4 , 1 ] NEW_LINE Q = [ [ 0 , 3 ] , [ 3 , 4 ] , [ 2 , 4 ] ] NEW_LINE N = len ( arr ) NEW_LINE M = len ( Q ) NEW_LINE findAPSequence ( arr , N , Q , M ) NEW_LINE DEDENT
Minimize difference between sum of two K | Python3 program for the above approach ; Stores the values at recursive states ; Function to find the minimum difference between sum of two K - length subsets ; Base Case ; If k1 and k2 are 0 , then return the absolute difference between sum1 and sum2 ; Otherwise , return INT_MAX ; If the result is already computed , return the result ; Store the 3 options ; Including the element in first subset ; Including the element in second subset ; Not including the current element in both the subsets ; Store minimum of 3 values obtained ; Return the value for the current states ; Driver Code
import sys NEW_LINE dp = [ [ [ - 1 for i in range ( 100 ) ] for i in range ( 100 ) ] for i in range ( 100 ) ] NEW_LINE def minSumDifference ( arr , n , k1 , k2 , sum1 , sum2 ) : NEW_LINE INDENT global dp NEW_LINE if ( n < 0 ) : NEW_LINE INDENT if ( k1 == 0 and k2 == 0 ) : NEW_LINE INDENT return abs ( sum1 - sum2 ) NEW_LINE DEDENT else : NEW_LINE INDENT return sys . maxsize + 1 NEW_LINE DEDENT DEDENT if ( dp [ n ] [ sum1 ] [ sum2 ] != - 1 ) : NEW_LINE INDENT return dp [ n ] [ sum1 ] [ sum2 ] NEW_LINE DEDENT op1 = sys . maxsize + 1 NEW_LINE op2 = sys . maxsize + 1 NEW_LINE op3 = sys . maxsize + 1 NEW_LINE if ( k1 > 0 ) : NEW_LINE INDENT op1 = minSumDifference ( arr , n - 1 , k1 - 1 , k2 , sum1 + arr [ n ] , sum2 ) NEW_LINE DEDENT if ( k2 > 0 ) : NEW_LINE INDENT op2 = minSumDifference ( arr , n - 1 , k1 , k2 - 1 , sum1 , sum2 + arr [ n ] ) NEW_LINE DEDENT op3 = minSumDifference ( arr , n - 1 , k1 , k2 , sum1 , sum2 ) NEW_LINE dp [ n ] [ sum1 ] [ sum2 ] = min ( op1 , min ( op2 , op3 ) ) NEW_LINE return dp [ n ] [ sum1 ] [ sum2 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 12 , 3 , 5 , 6 , 7 , 17 ] NEW_LINE K = 2 NEW_LINE N = len ( arr ) NEW_LINE print ( minSumDifference ( arr , N - 1 , K , K , 0 , 0 ) ) NEW_LINE DEDENT
Geek | Function to calculate the N - th Geek - onacci Number ; Stores the geekonacci series ; Store the first three terms of the series ; Iterate over the range [ 3 , N ] ; Update the value of arr [ i ] as the sum of previous 3 terms in the series ; Return the last element of arr [ ] as the N - th term ; Driver Code
def find ( A , B , C , N ) : NEW_LINE INDENT arr = [ 0 ] * N NEW_LINE arr [ 0 ] = A NEW_LINE arr [ 1 ] = B NEW_LINE arr [ 2 ] = C NEW_LINE for i in range ( 3 , N ) : NEW_LINE INDENT arr [ i ] = ( arr [ i - 1 ] + arr [ i - 2 ] + arr [ i - 3 ] ) NEW_LINE DEDENT return arr [ N - 1 ] NEW_LINE DEDENT A = 1 NEW_LINE B = 3 NEW_LINE C = 2 NEW_LINE N = 4 NEW_LINE print ( find ( A , B , C , N ) ) NEW_LINE
Maximum sum subsequence made up of at most K distant elements including the first and last array elements | Python program for the above approach ;  ; Function to find maximum sum of a subsequence satisfying the given conditions ; Stores the maximum sum ; Starting index of the subsequence ; Stores the pair of maximum value and the index of that value ; Traverse the array ; Increment the first value of deque by arr [ i ] and store it in dp [ i ] ; Delete all the values which are less than dp [ i ] in deque ; Append the current pair of value and index in deque ; If first value of the queue is at a distance > K ; Return the value at the last index ; Driver Code
from collections import deque NEW_LINE / * Pair class Store ( x , y ) Pair * / NEW_LINE def maxResult ( arr , k ) : NEW_LINE INDENT dp = [ 0 ] * len ( arr ) NEW_LINE dp [ 0 ] = arr [ 0 ] NEW_LINE q = deque ( [ ( arr [ 0 ] , 0 ) ] ) NEW_LINE for i in range ( 1 , len ( arr ) ) : NEW_LINE INDENT dp [ i ] = arr [ i ] + q [ 0 ] [ 0 ] NEW_LINE while q and q [ - 1 ] [ 0 ] < dp [ i ] : NEW_LINE INDENT q . pop ( ) NEW_LINE DEDENT q . append ( ( dp [ i ] , i ) ) NEW_LINE if i - k == q [ 0 ] [ 1 ] : NEW_LINE INDENT q . popleft ( ) NEW_LINE DEDENT DEDENT return dp [ - 1 ] NEW_LINE DEDENT arr = [ 10 , - 5 , - 2 , 4 , 0 , 3 ] NEW_LINE K = 3 NEW_LINE print ( maxResult ( arr , K ) ) NEW_LINE
Minimum days required to cure N persons | Function to find minimum count of days required to give a cure such that the high risk person and risk person does not get a dose on same day . ; Stores count of persons whose age is less than or equal to 10 and greater than or equal to 60. ; Stores the count of persons whose age is in the range [ 11 , 59 ] ; Traverse the array arr [ ] ; If age less than or equal to 10 or greater than or equal to 60 ; Update risk ; Update normal_risk ; Calculate days to cure risk and normal_risk persons ; Prthe days ; Driver Code ; Given array ; Size of the array ; Given P
def daysToCure ( arr , N , P ) : NEW_LINE INDENT risk = 0 NEW_LINE normal_risk = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] >= 60 or arr [ i ] <= 10 ) : NEW_LINE INDENT risk += 1 NEW_LINE DEDENT else : NEW_LINE INDENT normal_risk += 1 NEW_LINE DEDENT DEDENT days = ( risk // P ) + ( risk % P > 0 ) + ( normal_risk // P ) + ( normal_risk % P > 0 ) NEW_LINE print ( days ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 9 , 80 , 27 , 72 , 79 ] NEW_LINE N = len ( arr ) NEW_LINE P = 2 NEW_LINE daysToCure ( arr , N , P ) NEW_LINE DEDENT
Count numbers from a given range whose product of digits is K | Python 3 program to implement the above approach ; Function to count numbers in the range [ 0 , X ] whose product of digit is K ; If count of digits in a number greater than count of digits in X ; If product of digits of a number equal to K ; If overlapping subproblems already occurred ; Stores count of numbers whose product of digits is K ; Check if the numbers exceeds K or not ; Iterate over all possible value of i - th digits ; if number contains leading 0 ; Update res ; Update res ; Return res ; Utility function to count the numbers in the range [ L , R ] whose prod of digits is K ; Stores numbers in the form of string ; Stores overlapping subproblems ; Stores count of numbers in the range [ 0 , R ] whose product of digits is k ; Update str ; Stores count of numbers in ; the range [ 0 , L - 1 ] whose product of digits is k ; Driver Code
M = 100 NEW_LINE def cntNum ( X , i , prod , K , st , tight , dp ) : NEW_LINE INDENT end = 0 NEW_LINE if ( i >= len ( X ) or prod > K ) : NEW_LINE INDENT if ( prod == K ) : NEW_LINE return 1 NEW_LINE else : NEW_LINE return 0 NEW_LINE DEDENT if ( dp [ prod ] [ i ] [ tight ] [ st ] != - 1 ) : NEW_LINE INDENT return dp [ prod ] [ i ] [ tight ] [ st ] NEW_LINE DEDENT res = 0 NEW_LINE if ( tight != 0 ) : NEW_LINE INDENT end = ord ( X [ i ] ) - ord ( '0' ) NEW_LINE DEDENT for j in range ( end + 1 ) : NEW_LINE INDENT if ( j == 0 and st == 0 ) : NEW_LINE INDENT res += cntNum ( X , i + 1 , prod , K , False , ( tight & ( j == end ) ) , dp ) NEW_LINE DEDENT else : NEW_LINE INDENT res += cntNum ( X , i + 1 , prod * j , K , True , ( tight & ( j == end ) ) , dp ) NEW_LINE DEDENT DEDENT dp [ prod ] [ i ] [ tight ] [ st ] = res NEW_LINE return res NEW_LINE DEDENT def UtilCntNumRange ( L , R , K ) : NEW_LINE INDENT global M NEW_LINE str1 = str ( R ) NEW_LINE dp = [ [ [ [ - 1 for i in range ( 2 ) ] for j in range ( 2 ) ] for k in range ( M ) ] for l in range ( M ) ] NEW_LINE cntR = cntNum ( str1 , 0 , 1 , K , False , True , dp ) NEW_LINE str1 = str ( L - 1 ) NEW_LINE dp = [ [ [ [ - 1 for i in range ( 2 ) ] for j in range ( 2 ) ] for k in range ( M ) ] for l in range ( M ) ] NEW_LINE cntR = 20 NEW_LINE cntL = cntNum ( str1 , 0 , 1 , K , False , True , dp ) NEW_LINE return ( cntR - cntL ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 20 NEW_LINE R = 10000 NEW_LINE K = 14 NEW_LINE print ( UtilCntNumRange ( L , R , K ) ) NEW_LINE DEDENT