Found 206 Articles for Dynamic Programming

Maximum Sum Increasing Subsequence

George John
Updated on 17-Jun-2020 07:03:35

371 Views

Maximum Sum Increasing subsequence is a subsequence of a given list of integers, whose sum is maximum and in the subsequence, all elements are sorted in increasing order.Let there is an array to store max sum increasing subsequence, such that L[i] is the max sum increasing subsequence, which is ending with array[i].Input and OutputInput: Sequence of integers. {3, 2, 6, 4, 5, 1} Output: Increasing subsequence whose sum is maximum. {3, 4, 5}.AlgorithmmaxSumSubSeq(array, n)Input: The sequence of numbers, number of elements.Output: Maximum sum of the increasing sub sequence.Begin    define array of arrays named subSeqLen of size n.    add ... Read More

Maximum profit by buying and selling a share at most twice

Chandu yadav
Updated on 17-Jun-2020 07:07:12

308 Views

In a trading, one buyer buys and sells the shares, at morning and the evening respectively. If at most two transactions are allowed in a day. The second transaction can only start after the first one is completed. If stock prices are given, then find the maximum profit that the buyer can make.Input and OutputInput: A list of stock prices. {2, 30, 15, 10, 8, 25, 80} Output: Here the total profit is 100. As buying at price 2 and selling at price 30. so profit 28. Then buy at price 8 and sell it again at price 80. So ... Read More

Maximum size square submatrix with all 1s

Samual Sam
Updated on 17-Jun-2020 07:11:23

212 Views

When a binary matrix is given, our task is to find a square matrix whose all elements are 1.For this problem, we will make an auxiliary size matrix, whose order is the same as the given matrix. This size matrix will help to represent, in each entry Size[i, j], is the size of a square matrix with all 1s. From that size matrix, we will get the maximum number to get the size of the biggest square matrix.Input and OutputInput: The binary matrix. 0 1 1 0 1 1 1 0 1 0 0 1 1 1 0 1 1 ... Read More

Maximum Length Chain of Pairs

karthikeya Boyini
Updated on 17-Jun-2020 07:17:54

439 Views

There is a chain of pairs is given. In each pair, there are two integers and the first integer is always smaller, and the second one is greater, the same rule can also be applied for the chain construction. A pair (x, y) can be added after a pair (p, q), only if q < x.To solve this problem, at first, we have to sort given pairs in increasing order of the first element. After that, we will compare the second element of a pair, with the first element of the next pair.Input and OutputInput: A chain of number pairs. ... Read More

Matrix Chain Multiplication

George John
Updated on 17-Jun-2020 07:14:24

7K+ Views

If a chain of matrices is given, we have to find the minimum number of the correct sequence of matrices to multiply.We know that the matrix multiplication is associative, so four matrices ABCD, we can multiply A(BCD), (AB)(CD), (ABC)D, A(BC)D, in these sequences. Like these sequences, our task is to find which ordering is efficient to multiply.In the given input there is an array say arr, which contains arr[] = {1, 2, 3, 4}. It means the matrices are of the order (1 x 2), (2 x 3), (3 x 4).Input and OutputInput: The orders of the input matrices. {1, ... Read More

Longest Palindromic Subsequence

Ankith Reddy
Updated on 17-Jun-2020 07:21:05

780 Views

Longest Palindromic Subsequence is the subsequence of a given sequence, and the subsequence is a palindrome.In this problem, one sequence of characters is given, we have to find the longest length of a palindromic subsequence.To solve this problem, we can use the recursive formula, If L (0, n-1) is used to store a length of longest palindromic subsequence, thenL (0, n-1) := L (1, n-2) + 2 (When 0'th and (n-1)'th characters are same).Input and OutputInput: A string with different letters or symbols. Say the input is “ABCDEEAB” Output: The longest length of the largest palindromic subsequence. Here it is ... Read More

Longest Increasing Subsequence

George John
Updated on 16-Jun-2020 15:27:18

1K+ Views

Longest Increasing Subsequence is a subsequence where one item is greater than its previous item. Here we will try to find Longest Increasing Subsequence length, from a set of integers.Input and OutputInput: A set of integers. {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15} Output: The length of longest increasing subsequence. Here it is 6. The subsequence is 0, 2, 6, 9, 13, 15.AlgorithmlongestSubSeq(subarray, n)Input − The sub array and the size of sub array.Output − Longest increasing sub sequence length.Begin    define array length of size n    initially set 0 ... Read More

Longest consecutive path from a given starting character

karthikeya Boyini
Updated on 16-Jun-2020 15:31:14

282 Views

A matrix of different characters is given. Starting from one character we have to find the longest path by traversing all characters which are greater than the current character. The characters are consecutive to each other.To find the longest path, we will use the Depth First Search algorithm. During DFS, some subproblems may arise multiple times. To avoid the computation of that, again and again, we will use a dynamic programming approach.Input and OutputInput: The matrix as shown above. And the starting point. Here the starting point is e. Output: Enter Starting Point (a-i): e Maximum consecutive path: 5AlgorithmfindLongestLen(i, j, ... Read More

Longest Bitonic Subsequence

Ankith Reddy
Updated on 16-Jun-2020 15:33:35

216 Views

A sequence is said to be bitonic if it is first increasing and then decreasing. In this problem, an array of all positive integers is given. We have to find a subsequence which is increasing first and then decreasing.To solve this problem, we will define two subsequences, they are the Longest Increasing Subsequence and the Longest Decreasing Subsequence. The LIS array will hold the length of increasing subsequence ending with array[i]. The LDS array will store the length of decreasing subsequence starting from array[i]. Using these two arrays, we can get the length of longest bitonic subsequence.Input and OutputInput: A ... Read More

Longest Common Subsequence

Samual Sam
Updated on 16-Jun-2020 15:36:56

469 Views

The longest common subsequence is a type of subsequence which is present in both of the given sequences or arrays.We can see that there are many subproblems, which are computed again and again to solve this problem. By using the Overlapping Substructure Property of Dynamic programming, we can overcome the computational efforts. Once the result of subproblems is calculated and stored in the table, we can simply calculate the next results by use of the previous results.Input and OutputInput: Two strings with different letters or symbols. string 1: AGGTAB string 2: GXTXAYB Output: The length of the longest common subsequence. ... Read More

Advertisements