Stepping Numbers - GeeksforGeeks
Given two integers 'n' and 'm', find all the stepping numbers in range [n, m]. A number is called stepping number if all adjacent digits have an absolute difference of 1. 321 is a Stepping Number while 421 is not.
X. BFS
X. DFS
X.
Read full article from Stepping Numbers - GeeksforGeeks
Given two integers 'n' and 'm', find all the stepping numbers in range [n, m]. A number is called stepping number if all adjacent digits have an absolute difference of 1. 321 is a Stepping Number while 421 is not.
X. BFS
The idea is to use a Breadth First Search/Depth First Search traversal.
How to build the graph?
Every node in the graph represents a stepping number; there will be a directed edge from a node U to V if V can be transformed from U. (U and V are Stepping Numbers) A Stepping Number V can be transformed from U in following manner.
Every node in the graph represents a stepping number; there will be a directed edge from a node U to V if V can be transformed from U. (U and V are Stepping Numbers) A Stepping Number V can be transformed from U in following manner.
lastDigit refers to the last digit of U (i.e. U % 10)
An adjacent number V can be:
An adjacent number V can be:
- U*10 + lastDigit + 1 (Neighbor A)
- U*10 + lastDigit – 1 (Neighbor B)
By applying above operations a new digit is appended to U, it is either lastDigit-1 or lastDigit+1, so that the new number V formed from U is also a Stepping Number.
Therefore, every Node will have at most 2 neighboring Nodes.
Therefore, every Node will have at most 2 neighboring Nodes.
Edge Cases: When the last digit of U is 0 or 9
Case 1: lastDigit is 0 : In this case only digit ‘1’ can be appended.
Case 2: lastDigit is 9 : In this case only digit ‘8’ can be appended.
Case 2: lastDigit is 9 : In this case only digit ‘8’ can be appended.
What will be the source/starting Node?
- Every single digit number is considered as a stepping Number, so bfs traversal for every digit will give all the stepping numbers starting from that digit.
- Do a bfs/dfs traversal for all the numbers from [0,9].
Note: For node 0, no need to explore neighbors during BFS traversal since it will lead to 01, 012, 010 and these will be covered by the BFS traversal starting from node 1.
// Prints all stepping numbers reachable from num
// and in range [n, m]
public
static
void
bfs(
int
n,
int
m,
int
num)
{
// Queue will contain all the stepping Numbers
Queue<Integer> q =
new
LinkedList<Integer> ();
q.add(num);
while
(!q.isEmpty())
{
// Get the front element and pop from
// the queue
int
stepNum = q.poll();
// If the Stepping Number is in
// the range [n,m] then display
if
(stepNum <= m && stepNum >= n)
{
System.out.print(stepNum +
" "
);
}
// If Stepping Number is 0 or greater
// then m ,need to explore the neighbors
if
(stepNum ==
0
|| stepNum > m)
continue
;
// Get the last digit of the currently
// visited Stepping Number
int
lastDigit = stepNum %
10
;
// There can be 2 cases either digit
// to be appended is lastDigit + 1 or
// lastDigit - 1
int
stepNumA = stepNum *
10
+ (lastDigit-
1
);
int
stepNumB = stepNum *
10
+ (lastDigit +
1
);
// If lastDigit is 0 then only possible
// digit after 0 can be 1 for a Stepping
// Number
if
(lastDigit ==
0
)
q.add(stepNumB);
// If lastDigit is 9 then only possible
// digit after 9 can be 8 for a Stepping
// Number
else
if
(lastDigit ==
9
)
q.add(stepNumA);
else
{
q.add(stepNumA);
q.add(stepNumB);
}
}
}
// Prints all stepping numbers in range [n, m]
// using BFS.
public
static
void
displaySteppingNumbers(
int
n,
int
m)
{
// For every single digit Number 'i'
// find all the Stepping Numbers
// starting with i
for
(
int
i =
0
; i <=
9
; i++)
bfs(n, m, i);
}
X. DFS
// Method display's all the stepping numbers
// in range [n, m]
public
static
void
dfs(
int
n,
int
m,
int
stepNum)
{
// If Stepping Number is in the
// range [n,m] then display
if
(stepNum <= m && stepNum >= n)
System.out.print(stepNum +
" "
);
// If Stepping Number is 0 or greater
// than m then return
if
(stepNum ==
0
|| stepNum > m)
return
;
// Get the last digit of the currently
// visited Stepping Number
int
lastDigit = stepNum %
10
;
// There can be 2 cases either digit
// to be appended is lastDigit + 1 or
// lastDigit - 1
int
stepNumA = stepNum*
10
+ (lastDigit-
1
);
int
stepNumB = stepNum*
10
+ (lastDigit+
1
);
// If lastDigit is 0 then only possible
// digit after 0 can be 1 for a Stepping
// Number
if
(lastDigit ==
0
)
dfs(n, m, stepNumB);
// If lastDigit is 9 then only possible
// digit after 9 can be 8 for a Stepping
// Number
else
if
(lastDigit ==
9
)
dfs(n, m, stepNumA);
else
{
dfs(n, m, stepNumA);
dfs(n, m, stepNumB);
}
}
// Prints all stepping numbers in range [n, m]
// using DFS.
public
static
void
displaySteppingNumbers(
int
n,
int
m)
{
// For every single digit Number 'i'
// find all the Stepping Numbers
// starting with i
for
(
int
i =
0
; i <=
9
; i++)
dfs(n, m, i);
}
X.
// This Method checks if an integer n
// is a Stepping Number
public
static
boolean
isStepNum(
int
n)
{
// Initalize prevDigit with -1
int
prevDigit = -
1
;
// Iterate through all digits of n and compare
// difference between value of previous and
// current digits
while
(n >
0
)
{
// Get Current digit
int
curDigit = n %
10
;
// Single digit is consider as a
// Stepping Number
if
(prevDigit != -
1
)
{
// Check if absolute difference between
// prev digit and current digit is 1
if
(Math.abs(curDigit-prevDigit) !=
1
)
return
false
;
}
n /=
10
;
prevDigit = curDigit;
}
return
true
;
}
// A brute force approach based function to find all
// stepping numbers.
public
static
void
displaySteppingNumbers(
int
n,
int
m)
{
// Iterate through all the numbers from [N,M]
// and check if it is a stepping number.
for
(
int
i = n; i <= m; i++)
if
(isStepNum(i))
System.out.print(i+
" "
);
}