Print all Jumping Numbers smaller than or equal to a given value - GeeksforGeeks
A number is called as a Jumping Number if all adjacent digits in it differ by 1. The difference between '9' and '0' is not considered as 1.
All single digit numbers are considered as Jumping Numbers. For example 7, 8987 and 4343456 are Jumping numbers but 796 and 89098 are not.
Given a positive number x, print all Jumping Numbers smaller than or equal to x. The numbers can be printed in any order.
X. DFS
http://qa.geeksforgeeks.org/2657/print-all-jumping-numbers-in-the-range-epic-systems
Read full article from Print all Jumping Numbers smaller than or equal to a given value - GeeksforGeeks
A number is called as a Jumping Number if all adjacent digits in it differ by 1. The difference between '9' and '0' is not considered as 1.
All single digit numbers are considered as Jumping Numbers. For example 7, 8987 and 4343456 are Jumping numbers but 796 and 89098 are not.
Given a positive number x, print all Jumping Numbers smaller than or equal to x. The numbers can be printed in any order.
One Simple Solution is to traverse all numbers from 0 to x. For every traversed number, check if it is a Jumping number. If yes, then print it. Otherwise ignore it. Time Complexity of this solution is O(x).
An Efficient Solution can solve this problem in O(k) time where k is number of Jumping Numbers smaller than or equal to x. The idea is use BFS or DFS.
Assume that we have a graph where the starting node is 0 and we need to traverse it from the start node to all the reachable nodes.
With the restrictions given in the graph about the jumping numbers, what do you think should be the restrictions defining the next transitions in the graph.
Lets take a example for input x = 90 Start node = 0 From 0, we can move to 1 2 3 4 5 6 7 8 9 [these are not in our range so we don't add it] Now from 1, we can move to 12 and 10 From 2, 23 and 21 From 3, 34 and 32 . . . . . . and so on.
// Prints all jumping numbers smaller than or equal to x starting
// with 'num'. It mainly does BFS starting from 'num'.
void
bfs(
int
x,
int
num)
{
// Create a queue and enqueue 'i' to it
queue<
int
> q;
q.push(num);
// Do BFS starting from i
while
(!q.empty())
{
num = q.front();
q.pop();
if
(num <= x)
{
cout << num <<
" "
;
int
last_dig = num % 10;
// If last digit is 0, append next digit only
if
(last_dig == 0)
q.push((num*10) + (last_dig+1));
// If last digit is 9, append previous digit only
else
if
(last_dig == 9)
q.push( (num*10) + (last_dig-1) );
// If last digit is neighter 0 nor 9, append both
// previous and next digits
else
{
q.push((num*10) + (last_dig-1));
q.push((num*10) + (last_dig+1));
}
}
}
}
// Prints all jumping numbers smaller than or equal to
// a positive number x
void
printJumping(
int
x)
{
cout << 0 <<
" "
;
for
(
int
i=1; i<=9 && i<=x; i++)
bfs(x, i);
}
- Change the above solution to use DFS instead of BFS.
- Extend your solution to print all numbers in sorted order instead of any order.
- Further extend the solution to print all numbers in a given range.
http://qa.geeksforgeeks.org/2657/print-all-jumping-numbers-in-the-range-epic-systems
vector<
int
> Ans;
void
dfs(
int
len,
int
N,
int
M,
int
num = 0) {
if
(num >= N && num <= M) {
Ans.push_back(num);
}
if
(len == 0)
return
;
if
(num == 0) {
for
(
int
i = 1; i <= 9; ++i) {
dfs(len - 1, N, M, i);
}
return
;
}
int
last_dig = num%10;
if
(last_dig == 0) {
dfs(len - 1, N, M, (num * 10) + (last_dig + 1));
}
else
if
(last_dig == 9) {
dfs(len - 1, N, M, (num * 10) + (last_dig - 1));
}
else
{
dfs(len - 1, N, M, (num * 10) + (last_dig - 1));
dfs(len - 1, N, M, (num * 10) + (last_dig + 1));
}
}
void
solve(
int
N,
int
M) {
int
len = 0;
int
temp = M;
while
(temp) {
temp /= 10;
len++;
}
Ans.clear();
dfs(len, N, M);
sort(Ans.begin(), Ans.end());
}