A number is called stepping number if all adjacent digits have an absolute difference of 1, e.g. '321' is a Stepping Number while 421 is not. Given two integers n and m, find the count of all the stepping numbers in range [n, m]. - Codeprg

Breaking

programing News Travel Computer Engineering Science Blogging Earning

Thursday, 9 July 2020

A number is called stepping number if all adjacent digits have an absolute difference of 1, e.g. '321' is a Stepping Number while 421 is not. Given two integers n and m, find the count of all the stepping numbers in range [n, m].

find the count of all the stepping numbers in the range [n, m].
find the count of all the stepping numbers in the range [n, m].

A number is called stepping number if all adjacent digits have an absolute difference of 1, e.g. '321' is a Stepping Number while 421 is not. Given two integers n and m, find the count of all the stepping numbers in the range [n, m].


For each test case in a new line print an integer denoting the number of stepping numbers in the range between n and m.


Example:
Input:

3
0 21
10 15
0 1
Output:
13
2
2


#include <iostream>

using namespace std;
int test(int n) {

    int i = n % 10;
    n /= 10;
    while (n > 0) {
        if (abs(i - n % 10) != 1)
            return 0;
        i = n % 10;
        n /= 10;
    }

    return 1;

}
int main() {
    //code
    int t;
    cin >> t;
    while (t--) {
        int n, m, cnt = 0;
        cin >> n >> m;
        for (int i = n; i <= m; ++i) {
            if (test(i))
                ++cnt;
        }
        cout << cnt << endl;
    }
    return 0;
}