Given two N-digit prime numbers A and B, the task is to find the minimum number of steps taken to convert A to B. Condition for the conversion is that only 1 digit of the current prime number can be modified such that the new number formed is also a prime number. If no such conversion is possible, print -1.
Note: The range of N is [1, 5].
Input: N = 4, A = 1033, B = 8179
Explanation: The steps of conversion are 1033 -> 1733 -> 3733 -> 3739 -> 3779 -> 8779 -> 8179. While changing numbers from 1033->1733, they differ by only one digit and the same happens for subsequent steps.
Input: N = 4, A = 1373, B = 8179
Input: N = 2, A = 11, B = 37
Explanation: The steps of conversion are 11 -> 17 -> 37
Approach: Using Breadth First Search Algorithm
- Find all N digit prime numbers and make a graph with these numbers.
- Consider each prime number as a node of a graph and create an edge from one node to another if they differ by a single digit.
- Apply BFS traversal and find out the number of edges between A and B.
- If no path exists, print -1.
- Else print the no. of edges, which is the required solution.
Below is the implementation of the above approach.
Time Complexity: O(102N)
Auxillary Space Complexity: O(105)
- Number of steps to convert to prime factors
- Steps to reduce N to zero by subtracting its most significant digit at every step
- Count of prime factors of N to be added at each step to convert N to M
- Number of steps to sort the array by changing order of three elements in each step
- Number of steps required to convert a binary number to one
- Program for replacing one digit with other
- Minimum steps to reach N from 1 by multiplying each step by 2, 3, 4 or 5
- Minimize steps to reach K from 0 by adding 1 or doubling at each step
- Check if the first and last digit of number N is prime and their sum is less than K
- Count ways to reach the Nth stair using multiple 1 or 2 steps and a single step 3
- Convert a number of length N such that it contains any one digit at least 'K' times
- Smallest N digit number divisible by all possible prime digits
- Queries for the smallest and the largest prime number of given digit
- Check if the first and last digit of the smallest number forms a prime
- Largest number less than N whose each digit is prime number
- Minimum prime number operations to convert A to B
- Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
- Minimum splits required to convert a number into prime segments
- Print all possible combinations of the string by replacing '$' with any other digit from the string
- Convert 1 into X in min steps by multiplying with 2 or 3 or by adding 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.