├── 1.py ├── 10.py ├── 11.py ├── 12.py ├── 13.py ├── 14.py ├── 15.py ├── 16.py ├── 17.py ├── 18.py ├── 19.py ├── 2.py ├── 20.py ├── 21.py ├── 22.py ├── 23.py ├── 24.py ├── 25.py ├── 26.py ├── 27.py ├── 3.py ├── 4.py ├── 5.py ├── 6.py ├── 7.py ├── 8.py ├── 9.py └── README.md /1.py: -------------------------------------------------------------------------------- 1 | # 0-1 Knapsack 2 | 3 | W,n = map(int,input().split()) 4 | wt = list(map(int, input().split())) 5 | val = list(map(int, input().split())) 6 | 7 | def knapsack(wt, val, W, n): #base case 8 | if n==0 or W==0: # the smallest valid input 9 | return 0 10 | 11 | # choice diagram 12 | if (wt[n-1]<=W): #when the condition is valid but choice is optional 13 | return max(val[n-1] + knapsack(wt,val,W-wt[n-1],n-1), knapsack(wt,val,W,n-1)) 14 | 15 | elif(wt[n-1]>W): #when the condition is invalid (wt[n-1]>W) 16 | return knapsack(wt,val,W,n-1) 17 | 18 | print(knapsack(wt,val,W,n)) 19 | -------------------------------------------------------------------------------- /10.py: -------------------------------------------------------------------------------- 1 | # Dynamic Programming Python implementation of Coin Change Problem 2 | 3 | def count(A, m, n): 4 | # We need n+1 rows as the table is constructed 5 | # in bottom up manner using the base case 0 value 6 | # case (n = 0) 7 | table = [[0 for x in range(m)] for x in range(n+1)] 8 | 9 | # Fill the entries for 0 value case (n = 0) 10 | for i in range(m): 11 | table[0][i] = 1 12 | 13 | # Fill rest of the table entries in bottom up manner 14 | for i in range(1, n+1): 15 | for j in range(m): 16 | 17 | # Count of solutions including S[j] 18 | x = table[i - A[j]][j] if i-A[j] >= 0 else 0 19 | 20 | # Count of solutions excluding S[j] 21 | y = table[i][j-1] if j >= 1 else 0 22 | 23 | # total count 24 | table[i][j] = x + y 25 | 26 | return table[n][m-1] 27 | 28 | # Driver program to test above function 29 | arr = [1, 2, 3] 30 | m = len(arr) 31 | n = 6 32 | print(count(arr, m, n)) 33 | 34 | -------------------------------------------------------------------------------- /11.py: -------------------------------------------------------------------------------- 1 | # Dynamic Programming Python implementation of Coin Change Problem - II 2 | 3 | def change(amount,coins): 4 | dp = [0]*(amount + 1) 5 | dp[0] = 1 6 | for coin in coins: 7 | for i in range(amount + 1): 8 | if coin + i < amount + 1: 9 | dp[coin + i] += dp[i] 10 | return dp[amount] 11 | 12 | # Driver program to test above function 13 | arr = [1, 2, 5] 14 | 15 | n = 5 16 | print(change(n,arr)) 17 | 18 | -------------------------------------------------------------------------------- /12.py: -------------------------------------------------------------------------------- 1 | def maxProd(n): 2 | val= [0 for i in range(n+1)]; 3 | 4 | # Build the table val in bottom up manner and return 5 | # the last entry from the table 6 | for i in range(1,n+1): 7 | max_val = 0; 8 | for j in range(1,i): 9 | max_val = max(max_val, (i-j)*j, j*val[i-j]) 10 | val[i] = max_val; 11 | return val[n] 12 | 13 | 14 | # Driver program to test above functions 15 | print("Maximum Product is ", maxProd(10)) 16 | -------------------------------------------------------------------------------- /13.py: -------------------------------------------------------------------------------- 1 | def LCS(x,y,n,m): 2 | if n == 0 or m == 0: 3 | return 0 4 | 5 | elif(x[n-1] == y[m-1]): 6 | return 1 + LCS(x,y,n-1,m-1) 7 | 8 | else: 9 | return max(LCS(x,y,n-1,m),LCS(x,y,n,m-1)) 10 | 11 | 12 | x = input() 13 | y = input() 14 | n = len(x) 15 | m = len(y) 16 | print(LCS(x,y,n,m)) 17 | -------------------------------------------------------------------------------- /14.py: -------------------------------------------------------------------------------- 1 | # Dynamic Programming implementation of LCS problem 2 | 3 | def lcs(X, Y): 4 | # find the length of the strings 5 | m = len(X) 6 | n = len(Y) 7 | 8 | # declaring the array for storing the dp values 9 | L = [[None]*(n + 1) for i in range(m + 1)] 10 | 11 | """Following steps build L[m + 1][n + 1] in bottom up fashion 12 | Note: L[i][j] contains length of LCS of X[0..i-1] 13 | and Y[0..j-1]""" 14 | for i in range(m + 1): 15 | for j in range(n + 1): 16 | if i == 0 or j == 0 : 17 | L[i][j] = 0 18 | elif X[i-1] == Y[j-1]: 19 | L[i][j] = L[i-1][j-1]+1 20 | else: 21 | L[i][j] = max(L[i-1][j], L[i][j-1]) 22 | 23 | # L[m][n] contains the length of LCS of X[0..n-1] & Y[0..m-1] 24 | return L[m][n] 25 | # end of function lcs 26 | 27 | 28 | # Driver program to test the above function 29 | X = "AGGTAB" 30 | Y = "GXTXAYB" 31 | print(lcs(X, Y)) 32 | 33 | -------------------------------------------------------------------------------- /15.py: -------------------------------------------------------------------------------- 1 | def LCSubStr(X, Y, m, n): 2 | 3 | # Create a table to store lengths of 4 | # longest common suffixes of substrings. 5 | # Note that LCSuff[i][j] contains the 6 | # length of longest common suffix of 7 | # X[0...i-1] and Y[0...j-1]. The first 8 | # row and first column entries have no 9 | # logical meaning, they are used only 10 | # for simplicity of the program. 11 | 12 | # LCSuff is the table with zero 13 | # value initially in each cell 14 | LCSuff = [[0 for k in range(n+1)] for l in range(m+1)] 15 | 16 | # To store the length of 17 | # longest common substring 18 | result = 0 19 | 20 | # Following steps to build 21 | # LCSuff[m+1][n+1] in bottom up fashion 22 | for i in range(m + 1): 23 | for j in range(n + 1): 24 | if (i == 0 or j == 0): 25 | LCSuff[i][j] = 0 26 | elif (X[i-1] == Y[j-1]): 27 | LCSuff[i][j] = LCSuff[i-1][j-1] + 1 28 | result = max(result, LCSuff[i][j]) 29 | else: 30 | LCSuff[i][j] = 0 31 | return result 32 | 33 | 34 | # Driver Code 35 | X = 'OldSite:GeeksforGeeks.org' 36 | Y = 'NewSite:GeeksQuiz.com' 37 | 38 | m = len(X) 39 | n = len(Y) 40 | 41 | print('Length of Longest Common Substring is', 42 | LCSubStr(X, Y, m, n)) 43 | -------------------------------------------------------------------------------- /16.py: -------------------------------------------------------------------------------- 1 | # Dynamic programming implementation of LCS problem 2 | 3 | # Returns length of LCS for X[0..m-1], Y[0..n-1] 4 | 5 | 6 | def lcs(X, Y, m, n): 7 | L = [[0 for i in range(n+1)] for j in range(m+1)] 8 | 9 | # Following steps build L[m+1][n+1] in bottom up fashion. Note 10 | # that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1] 11 | for i in range(m+1): 12 | for j in range(n+1): 13 | if i == 0 or j == 0: 14 | L[i][j] = 0 15 | elif X[i-1] == Y[j-1]: 16 | L[i][j] = L[i-1][j-1] + 1 17 | else: 18 | L[i][j] = max(L[i-1][j], L[i][j-1]) 19 | 20 | # Create a string variable to store the lcs string 21 | lcs = "" 22 | 23 | # Start from the right-most-bottom-most corner and 24 | # one by one store characters in lcs[] 25 | i = m 26 | j = n 27 | while i > 0 and j > 0: 28 | 29 | # If current character in X[] and Y are same, then 30 | # current character is part of LCS 31 | if X[i-1] == Y[j-1]: 32 | lcs += X[i-1] 33 | i -= 1 34 | j -= 1 35 | 36 | # If not same, then find the larger of two and 37 | # go in the direction of larger value 38 | elif L[i-1][j] > L[i][j-1]: 39 | i -= 1 40 | 41 | else: 42 | j -= 1 43 | 44 | # We traversed the table in reverse order 45 | # LCS is the reverse of what we got 46 | lcs = lcs[::-1] 47 | print("LCS of " + X + " and " + Y + " is " + lcs) 48 | 49 | 50 | # Driver program 51 | X = "AGGTAB" 52 | Y = "GXTXAYB" 53 | m = len(X) 54 | n = len(Y) 55 | lcs(X, Y, m, n) 56 | -------------------------------------------------------------------------------- /17.py: -------------------------------------------------------------------------------- 1 | def printLCSSubStr(X: str, Y: str, 2 | m: int, n: int): 3 | 4 | # Create a table to store lengths of 5 | # longest common suffixes of substrings. 6 | # Note that LCSuff[i][j] contains length 7 | # of longest common suffix of X[0..i-1] and 8 | # Y[0..j-1]. The first row and first 9 | # column entries have no logical meaning, 10 | # they are used only for simplicity of program 11 | LCSuff = [[0 for i in range(n + 1)] 12 | for j in range(m + 1)] 13 | 14 | # To store length of the 15 | # longest common substring 16 | length = 0 17 | 18 | # To store the index of the cell 19 | # which contains the maximum value. 20 | # This cell's index helps in building 21 | # up the longest common substring 22 | # from right to left. 23 | row, col = 0, 0 24 | 25 | # Following steps build LCSuff[m+1][n+1] 26 | # in bottom up fashion. 27 | for i in range(m + 1): 28 | for j in range(n + 1): 29 | if i == 0 or j == 0: 30 | LCSuff[i][j] = 0 31 | elif X[i - 1] == Y[j - 1]: 32 | LCSuff[i][j] = LCSuff[i - 1][j - 1] + 1 33 | if length < LCSuff[i][j]: 34 | length = LCSuff[i][j] 35 | row = i 36 | col = j 37 | else: 38 | LCSuff[i][j] = 0 39 | 40 | # if true, then no common substring exists 41 | if length == 0: 42 | print("No Common Substring") 43 | return 44 | 45 | # allocate space for the longest 46 | # common substring 47 | resultStr = ['0'] * length 48 | 49 | # traverse up diagonally form the 50 | # (row, col) cell until LCSuff[row][col] != 0 51 | while LCSuff[row][col] != 0: 52 | length -= 1 53 | resultStr[length] = X[row - 1] # or Y[col-1] 54 | 55 | # move diagonally up to previous cell 56 | row -= 1 57 | col -= 1 58 | 59 | # required longest common substring 60 | print(''.join(resultStr)) 61 | 62 | # Driver Code 63 | if __name__ == "__main__": 64 | X = "MainakRiju" 65 | Y = "MainakChaudhuri" 66 | m = len(X) 67 | n = len(Y) 68 | 69 | printLCSSubStr(X, Y, m, n) 70 | -------------------------------------------------------------------------------- /18.py: -------------------------------------------------------------------------------- 1 | def lcs(str1, str2, m, n): 2 | 3 | L = [[0 for i in range(n + 1)] 4 | for i in range(m + 1)] 5 | 6 | # Following steps build L[m+1][n+1] 7 | # in bottom up fashion. Note that 8 | # L[i][j] contains length of LCS 9 | # of str1[0..i-1] and str2[0..j-1] 10 | for i in range(m + 1): 11 | for j in range(n + 1): 12 | if (i == 0 or j == 0): 13 | L[i][j] = 0 14 | elif(str1[i - 1] == str2[j - 1]): 15 | L[i][j] = L[i - 1][j - 1] + 1 16 | else: 17 | L[i][j] = max(L[i - 1][j], 18 | L[i][j - 1]) 19 | 20 | # L[m][n] contains length of LCS 21 | # for X[0..n-1] and Y[0..m-1] 22 | return L[m][n] 23 | 24 | # function to find minimum number of deletions and insertions 25 | 26 | 27 | def printMinDelAndInsert(str1, str2): 28 | m = len(str1) 29 | n = len(str2) 30 | leng = lcs(str1, str2, m, n) 31 | print("Minimum number of deletions = ", 32 | m - leng, sep=' ') 33 | print("Minimum number of insertions = ", 34 | n - leng, sep=' ') 35 | 36 | 37 | # Driver Code 38 | str1 = "heap" 39 | str2 = "pea" 40 | 41 | # Function Call 42 | printMinDelAndInsert(str1, str2) 43 | 44 | -------------------------------------------------------------------------------- /19.py: -------------------------------------------------------------------------------- 1 | def findLongestRepeatingSubSeq( str): 2 | 3 | n = len(str) 4 | 5 | # Create and initialize DP table 6 | dp=[[0 for i in range(n+1)] for j in range(n+1)] 7 | 8 | # Fill dp table (similar to LCS loops) 9 | for i in range(1,n+1): 10 | for j in range(1,n+1): 11 | # If characters match and indexes are 12 | # not same 13 | if (str[i-1] == str[j-1] and i != j): 14 | dp[i][j] = 1 + dp[i-1][j-1] 15 | 16 | # If characters do not match 17 | else: 18 | dp[i][j] = max(dp[i][j-1], dp[i-1][j]) 19 | 20 | 21 | return dp[n][n] 22 | 23 | 24 | # Driver Program 25 | if __name__=='__main__': 26 | str = "aabb" 27 | print("The length of the LRSS : ",findLongestRepeatingSubSeq(str)) 28 | 29 | -------------------------------------------------------------------------------- /2.py: -------------------------------------------------------------------------------- 1 | def knapSack(W, wt, val, n): 2 | K = [[0 for x in range(W + 1)] for x in range(n + 1)] 3 | 4 | # Build table K[][] in bottom up manner 5 | for i in range(n + 1): # converting recursions into iterations 6 | for j in range(W + 1): 7 | # Base condition 8 | if i == 0 or j == 0: 9 | K[i][j] = 0 10 | 11 | # Objects having weight less than knapSack 12 | elif wt[i-1] <= j: 13 | K[i][j] = max(val[i-1] + K[i-1][j-wt[i-1]], K[i-1][j]) 14 | 15 | # Objects with weight more than knapSack 16 | else: 17 | K[i][j] = K[i-1][j] 18 | 19 | return K[n][W] 20 | 21 | # Driver program to test above function 22 | val = [60, 100, 120] 23 | wt = [10, 20, 30] 24 | W = 50 25 | n = len(val) 26 | print(knapSack(W, wt, val, n)) 27 | -------------------------------------------------------------------------------- /20.py: -------------------------------------------------------------------------------- 1 | MAX = 1000 2 | 3 | # Return the maximum size of 4 | # substring of X which is 5 | # substring in Y. 6 | def maxSubsequenceSubstring(x, y, n, m): 7 | dp = [[0 for i in range(MAX)] 8 | for i in range(MAX)] 9 | 10 | # Initialize the dp[][] to 0. 11 | 12 | # Calculating value for each element. 13 | for i in range(1, m + 1): 14 | for j in range(1, n + 1): 15 | 16 | # If alphabet of string 17 | # X and Y are equal make 18 | # dp[i][j] = 1 + dp[i-1][j-1] 19 | if(x[j - 1] == y[i - 1]): 20 | dp[i][j] = 1 + dp[i - 1][j - 1] 21 | 22 | # Else copy the previous value 23 | # in the row i.e dp[i-1][j-1] 24 | else: 25 | dp[i][j] = dp[i][j - 1] 26 | 27 | # Finding the maximum length 28 | ans = 0 29 | for i in range(1, m + 1): 30 | ans = max(ans, dp[i][n]) 31 | return ans 32 | 33 | # Driver Code 34 | x = "ABCD" 35 | y = "BACDBDCD" 36 | n = len(x) 37 | m = len(y) 38 | print(maxSubsequenceSubstring(x, y, n, m)) 39 | -------------------------------------------------------------------------------- /21.py: -------------------------------------------------------------------------------- 1 | def issubsequence(s1, s2): 2 | 3 | n,m = len(s1),len(s2) 4 | i,j = 0,0 5 | while (i < n and j < m): 6 | if (s1[i] == s2[j]): 7 | i += 1 8 | j += 1 9 | 10 | # If i reaches end of s1,that mean we found all 11 | # characters of s1 in s2, 12 | # so s1 is subsequence of s2, else not 13 | return i == n 14 | 15 | 16 | # driver code 17 | s1 = "gksrek" 18 | s2 = "geeksforgeeks" 19 | print(issubsequence(s1, s2)) 20 | 21 | -------------------------------------------------------------------------------- /22.py: -------------------------------------------------------------------------------- 1 | def count(a, b): 2 | m = len(a) 3 | n = len(b) 4 | 5 | # Create a table to store results of sub-problems 6 | lookup = [[0] * (n + 1) for i in range(m + 1)] 7 | 8 | # If first string is empty 9 | for i in range(n+1): 10 | lookup[0][i] = 0 11 | 12 | # If second string is empty 13 | for i in range(m + 1): 14 | lookup[i][0] = 1 15 | 16 | # Fill lookup[][] in bottom up manner 17 | for i in range(1, m + 1): 18 | for j in range(1, n + 1): 19 | 20 | # If last characters are same, 21 | # we have two options - 22 | # 1. consider last characters of 23 | # both strings in solution 24 | # 2. ignore last character of first string 25 | if a[i - 1] == b[j - 1]: 26 | lookup[i][j] = lookup[i - 1][j - 1] + lookup[i - 1][j] 27 | 28 | else: 29 | # If last character are different, ignore 30 | # last character of first string 31 | lookup[i][j] = lookup[i - 1][j] 32 | 33 | return lookup[m][n] 34 | 35 | # Driver code 36 | if __name__ == '__main__': 37 | a = "GeeksforGeeks" 38 | b = "Gks" 39 | 40 | print(count(a, b)) 41 | -------------------------------------------------------------------------------- /23.py: -------------------------------------------------------------------------------- 1 | def lps(str): 2 | n = len(str) 3 | 4 | # Create a table to store results of subproblems 5 | L = [[0 for x in range(n)] for x in range(n)] 6 | 7 | # Strings of length 1 are palindrome of length 1 8 | for i in range(n): 9 | L[i][i] = 1 10 | 11 | 12 | for cl in range(2, n + 1): 13 | for i in range(n-cl + 1): 14 | j = i + cl-1 15 | if str[i] == str[j] and cl == 2: 16 | L[i][j] = 2 17 | elif str[i] == str[j]: 18 | L[i][j] = L[i + 1][j-1] + 2 19 | else: 20 | L[i][j] = max(L[i][j-1], L[i + 1][j]); 21 | 22 | return L[0][n-1] 23 | 24 | # Driver program to test above functions 25 | seq = "GEEKS FOR GEEKS" 26 | n = len(seq) 27 | print(str(lps(seq))) 28 | 29 | -------------------------------------------------------------------------------- /24.py: -------------------------------------------------------------------------------- 1 | def longestPalSubstr(string): 2 | n = len(string) # calculating size of string 3 | if (n < 2): 4 | return n # if string is empty then size will be 0. 5 | # if n==1 then, answer will be 1(single 6 | # character will always palindrome) 7 | start=0 8 | maxLength = 1 9 | for i in range(n): 10 | low = i - 1 11 | high = i + 1 12 | while (high < n and string[high] == string[i] ): 13 | high=high+1 14 | 15 | while (low >= 0 and string[low] == string[i] ): 16 | low=low-1 17 | 18 | while (low >= 0 and high < n and string[low] == string[high] ): 19 | low=low-1 20 | high=high+1 21 | 22 | 23 | length = high - low - 1 24 | if (maxLength < length): 25 | maxLength = length 26 | start=low+1 27 | 28 | print ("Longest palindrome substring is:",end=" ") 29 | print (string[start:start + maxLength]) 30 | 31 | return maxLength 32 | 33 | # Driver program to test above functions 34 | string = ("forgeeksskeegfor") 35 | print("Length is: " + str(longestPalSubstr(string))) 36 | -------------------------------------------------------------------------------- /25.py: -------------------------------------------------------------------------------- 1 | dp = [[-1 for i in range(1001)] 2 | for j in range(1001)] 3 | 4 | def isPal(s, i, j): 5 | 6 | # Base condition 7 | if (i > j): 8 | return 1 9 | 10 | # Check if the recursive tree 11 | # for given i, j 12 | # has already been executed 13 | if (dp[i][j] != -1): 14 | return dp[i][j] 15 | 16 | # If first and last characters of 17 | # substring are unequal 18 | if (s[i] != s[j]): 19 | dp[i][j] = 0 20 | return dp[i][j] 21 | 22 | # Memoization 23 | dp[i][j] = isPal(s, i + 1, j - 1) 24 | 25 | return dp[i][j] 26 | 27 | def countSubstrings(s): 28 | 29 | n = len(s) 30 | 31 | count = 0 32 | 33 | # 2 for loops are required to check for 34 | # all the palindromes in the string. 35 | for i in range(n): 36 | for j in range(i + 1, n): 37 | 38 | # Increment count for every palindrome 39 | if (isPal(s, i, j)): 40 | count += 1 41 | 42 | # Return total palindromic substrings 43 | return count 44 | 45 | # Driver code 46 | s = "abbaeae" 47 | 48 | print(countSubstrings(s)) 49 | 50 | -------------------------------------------------------------------------------- /26.py: -------------------------------------------------------------------------------- 1 | def lps(str): 2 | n = len(str) 3 | 4 | L = [[0 for x in range(n)]for y in range(n)] 5 | 6 | for i in range(n): 7 | L[i][i] = 1 8 | 9 | for cl in range( 2, n+1): 10 | for i in range(n - cl + 1): 11 | j = i + cl - 1 12 | if (str[i] == str[j] and cl == 2): 13 | L[i][j] = 2 14 | elif (str[i] == str[j]): 15 | L[i][j] = L[i + 1][j - 1] + 2 16 | else: 17 | L[i][j] = max(L[i][j - 1],L[i + 1][j]) 18 | 19 | 20 | return L[0][n - 1] 21 | 22 | 23 | def minimumNumberOfDeletions( str): 24 | 25 | n = len(str) 26 | 27 | l = lps(str) 28 | 29 | return (n - l) 30 | 31 | # Driver Code 32 | if __name__ == "__main__": 33 | 34 | str = "geeksforgeeks" 35 | print( "Minimum number of deletions = " 36 | , minimumNumberOfDeletions(str)) 37 | -------------------------------------------------------------------------------- /27.py: -------------------------------------------------------------------------------- 1 | def Min(a, b): 2 | return min(a, b) 3 | 4 | # A DP function to find minimum number 5 | # of insertions 6 | def findMinInsertionsDP(str1, n): 7 | 8 | # Create a table of size n*n. table[i][j] 9 | # will store minimum number of insertions 10 | # needed to convert str1[i..j] to a palindrome. 11 | table = [[0 for i in range(n)] 12 | for i in range(n)] 13 | l, h, gap = 0, 0, 0 14 | 15 | # Fill the table 16 | for gap in range(1, n): 17 | l = 0 18 | for h in range(gap, n): 19 | if str1[l] == str1[h]: 20 | table[l][h] = table[l + 1][h - 1] 21 | else: 22 | table[l][h] = (Min(table[l][h - 1], 23 | table[l + 1][h]) + 1) 24 | l += 1 25 | 26 | # Return minimum number of insertions 27 | # for str1[0..n-1] 28 | return table[0][n - 1]; 29 | 30 | # Driver Code 31 | str1 = "geeks" 32 | print(findMinInsertionsDP(str1, len(str1))) 33 | 34 | -------------------------------------------------------------------------------- /3.py: -------------------------------------------------------------------------------- 1 | def isSubsetSum(arr, n, sum): 2 | 3 | # The value of subset[i][j] will be 4 | # true if there is a 5 | # subset of set[0..j-1] with sum equal to i 6 | subset =([[False for i in range(sum + 1)] 7 | for i in range(n + 1)]) 8 | 9 | # If sum is 0, then answer is true 10 | for i in range(n + 1): 11 | subset[i][0] = True 12 | 13 | # If sum is not 0 and set is empty, 14 | # then answer is false 15 | for i in range(1, sum + 1): 16 | subset[0][i]= False 17 | 18 | # Fill the subset table in bottom up manner 19 | for i in range(1, n + 1): 20 | for j in range(1, sum + 1): 21 | if j= arr[i-1]: 24 | subset[i][j] = (subset[i-1][j] or 25 | subset[i - 1][j-arr[i-1]]) 26 | 27 | # uncomment this code to print table 28 | # for i in range(n + 1): 29 | # for j in range(sum + 1): 30 | # print (subset[i][j], end =" ") 31 | # print() 32 | return subset[n][sum] 33 | 34 | # Driver code 35 | if __name__=='__main__': 36 | arr = [2, 3, 7, 8, 10] 37 | sum = 11 38 | n = len(arr) 39 | print(isSubsetSum(arr, n, sum)) 40 | 41 | -------------------------------------------------------------------------------- /4.py: -------------------------------------------------------------------------------- 1 | def findPartition(arr, n): 2 | sum = 0 3 | i, j = 0, 0 4 | 5 | # calculate sum of all elements 6 | for i in range(n): 7 | sum += arr[i] 8 | 9 | if sum % 2 != 0: 10 | return false 11 | 12 | k = [[True for i in range(n + 1)] 13 | for j in range(sum // 2 + 1)] 14 | 15 | # initialize top row as true 16 | for i in range(0, n + 1): 17 | k[0][i] = True 18 | 19 | # initialize leftmost column, 20 | # except part[0][0], as 0 21 | for i in range(1, sum // 2 + 1): 22 | k[i][0] = False 23 | 24 | # fill the partition table in 25 | # bottom up manner 26 | for i in range(1, sum // 2 + 1): 27 | 28 | for j in range(1, n + 1): 29 | k[i][j] = k[i][j - 1] 30 | 31 | if i >= arr[j - 1]: 32 | k[i][j] = (k[i][j] or 33 | k[i - arr[j - 1]][j - 1]) 34 | 35 | return k[sum // 2][n] 36 | 37 | 38 | # Driver Code 39 | arr = [3, 1, 1, 2, 2, 1] 40 | n = len(arr) 41 | 42 | # Function call 43 | print(findPartition(arr, n)) 44 | -------------------------------------------------------------------------------- /5.py: -------------------------------------------------------------------------------- 1 | def findSubsetSum (A, N, summ): 2 | 3 | # First, we initialize the matrix 4 | 5 | matrix = [[1 for x in range(summ+1)] for x in range(N+1)] 6 | for k in range(1,summ+1): 7 | matrix[0][k] = 0 8 | for k in range(1,N+1): 9 | matrix[k][0] = 1 10 | 11 | for k in range(1,N+1): 12 | for l in range(1,summ+1): 13 | # If element value is greater than the sum value 14 | if(A[k - 1] > l): 15 | matrix[k][l] = matrix[k - 1][l] 16 | else: 17 | matrix[k][l] = matrix[k - 1][l] + matrix[k - 1][l - A[k - 1]]; 18 | 19 | 20 | return matrix[N][summ] 21 | A = [2,3,5,6,8,10] 22 | N = len(A) 23 | summ = 10 24 | print(findSubsetSum(A, N, summ)) 25 | -------------------------------------------------------------------------------- /6.py: -------------------------------------------------------------------------------- 1 | def minDifference(arr, n): 2 | sum = 0; 3 | for i in range(n): 4 | sum += arr[i]; 5 | y = sum // 2 + 1; 6 | 7 | # dp[i] gives whether is it possible to get i as 8 | # sum of elements dd is helper variable we use dd 9 | # to ignoring duplicates 10 | dp = [False for i in range(y)] 11 | dd = [False for i in range(y)] 12 | 13 | # Initialising dp and dd 14 | 15 | # sum = 0 is possible 16 | dd[0] = True; 17 | for i in range(n): 18 | 19 | # updating dd[k] as True if k can be formed 20 | # using elements from 1 to i+1 21 | for j in range(y): 22 | if (j + arr[i] < y and dp[j]): 23 | dd[j + arr[i]] = True; 24 | 25 | # updating dd 26 | for j in range(y): 27 | if (dd[j]): 28 | dp[j] = True; 29 | dd[j] = False; # reset dd 30 | 31 | # checking the number from sum/2 to 1 which is 32 | # possible to get as sum 33 | for i in range(y-1, 0, -1): 34 | if (dp[i]): 35 | return (sum - 2 * i); 36 | 37 | # since i is possible to form then another 38 | # number is sum-i so mindifference is sum-i-i 39 | return 0; 40 | 41 | 42 | if __name__ == '__main__': 43 | 44 | arr = [ 1, 6, 11, 5 ]; 45 | n = len(arr); 46 | print(minDifference(arr, n)); 47 | 48 | -------------------------------------------------------------------------------- /7.py: -------------------------------------------------------------------------------- 1 | def subsetSum(arr,n,s): 2 | n = len(arr) 3 | dp = [[-1]*(s+1) for x in range(n+1)] 4 | for i in range(1,n+1): 5 | dp[i][0] = 0 6 | for j in range(s+1): 7 | dp[0][j] = 1 8 | 9 | for i in range(1,n+1): 10 | for j in range(s+1): 11 | if arr[i-1] <= j: 12 | dp[i][j] = dp[i-1][j] + dp[i-1][j-arr[i-1]] 13 | else: 14 | dp[i][j] = dp[i-1][j] 15 | 16 | return dp[n][s] 17 | 18 | def countSumsetSum(arr, n, diff): 19 | s = 0 20 | s = sum(arr) 21 | 22 | reqSum = (diff+s)//2 23 | return subsetSum(arr,n,reqSum) 24 | 25 | arr = [1,7,4,8] 26 | diff = 3 27 | n = len(arr) 28 | print(subsetSum(arr,n,diff)) 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /8.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countSubsetSum(self, nums, S): 3 | n = len(nums) 4 | dp = [[-1]*(S + 1) for x in range(n + 1)] 5 | 6 | for j in range(S + 1): 7 | dp[0][j] = 0 8 | 9 | for i in range(n + 1): 10 | dp[i][0] = 1 11 | 12 | 13 | for i in range(1, n + 1): 14 | for j in range(S + 1): 15 | if nums[i - 1] <= j: 16 | dp[i][j] = dp[i - 1][j] + dp[i - 1][j - nums[i - 1]] 17 | else: 18 | dp[i][j] = dp[i - 1][j] 19 | 20 | return dp[n][S] 21 | 22 | 23 | 24 | def findTargetSumWays(self, nums: List[int], target: int) -> int: 25 | 26 | totalSum = sum(nums) 27 | 28 | S = (totalSum + target)//2 29 | 30 | if (totalSum + target) % 2 != 0 or totalSum < abs(target): 31 | return 0 32 | 33 | return self.countSubsetSum(nums, S) 34 | -------------------------------------------------------------------------------- /9.py: -------------------------------------------------------------------------------- 1 | def knapSack(W, wt, val, n): 2 | K = [[0 for x in range(W + 1)] for x in range(n + 1)] 3 | 4 | # Build table K[][] in bottom up manner 5 | for i in range(n + 1): # converting recursions into iterations 6 | for j in range(W + 1): 7 | # Base condition 8 | if i == 0 or j == 0: 9 | K[i][j] = 0 10 | 11 | # Objects having weight less than knapSack 12 | elif wt[i-1] <= j: 13 | K[i][j] = max(val[i-1] + K[i][j-wt[i-1]], K[i-1][j]) 14 | 15 | # Objects with weight more than knapSack 16 | else: 17 | K[i][j] = K[i-1][j] 18 | 19 | return K[n][W] 20 | 21 | # Driver program to test above function 22 | val = [60, 100, 120] 23 | wt = [10, 20, 30] 24 | W = 50 25 | n = len(val) 26 | print(knapSack(W, wt, val, n)) 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Dynamic-Programming 2 | A collection of dynamic programming solutions that can blow your mind 3 | 4 | **0-1 KNAPSACK** 5 | 6 | | Sl No. | Problem Statement | 7 | |--------|-------------------| 8 | | 1 | [0-1 Knapsack Problem (Recursive)](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/1.py) | 9 | | 2 | [0-1 Knapsack Problem (Bottom Up)](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/2.py) | 10 | | 3 | [Subset Sum](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/3.py) | 11 | | 4 | [Equal Sum Partition](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/4.py) | 12 | | 5 | [Count Subsets With Given Sum](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/5.py) | 13 | | 6 | [Minimum Subset Sum Difference](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/6.py) | 14 | | 7 | [Count Number of Subsets With Given Difference](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/7.py) | 15 | | 8 | [Target Sum](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/8.py) | 16 | 17 |
18 | 19 | **UNBOUNDED KNAPSACK** 20 | 21 | | Sl No. | Problem Statement | 22 | |--------|-------------------| 23 | | 1 | [Unbounded Knapsack](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/9.py) | 24 | | 2 | [Coin Change I (Max number of combinations)](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/10.py) | 25 | | 3 | [Coin Change II (Min number of combinations)](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/11.py) | 26 | | 4 | [Ribbon Cutting Max Product](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/12.py) | 27 | 28 |
29 | 30 | **LEAST COMMON SUBSEQUENCE** 31 | | Sl No. | Problem Statement | 32 | |--------|-------------------| 33 | | 1 | [Longest Common Subsequence (Recursive)](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/13.py) | 34 | | 2 | [Longest Common Subsequence (Bottom-Up)](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/14.py) | 35 | | 3 | [Longest Common Substring](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/15.py) | 36 | | 4 | [Print Longest Common Subsequence](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/16.py) | 37 | | 5 | [Print Longest Common Substring](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/17.py) | 38 | | 6 | [Minimum number of Insertions and Deletions to Transform a String to Another](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/18.py) | 39 | | 7 | [Longest Repeating Subsequence](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/19.py) | 40 | | 8 | [Length of longest subsequence of a string which is a substring of another](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/20.py) | 41 | | 9 | [Subsequence Pattern Matching](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/21.py) | 42 | | 10 | [Count number of times a string appears as a subsquence in another string](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/22.py) | 43 | | 11 | [Longest Palindrominc Subsequence](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/23.py) | 44 | | 12 | [Longest Palindromic Substring](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/24.py) | 45 | | 13 | [Count All Palindromic Substrings](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/25.py) | 46 | | 14 | [Minimum Number of Deletions in a string to make it Palindrome](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/26.py) | 47 | | 15 | [Minimum Number of Insertions in a string to make it Palindrome](https://github.com/MainakRepositor/Dynamic-Programming/blob/master/27.py) | 48 | 49 | **MATRIX CHAIN MULTIPLICATION** 50 | 51 | --------------------------------------------------------------------------------