codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of Nodes to be removed such that no subtree has more than K nodes | Variables used to store data globally ; Adjacency list representation of tree ; Function to perform DFS Traversal ; Mark the node as true ; Traverse adjacency list of child node ; If already visited then omit the node ; Add number of nodes in subtree ; Increment the count ; Return the nodes ; Function to add edges in graph ; Function that finds the number of nodes to be removed such that every subtree has size at most K ; Function Call to find the number of nodes to remove ; Print Removed Nodes ; Driver Code ;  ; Insert of nodes in graph ; Required subtree size ; Function Call
N = 20 NEW_LINE visited = [ False for i in range ( N ) ] NEW_LINE K = 0 NEW_LINE removals = 0 NEW_LINE removed_nodes = [ ] NEW_LINE adj = [ [ ] for i in range ( N ) ] NEW_LINE def dfs ( s ) : NEW_LINE INDENT global removals NEW_LINE visited [ s ] = True NEW_LINE nodes = 1 NEW_LINE for child in adj [ s ] : NEW_LINE INDENT if ( visited [ child ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT nodes += dfs ( child ) NEW_LINE DEDENT if ( nodes > K ) : NEW_LINE INDENT removals += 1 NEW_LINE removed_nodes . append ( s ) NEW_LINE nodes = 0 NEW_LINE DEDENT return nodes NEW_LINE DEDENT def addEdge ( a , b ) : NEW_LINE INDENT adj [ a ] . append ( b ) NEW_LINE adj [ b ] . append ( a ) NEW_LINE DEDENT def findRemovedNodes ( K ) : NEW_LINE INDENT dfs ( 1 ) NEW_LINE print ( " Number ▁ of ▁ nodes ▁ removed : ▁ " , removals ) NEW_LINE print ( " Removed ▁ Nodes : ▁ " , end = ' ▁ ' ) NEW_LINE for node in removed_nodes : NEW_LINE INDENT print ( node , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE / * Creating list for all nodes * / NEW_LINE INDENT addEdge ( 1 , 2 ) NEW_LINE addEdge ( 1 , 3 ) NEW_LINE addEdge ( 2 , 4 ) NEW_LINE addEdge ( 2 , 5 ) NEW_LINE addEdge ( 3 , 6 ) NEW_LINE K = 3 NEW_LINE findRemovedNodes ( K ) NEW_LINE DEDENT
Queries to find sum of distance of a given node to every leaf node in a Weighted Tree | MAX size ; Graph with { destination , weight } ; For storing the sum for ith node ; Leaves in subtree of ith . ; dfs to find sum of distance of leaves in the subtree of a node ; flag is the node is leaf or not ; Skipping if parent ; Setting flag to false ; Doing dfs call ; Doing calculation in postorder . ; If the node is leaf then we just increment the no . of leaves under the subtree of a node ; Adding num of leaves ; Calculating answer for the sum in the subtree ; dfs function to find the sum of distance of leaves outside the subtree ; Number of leaves other than the leaves in the subtree of i ; Adding the contribution of leaves outside to the ith node ; Adding the leafs outside to ith node 's leaves. ; Calculating the sum of distance of leaves in the subtree of a node assuming the root of the tree is 1 ; Calculating the sum of distance of leaves outside the subtree of node assuming the root of the tree is 1 ; Answering the queries ; Driver Code ; 1 ( 4 ) / \ ( 2 ) / \ 4 2 ( 5 ) / \ ( 3 ) / \ 5 3 ; Initialising tree
N = 10 ** 5 + 5 NEW_LINE v = [ [ ] for i in range ( N ) ] NEW_LINE dp = [ 0 ] * N NEW_LINE leaves = [ 0 ] * ( N ) NEW_LINE n = 0 NEW_LINE def dfs ( a , par ) : NEW_LINE INDENT leaf = 1 NEW_LINE for i in v [ a ] : NEW_LINE INDENT if ( i [ 0 ] == par ) : NEW_LINE INDENT continue NEW_LINE DEDENT leaf = 0 NEW_LINE dfs ( i [ 0 ] , a ) NEW_LINE DEDENT if ( leaf == 1 ) : NEW_LINE INDENT leaves [ a ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT for i in v [ a ] : NEW_LINE INDENT if ( i [ 0 ] == par ) : NEW_LINE INDENT continue NEW_LINE DEDENT leaves [ a ] += leaves [ i [ 0 ] ] NEW_LINE dp [ a ] = ( dp [ a ] + dp [ i [ 0 ] ] + leaves [ i [ 0 ] ] * i [ 1 ] ) NEW_LINE DEDENT DEDENT DEDENT def dfs2 ( a , par ) : NEW_LINE INDENT for i in v [ a ] : NEW_LINE INDENT if ( i [ 0 ] == par ) : NEW_LINE INDENT continue NEW_LINE DEDENT leafOutside = leaves [ a ] - leaves [ i [ 0 ] ] NEW_LINE dp [ i [ 0 ] ] += ( dp [ a ] - dp [ i [ 0 ] ] ) NEW_LINE dp [ i [ 0 ] ] += i [ 1 ] * ( leafOutside - leaves [ i [ 0 ] ] ) NEW_LINE leaves [ i [ 0 ] ] += leafOutside NEW_LINE dfs2 ( i [ 0 ] , a ) NEW_LINE DEDENT DEDENT def answerQueries ( queries ) : NEW_LINE INDENT dfs ( 1 , 0 ) NEW_LINE dfs2 ( 1 , 0 ) NEW_LINE for i in range ( len ( queries ) ) : NEW_LINE INDENT print ( dp [ queries [ i ] ] ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE v [ 1 ] . append ( [ 4 , 4 ] ) NEW_LINE v [ 4 ] . append ( [ 1 , 4 ] ) NEW_LINE v [ 1 ] . append ( [ 2 , 2 ] ) NEW_LINE v [ 2 ] . append ( [ 1 , 2 ] ) NEW_LINE v [ 2 ] . append ( [ 3 , 3 ] ) NEW_LINE v [ 3 ] . append ( [ 2 , 3 ] ) NEW_LINE v [ 2 ] . append ( [ 5 , 5 ] ) NEW_LINE v [ 5 ] . append ( [ 2 , 5 ] ) NEW_LINE queries = [ 1 , 3 , 5 ] NEW_LINE answerQueries ( queries ) NEW_LINE DEDENT
Minimum changes required to make each path in a matrix palindrome | Function for counting changes ; Maximum distance possible is ( n - 1 + m - 1 ) ; Stores the maximum element ; Update the maximum ; Stores frequencies of values for respective distances ; Initialize frequencies of cells as 0 ; Count frequencies of cell values in the matrix ; Increment frequency of value at distance i + j ; Store the most frequent value at i - th distance from ( 0 , 0 ) and ( N - 1 , M - 1 ) ; Calculate max frequency and total cells at distance i ; Count changes required to convert all cells at i - th distance to most frequent value ; Driver code
def countChanges ( matrix , n , m ) : NEW_LINE INDENT dist = n + m - 1 NEW_LINE Max_element = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT Max_element = max ( Max_element , matrix [ i ] [ j ] ) NEW_LINE DEDENT DEDENT freq = [ [ 0 for i in range ( Max_element + 1 ) ] for j in range ( dist ) ] NEW_LINE for i in range ( dist ) : NEW_LINE INDENT for j in range ( Max_element + 1 ) : NEW_LINE INDENT freq [ i ] [ j ] = 0 NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT freq [ i + j ] [ matrix [ i ] [ j ] ] += 1 NEW_LINE DEDENT DEDENT min_changes_sum = 0 NEW_LINE for i in range ( dist // 2 ) : NEW_LINE INDENT maximum = 0 NEW_LINE total_values = 0 NEW_LINE for j in range ( Max_element + 1 ) : NEW_LINE INDENT maximum = max ( maximum , freq [ i ] [ j ] + freq [ n + m - 2 - i ] [ j ] ) NEW_LINE total_values += ( freq [ i ] [ j ] + freq [ n + m - 2 - i ] [ j ] ) NEW_LINE DEDENT min_changes_sum += total_values - maximum NEW_LINE DEDENT return min_changes_sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 7 , 0 , 3 , 1 , 8 , 1 , 3 ] , [ 0 , 4 , 0 , 1 , 0 , 4 , 0 ] , [ 3 , 1 , 8 , 3 , 1 , 0 , 7 ] ] NEW_LINE minChanges = countChanges ( mat , 3 , 7 ) NEW_LINE print ( minChanges ) NEW_LINE DEDENT
Count of numbers upto N having absolute difference of at most K between any two adjacent digits | Table to store solution of each subproblem ; Function to calculate all possible numbers ; Check if position reaches end that is equal to length of N ; Check if the result is already computed simply return it ; Maximum limit upto which we can place digit . If tight is false , means number has already become smaller so we can place any digit , otherwise N [ pos ] ; Chekc if start is false the number has not started yet ; Check if we do not start the number at pos then recur forward ; If we start the number we can place any digit from 1 to upper_limit ; Finding the new tight ; Condition if the number has already started ; We can place digit upto upperbound & absolute difference with previous digit much be atmost K ; Absolute difference atmost K ; Store the solution to this subproblem ; Driver code ; Function call
dp = [ [ [ [ - 1 for i in range ( 2 ) ] for j in range ( 2 ) ] for i in range ( 10 ) ] for j in range ( 1002 ) ] NEW_LINE def possibleNumber ( pos , previous , tight , start , N , K ) : NEW_LINE INDENT if ( pos == len ( N ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( dp [ pos ] [ previous ] [ tight ] [ start ] != - 1 ) : NEW_LINE INDENT return dp [ pos ] [ previous ] [ tight ] [ start ] NEW_LINE DEDENT res = 0 NEW_LINE if ( tight ) : NEW_LINE INDENT upper_limit = ord ( N [ pos ] ) - ord ( '0' ) NEW_LINE DEDENT else : NEW_LINE INDENT upper_limit = 9 NEW_LINE DEDENT if ( not start ) : NEW_LINE INDENT res = possibleNumber ( pos + 1 , previous , False , False , N , K ) NEW_LINE for i in range ( 1 , upper_limit + 1 ) : NEW_LINE INDENT if ( tight and i == upper_limit ) : NEW_LINE INDENT new_tight = 1 NEW_LINE DEDENT else : NEW_LINE INDENT new_tight = 0 NEW_LINE DEDENT res += possibleNumber ( pos + 1 , i , new_tight , True , N , K ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for i in range ( upper_limit + 1 ) : NEW_LINE INDENT if ( tight and i == upper_limit ) : NEW_LINE INDENT new_tight = 1 NEW_LINE DEDENT else : NEW_LINE INDENT new_tight = 0 NEW_LINE DEDENT if ( abs ( i - previous ) <= K ) : NEW_LINE INDENT res += possibleNumber ( pos + 1 , i , new_tight , True , N , K ) NEW_LINE DEDENT DEDENT DEDENT dp [ pos ] [ previous ] [ tight ] [ start ] = res NEW_LINE return dp [ pos ] [ previous ] [ tight ] [ start ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = "20" NEW_LINE K = 2 NEW_LINE print ( possibleNumber ( 0 , 0 , True , False , N , K ) ) NEW_LINE DEDENT
Minimum cost of reducing Array by merging any adjacent elements repetitively | Python3 program for the above approach ; Function to find the total minimum cost of merging two consecutive numbers ; Find the size of numbers [ ] ; If array is empty , return 0 ; To store the prefix Sum of numbers array numbers [ ] ; Traverse numbers [ ] to find the prefix sum ; dp table to memoised the value ; For single numbers cost is zero ; Iterate for length >= 1 ; Find sum in range [ i . . j ] ; Initialise dp [ i ] [ j ] to _MAX ; Iterate for all possible K to find the minimum cost ; Update the minimum sum ; Return the final minimum cost ; Given set of numbers ; Function call
import sys NEW_LINE def mergeTwoNumbers ( numbers ) : NEW_LINE INDENT n = len ( numbers ) NEW_LINE if ( len ( numbers ) == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT prefixSum = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT prefixSum [ i ] = ( prefixSum [ i - 1 ] + numbers [ i - 1 ] ) NEW_LINE DEDENT dp = [ [ 0 for i in range ( n + 1 ) ] for j in range ( n + 1 ) ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT dp [ i ] [ i ] = 0 NEW_LINE DEDENT for p in range ( 2 , n + 1 ) : NEW_LINE INDENT for i in range ( 1 , n - p + 2 ) : NEW_LINE INDENT j = i + p - 1 NEW_LINE sum = prefixSum [ j ] - prefixSum [ i - 1 ] NEW_LINE dp [ i ] [ j ] = sys . maxsize NEW_LINE for k in range ( i , j ) : NEW_LINE INDENT dp [ i ] [ j ] = min ( dp [ i ] [ j ] , ( dp [ i ] [ k ] + dp [ k + 1 ] [ j ] + sum ) ) NEW_LINE DEDENT DEDENT DEDENT return dp [ 1 ] [ n ] NEW_LINE DEDENT arr1 = [ 6 , 4 , 4 , 6 ] NEW_LINE print ( mergeTwoNumbers ( arr1 ) ) NEW_LINE
Maximum sum possible for every node by including it in a segment of N | Stores the maximum sum possible for every node by including them in a segment with their successors ; Stores the maximum sum possible for every node by including them in a segment with their ancestors ; Store the maximum sum for every node by including it in a segment with its successors ; Update the maximum sums for each node by including them in a sequence with their ancestors ; Condition to check , if current node is not root ; Add edges ; Function to find the maximum answer for each node ; Compute the maximum sums with successors ; Store the computed maximums ; Update the maximum sums by including their ancestors ; Print the desired result ; Driver code ; Number of nodes ; Graph ; Add edges ; Weight of each node ; Compute the max sum of segments for each node ; Print the answer for every node
dp1 = [ 0 for i in range ( 100005 ) ] NEW_LINE dp2 = [ 0 for i in range ( 100005 ) ] NEW_LINE def dfs1 ( u , par , g , weight ) : NEW_LINE INDENT dp1 [ u ] = weight [ u ] NEW_LINE for c in g [ u ] : NEW_LINE INDENT if ( c != par ) : NEW_LINE INDENT dfs1 ( c , u , g , weight ) NEW_LINE dp1 [ u ] += max ( 0 , dp1 ) NEW_LINE DEDENT DEDENT DEDENT def dfs2 ( u , par , g , weight ) : NEW_LINE INDENT if ( par != 0 ) : NEW_LINE INDENT maxSumAncestors = dp2 [ par ] - max ( 0 , dp1 [ u ] ) NEW_LINE dp2 [ u ] = dp1 [ u ] + max ( 0 , maxSumAncestors ) NEW_LINE DEDENT for c in g [ u ] : NEW_LINE INDENT if ( c != par ) : NEW_LINE INDENT dfs2 ( c , u , g , weight ) NEW_LINE DEDENT DEDENT DEDENT def addEdge ( u , v , g ) : NEW_LINE INDENT g [ u ] . append ( v ) NEW_LINE g [ v ] . append ( u ) NEW_LINE DEDENT def maxSumSegments ( g , weight , n ) : NEW_LINE INDENT dfs1 ( 1 , 0 , g , weight ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT dp2 [ i ] = dp1 [ i ] NEW_LINE DEDENT dfs2 ( 1 , 0 , g , weight ) NEW_LINE DEDENT def printAns ( n ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT print ( dp2 [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 7 NEW_LINE u = 0 NEW_LINE v = 0 NEW_LINE g = [ [ ] for i in range ( 100005 ) ] NEW_LINE addEdge ( 1 , 2 , g ) NEW_LINE addEdge ( 1 , 3 , g ) NEW_LINE addEdge ( 2 , 4 , g ) NEW_LINE addEdge ( 2 , 5 , g ) NEW_LINE addEdge ( 3 , 6 , g ) NEW_LINE addEdge ( 4 , 7 , g ) NEW_LINE weight = [ 0 for i in range ( n + 1 ) ] NEW_LINE weight [ 1 ] = - 8 NEW_LINE weight [ 2 ] = 9 NEW_LINE weight [ 3 ] = 7 NEW_LINE weight [ 4 ] = - 4 NEW_LINE weight [ 5 ] = 5 NEW_LINE weight [ 6 ] = - 10 NEW_LINE weight [ 7 ] = - 6 NEW_LINE maxSumSegments ( g , weight , n ) NEW_LINE printAns ( n ) NEW_LINE DEDENT
Count of pairs in an Array with same number of set bits | Function to return the count of Pairs ; Get the maximum element ; Array to store count of bits of all elements upto maxm ; Store the set bits for powers of 2 ; Compute the set bits for the remaining elements ; Store the frequency of respective counts of set bits ; Driver Code
def countPairs ( arr , N ) : NEW_LINE INDENT maxm = max ( arr ) NEW_LINE i = 0 NEW_LINE k = 0 NEW_LINE bitscount = [ 0 for i in range ( maxm + 1 ) ] NEW_LINE i = 1 NEW_LINE while i <= maxm : NEW_LINE INDENT bitscount [ i ] = 1 NEW_LINE i *= 2 NEW_LINE DEDENT for i in range ( 1 , maxm + 1 ) : NEW_LINE INDENT if ( bitscount [ i ] == 1 ) : NEW_LINE INDENT k = i NEW_LINE DEDENT if ( bitscount [ i ] == 0 ) : NEW_LINE INDENT bitscount [ i ] = ( bitscount [ k ] + bitscount [ i - k ] ) NEW_LINE DEDENT DEDENT setbits = dict ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT if bitscount [ arr [ i ] ] in setbits : NEW_LINE INDENT setbits [ bitscount [ arr [ i ] ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT setbits [ bitscount [ arr [ i ] ] ] = 1 NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for it in setbits . values ( ) : NEW_LINE INDENT ans += it * ( it - 1 ) // 2 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 12 NEW_LINE arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 ] NEW_LINE print ( countPairs ( arr , N ) ) NEW_LINE DEDENT
Count of Binary strings of length N having atmost M consecutive 1 s or 0 s alternatively exactly K times | List to contain the final result ; Function to get the number of desirable binary strings ; If we reach end of string and groups are exhausted , return 1 ; If length is exhausted but groups are still to be made , return 0 ; If length is not exhausted but groups are exhausted , return 0 ; If both are negative just return 0 ; If already calculated , return it ; Initialise answer for each state ; Loop through every possible m ; Driver code
rows , cols = ( 1000 , 1000 ) NEW_LINE dp = [ [ 0 for i in range ( cols ) ] for j in range ( rows ) ] NEW_LINE def solve ( n , k , m ) : NEW_LINE INDENT if n == 0 and k == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT if n == 0 and k != 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT if n != 0 and k == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT if n < 0 or k < 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT if dp [ n ] [ k ] : NEW_LINE INDENT return dp [ n ] [ k ] NEW_LINE DEDENT ans = 0 NEW_LINE for j in range ( 1 , m + 1 ) : NEW_LINE INDENT ans = ans + solve ( n - j , k - 1 , m ) NEW_LINE DEDENT dp [ n ] [ k ] = ans NEW_LINE return dp [ n ] [ k ] NEW_LINE DEDENT N = 7 NEW_LINE K = 4 NEW_LINE M = 3 NEW_LINE print ( solve ( N , K , M ) ) NEW_LINE
Maximum neighbor element in a matrix within distance K | Function to print the matrix ; Loop to iterate over the matrix ; Function to find the maximum neighbor within the distance of less than equal to K ; Loop to find the maximum element within the distance of less than K ; Driver Code
def printMatrix ( A ) : NEW_LINE INDENT for i in range ( len ( A ) ) : NEW_LINE INDENT for j in range ( len ( A [ 0 ] ) ) : NEW_LINE INDENT print ( A [ i ] [ j ] , end = ' ▁ ' ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT def getMaxNeighbour ( A , K ) : NEW_LINE INDENT ans = A ; NEW_LINE for q in range ( 1 , K + 1 ) : NEW_LINE INDENT for i in range ( len ( A ) ) : NEW_LINE INDENT for j in range ( len ( A [ 0 ] ) ) : NEW_LINE INDENT maxi = ans [ i ] [ j ] ; NEW_LINE if ( i > 0 ) : NEW_LINE INDENT maxi = max ( maxi , ans [ i - 1 ] [ j ] ) ; NEW_LINE DEDENT if ( j > 0 ) : NEW_LINE INDENT maxi = max ( maxi , ans [ i ] [ j - 1 ] ) ; NEW_LINE DEDENT if ( i < len ( A ) - 1 ) : NEW_LINE INDENT maxi = max ( maxi , ans [ i + 1 ] [ j ] ) ; NEW_LINE DEDENT if ( j < len ( A [ 0 ] ) - 1 ) : NEW_LINE INDENT maxi = max ( maxi , ans [ i ] [ j + 1 ] ) ; NEW_LINE DEDENT A [ i ] [ j ] = maxi ; NEW_LINE DEDENT DEDENT ans = A ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT B = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] NEW_LINE printMatrix ( getMaxNeighbour ( B , 2 ) ) ; NEW_LINE DEDENT
Number of distinct ways to represent a number as sum of K unique primes | Prime list ; Sieve array of prime ; DP array ; Sieve of Eratosthenes . ; Push all the primes into prime vector ; Function to get the number of distinct ways to get sum as K different primes ; If index went out of prime array size or the sum became larger than n return 0 ; If sum becomes equal to n and j becomes exactly equal to k . Return 1 , else if j is still not equal to k , return 0 ; If sum != n and still j as exceeded , return 0 ; If that state is already calculated , return directly the ans ; Include the current prime ; Exclude the current prime ; Return by memoizing the ans ; Driver code ; Precompute primes by sieve
prime = [ ] NEW_LINE isprime = [ True ] * 1000 NEW_LINE dp = [ [ [ '0' for col in range ( 200 ) ] for col in range ( 20 ) ] for row in range ( 1000 ) ] NEW_LINE def sieve ( ) : NEW_LINE INDENT for i in range ( 2 , 1000 ) : NEW_LINE INDENT if ( isprime [ i ] ) : NEW_LINE INDENT for j in range ( i * i , 1000 , i ) : NEW_LINE INDENT isprime [ j ] = False NEW_LINE DEDENT DEDENT DEDENT for i in range ( 2 , 1000 ) : NEW_LINE INDENT if ( isprime [ i ] ) : NEW_LINE INDENT prime . append ( i ) NEW_LINE DEDENT DEDENT DEDENT def CountWays ( i , j , sums , n , k ) : NEW_LINE INDENT if ( i >= len ( prime ) or sums > n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( sums == n ) : NEW_LINE INDENT if ( j == k ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT if j == k : NEW_LINE INDENT return 0 NEW_LINE DEDENT if dp [ i ] [ j ] [ sums ] == 0 : NEW_LINE INDENT return dp [ i ] [ j ] [ sums ] NEW_LINE DEDENT inc = 0 NEW_LINE exc = 0 NEW_LINE inc = CountWays ( i + 1 , j + 1 , sums + prime [ i ] , n , k ) NEW_LINE exc = CountWays ( i + 1 , j , sums , n , k ) NEW_LINE dp [ i ] [ j ] [ sums ] = inc + exc NEW_LINE return dp [ i ] [ j ] [ sums ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT sieve ( ) NEW_LINE N = 100 NEW_LINE K = 5 NEW_LINE print ( CountWays ( 0 , 0 , 0 , N , K ) ) NEW_LINE DEDENT
Count minimum factor jumps required to reach the end of an Array | Vector to store factors of each integer ; Initialize the dp array ; Precompute all the factors of every integer ; Function to count the minimum factor jump ; 0 jumps required to reach the first cell ; Iterate over all cells ; calculating for each jump ; If a cell is in bound ; Return minimum jumps to reach last cell ; Driver code ; Pre - calculating the factors ; Function call
factors = [ [ ] for i in range ( 100005 ) ] ; NEW_LINE dp = [ 1000000000 for i in range ( 100005 ) ] ; NEW_LINE def precompute ( ) : NEW_LINE INDENT for i in range ( 1 , 100001 ) : NEW_LINE INDENT for j in range ( i , 100001 , i ) : NEW_LINE INDENT factors [ j ] . append ( i ) ; NEW_LINE DEDENT DEDENT DEDENT def solve ( arr , n ) : NEW_LINE INDENT dp [ 0 ] = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in factors [ arr [ i ] ] : NEW_LINE INDENT if ( i + j < n ) : NEW_LINE INDENT dp [ i + j ] = min ( dp [ i + j ] , 1 + dp [ i ] ) ; NEW_LINE DEDENT DEDENT DEDENT return dp [ n - 1 ] ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT precompute ( ) ; NEW_LINE arr = [ 2 , 8 , 16 , 55 , 99 , 100 ] NEW_LINE n = len ( arr ) NEW_LINE print ( solve ( arr , n ) ) NEW_LINE DEDENT
Find the length of the Largest subset such that all elements are Pairwise Coprime | Dynamic programming table ; Function to obtain the mask for any integer ; List of prime numbers till 50 ; Iterate through all prime numbers to obtain the mask ; Set this prime 's bit ON in the mask ; Return the mask value ; Function to count the number of ways ; Check if subproblem has been solved ; Excluding current element in the subset ; Check if there are no common prime factors then only this element can be included ; Calculate the new mask if this element is included ; Store and return the answer ; Function to find the count of subarray with all digits unique ; Driver code
dp = [ [ - 1 ] * ( ( 1 << 10 ) + 5 ) ] * 5000 NEW_LINE def getmask ( val ) : NEW_LINE INDENT mask = 0 NEW_LINE prime = [ 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 ] NEW_LINE for i in range ( 1 , 15 ) : NEW_LINE INDENT if val % prime [ i ] == 0 : NEW_LINE INDENT mask = mask | ( 1 << i ) NEW_LINE DEDENT DEDENT return mask NEW_LINE DEDENT def calculate ( pos , mask , a , n ) : NEW_LINE INDENT if ( ( pos == n ) or ( mask == ( 1 << n - 1 ) ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if dp [ pos ] [ mask ] != - 1 : NEW_LINE INDENT return dp [ pos ] [ mask ] NEW_LINE DEDENT size = 0 NEW_LINE size = max ( size , calculate ( pos + 1 , mask , a , n ) ) NEW_LINE if ( getmask ( a [ pos ] ) & mask ) == 0 : NEW_LINE INDENT new_mask = ( mask | ( getmask ( a [ pos ] ) ) ) NEW_LINE size = max ( size , 1 + calculate ( pos + 1 , new_mask , a , n ) ) NEW_LINE DEDENT dp [ pos ] [ mask ] = size NEW_LINE return dp [ pos ] [ mask ] NEW_LINE DEDENT def largestSubset ( A , n ) : NEW_LINE INDENT return calculate ( 0 , 0 , A , n ) ; NEW_LINE DEDENT A = [ 2 , 3 , 13 , 5 , 14 , 6 , 7 , 11 ] NEW_LINE N = len ( A ) NEW_LINE print ( largestSubset ( A , N ) ) NEW_LINE
Maximum sum such that exactly half of the elements are selected and no two adjacent | Function return the maximum sum possible under given condition ; Base case ; When i is odd ; When i is even ; Maximum of if we pick last element or not ; Driver code
def MaximumSum ( a , n ) : NEW_LINE INDENT dp = [ [ 0 for x in range ( 2 ) ] for y in range ( n + 1 ) ] NEW_LINE dp [ 2 ] [ 1 ] = a [ 1 ] NEW_LINE dp [ 2 ] [ 0 ] = a [ 0 ] NEW_LINE for i in range ( 3 , n + 1 ) : NEW_LINE INDENT if ( i & 1 ) : NEW_LINE INDENT temp = max ( [ dp [ i - 3 ] [ 1 ] , dp [ i - 3 ] [ 0 ] , dp [ i - 2 ] [ 1 ] , dp [ i - 2 ] [ 0 ] ] ) NEW_LINE dp [ i ] [ 1 ] = a [ i - 1 ] + temp NEW_LINE dp [ i ] [ 0 ] = max ( [ a [ i - 2 ] + dp [ i - 2 ] [ 0 ] , a [ i - 2 ] + dp [ i - 3 ] [ 1 ] , a [ i - 2 ] + dp [ i - 3 ] [ 0 ] , a [ i - 3 ] + dp [ i - 3 ] [ 0 ] ] ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ 1 ] = ( a [ i - 1 ] + max ( [ dp [ i - 2 ] [ 1 ] , dp [ i - 2 ] [ 0 ] , dp [ i - 1 ] [ 0 ] ] ) ) NEW_LINE dp [ i ] [ 0 ] = a [ i - 2 ] + dp [ i - 2 ] [ 0 ] NEW_LINE DEDENT DEDENT return max ( dp [ n ] [ 1 ] , dp [ n ] [ 0 ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE N = len ( A ) NEW_LINE print ( MaximumSum ( A , N ) ) NEW_LINE DEDENT