Hi guys, I'm trying to solve this interview question:
"Write a function which, given n, prints all well-ordered integers of n digits. A well ordered number is one where the value of the i-th digit is less than the value of the i+1 digit."

I've written the code where if n=3. Then I go from 100 to 999. And check if it is well-ordered. But my approach doesn't take care of numbers like 012, 018 etc...Numbers which start with a 0.
Please tell me how to do it.

Here is my current code:

using namespace std;

bool desirable(int num, int N)
    int digits[N];
    for (int i=0; i<N; i++)
    for (int i=0;i<N-1;i++)
        if (digits[i]>digits[i+1])
            return false;
    return true;

int main()
    printf("Enter: ");
    int N;
    int low = (int)pow(10.0, double(N-1));
    int high = (int)pow(10.0, double(N)) -1;

    for (int i=low; i<=high; i++)
    return 0;

Recommended Answers

All 2 Replies

Numbers which start with a 0.

You should ask whether leading 0's should be taken into account, because if you write code that does so and it is not part of the problem, you will not get the question right. Leading 0's are usually only used as padding for display and sorting, not as significant value digits. If your algorithm assumes that padding is significant, it will always start at 0 instead of the first value with n significant digits, which is probably not what the interviewer wants. Solving the wrong problem is a great way to fail interviews.

Ok. Thanks for advice

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.