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
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
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
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
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
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
One weighted directed acyclic graph is given. Another source vertex is also provided. Now we have to find the shortest distance from the starting node to all other vertices, in the graph.To detect Smaller distance, we can use another algorithm like Bellman-Ford for the graph with negative weight, for positive weight the Dijkstra’s algorithm is also helpful. Here for Directed Acyclic Graph, we will use the topological sorting technique to reduce complexity.Input and OutputInput: The cost matrix of the graph. 0 5 3 -∞ -∞ -∞ -∞ 0 2 6 -∞ -∞ -∞ -∞ 0 7 4 2 -∞ ... Read More
In a directed graph is said to be strongly connected, when there is a path between each pair of vertices in one component.To solve this algorithm, firstly, DFS algorithm is used to get the finish time of each vertex, now find the finish time of the transposed graph, then the vertices are sorted in descending order by topological sort.Input and OutputInput: Adjacency matrix of the graph. 0 0 1 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 Output: Following are strongly connected components in given ... Read More
We know about the famous game Snake and Ladder. In this game, some rooms are present on the board, with the room number. Some rooms are connected with a ladder or with snakes. When we get a ladder, we can climb up to some rooms to reach near to the destination without moving sequentially. Similarly, when we get some snake, it sends us to a lower room to start the journey again from that room.In this problem, we have to find the minimum number of the dice throw is required to reach start to destination.Input and OutputInput: The starting and ... Read More
Tarjan’s Algorithm is used to find strongly connected components of a directed graph. It requires only one DFS traversal to implement this algorithm.Using DFS traversal we can find DFS tree of the forest. From the DFS tree, strongly connected components are found. When the root of such sub-tree is found we can display the whole subtree. That subtree is one strongly connected component.Input and OutputInput: Adjacency matrix of the graph. 0 0 1 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 Output: The strongly connected ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP