Given two natural numbers N and M, Create a graph using these two natural numbers using relation that a number is connected to its largest factor other than itself. The task is to find the shortest path between these two numbers after creating a graph.
Input: N = 6, M = 18
Output: 6 <–> 3 <–> 9 <–> 18
For N = 6, the connection of graph is:
6 — 3 — 1
For N = 18, the connection of graph is:
18 — 9 — 3 — 1
Combining the above two graphs, the shortest path is given by:
6 — 3 — 9 — 18
Input: N = 4, M = 8
Output: 4 <–> 8
Approach: The idea is to find the largest factors of each number other than itself and create a graph by connecting these factors and then find the shortest path between them. Below are the steps:
- Find the largest common factor of M and store it and set it as M.
- Now, until M doesn’t equal to 1 keep repeating the above steps and store the factors generated in an array mfactor.
- Repeat step 1 and step 2 by taking N as the number and store the factors generated in an array nfactor.
- Now, traverse both the arrays mfactor and mfactor and print the shortest path.
Below is the implementation of the above approach:
18 <--> 9 <--> 3 <--> 1 <--> 19
Time Complexity: O(log (max(M, N))
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Largest component size in a graph formed by connecting non-co-prime nodes
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Shortest path with exactly k edges in a directed and weighted graph
- Shortest path with exactly k edges in a directed and weighted graph | Set 2
- Building an undirected graph and finding shortest path using Dictionaries in Python
- Shortest Path in Directed Acyclic Graph
- Shortest Path in a weighted Graph where weight of an edge is 1 or 2
- 0-1 BFS (Shortest Path in a Binary Weight Graph)
- Multistage Graph (Shortest Path)
- Shortest path in an unweighted graph
- Multi Source Shortest Path in Unweighted Graph
- Check if given path between two nodes of a graph represents a shortest paths
- Shortest path in a graph from a source S to destination D with exactly K edges for multiple Queries
- Shortest path in a directed graph by Dijkstra’s algorithm
- Shortest path in a complement graph
- Detect a negative cycle in a Graph using Shortest Path Faster Algorithm
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Find sum of divisors of all the divisors of a natural number
- Number of ways a convex polygon of n+2 sides can split into triangles by connecting vertices
- Check if a number has an odd count of odd divisors and even count of even divisors
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.