Forming the smallest number

Syed Ali

Forming the smallest number from the input number for eg.:

 Input:  991233612
 Output: 12369

What is the proper algorithm to solve this without using an array? I was asked this question at an interview and still can't figure out the correct way of doing it.

Goswin von Brederlow

As others have pointed out sorting and removing duplicates is possible. But how about this algorithm (in pseudocode, implementation is left to the reader)?

bool contains(int x, int digit); // returns true if x contains digit in base 10 notation

int res = 0;
for (int digit = 0; digit <= 9; ++i) {
    if (contains(intput, digit)) res = 10 * res + digit;
}
return res;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related