Found 206 Articles for Dynamic Programming

Largest Sum Contiguous Subarray

karthikeya Boyini
Updated on 16-Jun-2020 15:40:34

624 Views

An array of integers is given. We have to find the sum of all elements which are contiguous, whose sum is largest, that will be sent as output.Using dynamic programming we will store the maximum sum up to current term. It will help to find the sum for contiguous elements in the array.Input and OutputInput: An array of integers. {-2, -3, 4, -1, -2, 1, 5, -3} Output: Maximum Sum of the Subarray is: 7AlgorithmmaxSum(array, n)Input − The main array, the size of the array.Output − maximum sum.Begin    tempMax := array[0]    currentMax = tempMax    for i := ... Read More

Largest Independent Set Problem

George John
Updated on 16-Jun-2020 15:44:45

275 Views

The Independent Set is the subset of all binary tree nodes when there is no edge between any two nodes in that subset. Now from a set of elements, we will find the longest independent set. i.e. If the elements are used to form a binary tree, then all largest subset, where no elements in that subset are connected to each other.Input and OutputInput: A binary tree. Output: Size of the Largest Independent Set is: 5AlgorithmlongSetSize(root)In this algorithm Binary tree will be formed, each node of that tree will hold data and setSize.Input − Root node of the binary tree.Output − ... Read More

How to print maximum number of A’s using given four keys

Samual Sam
Updated on 16-Jun-2020 15:47:10

154 Views

Let us consider, we will try to write the letter ‘A’, using the keyboard. Our goal is to use only four keys and try to write maximum ‘A’s on the text field. The keys are ‘A’, ‘C’, ‘V’ and ‘Ctrl’.To write the maximum number of A, we will use Ctrl + A to select All, Ctrl + C to copy and Ctrl + V to paste.Input and OutputInput: Number of keystrokes, say 7 Output: Maximum Number of A's with 7 keystrokes is: 9 Press A three times. Then Ctrl+A, Ctrl+C, Ctrl+V, Ctrl+VAlgorithmkeyNumbers(keyStrokes)Input: number of keystrokes.Output: Maximum number of letters using these ... Read More

Generate Fibonacci Series

Chandu yadav
Updated on 16-Jun-2020 15:49:35

1K+ Views

The Fibonacci sequence is like this, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ……In this sequence, the nth term is the sum of (n-1)'th and (n-2)'th terms.To generate we can use the recursive approach, but in dynamic programming, the procedure is simpler. It can store all Fibonacci numbers in a table, by using that table it can easily generate the next terms in this sequence.Input and OutputInput: Take the term number as an input. Say it is 10 Output: Enter number of terms: 10 10th fibinacci Terms: 55AlgorithmgenFiboSeries(n)Input: max number of terms.Output − The nth Fibonacci ... Read More

Floyd Warshall Algorithm

karthikeya Boyini
Updated on 16-Jun-2020 15:52:47

6K+ Views

Floyd-Warshall algorithm is used to find all pair shortest path problem from a given weighted graph. As a result of this algorithm, it will generate a matrix, which will represent the minimum distance from any node to all other nodes in the graph.At first, the output matrix is the same as the given cost matrix of the graph. After that, the output matrix will be updated with all vertices k as the intermediate vertex.The time complexity of this algorithm is O(V^3), where V is the number of vertices in the graph.Input and OutputInput: The cost matrix of the graph. 0 ... Read More

Find the minimum cost to reach destination using a train

Arjun Thakur
Updated on 16-Jun-2020 14:24:16

461 Views

For this problem, there are N stops on a journey. The vehicle starts the journey from stop 0 to N-1. In a table, ticket costs for all pair of stations are given. We have to find the minimum cost to reach the destination with those given costs.Input and OutputInput: The cost matrix of the journey. 0 15 80 90 ∞  0 40 50 ∞  ∞  0 70 ∞  ∞  ∞  0 Output: The minimum cost is 65. At first go to the destination 1 from 0. (cost 15), then 1 to 4 (cost 50). So total cost 65.AlgorithmfindMinCost(cost)Input − ... Read More

Find numbers whose sum of digits equals a value

Samual Sam
Updated on 16-Jun-2020 14:28:01

949 Views

There are a number n and a value. We have to find all n digit numbers, where the sum of all n digit the s is the same as the given value. Here 0 is noa t counted as a digit.The number n must be in the range 1 to 100, and value must be in range 1 to 500.Input and OutputInput: This algorithm takes number of digits, and the sum value. Let the digit count is 3. and sum is 15. Output: Display the number of different 3-digit numbers whose sum is 15. The result is 69. (There are ... Read More

Edit Distance

Ankith Reddy
Updated on 16-Jun-2020 14:38:19

928 Views

There are two strings given. The first string is the source string and the second string is the target string. In this program, we have to find how many possible edits are needed to convert first string to the second string. The edit of strings can be either Insert some elements, delete something from the first string or modify something to convert into the second string.Input and OutputInput: Two strings to compare. string 1: Programming string 2: Programs Output: Enter the initial string: Programming Enter the final string: Programs The number of changes required to convert Programming to Programs is 4AlgorithmeditCount(initStr, ... Read More

Egg Dropping Puzzle

karthikeya Boyini
Updated on 16-Jun-2020 14:42:42

460 Views

This is a famous puzzle problem. Suppose there is a building with n floors, if we have m eggs, then how can we find the minimum number of drops needed to find a floor from which it is safe to drop an egg without breaking it.There some important points to remember −When an egg does not break from a given floor, then it will not break for any lower floor also.If an egg breaks from a given floor, then it will break for all upper floors.When an egg breaks, it must be discarded, otherwise, we can use it again.Input and ... Read More

Count ways to reach the n’th stair

Samual Sam
Updated on 16-Jun-2020 14:45:40

262 Views

There are n stairs. One person will go to 1'st to the n'th stair. Maximum how many stairs he/she can cross in one step is also given. With this information, we have to find possible ways to go to the n'th stairs.Let us consider one can cross a maximum two stairs in each step. So we can find recursive relations to solve this problem. One can move to n'th stair, either from (n-1)'th stair or from (n-2)'th stair. So ways(n) = ways(n-1) + ways(n-2).Input and OutputInput: The number of stairs, say 10 the maximum number of stairs that can be ... Read More

Advertisements