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].

**Examples:**

Input:N = 4, A = 1033, B = 8179

Output:6

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

Output:7

Input:N = 2, A = 11, B = 37

Output:2

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.

`// C++ program for the above problem ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `#define ll long long ` `#define mod 1000000007 ` `#define pb push_back ` `#define mod 1000000007 ` `#define vi vector<int> ` ` ` `// adjacency list for numbers ` `// till 100001 ` `vi lis[100001]; ` `vi primes; ` ` ` `// visited array ` `int` `vis[100001]; ` ` ` `// to store distance of every ` `// vertex from A ` `int` `dis[100001]; ` ` ` `// function to check if number ` `// is a prime ` `bool` `isPrime(` `int` `n) ` `{ ` ` ` `for` `(` `int` `i = 2; ` ` ` `i * i <= n; i++) { ` ` ` `if` `(n % i == 0) ` ` ` `return` `false` `; ` ` ` `} ` ` ` `return` `true` `; ` `} ` ` ` `// function to check if numbers ` `// differ by only a single-digit ` `bool` `valid(` `int` `a, ` `int` `b) ` `{ ` ` ` `int` `c = 0; ` ` ` `while` `(a) { ` ` ` ` ` `// check the last digit of ` ` ` `// both numbers and increase ` ` ` `// count if different ` ` ` `if` `((a % 10) != (b % 10)) { ` ` ` `c++; ` ` ` `} ` ` ` `a = a / 10; ` ` ` `b = b / 10; ` ` ` `} ` ` ` `if` `(c == 1) { ` ` ` `return` `true` `; ` ` ` `} ` ` ` `else` `{ ` ` ` `return` `false` `; ` ` ` `} ` `} ` ` ` `void` `makePrimes(` `int` `N) ` `{ ` ` ` `int` `i, j; ` ` ` `int` `L = ` `pow` `(10, N - 1); ` ` ` `int` `R = ` `pow` `(10, N) - 1; ` ` ` `// generate all N digit primes ` ` ` `for` `(` `int` `i = L; i <= R; i++) { ` ` ` `if` `(isPrime(i)) { ` ` ` `primes.pb(i); ` ` ` `} ` ` ` `} ` ` ` `for` `(i = 0; ` ` ` `i < primes.size(); i++) { ` ` ` ` ` `// for every prime number i ` ` ` `// check if an edge ` ` ` `// can be made. ` ` ` `for` `(j = i + 1; ` ` ` `j < primes.size(); j++) { ` ` ` `int` `a = primes[i]; ` ` ` `int` `b = primes[j]; ` ` ` ` ` `// for every prime number ` ` ` `// i check if an edge can ` ` ` `// be made from i to j. ` ` ` `if` `(valid(a, b)) { ` ` ` ` ` `// if edge is possible then ` ` ` `// insert in the adjacency ` ` ` `// list ` ` ` `lis[a].pb(b); ` ` ` `lis[b].pb(a); ` ` ` `} ` ` ` `} ` ` ` `} ` `} ` ` ` `// function to count distance ` `void` `bfs(` `int` `src) ` `{ ` ` ` `queue<` `int` `> q; ` ` ` `q.push(src); ` ` ` `vis[src] = 1; ` ` ` `dis[src] = 0; ` ` ` `while` `(!q.empty()) { ` ` ` `int` `curr = q.front(); ` ` ` `q.pop(); ` ` ` `for` `(` `int` `x : lis[curr]) { ` ` ` ` ` `// if unvisited push onto queue ` ` ` `// and mark visited as 1 and ` ` ` `// add the distance of curr+1. ` ` ` `if` `(vis[x] == 0) { ` ` ` `vis[x] = 1; ` ` ` `q.push(x); ` ` ` `dis[x] = dis[curr] + 1; ` ` ` `} ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 4; ` ` ` `makePrimes(N); ` ` ` `int` `A = 1033, B = 8179; ` ` ` ` ` `// Call bfs traversal ` ` ` `// with root as node A ` ` ` `bfs(A); ` ` ` ` ` `if` `(dis[B] == -1) ` ` ` `// incdicates not possible ` ` ` `cout << ` `"-1"` `<< endl; ` ` ` `else` ` ` `cout << dis[B] << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

6

**Time Complexity: **O(10^{2N})

**Auxillary Space Complexity: **O(10^{5})

## Recommended Posts:

- 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
- Convert 1 into X in min steps by multiplying with 2 or 3 or by adding 1
- Steps to reduce N to zero by subtracting its most significant digit at every step
- 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
- Program to make Indian Flag in Python | Step by Step
- Min steps to empty an Array by removing a pair each time with sum at most K
- Split Array into min number of subsets with difference between each pair greater than 1
- Count ways to reach the Nth stair using multiple 1 or 2 steps and a single step 3
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Number of steps to convert to prime factors
- Largest number less than N whose each digit is prime number
- Program for replacing one digit with other
- Print all possible combinations of the string by replacing '$' with any other digit from the string
- Number of odd and even results for every value of x in range [min, max] after performing N steps
- Count of N-digit numbers having digit XOR as single digit
- Split the string into minimum parts such that each part is in the another string
- Check if frequency of each digit is less than the digit
- Minimize Array length by repeatedly replacing co-prime pairs with 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 contribute@geeksforgeeks.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.