codeparrot/xlcost-text-to-code huggingface dataset:
Count possible permutations of given array satisfying the given conditions | Function to get the value of binomial coefficient ; Stores the value of binomial coefficient ; Since C ( N , R ) = C ( N , N - R ) ; Calculate the value of C ( N , R ) ; Function to get the count of permutations of the array that satisfy the condition ; Stores the value of C ( 2 N , N ) ; Stores the value of catalan number ; Return answer ; Driver Code
def binCoff ( N , R ) : NEW_LINE INDENT res = 1 NEW_LINE if ( R > ( N - R ) ) : NEW_LINE INDENT R = ( N - R ) NEW_LINE DEDENT for i in range ( R ) : NEW_LINE INDENT res *= ( N - i ) NEW_LINE res //= ( i + 1 ) NEW_LINE DEDENT return res NEW_LINE DEDENT def cntPermutation ( N ) : NEW_LINE INDENT C_2N_N = binCoff ( 2 * N , N ) NEW_LINE cntPerm = C_2N_N // ( N + 1 ) NEW_LINE return cntPerm NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE print ( cntPermutation ( N // 2 ) ) NEW_LINE DEDENT
Smallest submatrix with Kth maximum XOR | Function to print smallest index of Kth maximum Xor value of submatrices ; Dimensions of matrix ; Stores XOR values for every index ; Min heap to find the kth maximum XOR value ; Stores indices for corresponding XOR values ; Traversing matrix to calculate XOR values ; Insert calculated value in Min Heap ; If size exceeds k ; Remove the minimum ; Store smallest index containing xor [ i , j ] ; Stores the kth maximum element ; Print the required index ; Driver code ; Function call
def smallestPosition ( m , k ) : NEW_LINE INDENT n = len ( m ) NEW_LINE mm = len ( m [ 1 ] ) NEW_LINE xor = [ [ 0 for i in range ( mm ) ] for j in range ( n ) ] NEW_LINE minHeap = [ ] NEW_LINE Map = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( mm ) : NEW_LINE INDENT if i - 1 >= 0 : NEW_LINE INDENT a = xor [ i - 1 ] [ j ] NEW_LINE DEDENT else : NEW_LINE INDENT a = 0 NEW_LINE DEDENT if j - 1 >= 0 : NEW_LINE INDENT b = xor [ i ] [ j - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT b = 0 NEW_LINE DEDENT if i - 1 >= 0 and j - 1 >= 0 : NEW_LINE INDENT c = xor [ i - 1 ] [ j - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT c = 0 NEW_LINE DEDENT xor [ i ] [ j ] = m [ i ] [ j ] ^ a ^ b ^ c NEW_LINE minHeap . append ( xor [ i ] [ j ] ) NEW_LINE minHeap . sort ( ) NEW_LINE if ( len ( minHeap ) > k ) : NEW_LINE INDENT del minHeap [ 0 ] NEW_LINE DEDENT if xor [ i ] [ j ] not in Map : NEW_LINE INDENT Map [ xor [ i ] [ j ] ] = [ i , j ] NEW_LINE DEDENT DEDENT DEDENT minHeap . sort ( ) NEW_LINE kth_max_e = minHeap [ 0 ] NEW_LINE print ( ( Map [ kth_max_e ] [ 0 ] + 1 ) , ( Map [ kth_max_e ] [ 1 ] + 1 ) ) NEW_LINE DEDENT m = [ [ 1 , 2 , 3 ] , [ 2 , 2 , 1 ] , [ 2 , 4 , 2 ] ] NEW_LINE k = 1 NEW_LINE smallestPosition ( m , k ) NEW_LINE
Length of Longest Palindrome Substring | Function to obtain the length of the longest palindromic substring ; Length of given string ; Stores the maximum length ; Iterate over the string ; Iterate over the string ; Check for palindrome ; If string [ i , j - i + 1 ] is palindromic ; Return length of LPS ; Given string ; Function call
def longestPalSubstr ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE maxLength = 1 NEW_LINE start = 0 NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT for j in range ( i , len ( str ) , 1 ) : NEW_LINE INDENT flag = 1 NEW_LINE for k in range ( ( j - i + 1 ) // 2 ) : NEW_LINE INDENT if ( str [ i + k ] != str [ j - k ] ) : NEW_LINE INDENT flag = 0 NEW_LINE DEDENT DEDENT if ( flag != 0 and ( j - i + 1 ) > maxLength ) : NEW_LINE INDENT start = i NEW_LINE maxLength = j - i + 1 NEW_LINE DEDENT DEDENT DEDENT return maxLength NEW_LINE DEDENT str = " forgeeksskeegfor " NEW_LINE print ( longestPalSubstr ( str ) ) NEW_LINE
Maximize cost to empty an array by removing contiguous subarrays of equal elements | Initialize dp array ; Function that removes elements from array to maximize the cost ; Base case ; Check if an answer is stored ; Deleting count + 1 i . e . including the first element and starting a new sequence ; Removing [ left + 1 , i - 1 ] elements to continue with previous sequence ; Store the result ; Return answer ; Function to remove the elements ; Function Call ; Driver Code ; Given array ; Function Call
dp = [ [ [ - 1 for x in range ( 101 ) ] for y in range ( 101 ) ] for z in range ( 101 ) ] NEW_LINE def helper ( arr , left , right , count , m ) : NEW_LINE INDENT if ( left > right ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ left ] [ right ] [ count ] != - 1 ) : NEW_LINE INDENT return dp [ left ] [ right ] [ count ] NEW_LINE DEDENT ans = ( ( count + 1 ) * m + helper ( arr , left + 1 , right , 0 , m ) ) NEW_LINE for i in range ( left + 1 , right + 1 ) : NEW_LINE INDENT if ( arr [ i ] == arr [ left ] ) : NEW_LINE ans = ( max ( ans , helper ( arr , left + 1 , i - 1 , 0 , m ) + helper ( arr , i , right , count + 1 , m ) ) ) NEW_LINE dp [ left ] [ right ] [ count ] = ans NEW_LINE return ans NEW_LINE DEDENT DEDENT def maxPoints ( arr , n , m ) : NEW_LINE INDENT length = n NEW_LINE global dp NEW_LINE return helper ( arr , 0 , length - 1 , 0 , m ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 3 , 2 , 2 , 2 , 3 , 4 , 3 , 1 ] NEW_LINE M = 3 NEW_LINE N = len ( arr ) NEW_LINE print ( maxPoints ( arr , N , M ) ) NEW_LINE DEDENT
Minimum cost to convert M to N by repeated addition of its even divisors | Python3 program for the above approach ; Function to find the value of minimum steps to convert m to n ; Base Case ; If n exceeds m ; Iterate through all possible even divisors of m ; If m is divisible by i , then find the minimum cost ; Add the cost to convert m to m + i and recursively call next state ; Return min_cost ; Driver Code ; Function call ; If conversion is not possible ; Print the cost
inf = 1000000008 NEW_LINE def minSteps ( m , n ) : NEW_LINE INDENT if ( n == m ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( m > n ) : NEW_LINE INDENT return inf NEW_LINE DEDENT min_cost = inf NEW_LINE for i in range ( 2 , m , 2 ) : NEW_LINE INDENT if ( m % i == 0 ) : NEW_LINE INDENT min_cost = min ( min_cost , m / i + minSteps ( m + i , n ) ) NEW_LINE DEDENT DEDENT return min_cost NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT M = 6 NEW_LINE N = 24 NEW_LINE minimum_cost = minSteps ( M , N ) NEW_LINE if minimum_cost == inf : NEW_LINE INDENT minimum_cost = - 1 NEW_LINE DEDENT print ( minimum_cost ) NEW_LINE DEDENT
Represent a number as the sum of positive numbers ending with 9 | Function to find the minimum count of numbers ending with 9 to form N ; Extract last digit of N ; Calculate the last digit ; If the last digit satisfies the condition ; Driver Code
def minCountOfNumbers ( N ) : NEW_LINE INDENT k = N % 10 NEW_LINE z = N - ( 9 * ( 9 - k ) ) NEW_LINE if ( z >= 9 and z % 10 == 9 ) : NEW_LINE INDENT return 10 - k NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 156 NEW_LINE print ( minCountOfNumbers ( N ) ) NEW_LINE DEDENT
Binary Matrix after flipping submatrices in given range for Q queries | Function to flip a submatrices ; Boundaries of the submatrix ; Iterate over the submatrix ; Check for 1 or 0 and flip accordingly ; Function to perform the queries ; Driver Code ; Function call
def manipulation ( matrix , q ) : NEW_LINE INDENT x1 , y1 , x2 , y2 = q NEW_LINE for i in range ( x1 - 1 , x2 ) : NEW_LINE INDENT for j in range ( y1 - 1 , y2 ) : NEW_LINE INDENT if matrix [ i ] [ j ] : NEW_LINE INDENT matrix [ i ] [ j ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT matrix [ i ] [ j ] = 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT def queries_fxn ( matrix , queries ) : NEW_LINE INDENT for q in queries : NEW_LINE INDENT manipulation ( matrix , q ) NEW_LINE DEDENT DEDENT matrix = [ [ 0 , 1 , 0 ] , [ 1 , 1 , 0 ] ] NEW_LINE queries = [ [ 1 , 1 , 2 , 3 ] , [ 1 , 1 , 1 , 1 ] , [ 1 , 2 , 2 , 3 ] ] NEW_LINE queries_fxn ( matrix , queries ) NEW_LINE print ( matrix ) NEW_LINE
Minimum repeated addition of even divisors of N required to convert N to M | INF is the maximum value which indicates Impossible state ; Recursive Function that considers all possible even divisors of cur ; Indicates impossible state ; Return 0 if cur == M ; Initially it is set to INF that means we can 't transform cur to M ; Loop to find even divisors of cur ; If i is divisor of cur ; If i is even ; Finding divisors recursively for cur + i ; Check another divisor ; Find recursively for cur + ( cur / i ) ; Return the answer ; Function that finds the minimum operation to reduce N to M ; INF indicates impossible state ; Driver Code ; Given N and M ; Function call
INF = int ( 1e7 ) ; NEW_LINE def min_op ( cur , M ) : NEW_LINE INDENT if ( cur > M ) : NEW_LINE INDENT return INF ; NEW_LINE DEDENT if ( cur == M ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT op = int ( INF ) ; NEW_LINE for i in range ( 2 , int ( cur ** 1 / 2 ) + 1 ) : NEW_LINE INDENT if ( cur % i == 0 ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT op = min ( op , 1 + min_op ( cur + i , M ) ) ; NEW_LINE DEDENT if ( ( cur / i ) != i and ( cur / i ) % 2 == 0 ) : NEW_LINE INDENT op = min ( op , 1 + min_op ( cur + ( cur // i ) , M ) ) ; NEW_LINE DEDENT DEDENT DEDENT return op ; NEW_LINE DEDENT def min_operations ( N , M ) : NEW_LINE INDENT op = min_op ( N , M ) ; NEW_LINE if ( op >= INF ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( op ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 ; NEW_LINE M = 24 ; NEW_LINE min_operations ( N , M ) ; NEW_LINE DEDENT
Minimum length of subsequence having unit GCD | Function that finds the prime factors of a number ; To store the prime factor ; 2 s that divide n ; N must be odd at this point Skip one element ; Update the prime factor ; If n is a prime number greater than 2 ; Function that finds the shortest subsequence ; Check if the prime factor of first number , is also the prime factor of the rest numbers in array ; Set corresponding bit of prime factor to 1 , it means both these numbers have the same prime factor ; If no states encountered so far continue for this combination of bits ; Update this state with minimum ways to reach this state ; Function that print the minimum length of subsequence ; Find the prime factors of the first number ; Initialize the array with maximum steps , size of the array + 1 for instance ; Total number of set bits is equal to the total number of prime factors ; Indicates there is one way to reach the number under consideration ; State 0 corresponds to gcd of 1 ; If not found such subsequence then print " - 1" ; Else print the length ; Driver Code ; Given array arr [ ] ; Function Call
def findPrimeFactors ( n ) : NEW_LINE INDENT primeFactors = [ 0 for i in range ( 9 ) ] NEW_LINE j = 0 NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT primeFactors [ j ] = 2 NEW_LINE j += 1 NEW_LINE while ( n % 2 == 0 ) : NEW_LINE INDENT n >>= 1 NEW_LINE DEDENT DEDENT i = 3 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT primeFactors [ j ] = i NEW_LINE j += 1 NEW_LINE while ( n % i == 0 ) : NEW_LINE INDENT n //= i NEW_LINE DEDENT DEDENT i += 2 NEW_LINE DEDENT if ( n > 2 ) : NEW_LINE INDENT primeFactors [ j ] = n NEW_LINE j += 1 NEW_LINE DEDENT for i in range ( 0 , j + 1 ) : NEW_LINE INDENT primeFactors [ i ] = 0 NEW_LINE DEDENT return primeFactors NEW_LINE DEDENT def findShortestSubsequence ( dp , a , index , primeFactors ) : NEW_LINE INDENT n = len ( a ) NEW_LINE for j in range ( index , n ) : NEW_LINE INDENT bitmask = 0 NEW_LINE for p in range ( len ( primeFactors ) ) : NEW_LINE INDENT if ( primeFactors [ p ] != 0 and a [ j ] % primeFactors [ p ] == 0 ) : NEW_LINE INDENT bitmask ^= ( 1 << p ) NEW_LINE DEDENT DEDENT for i in range ( len ( dp ) ) : NEW_LINE INDENT if ( dp [ i ] == n + 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT dp [ bitmask & i ] = min ( dp [ bitmask & i ] , dp [ i ] + 1 ) NEW_LINE DEDENT DEDENT DEDENT def printMinimumLength ( a ) : NEW_LINE INDENT mn = len ( a ) + 1 NEW_LINE for i in range ( len ( a ) - 1 ) : NEW_LINE INDENT primeFactors = findPrimeFactors ( a [ i ] ) NEW_LINE n = len ( primeFactors ) NEW_LINE dp = [ 0 for i in range ( 1 << n ) ] NEW_LINE dp = [ len ( a ) + 1 for i in range ( len ( dp ) ) ] NEW_LINE setBits = ( 1 << n ) - 1 NEW_LINE dp [ setBits ] = 1 NEW_LINE findShortestSubsequence ( dp , a , i + 1 , primeFactors ) NEW_LINE mn = min ( dp [ 0 ] , mn ) NEW_LINE DEDENT if ( mn == len ( a ) + 1 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( mn ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 6 , 12 , 3 ] NEW_LINE printMinimumLength ( arr ) NEW_LINE DEDENT
Maximum sum of Bitwise XOR of all elements of two equal length subsets | Function that finds the maximum Bitwise XOR sum of the two subset ; Check if the current state is already computed ; Initialize answer to minimum value ; Iterate through all possible pairs ; Check whether ith bit and jth bit of mask is not set then pick the pair ; For all possible pairs find maximum value pick current a [ i ] , a [ j ] and set i , j th bits in mask ; Store the maximum value and return the answer ; Driver Code ; Given array arr [ ] ; Declare Initialize the dp states ; Function call
def xorSum ( a , n , mask , dp ) : NEW_LINE INDENT if ( dp [ mask ] != - 1 ) : NEW_LINE INDENT return dp [ mask ] NEW_LINE DEDENT max_value = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( i != j and ( mask & ( 1 << i ) ) == 0 and ( mask & ( 1 << j ) ) == 0 ) : NEW_LINE INDENT max_value = max ( max_value , ( a [ i ] ^ a [ j ] ) + xorSum ( a , n , ( mask | ( 1 << i ) | ( 1 << j ) ) , dp ) ) NEW_LINE DEDENT DEDENT DEDENT dp [ mask ] = max_value NEW_LINE return dp [ mask ] NEW_LINE DEDENT n = 4 NEW_LINE arr = [ 1 , 2 , 3 , 4 ] NEW_LINE dp = [ - 1 ] * ( ( 1 << n ) + 5 ) NEW_LINE print ( xorSum ( arr , n , 0 , dp ) ) NEW_LINE
Count of ways in which N can be represented as sum of Fibonacci numbers without repetition | Python3 program for the above approach ; Function to generate the fibonacci number ; First two number of fibonacci sqequence ; Function to find maximum ways to represent num as the sum of fibonacci number ; Generate the Canonical form of given number ; Reverse the number ; Base condition of dp1 and dp2 ; Iterate from 1 to cnt ; Calculate dp1 [ ] ; Calculate dp2 [ ] ; Return final ans ; Function call to generate the fibonacci numbers ; Given number ; Function call
fib = [ 0 ] * 101 NEW_LINE dp1 = [ 0 ] * 101 NEW_LINE dp2 = [ 0 ] * 101 NEW_LINE v = [ 0 ] * 101 NEW_LINE def fibonacci ( ) : NEW_LINE INDENT fib [ 1 ] = 1 NEW_LINE fib [ 2 ] = 2 NEW_LINE for i in range ( 3 , 87 + 1 ) : NEW_LINE INDENT fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] NEW_LINE DEDENT DEDENT def find ( num ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( 87 , 0 , - 1 ) : NEW_LINE INDENT if ( num >= fib [ i ] ) : NEW_LINE INDENT v [ cnt ] = i NEW_LINE cnt += 1 NEW_LINE num -= fib [ i ] NEW_LINE DEDENT DEDENT v [ : : - 1 ] NEW_LINE dp1 [ 0 ] = 1 NEW_LINE dp2 [ 0 ] = ( v [ 0 ] - 1 ) // 2 NEW_LINE for i in range ( 1 , cnt ) : NEW_LINE INDENT dp1 [ i ] = dp1 [ i - 1 ] + dp2 [ i - 1 ] NEW_LINE dp2 [ i ] = ( ( ( v [ i ] - v [ i - 1 ] ) // 2 ) * dp2 [ i - 1 ] + ( ( v [ i ] - v [ i - 1 ] - 1 ) // 2 ) * dp1 [ i - 1 ] ) NEW_LINE DEDENT return dp1 [ cnt - 1 ] + dp2 [ cnt - 1 ] NEW_LINE DEDENT fibonacci ( ) NEW_LINE num = 13 NEW_LINE print ( find ( num ) ) NEW_LINE
Count of N | Function to find count of N - digit numbers with single digit XOR ; Range of numbers ; Calculate XOR of digits ; If XOR <= 9 , then increment count ; Print the count ; Given number ; Function call
def countNums ( N ) : NEW_LINE INDENT l = pow ( 10 , N - 1 ) NEW_LINE r = pow ( 10 , N ) - 1 NEW_LINE count = 0 NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT xorr = 0 NEW_LINE temp = i NEW_LINE while ( temp > 0 ) : NEW_LINE INDENT xorr = xorr ^ ( temp % 10 ) NEW_LINE temp //= 10 NEW_LINE DEDENT if ( xorr <= 9 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE DEDENT N = 2 NEW_LINE countNums ( N ) NEW_LINE
Maximize length of Non | Python3 program to implement the above approach ; Function to find the maximum length non decreasing subarray by reversing at most one subarray ; dp [ i ] [ j ] be the longest subsequence of a [ 0. . . i ] with first j parts ; Maximum length sub - sequence of ( 0. . ) ; Maximum length sub - sequence of ( 0. . 1. . ) ; Maximum length sub - sequence of ( 0. . 1. .0 . . ) ; Maximum length sub - sequence of ( 0. . 1. .0 . .1 . . ) ; Find the max length subsequence ; Print the answer ; Driver Code
import sys NEW_LINE def main ( arr , n ) : NEW_LINE INDENT dp = [ [ 0 for x in range ( n ) ] for y in range ( 4 ) ] NEW_LINE if arr [ 0 ] == 0 : NEW_LINE INDENT dp [ 0 ] [ 0 ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT dp [ 1 ] [ 0 ] = 1 NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT if arr [ i ] == 0 : NEW_LINE INDENT dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] NEW_LINE DEDENT DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT if arr [ i ] == 1 : NEW_LINE INDENT dp [ 1 ] [ i ] = max ( dp [ 1 ] [ i - 1 ] + 1 , dp [ 0 ] [ i - 1 ] + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ 1 ] [ i ] = dp [ 1 ] [ i - 1 ] NEW_LINE DEDENT DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT if arr [ i ] == 0 : NEW_LINE INDENT dp [ 2 ] [ i ] = max ( [ dp [ 2 ] [ i - 1 ] + 1 , dp [ 1 ] [ i - 1 ] + 1 , dp [ 0 ] [ i - 1 ] + 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ 2 ] [ i ] = dp [ 2 ] [ i - 1 ] NEW_LINE DEDENT DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT if arr [ i ] == 1 : NEW_LINE INDENT dp [ 3 ] [ i ] = max ( [ dp [ 3 ] [ i - 1 ] + 1 , dp [ 2 ] [ i - 1 ] + 1 , dp [ 1 ] [ i - 1 ] + 1 , dp [ 0 ] [ i - 1 ] + 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ 3 ] [ i ] = dp [ 3 ] [ i - 1 ] NEW_LINE DEDENT DEDENT ans = max ( [ dp [ 2 ] [ n - 1 ] , dp [ 1 ] [ n - 1 ] , dp [ 0 ] [ n - 1 ] , dp [ 3 ] [ n - 1 ] ] ) NEW_LINE print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE arr = [ 0 , 1 , 0 , 1 ] NEW_LINE main ( arr , n ) NEW_LINE DEDENT
Print the Maximum Subarray Sum | Function to print the elements of Subarray with maximum sum ; Initialize currMax and globalMax with first value of nums ; Iterate for all the elemensts of the array ; Update currMax ; Check if currMax is greater than globalMax ; Traverse in left direction to find start Index of subarray ; Decrement the start index ; Printing the elements of subarray with max sum ; Given array arr [ ] ; Function call
def SubarrayWithMaxSum ( nums ) : NEW_LINE INDENT currMax = nums [ 0 ] NEW_LINE globalMax = nums [ 0 ] NEW_LINE for i in range ( 1 , len ( nums ) ) : NEW_LINE INDENT currMax = max ( nums [ i ] , nums [ i ] + currMax ) NEW_LINE if ( currMax > globalMax ) : NEW_LINE INDENT globalMax = currMax NEW_LINE endIndex = i NEW_LINE DEDENT DEDENT startIndex = endIndex NEW_LINE while ( startIndex >= 0 ) : NEW_LINE INDENT globalMax -= nums [ startIndex ] NEW_LINE if ( globalMax == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT startIndex -= 1 NEW_LINE DEDENT for i in range ( startIndex , endIndex + 1 ) : NEW_LINE INDENT print ( nums [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ - 2 , - 5 , 6 , - 2 , - 3 , 1 , 5 , - 6 ] NEW_LINE SubarrayWithMaxSum ( arr ) NEW_LINE
Count of distinct Primonacci Numbers in a given range [ L , R ] | Python3 implementation of the above approach ; Stores list of all primes ; Function to find all primes ; To mark the prime ones ; Initially all indices as prime ; If i is prime ; Set all multiples of i as non - prime ; Adding all primes to a list ; Function to return the count of Primonacci Numbers in the range [ l , r ] ; dp [ i ] contains ith Primonacci Number ; Stores the Primonacci Numbers ; Iterate over all smaller primes ; If Primonacci number lies within the range [ L , R ] ; Count of Primonacci Numbers ; Driver Code
M = 100005 NEW_LINE primes = [ ] NEW_LINE def sieve ( ) : NEW_LINE INDENT mark = [ False ] * M NEW_LINE for i in range ( 2 , M ) : NEW_LINE INDENT mark [ i ] = True NEW_LINE DEDENT i = 2 NEW_LINE while i * i < M : NEW_LINE INDENT if ( mark [ i ] ) : NEW_LINE INDENT j = i * i NEW_LINE while j < M : NEW_LINE INDENT mark [ j ] = False NEW_LINE j += i NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT for i in range ( 2 , M ) : NEW_LINE INDENT if ( mark [ i ] ) : NEW_LINE INDENT primes . append ( i ) NEW_LINE DEDENT DEDENT DEDENT def countPrimonacci ( l , r ) : NEW_LINE INDENT dp = [ ] NEW_LINE dp . append ( 1 ) NEW_LINE dp . append ( 1 ) NEW_LINE i = 2 NEW_LINE s = set ( ) NEW_LINE while ( True ) : NEW_LINE INDENT x = 0 NEW_LINE for j in range ( len ( primes ) ) : NEW_LINE INDENT p = primes [ j ] NEW_LINE if ( p >= i ) : NEW_LINE INDENT break NEW_LINE DEDENT x += dp [ i - p ] NEW_LINE DEDENT if ( x >= l and x <= r ) : NEW_LINE INDENT s . add ( x ) NEW_LINE DEDENT if ( x > r ) : NEW_LINE INDENT break NEW_LINE DEDENT dp . append ( x ) NEW_LINE i += 1 NEW_LINE DEDENT print ( len ( s ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT sieve ( ) NEW_LINE L , R = 1 , 10 NEW_LINE countPrimonacci ( L , R ) NEW_LINE DEDENT
Queries to count distinct Binary Strings of all lengths from N to M satisfying given properties | Python3 program to implement the above approach ; Function to calculate the count of possible strings ; Initialize dp [ 0 ] ; dp [ i ] represents count of strings of length i ; Add dp [ i - k ] if i >= k ; Update Prefix Sum Array ; Driver Code
N = int ( 1e5 + 5 ) NEW_LINE MOD = 1000000007 NEW_LINE dp = [ 0 ] * N NEW_LINE def countStrings ( K , Q ) : NEW_LINE INDENT dp [ 0 ] = 1 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT dp [ i ] = dp [ i - 1 ] NEW_LINE if ( i >= K ) : NEW_LINE INDENT dp [ i ] = ( dp [ i ] + dp [ i - K ] ) % MOD NEW_LINE DEDENT DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT dp [ i ] = ( dp [ i ] + dp [ i - 1 ] ) % MOD NEW_LINE DEDENT for i in range ( len ( Q ) ) : NEW_LINE INDENT ans = dp [ Q [ i ] [ 1 ] ] - dp [ Q [ i ] [ 0 ] - 1 ] NEW_LINE if ( ans < 0 ) : NEW_LINE INDENT ans += MOD NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT DEDENT K = 3 NEW_LINE Q = [ [ 1 , 4 ] , [ 3 , 7 ] ] NEW_LINE countStrings ( K , Q ) NEW_LINE
Minimum possible sum of prices of a Triplet from the given Array | Python3 program to implement the above approach ; Function to minimize the sum of price by taking a triplet ; Initialize a dp [ ] list ; Stores the final result ; Iterate for all values till N ; Check if num [ j ] > num [ i ] ; Update dp [ j ] if it is greater than stored value ; Update the minimum sum as ans ; If there is no minimum sum exist then print - 1 else print the ans ; Driver code
import sys ; NEW_LINE def minSum ( n , num , price ) : NEW_LINE INDENT dp = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT dp [ i ] = sys . maxsize NEW_LINE DEDENT ans = sys . maxsize NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( num [ j ] > num [ i ] ) : NEW_LINE INDENT dp [ j ] = min ( dp [ j ] , price [ i ] + price [ j ] ) NEW_LINE ans = min ( ans , dp [ i ] + price [ j ] ) NEW_LINE DEDENT DEDENT DEDENT if ans is not sys . maxsize : NEW_LINE INDENT return ans NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT num = [ 2 , 4 , 6 , 7 , 8 ] NEW_LINE price = [ 10 , 20 , 100 , 20 , 40 ] NEW_LINE n = len ( price ) NEW_LINE print ( minSum ( n , num , price ) ) NEW_LINE DEDENT