## The challenge

You have a positive number `n` consisting of digits. You can do at most one operation: Choosing the index of a digit in the number, remove this digit at that index and insert it back to another or at the same place in the number in order to find the smallest number you can get.

#Task: Return an array or a tuple or a string depending on the language (see “Sample Tests”) with

1. the smallest number you got
1. the index `i` of the digit `d` you took, `i` as small as possible
1. the index `j` (as small as possible) where you insert this digit `d` to have the smallest number.

Example:

 ``````1 `````` ``````smallest(261235) --> [126235, 2, 0] or (126235, 2, 0) or "126235, 2, 0" ``````

`126235` is the smallest number gotten by taking `1` at index `2` and putting it at index ``

 ``````1 2 3 4 `````` ``````smallest(209917) --> [29917, 0, 1] or ... [29917, 1, 0] could be a solution too but index `i` in [29917, 1, 0] is greater than index `i` in [29917, 0, 1]. ``````

`29917` is the smallest number gotten by taking `2` at index `` and putting it at index `1` which gave `029917` which is the number `29917`.

 ``````1 `````` ``````smallest(1000000) --> [1, 0, 6] or ... ``````

## The solution in Java code

Option 1:

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 `````` ``````import java.lang.StringBuilder; public class ToSmallest { public static long[] smallest(long n) { long min = n; int index1 = 0; int index2 = 0; String number = String.valueOf(n); for (int i=0; i

Option 2:

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 `````` ``````public class ToSmallest { private static int smallestPosition; private static int replacedPosition = 0; public static long[] smallest(long n) { String value = String.valueOf(n); String smallestPossibleValue = value; for(int i = 0; i < value.length(); i++){ char currentDigit = value.charAt(i); for(int j = 0; j < value.length(); j++){ StringBuilder sb = new StringBuilder(value); sb.deleteCharAt(i); sb.insert(j, currentDigit); String newValue = sb.toString(); if(Long.parseLong(newValue, 10) < Long.parseLong(smallestPossibleValue, 10)){ smallestPossibleValue = newValue; smallestPosition = i; replacedPosition = j; } } } return new long[]{Long.parseLong(smallestPossibleValue), smallestPosition, replacedPosition}; } } ``````

Option 3:

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 `````` ``````import java.util.*; public class ToSmallest { public static long[] smallest(long n) { final String s = ""+n; long[] result = new long[]{Long.MAX_VALUE,0,0}; for (int i=s.length()-1; i>=0; i--) { final String s1=s.substring(0,i)+s.substring(i+1); for (int j=s.length()-1; j>=0; j--) { final long tmp = Long.valueOf(s1.substring(0,j)+s.charAt(i)+s1.substring(j)); if (tmp <= result) result = new long[]{tmp,i,j}; } } return result; } } ``````

## Test cases to validate our solution

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 `````` ``````import static org.junit.Assert.*; import java.util.Arrays; import org.junit.Test; public class ToSmallestTest { private static void testing(long n, String res) { assertEquals(res, Arrays.toString(ToSmallest.smallest(n))); } @Test public void test() { System.out.println("Basic Tests smallest"); testing(261235, "[126235, 2, 0]"); testing(209917, "[29917, 0, 1]"); testing(285365, "[238565, 3, 1]"); testing(269045, "[26945, 3, 0]"); testing(296837, "[239687, 4, 1]"); } } ``````

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 `````` ``````import static org.junit.Assert.*; import java.util.Arrays; import java.util.Random; import org.junit.Test; public class ToSmallestTest { private static void testing(long n, String res) { assertEquals(res, Arrays.toString(ToSmallest.smallest(n))); } @Test public void test() { System.out.println("Basic Tests smallest"); testing(261235, "[126235, 2, 0]"); testing(209917, "[29917, 0, 1]"); testing(285365, "[238565, 3, 1]"); testing(269045, "[26945, 3, 0]"); testing(296837, "[239687, 4, 1]"); testing(187863002809L, "[18786300289, 10, 0]"); testing(199819884756L, "[119989884756, 4, 0]"); testing(94883608842L, "[9488368842, 6, 0]"); testing(256687587015L, "[25668758715, 9, 0]"); testing(935855753L, "[358557539, 0, 8]"); } //........ private static long[] smallestSol(long n) { String s = Long.toString(n), tmp = s; long [] mem = new long[] {-1, -1, -1}; int l = s.length(); for (int i = 0; i < l; i++) { char c = s.charAt(i); String str1 = s.substring(0, i) + s.substring(i+1, l); for (int j = 0; j < l; j++) { String str2 = str1.substring(0, j) + c + str1.substring(j, str1.length()); int cmp = str2.compareTo(tmp); if (cmp < 0) { tmp = str2; mem = Long.parseLong(tmp); mem = i; mem = j; } } } return mem; } //........ private static long randint(Random rnd, long min, long max) { long randomNumber = (long)(rnd.nextDouble()*(max - min)); return randomNumber; } private static void wTests1() { for (int i = 0; i < 200; i++) { Random rnd = new Random(); long nmx = randint(rnd, 4000, 1000000000000000000L); String ans = Arrays.toString(smallestSol(nmx)); //System.out.println("number " + nmx + " --> " + ans); testing(nmx, ans); } } @Test public void tests1() { System.out.println("Random Tests smallest ****"); wTests1(); } } ``````