codeparrot/xlcost-text-to-code huggingface dataset:
Split the given string into Odds : Digit DP | Function to check whether a string is an odd number or not ; A function to find the minimum number of segments the given string can be divided such that every segment is a odd number ; Declare a splitdp [ ] array and initialize to - 1 ; Build the DP table in a bottom - up manner ; Initially Check if the entire prefix is odd ; If the Given Prefix can be split into Odds then for the remaining string from i to j Check if Odd . If yes calculate the minimum split till j ; To check if the substring from i to j is a odd number or not ; If it is an odd number , then update the dp array ; Return the minimum number of splits for the entire string ; Driver code
def checkOdd ( number ) : NEW_LINE INDENT n = len ( number ) NEW_LINE num = ord ( number [ n - 1 ] ) - 48 NEW_LINE return ( num & 1 ) NEW_LINE DEDENT def splitIntoOdds ( number ) : NEW_LINE INDENT numLen = len ( number ) NEW_LINE splitDP = [ - 1 for i in range ( numLen + 1 ) ] NEW_LINE for i in range ( 1 , numLen + 1 ) : NEW_LINE INDENT if ( i <= 9 and checkOdd ( number [ 0 : i ] ) > 0 ) : NEW_LINE INDENT splitDP [ i ] = 1 NEW_LINE DEDENT if ( splitDP [ i ] != - 1 ) : NEW_LINE INDENT for j in range ( 1 , 10 ) : NEW_LINE INDENT if ( i + j > numLen ) : NEW_LINE INDENT break ; NEW_LINE DEDENT if ( checkOdd ( number [ i : i + j ] ) ) : NEW_LINE INDENT if ( splitDP [ i + j ] == - 1 ) : NEW_LINE INDENT splitDP [ i + j ] = 1 + splitDP [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT splitDP [ i + j ] = min ( splitDP [ i + j ] , 1 + splitDP [ i ] ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT return splitDP [ numLen ] NEW_LINE DEDENT print ( splitIntoOdds ( "123456789123456789123" ) ) NEW_LINE
Split the given string into Primes : Digit DP | Function to check whether a string is a prime number or not ; A recursive function to find the minimum number of segments the given string can be divided such that every segment is a prime ; If the number is null ; checkPrime function is called to check if the number is a prime or not . ; A very large number denoting maximum ; Consider a minimum of 6 and length since the primes are less than 10 ^ 6 ; Recursively call the function to check for the remaining string ; Evaluating minimum splits into Primes for the suffix ; Checks if no combination found ; Driver code
def checkPrime ( number ) : NEW_LINE INDENT num = int ( number ) NEW_LINE for i in range ( 2 , int ( num ** 0.5 ) ) : NEW_LINE INDENT if ( ( num % i ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def splitIntoPrimes ( number ) : NEW_LINE INDENT if ( number == ' ' ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( len ( number ) <= 6 and checkPrime ( number ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT numLen = len ( number ) NEW_LINE ans = 1000000 NEW_LINE for i in range ( 1 , ( min ( 6 , numLen ) + 1 ) ) : NEW_LINE INDENT if ( checkPrime ( number [ : i ] ) ) : NEW_LINE INDENT val = splitIntoPrimes ( number [ i : ] ) NEW_LINE if ( val != - 1 ) : NEW_LINE INDENT ans = min ( ans , 1 + val ) NEW_LINE DEDENT DEDENT DEDENT if ( ans == 1000000 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT DEDENT print ( splitIntoPrimes ( "13499315" ) ) NEW_LINE print ( splitIntoPrimes ( "43" ) ) NEW_LINE
Find the numbers from 1 to N that contains exactly k non | Function to find number less than N having k non - zero digits ; Store the memorised values ; Initialise ; Base ; Calculate all states For every state , from numbers 1 to N , the count of numbers which contain exactly j non zero digits is being computed and updated in the dp array . ; Return the required answer ; Driver code ; Function call
def k_nonzero_numbers ( s , n , k ) : NEW_LINE INDENT dp = [ [ [ 0 for i in range ( k + 2 ) ] for i in range ( 2 ) ] for i in range ( n + 2 ) ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( 2 ) : NEW_LINE INDENT for x in range ( k + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] [ x ] = 0 NEW_LINE DEDENT DEDENT DEDENT dp [ 0 ] [ 0 ] [ 0 ] = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sm = 0 NEW_LINE while ( sm < 2 ) : NEW_LINE INDENT for j in range ( k + 1 ) : NEW_LINE INDENT x = 0 NEW_LINE y = 0 NEW_LINE if sm : NEW_LINE INDENT y = 9 NEW_LINE DEDENT else : NEW_LINE INDENT y = ord ( s [ i ] ) - ord ( '0' ) NEW_LINE DEDENT while ( x <= y ) : NEW_LINE INDENT dp [ i + 1 ] [ ( sm or x < ( ord ( s [ i ] ) - ord ( '0' ) ) ) ] [ j + ( x > 0 ) ] += dp [ i ] [ sm ] [ j ] NEW_LINE x += 1 NEW_LINE DEDENT DEDENT sm += 1 NEW_LINE DEDENT DEDENT return dp [ n ] [ 0 ] [ k ] + dp [ n ] [ 1 ] [ k ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "25" NEW_LINE k = 2 NEW_LINE n = len ( s ) NEW_LINE print ( k_nonzero_numbers ( s , n , k ) ) NEW_LINE DEDENT
Count maximum occurrence of subsequence in string such that indices in subsequence is in A . P . | Function to find the maximum occurrence of the subsequence such that the indices of characters are in arithmetic progression ; Frequencies of subsequence ; Loop to find the frequencies of subsequence of length 1 ; Loop to find the frequencies subsequence of length 2 ; Finding maximum frequency ; Driver Code
def maximumOccurrence ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE freq = { } NEW_LINE for i in s : NEW_LINE INDENT temp = " " NEW_LINE temp += i NEW_LINE freq [ temp ] = freq . get ( temp , 0 ) + 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT temp = " " NEW_LINE temp += s [ i ] NEW_LINE temp += s [ j ] NEW_LINE freq [ temp ] = freq . get ( temp , 0 ) + 1 NEW_LINE DEDENT DEDENT answer = - 10 ** 9 NEW_LINE for it in freq : NEW_LINE INDENT answer = max ( answer , freq [ it ] ) NEW_LINE DEDENT return answer NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " xxxyy " NEW_LINE print ( maximumOccurrence ( s ) ) NEW_LINE DEDENT
Count ways to partition a string such that both parts have equal distinct characters | Function to count the distinct characters in the string ; Frequency of each character ; Loop to count the frequency of each character of the string ; If frequency is greater than 0 then the character occured ; Function to count the number of ways to partition the string such that each partition have same number of distinct character ; Loop to choose the partition index for the string ; Divide in two parts ; Check whether number of distinct characters are equal ; Driver Code
def distinctChars ( s ) : NEW_LINE INDENT freq = [ 0 ] * 26 ; NEW_LINE count = 0 ; NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT freq [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 ; NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( freq [ i ] > 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT def waysToSplit ( s ) : NEW_LINE INDENT n = len ( s ) ; NEW_LINE answer = 0 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT left = s [ 0 : i ] ; NEW_LINE right = s [ i : n ] ; NEW_LINE if ( distinctChars ( left ) == distinctChars ( right ) ) : NEW_LINE INDENT answer += 1 ; NEW_LINE DEDENT DEDENT return answer ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " ababa " ; NEW_LINE print ( waysToSplit ( s ) ) ; NEW_LINE DEDENT
Count ways to partition a string such that both parts have equal distinct characters | Function to count the number of ways to partition the string such that each partition have same number of distinct character ; Prefix and suffix array for distinct character from start and end ; To check whether a character has appeared till ith index ; Calculating prefix array ; Character appears for the first time in string ; Character is visited ; Resetting seen for suffix calculation ; Calculating the suffix array ; Character appears for the first time ; This character has now appeared ; Loop to calculate the number partition points in the string ; Check whether number of distinct characters are equal ; Driver Code
def waysToSplit ( s ) : NEW_LINE INDENT n = len ( s ) ; NEW_LINE answer = 0 ; NEW_LINE prefix = [ 0 ] * n ; NEW_LINE suffix = [ 0 ] * n ; NEW_LINE seen = [ 0 ] * 26 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT prev = prefix [ i - 1 ] if ( i - 1 >= 0 ) else 0 ; NEW_LINE if ( seen [ ord ( s [ i ] ) - ord ( ' a ' ) ] == 0 ) : NEW_LINE INDENT prefix [ i ] += ( prev + 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT prefix [ i ] = prev ; NEW_LINE DEDENT seen [ ord ( s [ i ] ) - ord ( ' a ' ) ] = 1 ; NEW_LINE DEDENT seen = [ 0 ] * len ( seen ) ; NEW_LINE suffix [ n - 1 ] = 0 ; NEW_LINE for i in range ( n - 1 , 0 , - 1 ) : NEW_LINE INDENT prev = suffix [ i ] ; NEW_LINE if ( seen [ ord ( s [ i ] ) - ord ( ' a ' ) ] == 0 ) : NEW_LINE INDENT suffix [ i - 1 ] += ( prev + 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT suffix [ i - 1 ] = prev ; NEW_LINE DEDENT seen [ ord ( s [ i ] ) - ord ( ' a ' ) ] = 1 ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( prefix [ i ] == suffix [ i ] ) : NEW_LINE INDENT answer += 1 ; NEW_LINE DEDENT DEDENT return answer ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " ababa " ; NEW_LINE print ( waysToSplit ( s ) ) ; NEW_LINE DEDENT
Counts paths from a point to reach Origin : Memory Optimized | Function to count the paths from a point to the origin ; Base Case when the point is already at origin ; Base Case when the point is on the x or y - axis ; Loop to fill all the position as 1 in the array ; Loop to count the number of paths from a point to origin in bottom - up manner ; Driver Code ; Function call
def Count_Paths ( x , y ) : NEW_LINE INDENT if ( x == 0 and y == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( x == 0 and y == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT dp = [ 0 ] * ( max ( x , y ) + 1 ) NEW_LINE p = max ( x , y ) NEW_LINE q = min ( x , y ) NEW_LINE for i in range ( p + 1 ) : NEW_LINE INDENT dp [ i ] = 1 NEW_LINE DEDENT for i in range ( 1 , q + 1 ) : NEW_LINE INDENT for j in range ( 1 , p + 1 ) : NEW_LINE INDENT dp [ j ] += dp [ j - 1 ] NEW_LINE DEDENT DEDENT return dp [ p ] NEW_LINE DEDENT x = 3 NEW_LINE y = 3 NEW_LINE print ( " Number ▁ of ▁ Paths ▁ " , Count_Paths ( x , y ) ) NEW_LINE
Find the numbers present at Kth level of a Fibonacci Binary Tree | Initializing the max value ; Array to store all the fibonacci numbers ; Function to generate fibonacci numbers using Dynamic Programming ; 0 th and 1 st number of the series are 0 and 1 ; Add the previous two numbers in the series and store it ; Function to print the Fibonacci numbers present at Kth level of a Binary Tree ; Finding the left and right index ; Iterating and printing the numbers ; Precomputing Fibonacci numbers
MAX_SIZE = 100005 NEW_LINE fib = [ 0 ] * ( MAX_SIZE + 1 ) NEW_LINE def fibonacci ( ) : NEW_LINE INDENT fib [ 0 ] = 0 NEW_LINE fib [ 1 ] = 1 NEW_LINE for i in range ( 2 , MAX_SIZE + 1 ) : NEW_LINE INDENT fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] NEW_LINE DEDENT DEDENT def printLevel ( level ) : NEW_LINE INDENT left_index = pow ( 2 , level - 1 ) NEW_LINE right_index = pow ( 2 , level ) - 1 NEW_LINE for i in range ( left_index , right_index + 1 ) : NEW_LINE INDENT print ( fib [ i - 1 ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT fibonacci ( ) NEW_LINE K = 4 NEW_LINE printLevel ( K ) NEW_LINE
Count the number of ways to divide N in k groups incrementally | Function to count the number of ways to divide the number N in groups such that each group has K number of elements ; Base Case ; If N is divides completely into less than k groups ; Put all possible values greater equal to prev ; Function to count the number of ways to divide the number N ; Driver Code
def calculate ( pos , prev , left , k ) : NEW_LINE INDENT if ( pos == k ) : NEW_LINE INDENT if ( left == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT DEDENT if ( left == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT answer = 0 ; NEW_LINE for i in range ( prev , left + 1 ) : NEW_LINE INDENT answer += calculate ( pos + 1 , i , left - i , k ) ; NEW_LINE DEDENT return answer ; NEW_LINE DEDENT def countWaystoDivide ( n , k ) : NEW_LINE INDENT return calculate ( 0 , 1 , n , k ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 8 ; NEW_LINE K = 4 ; NEW_LINE print ( countWaystoDivide ( N , K ) ) ; NEW_LINE DEDENT
Count the number of ways to divide N in k groups incrementally | DP Table ; Function to count the number of ways to divide the number N in groups such that each group has K number of elements ; Base Case ; if N is divides completely into less than k groups ; If the subproblem has been solved , use the value ; put all possible values greater equal to prev ; Function to count the number of ways to divide the number N in groups ; Initialize DP Table as - 1 ; Driver Code
dp = [ [ [ 0 for i in range ( 50 ) ] for j in range ( 50 ) ] for j in range ( 50 ) ] NEW_LINE def calculate ( pos , prev , left , k ) : NEW_LINE INDENT if ( pos == k ) : NEW_LINE INDENT if ( left == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT DEDENT if ( left == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( dp [ pos ] [ prev ] [ left ] != - 1 ) : NEW_LINE INDENT return dp [ pos ] [ prev ] [ left ] ; NEW_LINE DEDENT answer = 0 ; NEW_LINE for i in range ( prev , left + 1 ) : NEW_LINE INDENT answer += calculate ( pos + 1 , i , left - i , k ) ; NEW_LINE DEDENT dp [ pos ] [ prev ] [ left ] = answer ; NEW_LINE return dp [ pos ] [ prev ] [ left ] ; NEW_LINE DEDENT def countWaystoDivide ( n , k ) : NEW_LINE INDENT for i in range ( 50 ) : NEW_LINE INDENT for j in range ( 50 ) : NEW_LINE INDENT for l in range ( 50 ) : NEW_LINE INDENT dp [ i ] [ j ] [ l ] = - 1 ; NEW_LINE DEDENT DEDENT DEDENT return calculate ( 0 , 1 , n , k ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 8 ; NEW_LINE K = 4 ; NEW_LINE print ( countWaystoDivide ( N , K ) ) ; NEW_LINE DEDENT
Maximum score possible after performing given operations on an Array | Function to calculate maximum score recursively ; Base case ; Sum of array in range ( l , r ) ; If the operation is even - numbered the score is decremented ; Exploring all paths , by removing leftmost and rightmost element and selecting the maximum value ; Function to find the max score ; Prefix sum array ; Calculating prefix_sum ; Driver code
def maxScore ( l , r , prefix_sum , num ) : NEW_LINE INDENT if ( l > r ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( ( l - 1 ) >= 0 ) : NEW_LINE INDENT current_sum = ( prefix_sum [ r ] - prefix_sum [ l - 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT current_sum = prefix_sum [ r ] - 0 NEW_LINE DEDENT if ( num % 2 == 0 ) : NEW_LINE INDENT current_sum *= - 1 ; NEW_LINE DEDENT return current_sum + max ( maxScore ( l + 1 , r , prefix_sum , num + 1 ) , maxScore ( l , r - 1 , prefix_sum , num + 1 ) ) ; NEW_LINE DEDENT def findMaxScore ( a , n ) : NEW_LINE INDENT prefix_sum = [ 0 ] * n NEW_LINE prefix_sum [ 0 ] = a [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT prefix_sum [ i ] = prefix_sum [ i - 1 ] + a [ i ] ; NEW_LINE DEDENT return maxScore ( 0 , n - 1 , prefix_sum , 1 ) ; NEW_LINE DEDENT n = 6 ; NEW_LINE A = [ 1 , 2 , 3 , 4 , 2 , 6 ] NEW_LINE ans = findMaxScore ( A , n ) NEW_LINE print ( ans ) NEW_LINE
Make all array elements divisible by a number K | Function to make array divisible ; For each element of array how much number to be subtracted to make it divisible by k ; For each element of array how much number to be added to make it divisible by K ; Calculate minimum difference ; Driver Code
def makeDivisble ( arr , k ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE b1 = [ ] NEW_LINE b2 = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT b1 . append ( arr [ i ] % k ) NEW_LINE DEDENT for j in range ( n ) : NEW_LINE INDENT if ( ( arr [ j ] % k ) != 0 ) : NEW_LINE INDENT b2 . append ( k - ( arr [ j ] % k ) ) NEW_LINE DEDENT else : NEW_LINE INDENT b2 . append ( 0 ) NEW_LINE DEDENT DEDENT c = 0 NEW_LINE mini = float ( ' inf ' ) NEW_LINE suml = 0 NEW_LINE sumr = 0 NEW_LINE index = - 1 NEW_LINE for c in range ( 0 , n + 1 , 1 ) : NEW_LINE INDENT suml = sum ( b1 [ : c + 1 ] ) NEW_LINE sumr = sum ( b2 ) NEW_LINE if suml >= sumr : NEW_LINE INDENT rem = suml - sumr NEW_LINE if rem < mini : NEW_LINE INDENT mini = rem NEW_LINE index = c NEW_LINE DEDENT DEDENT DEDENT return index , mini NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 14 , 4 , 41 , 1 ] NEW_LINE k = 7 NEW_LINE index , diff = makeDivisble ( arr , k ) NEW_LINE print ( index , diff ) NEW_LINE DEDENT
Finding powers of any number P in N ! | Python3 program to find the power of P in N ! ; Map to store all the prime factors of P ; Function to find the prime factors of N im Map ; Clear map ; Check for factors of 2 ; Find all the prime factors ; If i is a factors then increase the frequency of i ; Function to find the power of prime number P in N ! ; Loop until temp <= N ; Add the number of numbers divisible by N ; Each time multiply temp by P ; Returns ans ; Function that find the powers of any P in N ! ; Find all prime factors of number P ; To store the powers of all prime factors ; Traverse the map ; Prime factor and corres . powers ; Find power of prime factor primeFac ; Divide frequency by facPow ; Store the power of primeFac ^ facPow ; Return the minimum element in Power array ; Driver code ; Function to find power of P in N !
import math NEW_LINE Map = { } NEW_LINE def findPrimeFactors ( N ) : NEW_LINE INDENT Map . clear ( ) NEW_LINE while ( N % 2 == 0 ) : NEW_LINE INDENT if 2 in Map : NEW_LINE INDENT Map [ 2 ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT Map [ 2 ] = 1 NEW_LINE DEDENT N = N // 2 NEW_LINE DEDENT for i in range ( 3 , int ( math . sqrt ( N ) ) + 1 , 2 ) : NEW_LINE INDENT while ( N % i == 0 ) : NEW_LINE INDENT if i in Map : NEW_LINE INDENT Map [ i ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT Map [ i ] = 1 NEW_LINE DEDENT N = N // i NEW_LINE DEDENT DEDENT if ( N > 2 ) : NEW_LINE INDENT if N in Map : NEW_LINE INDENT Map [ N ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT Map [ N ] = 1 NEW_LINE DEDENT DEDENT DEDENT def PowInFactN ( N , P ) : NEW_LINE INDENT ans = 0 NEW_LINE temp = P NEW_LINE while ( temp <= N ) : NEW_LINE INDENT ans = ans + ( N // temp ) NEW_LINE temp = temp * P NEW_LINE DEDENT return ans NEW_LINE DEDENT def findPowers ( N , P ) : NEW_LINE INDENT findPrimeFactors ( P ) NEW_LINE Powers = [ ] NEW_LINE for it1 , it2 in Map . items ( ) : NEW_LINE INDENT primeFac = it1 NEW_LINE facPow = it2 NEW_LINE p = PowInFactN ( N , primeFac ) NEW_LINE p = p // facPow NEW_LINE Powers . append ( p ) NEW_LINE DEDENT return min ( Powers ) NEW_LINE DEDENT N , P = 24 , 4 NEW_LINE if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( findPowers ( N , P ) ) NEW_LINE DEDENT
Longest common subarray in the given two arrays | Function to find the maximum length of equal subarray ; Auxiliary dp [ ] [ ] array ; Updating the dp [ ] [ ] table in Bottom Up approach ; If A [ i ] is equal to B [ i ] then dp [ j ] [ i ] = dp [ j + 1 ] [ i + 1 ] + 1 ; Find maximum of all the values in dp [ ] [ ] array to get the maximum length ; Update the length ; Return the maximum length ; Driver Code ; Function call to find maximum length of subarray
def FindMaxLength ( A , B ) : NEW_LINE INDENT n = len ( A ) NEW_LINE m = len ( B ) NEW_LINE dp = [ [ 0 for i in range ( n + 1 ) ] for i in range ( m + 1 ) ] NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( m - 1 , - 1 , - 1 ) : NEW_LINE INDENT if A [ i ] == B [ j ] : NEW_LINE INDENT dp [ j ] [ i ] = dp [ j + 1 ] [ i + 1 ] + 1 NEW_LINE DEDENT DEDENT DEDENT maxm = 0 NEW_LINE for i in dp : NEW_LINE INDENT for j in i : NEW_LINE INDENT maxm = max ( maxm , j ) NEW_LINE DEDENT DEDENT return maxm NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 2 , 8 , 2 , 1 ] NEW_LINE B = [ 8 , 2 , 1 , 4 , 7 ] NEW_LINE print ( FindMaxLength ( A , B ) ) NEW_LINE DEDENT
Maximum sum of elements divisible by K from the given array |
k = 16 NEW_LINE arr = [ 43 , 1 , 17 , 26 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE dp = [ [ 0 for i in range ( k ) ] for j in range ( n + 2 ) ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( k ) : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j ] NEW_LINE DEDENT dp [ i ] [ arr [ i - 1 ] % k ] = max ( dp [ i ] [ arr [ i - 1 ] % k ] , arr [ i - 1 ] ) NEW_LINE for j in range ( k ) : NEW_LINE INDENT m = ( j + arr [ i - 1 ] ) % k NEW_LINE if dp [ i - 1 ] [ j ] != 0 : NEW_LINE INDENT dp [ i ] [ m ] = max ( dp [ i ] [ m ] , arr [ i - 1 ] + dp [ i - 1 ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT print ( dp [ n ] [ 0 ] ) NEW_LINE
Maximum contiguous decreasing sequence obtained by removing any one element | Function to find the maximum length ; Initialise maximum length to 1 ; Initialise left [ ] to find the length of decreasing sequence from left to right ; Initialise right [ ] to find the length of decreasing sequence from right to left ; Initially store 1 at each index of left and right array ; Iterate over the array arr [ ] to store length of decreasing sequence that can be obtained at every index in the right array ; Store the length of longest continuous decreasing sequence in maximum ; Iterate over the array arr [ ] to store length of decreasing sequence that can be obtained at every index in the left array ; Check if we can obtain a longer decreasing sequence after removal of any element from the array arr [ ] with the help of left [ ] & right [ ] ; Return maximum length of sequence ; Driver code ; Function calling
def maxLength ( a , n ) : NEW_LINE INDENT maximum = 1 ; NEW_LINE left = [ 0 ] * n ; NEW_LINE right = [ 0 ] * n ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT left [ i ] = 1 ; NEW_LINE right [ i ] = 1 ; NEW_LINE DEDENT for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( a [ i ] > a [ i + 1 ] ) : NEW_LINE INDENT right [ i ] = right [ i + 1 ] + 1 ; NEW_LINE DEDENT maximum = max ( maximum , right [ i ] ) ; NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT if ( a [ i ] < a [ i - 1 ] ) : NEW_LINE INDENT left [ i ] = left [ i - 1 ] + 1 ; NEW_LINE DEDENT DEDENT if ( n > 2 ) : NEW_LINE INDENT for i in range ( 1 , n - 1 ) : NEW_LINE INDENT if ( a [ i - 1 ] > a [ i + 1 ] ) : NEW_LINE INDENT maximum = max ( maximum , left [ i - 1 ] + right [ i + 1 ] ) ; NEW_LINE DEDENT DEDENT DEDENT return maximum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 8 , 7 , 3 , 5 , 2 , 9 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( maxLength ( arr , n ) ) ; NEW_LINE DEDENT
Longest alternating subsequence in terms of positive and negative integers | Python3 program to find the length of longest alternate subsequence ; LAS [ i ] [ pos ] array to find the length of LAS till index i by including or excluding element arr [ i ] on the basis of value of pos ; Base Case ; If current element is positive and pos is true Include the current element and change pos to false ; Recurr for the next iteration ; If current element is negative and pos is false Include the current element and change pos to true ; Recurr for the next iteration ; If current element is excluded , reccur for next iteration ; Driver 's Code ; Print LAS
import numpy as np NEW_LINE LAS = np . zeros ( ( 1000 , 2 ) ) NEW_LINE for i in range ( 1000 ) : NEW_LINE INDENT for j in range ( 2 ) : NEW_LINE INDENT LAS [ i ] [ j ] = False NEW_LINE DEDENT DEDENT def solve ( arr , n , i , pos ) : NEW_LINE INDENT if ( i == n ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( LAS [ i ] [ pos ] ) : NEW_LINE INDENT return LAS [ i ] [ pos ] ; NEW_LINE DEDENT inc = 0 ; exc = 0 ; NEW_LINE if ( arr [ i ] > 0 and pos == True ) : NEW_LINE INDENT pos = False ; NEW_LINE inc = 1 + solve ( arr , n , i + 1 , pos ) ; NEW_LINE DEDENT elif ( arr [ i ] < 0 and pos == False ) : NEW_LINE INDENT pos = True ; NEW_LINE inc = 1 + solve ( arr , n , i + 1 , pos ) ; NEW_LINE DEDENT exc = solve ( arr , n , i + 1 , pos ) ; NEW_LINE LAS [ i ] [ pos ] = max ( inc , exc ) ; NEW_LINE return LAS [ i ] [ pos ] ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ - 1 , 2 , 3 , 4 , 5 , - 6 , 8 , - 99 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( max ( solve ( arr , n , 0 , 0 ) , solve ( arr , n , 0 , 1 ) ) ) ; NEW_LINE DEDENT