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++
#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>
vi lis[100001];
vi primes;
int vis[100001];
int dis[100001];
bool isPrime( int n)
{
for ( int i = 2;
i * i <= n; i++) {
if (n % i == 0)
return false ;
}
return true ;
}
bool valid( int a, int b)
{
int c = 0;
while (a) {
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;
for ( int i = L; i <= R; i++) {
if (isPrime(i)) {
primes.pb(i);
}
}
for (i = 0;
i < primes.size(); i++) {
for (j = i + 1;
j < primes.size(); j++) {
int a = primes[i];
int b = primes[j];
if (valid(a, b)) {
lis[a].pb(b);
lis[b].pb(a);
}
}
}
}
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 (vis[x] == 0) {
vis[x] = 1;
q.push(x);
dis[x] = dis[curr] + 1;
}
}
}
}
int main()
{
int N = 4;
makePrimes(N);
int A = 1033, B = 8179;
bfs(A);
if (dis[B] == -1)
cout << "-1" << endl;
else
cout << dis[B] << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static Vector<Vector<Integer>> lis = new Vector<Vector<Integer>>();
static Vector<Integer> primes = new Vector<Integer>();
static int [] vis = new int [ 100001 ];
static int [] dis = new int [ 100001 ];
static boolean isPrime( int n)
{
int i = 2 ;
while (i * i <= n)
{
if (n % i == 0 )
return false ;
i += 1 ;
}
return true ;
}
static boolean valid( int a, int b)
{
int c = 0 ;
while (a > 0 )
{
if ((a % 10 ) != (b % 10 ))
c += 1 ;
a = a / 10 ;
b = b / 10 ;
}
if (c == 1 )
return true ;
else
return false ;
}
static void makePrimes( int N)
{
int L = ( int )Math.pow( 10 , N - 1 );
int R = ( int )Math.pow( 10 , N) - 1 ;
for ( int i = L; i < R + 1 ; i++)
{
if (isPrime(i))
primes.add(i);
}
for ( int i = 0 ; i < primes.size(); i++)
{
for ( int j = i + 1 ; j < primes.size(); j++)
{
int a = primes.get(i);
int b = primes.get(j);
if (valid(a, b))
{
lis.get(a).add(b);
lis.get(b).add(a);
}
}
}
}
static void bfs( int src)
{
Vector<Integer> q = new Vector<Integer>();
q.add(src);
vis[src] = 1 ;
dis[src] = 0 ;
while (q.size() != 0 )
{
int curr = q.get( 0 );
q.remove( 0 );
for ( int x : lis.get(curr))
{
if (vis[x] == 0 )
{
vis[x] = 1 ;
q.add(x);
dis[x] = dis[curr] + 1 ;
}
}
}
}
public static void main(String[] args)
{
for ( int i = 0 ; i < 100001 ; i++)
{
lis.add( new Vector<Integer>());
}
int N = 4 ;
makePrimes(N);
int A = 1033 ;
int B = 8179 ;
bfs(A);
if (dis[B] == - 1 )
{
System.out.print(- 1 );
}
else
{
System.out.print(dis[B]);
}
}
}
|
Python3
mod = 1000000007
lis = [[] for i in range ( 100001 )]
primes = []
vis = [ 0 for i in range ( 100001 )]
dis = [ 0 for i in range ( 100001 )]
def isPrime(n):
i = 2
while (i * i < = n):
if (n % i = = 0 ):
return False
i + = 1
return True
def valid(a, b):
c = 0
while (a):
if ((a % 10 ) ! = (b % 10 )):
c + = 1
a = int (a / 10 )
b = int (b / 10 )
if (c = = 1 ):
return True
else :
return False
def makePrimes(N):
global primes
global lis
i = 0
j = 0
L = pow ( 10 , N - 1 )
R = pow ( 10 , N) - 1
for i in range (L, R + 1 ):
if (isPrime(i)):
primes.append(i)
for i in range ( len (primes)):
for j in range (i + 1 , len (primes)):
a = primes[i]
b = primes[j]
if (valid(a, b)):
lis[a].append(b)
lis[b].append(a)
def bfs(src):
global vis
global dis
q = []
q.append(src)
vis[src] = 1
dis[src] = 0
while ( len (q) ! = 0 ):
curr = q[ 0 ]
q.pop( 0 )
for x in lis[curr]:
if (vis[x] = = 0 ):
vis[x] = 1
q.append(x)
dis[x] = dis[curr] + 1
N = 4
makePrimes(N)
A = 1033
B = 8179
bfs(A)
if (dis[B] = = - 1 ):
print ( - 1 )
else :
print (dis[B])
|
C#
using System;
using System.Collections.Generic;
class GFG {
static List<List< int >> lis = new List<List< int >>();
static List< int > primes = new List< int >();
static int [] vis = new int [100001];
static int [] dis = new int [100001];
static bool isPrime( int n)
{
int i = 2;
while (i * i <= n)
{
if (n % i == 0)
return false ;
i += 1;
}
return true ;
}
static bool valid( int a, int b)
{
int c = 0;
while (a > 0)
{
if ((a % 10) != (b % 10))
c += 1;
a = a / 10;
b = b / 10;
}
if (c == 1)
return true ;
else
return false ;
}
static void makePrimes( int N)
{
int L = ( int )Math.Pow(10, N - 1);
int R = ( int )Math.Pow(10, N) - 1;
for ( int i = L; i < R + 1; i++)
{
if (isPrime(i))
primes.Add(i);
}
for ( int i = 0; i < primes.Count; i++)
{
for ( int j = i + 1; j < primes.Count; j++)
{
int a = primes[i];
int b = primes[j];
if (valid(a, b))
{
lis[a].Add(b);
lis[b].Add(a);
}
}
}
}
static void bfs( int src)
{
List< int > q = new List< int >();
q.Add(src);
vis[src] = 1;
dis[src] = 0;
while (q.Count != 0)
{
int curr = q[0];
q.RemoveAt(0);
foreach ( int x in lis[curr])
{
if (vis[x] == 0)
{
vis[x] = 1;
q.Add(x);
dis[x] = dis[curr] + 1;
}
}
}
}
static void Main()
{
for ( int i = 0; i < 100001; i++)
{
lis.Add( new List< int >());
}
int N = 4;
makePrimes(N);
int A = 1033;
int B = 8179;
bfs(A);
if (dis[B] == -1)
{
Console.Write(-1);
}
else
{
Console.Write(dis[B]);
}
}
}
|
Javascript
let mod = 1000000007
let lis = new Array(100001);
for ( var i = 0; i < 100001; i++)
lis[i] = new Array();
let primes = [];
let vis = new Array(100001).fill(0);
let dis = new Array(100001).fill(0);
function isPrime(n)
{
for (let i = 2;
i * i <= n; i++) {
if (n % i == 0)
return false ;
}
return true ;
}
function valid(a, b)
{
let c = 0;
while (a > 0) {
if ((a % 10) != (b % 10)) {
c++;
}
a = Math.floor(a / 10);
b = Math.floor(b / 10);
}
if (c == 1) {
return true ;
}
else {
return false ;
}
}
function makePrimes( N)
{
let i, j;
let L = 10 ** (N - 1);
let R = (10 ** N) - 1;
for (i = L; i <= R; i++) {
if (isPrime(i)) {
primes.push(i);
}
}
for (i = 0; i < primes.length; i++) {
for (j = i + 1; j < primes.length; j++) {
let a = primes[i];
let b = primes[j];
if (valid(a, b)) {
let l1 = lis[a]
let l2 = lis[b]
l1.push(b)
l2.push(a)
lis[a] = l1
lis[b] = l2;
}
}
}
}
function bfs(src)
{
let q = [];
q.push(src);
vis[src] = 1;
dis[src] = 0;
while ( q.length > 0) {
let curr = q[0];
q.shift();
for (let x of lis[curr]) {
if (vis[x] == 0) {
vis[x] = 1;
q.push(x);
dis[x] = dis[curr] + 1;
}
}
}
}
let N = 4;
makePrimes(N);
let A = 1033, B = 8179;
bfs(A);
if (dis[B] == -1)
console.log(-1)
else
console.log(dis[B]);
|
Time Complexity: O(102N)
Auxiliary Space Complexity: O(105)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!