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:

 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 ``

 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.

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

The solution in Java code

Option 1:

 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:

 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:

 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[0]) result = new long[]{tmp,i,j}; } } return result; } }

Test cases to validate our solution

 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]"); } }