In this challenge, you are given a number x. You have to find the minimum number of steps required to reach x from 1. At a particular point, you have two choices:
1) Increment the number by 1.
2) Reverse the integer (remove leading zeros after reversing)
Input: n=42
Output: 1>2>3>4>5>6>7>8>9>10>11>12>13>14>41>42 **(15 steps)**
The minimum steps to reach 42 from 1 is 15. This can be achieved if we increment numbers by 1 from 1 to 14 (13 steps). After reaching 14 we can reverse the number which will give us 41 (1 step). From 41 we can increment number by 1 to reach 42(1 step). Hence the total number is 15 steps, which is then the minimum.
Note that if we reverse the number after reaching 12 or 13, we will not get the minimum steps.
1>2>3>4>5>6>7>8>9>10>11>12>21>22>23>32>33>34>35>36>37>38>39>40>41>42 (25 steps)
Input: n=16
Output: 1>2>3>4>5>6>7>8>9>10>11>12>13>14>15>16 **(15 steps)**
In this case we have to increment the numbers until we get 16, which will give us a minimum of 15 steps.
Note: Starting from 0 is also allowed, which will increase all output by 1.
reverse(reverse(10))
andreverse(2)
? \$\endgroup\$1>2>3>4>5>6>7>8>9>10>11>12>21>22>23>24>42
(16 steps), which is still more than the correct solution (15 steps), but... less worse :-) \$\endgroup\$