-6

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.

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Syed Ali
  • 219
  • 3
  • 11
  • 1
    What have you tried? Post your code. – Andrew Henle Jun 19 '18 at 13:05
  • 2
    Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Jun 19 '18 at 13:05
  • 4
    What constitutes the "smallest number?" Why isn't `1` the correct solution? – Sean Bright Jun 19 '18 at 13:06
  • are you looking for sort + remove duplicates ? – Sander De Dycker Jun 19 '18 at 13:06
  • only constraint was not to use arrays – Syed Ali Jun 19 '18 at 13:07
  • Hint: use operator `%` and operator `/` – DDS Jun 19 '18 at 13:09
  • 1
    Hint: Sort in place the input. Sorting requires swapping, which you can do numerically. Then remove duplicates. Again you can remove duplicates numerically. Done! – Bathsheba Jun 19 '18 at 13:12
  • First question I'd asked would have been: *Do you consider `malloc`ed memory an array?*. (Because I don't). – tofro Jun 19 '18 at 13:21
  • @tofro: The old adage *pointer arithmetic is only valid within arrays* implies that you get back an array from `malloc`; but that point of devilry had crossed my mind too. – Bathsheba Jun 19 '18 at 13:26
  • You should be more precise what "this" in "to solve this without using an array" is. If you mean "the smallest number that may be created by rearranging unique digits from the input" then tell us. – Gerhardh Jun 19 '18 at 13:33
  • 1
    I would have said it's impossible because the input is an array and you aren't allowed to use that. No input, no output. – Goswin von Brederlow Jun 19 '18 at 13:33
  • @GoswinvonBrederlow The input might as well be an integer. – Gerhardh Jun 19 '18 at 13:33
  • input is an integer – Syed Ali Jun 19 '18 at 13:34
  • [This answer](https://stackoverflow.com/a/2130729/5875805) might be helpful – Tormund Giantsbane Jun 19 '18 at 13:45
  • @Bathsheba We all know that the solution to such silly interview questions is (nearly) entirely irrelevant - They want to hear your line of thought and wether you can come up with creative solutions. – tofro Jun 19 '18 at 14:05

2 Answers2

4

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;
Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42
0

Sounds like the point of this question is to remove duplicate digits, sort the remaining digits in increasing order and output the result as a number.

def minimumNumber(n):
    prevDigit = 0
    digit = 9
    result = 0
    foundDigit = true

    # repeat while there still are digits left
    while foundDigit:
        foundDigit = false
        num = n

        # search for the smallest digit larger than the previous digit
        while num > 0:
            d = num / 10
            num %= 10

            # found a digit that matches the constraints
            if d < digit and d > prevDigit
                digit = d
                foundDigit = true

        if foundDigit:
            # add digit to result
            result = result * 10 + d