CS203 Assign
CS203 Assign
(c) 2T (√n) + n
T(n) = 2T(√n) + n
Let n = 2m -> n1/2 = 2m/2
T(2m) = T(2m/2) + 2m
S(m) = S(m/2) + m
a = 1 , b = 2 , k =1 and p = 0
Applying master theorem
We get TC = O(nk log0n)
TC = O(n).
5 Problem
Given two prime numbers, P1 and P2 , your task is to reach from P1 to P2 by
changing only one digit at a time and also the number you get after changing a digit
must also be a prime. For example suppose that P1 is 1033 and P2 is 8179 (both
primes), we can reach from P1 to P2 in following way :-
1033− > 1733− > 3733− > 3739− > 3779− > 8779− > 8179.
Solution:
1. Find all N digit prime numbers and make a graph with these numbers.
2. 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.
3. Apply BFS traversal and find out the number of edges between A and B.
4. If no path exists, print -1.
5. Else print the no. of edges, which is the required solution.
6. Time Complexity: O(102N)
Auxiliary Space Complexity: O(10