Found 546 Articles for Algorithms

Find the minimum cost to reach destination using a train

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

465 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

956 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

942 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

465 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

269 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

Count possible ways to construct buildings

Arjun Thakur
Updated on 16-Jun-2020 14:50:22

502 Views

Here n number of sections are given, in each section, there are two sides on the road to constructing buildings. If there is one empty space between two houses are needed, then how many possible ways to construct buildings in the plot.There are four possibilities to construct buildings One side of the road Another side of the road No building can be constructed Both sides of the roadInput and OutputInput: It takes the number of sections to construct buildings. Say the input is 3. Output: Enter Number of sections: 3 Buildings can be constructed in 25 different ways.AlgorithmconstructionWays(n)Input: There are n number of section.Output ... Read More

Count number of ways to reach a given score in a game

karthikeya Boyini
Updated on 16-Jun-2020 14:52:58

502 Views

Let us consider a game, in which a player can get some score with 3, 5 or 10 in each move. A target score is also given. Our task is to find how many possible ways are there to reach that target score with those three points.By the dynamic programming approach, we will create a list of all score from 0 to n, and for each value of 3, 5, 10, we simply update the table.Input and OutputInput: The maximum score to reach using 3, 5 and 10. Let the input is 50. Output: Number of ways to reach using ... Read More

Count Binary String without Consecutive 1's

Ankith Reddy
Updated on 16-Jun-2020 14:57:01

557 Views

In this problem, we have to find some binary numbers which have no consecutive 1s. In a 3-bit binary string, there are three binary numbers 011, 110, 111, who have consecutive 1s, and five numbers are there which have no consecutive 1s. So after applying this algorithm to 3-bit numbers, the answer will be 5. If a[i] be the set of binary numbers, whose number of bits are I, and not containing any consecutive 1s, and b[i] is the set of binary number, where a number of bits are I, and containing consecutive 1s, then there are recurrence relations like:a[i] := ... Read More

Compute sum of digits in all numbers from 1 to n

Samual Sam
Updated on 16-Jun-2020 15:15:00

555 Views

In this problem, we have to find the sum of digits of all numbers in range 1 to n. For an example the sum of digits of 54 is 5 + 4 = 9, Like this, we have to find all the numbers and their sum of digits.We know that there are 10d - 1 numbers can be generated, whose number of digits is d. To find the sum of all those numbers of digit d, we can use a recursive formula.sum(10d- 1)=sum(10d-1- 1)*10+45*(10d-1)Input and OutputInput: This algorithm takes the upper limit of the range, say it is 20. Output: ... Read More

Collect maximum points in a grid using two traversals

Arjun Thakur
Updated on 16-Jun-2020 15:18:04

221 Views

There is a matrix with points in each cell, how to get maximum points from that grid using two traversals.There is some condition to satisfy −The first traversal starts from the top left cell in the grid and should go to the bottom left corner.      And in the second traversal starting from top right corner to bottom right cornerFrom one cell we can only move to bottom, bottom left of the current cell and bottom right of the current cells only.If one traversal already gets some points from a cell, In the next traversal no points will be ... Read More

Advertisements